Play

Check-in [d66b3a6dad]
Login
Overview
Comment:Implement conversion from String to SemanticVersion.
Timelines: family | ancestors | descendants | both | modules
Files: files | file ages | folders
SHA3-256: d66b3a6dadc12176565a52dc91dc9bd8ba6805eee60011702e4b69d4fa7c79c0
User & Date: robin.hansen on 2021-02-12 13:16:05
Other Links: branch diff | manifest | tags
Context
2021-02-13
07:45
Implement conversion from String to PackagePath. check-in: a1c159f148 user: robin.hansen tags: modules
2021-02-12
13:16
Implement conversion from String to SemanticVersion. check-in: d66b3a6dad user: robin.hansen tags: modules
12:56
Factor out test helper functions. check-in: 8dca27fd4f user: robin.hansen tags: modules
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Data/SemanticVersion.elm from [ee5d58bf40] to [8d8ba8498e].

1


2
3

4
5












































module Play.Data.SemanticVersion exposing (SemanticVersion)





type SemanticVersion
    = SemanticVersion Int Int Int












































|
>
>
|

>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
module Play.Data.SemanticVersion exposing
    ( SemanticVersion
    , fromString
    )


type SemanticVersion
    = SemanticVersion Int Int Int


fromString : String -> Result () SemanticVersion
fromString str =
    case String.split "." str of
        [ major ] ->
            toInt major Nothing Nothing

        [ major, minor ] ->
            toInt major (Just minor) Nothing

        [ major, minor, patch ] ->
            toInt major (Just minor) (Just patch)

        _ ->
            Err ()


toInt : String -> Maybe String -> Maybe String -> Result () SemanticVersion
toInt majorStr maybeMinorStr maybePatchStr =
    let
        minorStr =
            Maybe.withDefault "0" maybeMinorStr

        patchStr =
            Maybe.withDefault "0" maybePatchStr

        intVersions =
            [ majorStr, minorStr, patchStr ]
                |> List.filterMap String.toInt
    in
    case intVersions of
        [ major, minor, patch ] ->
            if major < 0 || minor < 0 || patch < 0 then
                Err ()

            else if major == 0 && minor == 0 && patch < 1 then
                Err ()

            else
                Ok <| SemanticVersion major minor patch

        _ ->
            Err ()

Added tests/Test/Data/SemanticVersion.elm version [8794572d0d].









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
module Test.Data.SemanticVersion exposing (suite)

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


suite : Test
suite =
    describe "SemanticVersion"
        [ test "Valid versions" <|
            \_ ->
                PlayExpect.allOk SemanticVersion.fromString
                    [ "1"
                    , "1.2"
                    , "1.2.3"
                    , "10.100.1000"
                    , "0.0.1"
                    , "0.1.0"
                    ]
        , test "Cannot contain more than three parts" <|
            \_ ->
                SemanticVersion.fromString "1.2.3.4"
                    |> Expect.err
        , test "Cannot be empty string" <|
            \_ ->
                SemanticVersion.fromString ""
                    |> Expect.err
        , test "Must be numbers" <|
            \_ ->
                PlayExpect.allErr SemanticVersion.fromString
                    [ "A"
                    , "*"
                    , "~"
                    , "1.0.x"
                    , "~1.0.0"
                    , "1.0.0-alpha1"
                    , "alpha.1"
                    ]
        , test "Minimum version is 0.0.1" <|
            \_ ->
                SemanticVersion.fromString "0.0.0"
                    |> Expect.err
        , test "Cannot contain negative versions" <|
            \_ ->
                PlayExpect.allErr SemanticVersion.fromString
                    [ "-1"
                    , "-1.2"
                    , "1.-2.0"
                    ]
        ]