Play

Check-in [b410ba3fed]
Login
Overview
Comment:Implemented conversion from String to opaque PackageName type.
Timelines: family | ancestors | descendants | both | modules
Files: files | file ages | folders
SHA3-256: b410ba3fed33c53ca042ca77c620f87a2dc54b6363271ffa0f33fadb1c936c8f
User & Date: robin.hansen on 2021-02-12 12:21:37
Other Links: branch diff | manifest | tags
Context
2021-02-12
12:56
Factor out test helper functions. check-in: 8dca27fd4f user: robin.hansen tags: modules
12:21
Implemented conversion from String to opaque PackageName type. check-in: b410ba3fed user: robin.hansen tags: modules
09:19
Merge new test script names from trunk. check-in: 4a3f3a82db user: robin.hansen tags: modules
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Data/PackageName.elm from [f9a885d0a2] to [0c8e8634cf].

1


2
3

4
5



























module Play.Data.PackageName exposing (PackageName)





type PackageName
    = PackageName String String



























|
>
>
|

>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Play.Data.PackageName exposing
    ( PackageName
    , fromString
    )


type PackageName
    = PackageName String String


fromString : String -> Result () PackageName
fromString str =
    case String.split "/" str of
        [ part1, part2 ] ->
            if invalidPart part1 || invalidPart part2 then
                Err ()

            else
                Ok <| PackageName part1 part2

        _ ->
            Err ()


invalidPart : String -> Bool
invalidPart str =
    case String.uncons str of
        Nothing ->
            True

        Just ( first, rest ) ->
            not
                (Char.isLower first
                    && String.all (\c -> Char.isLower c || Char.isDigit c || c == '_') rest
                )

Added tests/Test/Data/PackageName.elm version [244389ec31].















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module Test.Data.PackageName exposing (suite)

import Expect
import Play.Data.PackageName as PackageName
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "PackageName"
        [ test "valid package names" <|
            \_ ->
                expectAllOk PackageName.fromString
                    [ "some/package"
                    , "play/std_lib"
                    , "number1/package123"
                    , "play_std/lib"
                    ]
        , test "Must contain exactly two parts, seperated by a single slash" <|
            \_ ->
                expectAllErr PackageName.fromString
                    [ "/"
                    , "one/"
                    , "/one"
                    , "one"
                    , "one/two/three"
                    ]
        , test "Both parts of a name must start with lower cased ascii character" <|
            \_ ->
                expectAllErr PackageName.fromString
                    [ "1pack/name"
                    , "_priv/pack"
                    , "#whaaat/events"
                    , "Some/package"
                    , "pack/1name"
                    , "priv/_pack"
                    , "whaaat/#events"
                    , "some/Package"
                    ]
        , test "Both parts of a name cannot contain upper case letters" <|
            \_ ->
                expectAllErr PackageName.fromString
                    [ "myPack/name"
                    , "mypackage/someName"
                    , "mypackage/some_Name"
                    ]
        , test "Both parts of a name cannot contain non ascii or digit letters" <|
            \_ ->
                expectAllErr PackageName.fromString
                    [ "my#pack/name"
                    , "mypackage/bills$"
                    ]
        ]


expectAllOk : (a -> Result err ok) -> List a -> Expect.Expectation
expectAllOk fn values =
    let
        expectationList =
            List.map (\val -> \f -> f val |> expectOk val) values

        expectOk original result =
            case result of
                Ok _ ->
                    Expect.pass

                Err _ ->
                    Expect.fail <| "Expected Ok for input " ++ Debug.toString original ++ ", was: " ++ Debug.toString result
    in
    Expect.all expectationList fn


expectAllErr : (a -> Result err ok) -> List a -> Expect.Expectation
expectAllErr fn values =
    let
        expectationList =
            List.map (\val -> \f -> f val |> expectErr val) values

        expectErr original result =
            case result of
                Ok _ ->
                    Expect.fail <| "Expected Err for input " ++ Debug.toString original ++ ", was: " ++ Debug.toString result

                Err _ ->
                    Expect.pass
    in
    Expect.all expectationList fn