github.com/simpleiot/simpleiot@v0.18.3/frontend/tests/Sanitize.elm (about)

     1  module Sanitize exposing (date, parseDate, parseHM, parseNumber)
     2  
     3  import Expect
     4  import Parser exposing (run)
     5  import Test exposing (..)
     6  import UI.Sanitize as Sanitize
     7  
     8  
     9  date : Test
    10  date =
    11      describe "Test date sanitizer"
    12          [ test "2023-06-01" <|
    13              \_ -> Expect.equal (Sanitize.date "2023-06-01") "2023-06-01"
    14          , test "2023-066" <|
    15              \_ -> Expect.equal (Sanitize.date "2023-066") "2023-06"
    16          , test "2023-6-1" <|
    17              \_ -> Expect.equal (Sanitize.date "2023-6-01") "2023-06-01"
    18          , test "3000" <|
    19              \_ -> Expect.equal (Sanitize.date "3000") ""
    20          , test "2300" <|
    21              \_ -> Expect.equal (Sanitize.date "2300") "2"
    22          , test "2023-61" <|
    23              \_ -> Expect.equal (Sanitize.date "2023-61") "2023-6"
    24          , test "2023-12-31" <|
    25              \_ -> Expect.equal (Sanitize.date "2023-12-31") "2023-12-31"
    26          , test "2023-13-31" <|
    27              \_ -> Expect.equal (Sanitize.date "2023-13-31") "2023-1"
    28          , test "2099-12-32" <|
    29              \_ -> Expect.equal (Sanitize.date "2099-12-32") "2099-12-3"
    30          ]
    31  
    32  
    33  parseDate : Test
    34  parseDate =
    35      describe "Test date parsing"
    36          [ test "valid date" <|
    37              \_ ->
    38                  Expect.equal (Sanitize.parseDate "2023-01-23") (Just "2023-01-23")
    39          , test "invalid year" <|
    40              \_ ->
    41                  Expect.equal (Sanitize.parseDate "202-01-23") Nothing
    42          , test "single digit month" <|
    43              \_ ->
    44                  Expect.equal (Sanitize.parseDate "2023-1-23") Nothing
    45          , test "single digit day" <|
    46              \_ ->
    47                  Expect.equal (Sanitize.parseDate "2023-11-8") Nothing
    48          ]
    49  
    50  
    51  parseHM : Test
    52  parseHM =
    53      describe "Test Hour/Minute parsing"
    54          [ test "single digit min" <|
    55              \_ ->
    56                  Expect.equal (Sanitize.parseHM "1:2") Nothing
    57          , test "2-digit hour" <|
    58              \_ ->
    59                  Expect.equal (Sanitize.parseHM "12:43") (Just "12:43")
    60          , test "leading 0 min" <|
    61              \_ ->
    62                  Expect.equal (Sanitize.parseHM "1:02") (Just "1:02")
    63          , test "leading 0 hour" <|
    64              \_ ->
    65                  Expect.equal (Sanitize.parseHM "01:02") (Just "01:02")
    66          , test "min greater 59" <|
    67              \_ ->
    68                  Expect.equal (Sanitize.parseHM "01:60") Nothing
    69          , test "hour is 23" <|
    70              \_ ->
    71                  Expect.equal (Sanitize.parseHM "23:15") (Just "23:15")
    72          , test "hour is > 23" <|
    73              \_ ->
    74                  Expect.equal (Sanitize.parseHM "24:23") Nothing
    75          , test "hour/min is 0" <|
    76              \_ ->
    77                  Expect.equal (run Sanitize.hmParser "0:00") (Ok "0:00")
    78          , test "hour/min is 00:00" <|
    79              \_ ->
    80                  Expect.equal (run Sanitize.hmParser "00:00") (Ok "00:00")
    81          ]
    82  
    83  
    84  parseNumber : Test
    85  parseNumber =
    86      describe "Test number parsing"
    87          [ test "test simple number" <|
    88              \_ ->
    89                  Expect.equal (Sanitize.float "1.2") "1.2"
    90          , test "test leading dec" <|
    91              \_ ->
    92                  Expect.equal (Sanitize.float ".2") ".2"
    93          , test "test non digit" <|
    94              \_ ->
    95                  Expect.equal (Sanitize.float "1.2a") "1.2"
    96          , test "test negative number" <|
    97              \_ ->
    98                  Expect.equal (Sanitize.float "-1.2") "-1.2"
    99          , test "test misplaced dash" <|
   100              \_ ->
   101                  Expect.equal (Sanitize.float "1-2") "1"
   102          ]