|
| 1 | +/- |
| 2 | + Copyright Cedar Contributors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +-/ |
| 16 | + |
| 17 | +import Cedar.Spec.Ext.Datetime |
| 18 | +import UnitTest.Run |
| 19 | + |
| 20 | +/-! This file defines unit tests for Datetime functions. -/ |
| 21 | + |
| 22 | +namespace UnitTest.Datetime |
| 23 | + |
| 24 | +open Cedar.Spec.Ext.Datetime |
| 25 | + |
| 26 | +theorem test1 : toString ((Duration.parse "1ms").get!) = "1" := by native_decide |
| 27 | +theorem test2 : toString ((Duration.parse "1s").get!) = "1000" := by native_decide |
| 28 | +theorem test3 : toString ((Duration.parse "1m").get!) = "60000" := by native_decide |
| 29 | +theorem test4 : toString ((Duration.parse "1h").get!) = "360000" := by native_decide |
| 30 | +theorem test5 : toString ((Duration.parse "1d").get!) = "86400000" := by native_decide |
| 31 | +theorem test6 : toString ((Duration.parse "1d2h3m4s5ms").get!) = "87304005" := by native_decide |
| 32 | +theorem test7 : toString ((Duration.parse "2d12h").get!) = "177120000" := by native_decide |
| 33 | +theorem test8 : toString ((Duration.parse "3m30s").get!) = "210000" := by native_decide |
| 34 | +theorem test9 : toString ((Duration.parse "1h30m45s").get!) = "2205000" := by native_decide |
| 35 | +theorem test10 : toString ((Duration.parse "2d5h20m").get!) = "175800000" := by native_decide |
| 36 | +theorem test11 : toString ((Duration.parse "-1d12h").get!) = "-90720000" := by native_decide |
| 37 | +theorem test12 : toString ((Duration.parse "-3h45m").get!) = "-3780000" := by native_decide |
| 38 | +theorem test13 : toString ((Duration.parse "1d1ms").get!) = "86400001" := by native_decide |
| 39 | +theorem test14 : toString ((Duration.parse "59m59s999ms").get!) = "3599999" := by native_decide |
| 40 | +theorem test15 : toString ((Duration.parse "23h59m59s999ms").get!) = "11879999" := by native_decide |
| 41 | +theorem test16 : toString ((Duration.parse "0d0h0m0s0ms").get!) = "0" := by native_decide |
| 42 | + |
| 43 | +private def testValid (str : String) (rep : Int) : TestCase IO := |
| 44 | + test str ⟨λ _ => checkEq (Duration.parse str) (duration? rep)⟩ |
| 45 | + |
| 46 | +def testsForValidDurationStrings := |
| 47 | + suite "Duration.parse for valid strings" |
| 48 | + [ |
| 49 | + testValid "0ms" 0, |
| 50 | + testValid "0d0s" 0, |
| 51 | + testValid "1ms" 1, |
| 52 | + testValid "1s" 1000, |
| 53 | + testValid "1m" 60000, |
| 54 | + testValid "1h" 360000, |
| 55 | + testValid "1d" 86400000, |
| 56 | + testValid "12s340ms" 12340, |
| 57 | + testValid "1s234ms" 1234, |
| 58 | + testValid "-1s" (-1000), |
| 59 | + testValid "-4s200ms" (-4200), |
| 60 | + testValid "-9s876ms" (-9876), |
| 61 | + testValid "106751d23h47m16s854ms" 9223297516854, |
| 62 | + testValid "-106751d23h47m16s854ms" (-9223297516854), |
| 63 | + testValid "1d2h3m4s5ms" 87304005, |
| 64 | + testValid "2d12h" 177120000, |
| 65 | + testValid "3m30s" 210000, |
| 66 | + testValid "1h30m45s" 2205000, |
| 67 | + testValid "2d5h20m" 175800000, |
| 68 | + testValid "-1d12h" (-90720000), |
| 69 | + testValid "-3h45m" (-3780000), |
| 70 | + testValid "1d1ms" 86400001, |
| 71 | + testValid "59m59s999ms" 3599999, |
| 72 | + testValid "23h59m59s999ms" 11879999 |
| 73 | + ] |
| 74 | + |
| 75 | +private def testInvalid (str : String) (msg : String) : TestCase IO := |
| 76 | + test s!"{str} [{msg}]" ⟨λ _ => checkEq (Duration.parse str) .none⟩ |
| 77 | + |
| 78 | +def testsForInvalidDurationStrings := |
| 79 | + suite "Duration.parse for invalid strings" |
| 80 | + [ |
| 81 | + testInvalid "" "empty string", |
| 82 | + testInvalid "d" "unit but no amount", |
| 83 | + testInvalid "1d-1s" "invalid use of -", |
| 84 | + testInvalid "1d2h3m4s5ms6" "trailing amount", |
| 85 | + testInvalid "1x2m3s" "invalid unit", |
| 86 | + testInvalid "1.23s" "amounts must be integral", |
| 87 | + testInvalid "1s1d" "invalid order", |
| 88 | + testInvalid "1s1s" "repeated units", |
| 89 | + testInvalid "1d2h3m4s5ms " "trailing space", |
| 90 | + testInvalid " 1d2h3m4s5ms" "leading space", |
| 91 | + testInvalid "1d9223372036854775807ms" "overflow", |
| 92 | + testInvalid "1d92233720368547758071ms" "overflow ms", |
| 93 | + testInvalid "9223372036854776s1ms" "overflow s" |
| 94 | + ] |
| 95 | + |
| 96 | +def tests := [testsForValidDurationStrings, testsForInvalidDurationStrings] |
| 97 | + |
| 98 | +-- Uncomment for interactive debugging |
| 99 | +-- #eval TestSuite.runAll tests |
| 100 | + |
| 101 | +end UnitTest.Datetime |
0 commit comments