diff --git a/elm.json b/elm.json index de251a98..15e7b5a8 100644 --- a/elm.json +++ b/elm.json @@ -24,6 +24,8 @@ "test-dependencies": { "arowM/elm-multiline-string": "1.0.0 <= v < 2.0.0", "elm/json": "1.1.3 <= v < 2.0.0", - "elm-explorations/test": "2.0.0 <= v < 3.0.0" + "elm-explorations/test": "2.0.0 <= v < 3.0.0", + "wolfadex/elm-ansi": "3.0.0 <= v < 4.0.0", + "miniBill/elm-diff": "1.1.0 <= v < 2.0.0" } } diff --git a/tests/Case.elm b/tests/Case.elm index 3c6e784c..29af5b21 100644 --- a/tests/Case.elm +++ b/tests/Case.elm @@ -10,17 +10,16 @@ elmCaseString : Test elmCaseString = test "Elm.Case.string" <| \_ -> - Elm.Expect.renderedAs - (Elm.Case.string (Elm.string "Hello") - { cases = [ ( "World", Elm.int 1 ) ] - , otherwise = Elm.int 0 - } - ) - """ - case "Hello" of - "World" -> - 1 + Elm.Case.string (Elm.string "Hello") + { cases = [ ( "World", Elm.int 1 ) ] + , otherwise = Elm.int 0 + } + |> Elm.Expect.renderedAs + """ + case "Hello" of + "World" -> + 1 - _ -> - 0 - """ + _ -> + 0 + """ diff --git a/tests/Declare.elm b/tests/Declare.elm index 368291b0..f91331a0 100644 --- a/tests/Declare.elm +++ b/tests/Declare.elm @@ -30,18 +30,22 @@ newStyleTest = describe "New style" [ test "usage" <| \_ -> - Elm.Expect.renderedAs inner - """\\( a, b ) c -> ( a, b, c )""" + inner + |> Elm.Expect.renderedAs + """\\( a, b ) c -> ( a, b, c )""" , test "fnDone == body" <| - \_ -> Elm.Expect.equal inner innerViaBody + \_ -> + innerViaBody + |> Elm.Expect.equal inner , test "declaration" <| \_ -> - Elm.Expect.declarationAs newStyle.declaration - (String.trim """ - -name : ( a, b ) -> c -> ( a, b, c ) -name ( a, b ) c = - ( a, b, c )""") + newStyle.declaration + |> Elm.Expect.declarationAs + """ + name : ( a, b ) -> c -> ( a, b, c ) + name ( a, b ) c = + ( a, b, c ) + """ ] @@ -84,23 +88,23 @@ declarations = describe "declarations" [ test "Basic function dec" <| \_ -> - Elm.Expect.declarationAs myFn.declaration - """ - myFn : Int -> Int - myFn myInt = - 5 + myInt - """ + myFn.declaration + |> Elm.Expect.declarationAs + """ + myFn : Int -> Int + myFn myInt = + 5 + myInt + """ , test "Call correctly" <| \_ -> - Elm.Expect.declarationAs - (Elm.declaration "mySweetNumber" - (myFn.call (Elm.int 82)) - ) - """ - mySweetNumber : Int - mySweetNumber = - myFn 82 - """ + Elm.declaration "mySweetNumber" + (myFn.call (Elm.int 82)) + |> Elm.Expect.declarationAs + """ + mySweetNumber : Int + mySweetNumber = + myFn 82 + """ ] @@ -108,78 +112,74 @@ aliasTest : Test aliasTest = test "Elm.alias" <| \_ -> - Elm.Expect.declarationAs - (Elm.alias "MyAlias" - (Type.record - [ ( "one", Type.var "oneVar" ) - , ( "two", Type.var "twoVar" ) - , ( "three", Type.var "threeVar" ) - ] - ) + Elm.alias "MyAlias" + (Type.record + [ ( "one", Type.var "oneVar" ) + , ( "two", Type.var "twoVar" ) + , ( "three", Type.var "threeVar" ) + ] ) - """ - type alias MyAlias oneVar twoVar threeVar = - { one : oneVar, two : twoVar, three : threeVar } - """ + |> Elm.Expect.declarationAs + """ + type alias MyAlias oneVar twoVar threeVar = + { one : oneVar, two : twoVar, three : threeVar } + """ aliasWithTest : Test aliasWithTest = test "Elm.aliasWith" <| \_ -> - Elm.Expect.declarationAs - (Elm.aliasWith "MyAlias" - [ "twoVar", "nonexistingVar", "oneVar" ] - (Type.record - [ ( "one", Type.var "oneVar" ) - , ( "two", Type.var "twoVar" ) - , ( "three", Type.var "threeVar" ) - ] - ) + Elm.aliasWith "MyAlias" + [ "twoVar", "nonexistingVar", "oneVar" ] + (Type.record + [ ( "one", Type.var "oneVar" ) + , ( "two", Type.var "twoVar" ) + , ( "three", Type.var "threeVar" ) + ] ) - """ - type alias MyAlias twoVar oneVar threeVar = - { one : oneVar, two : twoVar, three : threeVar } - """ + |> Elm.Expect.declarationAs + """ + type alias MyAlias twoVar oneVar threeVar = + { one : oneVar, two : twoVar, three : threeVar } + """ customTypeTest : Test customTypeTest = test "Elm.customType" <| \_ -> - Elm.Expect.declarationAs - (Elm.customType "MyType" - [ Elm.variantWith "One" - [ Type.var "oneVar" ] - , Elm.variantWith "Two" - [ Type.var "twoVar" ] - ] - ) - """ - type MyType oneVar twoVar - = One oneVar - | Two twoVar - """ + Elm.customType "MyType" + [ Elm.variantWith "One" + [ Type.var "oneVar" ] + , Elm.variantWith "Two" + [ Type.var "twoVar" ] + ] + |> Elm.Expect.declarationAs + """ + type MyType oneVar twoVar + = One oneVar + | Two twoVar + """ customTypeWithTest : Test customTypeWithTest = test "Elm.customTypeWith" <| \_ -> - Elm.Expect.declarationAs - (Elm.customTypeWith "MyType" - [ "addVar", "twoVar" ] - [ Elm.variantWith "One" - [ Type.var "oneVar" ] - , Elm.variantWith "Two" - [ Type.var "twoVar" ] - ] - ) - """ - type MyType addVar twoVar oneVar - = One oneVar - | Two twoVar - """ + Elm.customTypeWith "MyType" + [ "addVar", "twoVar" ] + [ Elm.variantWith "One" + [ Type.var "oneVar" ] + , Elm.variantWith "Two" + [ Type.var "twoVar" ] + ] + |> Elm.Expect.declarationAs + """ + type MyType addVar twoVar oneVar + = One oneVar + | Two twoVar + """ type alias MyMaybe = @@ -225,58 +225,55 @@ makeAndCaseGeneration = in Expect.all [ \_ -> - Elm.Expect.declarationAs maybe.declaration - """ - type Maybe a - = Nothing - | Just a - """ + maybe.declaration + |> Elm.Expect.declarationAs + """ + type Maybe a + = Nothing + | Just a + """ , \_ -> - Elm.Expect.renderedAs - maybe.make_.nothing - "Nothing" + maybe.make_.nothing + |> Elm.Expect.renderedAs "Nothing" , \_ -> - Elm.Expect.renderedAs - (maybe.make_.just Elm.unit) - "Just ()" + maybe.make_.just Elm.unit + |> Elm.Expect.renderedAs "Just ()" , \_ -> - Elm.Expect.renderedAs - (maybe.case_ (Elm.val "a") - { just = \a -> a - , nothing = Elm.string "" - } - ) - """ - case a of - Nothing -> - "" - - Just arg0 -> - arg0 - """ + maybe.case_ (Elm.val "a") + { just = \a -> a + , nothing = Elm.string "" + } + |> Elm.Expect.renderedAs + """ + case a of + Nothing -> + "" + + Just arg0 -> + arg0 + """ , \_ -> - Elm.Expect.renderedAs - external.call.maybe.make_.nothing - "MyMaybe.Nothing" + external.call.maybe.make_.nothing + |> Elm.Expect.renderedAs + "MyMaybe.Nothing" , \_ -> - Elm.Expect.renderedAs - (external.call.maybe.make_.just Elm.unit) - "MyMaybe.Just ()" + external.call.maybe.make_.just Elm.unit + |> Elm.Expect.renderedAs + "MyMaybe.Just ()" , \_ -> - Elm.Expect.renderedAs - (external.call.maybe.case_ (Elm.val "a") - { just = \a -> a - , nothing = Elm.string "" - } - ) - """ - case a of - MyMaybe.Nothing -> - "" - - MyMaybe.Just arg0 -> - arg0 - """ + external.call.maybe.case_ (Elm.val "a") + { just = \a -> a + , nothing = Elm.string "" + } + |> Elm.Expect.renderedAs + """ + case a of + MyMaybe.Nothing -> + "" + + MyMaybe.Just arg0 -> + arg0 + """ ] () @@ -315,94 +312,87 @@ makeAndCaseGenerationFullyDynamic = in Expect.all [ \_ -> - Elm.Expect.declarationAs semaphore.declaration - """ - type Semaphore - = Red - | Yellow - | Green - """ + semaphore.declaration + |> Elm.Expect.declarationAs + """ + type Semaphore + = Red + | Yellow + | Green + """ , \_ -> - Elm.Expect.renderedAs - (semaphore.make_ "Red") - "Red" + semaphore.make_ "Red" + |> Elm.Expect.renderedAs "Red" , \_ -> - Elm.Expect.renderedAs - (semaphore.make_ "Yellow") - "Yellow" + semaphore.make_ "Yellow" + |> Elm.Expect.renderedAs "Yellow" , \_ -> - Elm.Expect.renderedAs - (semaphore.make_ "Green") - "Green" + semaphore.make_ "Green" + |> Elm.Expect.renderedAs "Green" , \_ -> - Elm.Expect.renderedAs - (semaphore.case_ (Elm.val "a") - (\color -> - case color of - "Red" -> - Elm.string "Red" - - "Green" -> - Elm.string "Green" - - "Yellow" -> - Elm.string "Yellow" - - _ -> - Gen.Debug.todo "This doesn't happen" - ) - ) - """ - case a of - Red -> - "Red" + semaphore.case_ (Elm.val "a") + (\color -> + case color of + "Red" -> + Elm.string "Red" - Yellow -> - "Yellow" + "Green" -> + Elm.string "Green" - Green -> - "Green" - """ + "Yellow" -> + Elm.string "Yellow" + + _ -> + Gen.Debug.todo "This doesn't happen" + ) + |> Elm.Expect.renderedAs + """ + case a of + Red -> + "Red" + + Yellow -> + "Yellow" + + Green -> + "Green" + """ , \_ -> - Elm.Expect.renderedAs - (external.call.make_ "Red") - "Semaphore.Red" + external.call.make_ "Red" + |> Elm.Expect.renderedAs "Semaphore.Red" , \_ -> - Elm.Expect.renderedAs - (external.call.make_ "Yellow") - "Semaphore.Yellow" + external.call.make_ "Yellow" + |> Elm.Expect.renderedAs "Semaphore.Yellow" , \_ -> - Elm.Expect.renderedAs - (external.call.make_ "Green") - "Semaphore.Green" + external.call.make_ "Green" + |> Elm.Expect.renderedAs "Semaphore.Green" , \_ -> - Elm.Expect.renderedAs - (external.call.case_ (Elm.val "a") - (\color -> - case color of - "Red" -> - Elm.string "Red" - - "Green" -> - Elm.string "Green" - - "Yellow" -> - Elm.string "Yellow" - - _ -> - Gen.Debug.todo "This doesn't happen" - ) - ) - """ - case a of - Semaphore.Red -> - "Red" + external.call.case_ (Elm.val "a") + (\color -> + case color of + "Red" -> + Elm.string "Red" - Semaphore.Yellow -> - "Yellow" + "Green" -> + Elm.string "Green" - Semaphore.Green -> - "Green" - """ + "Yellow" -> + Elm.string "Yellow" + + _ -> + Gen.Debug.todo "This doesn't happen" + ) + |> Elm.Expect.renderedAs + """ + case a of + Semaphore.Red -> + "Red" + + Semaphore.Yellow -> + "Yellow" + + Semaphore.Green -> + "Green" + """ ] () diff --git a/tests/Elm/Expect.elm b/tests/Elm/Expect.elm index 0a8c83ca..9bb5b32d 100644 --- a/tests/Elm/Expect.elm +++ b/tests/Elm/Expect.elm @@ -1,55 +1,107 @@ -module Elm.Expect exposing (declarationAs, equal, fileContentAs, importAs, renderedAs) +module Elm.Expect exposing (declarationAs, equal, expectEqualMultiline, fileContentAs, importAs, importAsWith, renderedAs, renderedAsWith) +import Ansi.Color +import Diff +import Diff.ToString import Elm import Elm.ToString import Expect exposing (Expectation) import String.Multiline -renderedAs : Elm.Expression -> String -> Expectation -renderedAs expression str = - (Elm.ToString.expression expression +renderedAs : String -> Elm.Expression -> Expectation +renderedAs str expression = + Elm.ToString.expression expression |> .body - ) - |> Expect.equal + |> expectEqualMultiline (String.Multiline.here str) -importAs : Elm.Expression -> String -> Expectation -importAs expression str = - Expect.equal - (Elm.ToString.expression expression - |> .imports - ) - (String.Multiline.here str) +renderedAsWith : + { aliases : List ( List String, String ) } + -> String + -> Elm.Expression + -> Expectation +renderedAsWith options str expression = + Elm.ToString.expressionWith options expression + |> .body + |> expectEqualMultiline + (String.Multiline.here str) + + +importAs : String -> Elm.Expression -> Expectation +importAs str expression = + Elm.ToString.expression expression + |> .imports + |> expectEqualMultiline + (String.Multiline.here str) + + +importAsWith : + { aliases : List ( List String, String ) } + -> String + -> Elm.Expression + -> Expectation +importAsWith options str expression = + Elm.ToString.expressionWith options expression + |> .imports + |> expectEqualMultiline + (String.Multiline.here str) -declarationAs : Elm.Declaration -> String -> Expectation -declarationAs decl str = - Expect.equal - (Elm.ToString.declaration decl - |> List.map (\{ body } -> String.trim body) - |> String.join "\n\n" - ) - (String.Multiline.here str) +declarationAs : String -> Elm.Declaration -> Expectation +declarationAs str decl = + Elm.ToString.declaration decl + |> List.map (\{ body } -> body) + |> String.join "\n\n" + |> expectEqualMultiline + (String.Multiline.here str) -fileContentAs : Elm.File -> String -> Expectation -fileContentAs file str = - Expect.equal - (file - |> .contents - |> String.trim - ) - (String.Multiline.here str) +fileContentAs : String -> Elm.File -> Expectation +fileContentAs str file = + file + |> .contents + |> String.trim + |> expectEqualMultiline + (String.Multiline.here str) equal : Elm.Expression -> Elm.Expression -> Expectation equal one two = - Expect.equal - (Elm.ToString.expression one - |> .body - ) - (Elm.ToString.expression two - |> .body - ) + Elm.ToString.expression two + |> .body + |> expectEqualMultiline + (Elm.ToString.expression one + |> .body + ) + + +expectEqualMultiline : String -> String -> Expect.Expectation +expectEqualMultiline exp actual = + let + trimLeadingWhitespace str = + str + |> String.split "\n" + |> List.map String.trimRight + |> String.join "\n" + in + if trimLeadingWhitespace exp == trimLeadingWhitespace actual then + Expect.pass + + else + let + header : String + header = + Ansi.Color.fontColor Ansi.Color.blue "Diff from expected to actual:" + in + Expect.fail + (header + ++ "\n" + ++ (Diff.diffLinesWith + Diff.defaultOptions + exp + actual + |> Diff.ToString.diffToString { context = 4, color = True } + ) + ) diff --git a/tests/File.elm b/tests/File.elm index 53124f04..97781279 100644 --- a/tests/File.elm +++ b/tests/File.elm @@ -39,70 +39,74 @@ suite = , describe "module doc" [ test "present when has content" <| \_ -> - Elm.Expect.fileContentAs (Elm.file [ "Test" ] [ Elm.expose testFn1 ]) - """ -module Test exposing ( testFn1 ) + Elm.file [ "Test" ] [ Elm.expose testFn1 ] + |> Elm.Expect.fileContentAs + """ + module Test exposing ( testFn1 ) -{-| -@docs testFn1 --} + {-| + @docs testFn1 + -} -testFn1 : arg -> arg -testFn1 arg = - arg -""" + testFn1 : arg -> arg + testFn1 arg = + arg + """ , test "absent when has no content" <| \_ -> - Elm.Expect.fileContentAs (Elm.file [ "Test" ] [ testFn1 ]) - """ -module Test exposing (..) + Elm.file [ "Test" ] [ testFn1 ] + |> Elm.Expect.fileContentAs + """ + module Test exposing (..) -testFn1 : arg -> arg -testFn1 arg = - arg -""" + testFn1 : arg -> arg + testFn1 arg = + arg + """ , test "declaration are in the correct order" <| \_ -> - Elm.Expect.fileContentAs (Elm.file [ "Test" ] [ Elm.expose testFn1, Elm.expose testFn2 ]) - """ -module Test exposing ( testFn1, testFn2 ) + Elm.file [ "Test" ] [ Elm.expose testFn1, Elm.expose testFn2 ] + |> Elm.Expect.fileContentAs + """ + module Test exposing ( testFn1, testFn2 ) -{-| -@docs testFn1, testFn2 --} + {-| + @docs testFn1, testFn2 + -} -testFn1 : arg -> arg -testFn1 arg = - arg + testFn1 : arg -> arg + testFn1 arg = + arg -testFn2 : arg -> arg -testFn2 arg = - arg -""" + testFn2 : arg -> arg + testFn2 arg = + arg + """ ] , describe "trailing lines" [ test "no extra lines at the end of the file" <| \_ -> - Elm.Expect.fileContentAs (Elm.file [ "Test" ] [ testFn1, testFn2 ]) - """ -module Test exposing (..) + Elm.file [ "Test" ] [ testFn1, testFn2 ] + |> Elm.Expect.fileContentAs + """ + module Test exposing (..) -testFn1 : arg -> arg -testFn1 arg = - arg + testFn1 : arg -> arg + testFn1 arg = + arg -testFn2 : arg -> arg -testFn2 arg = - arg -""" + testFn2 : arg -> arg + testFn2 arg = + arg + """ ] ] @@ -112,133 +116,133 @@ grouping = describe "Grouping exposed" [ test "Group items into sections" <| \_ -> - Elm.Expect.fileContentAs - (Elm.file [ "Test" ] - [ placeholder "one" - , placeholder "two" - , Elm.group - [ Elm.docs "## My group" - , Elm.docs "Here's how to use it" - , placeholder "three" - , placeholder "four" - ] - , placeholder "five" - , placeholder "six" + Elm.file [ "Test" ] + [ placeholder "one" + , placeholder "two" + , Elm.group + [ Elm.docs "## My group" + , Elm.docs "Here's how to use it" + , placeholder "three" + , placeholder "four" ] - ) - """module Test exposing (..) + , placeholder "five" + , placeholder "six" + ] + |> Elm.Expect.fileContentAs + """ + module Test exposing (..) -{-| -## My group + {-| + ## My group -Here's how to use it --} + Here's how to use it + -} -one : arg -> arg -one arg = - arg + one : arg -> arg + one arg = + arg -two : arg -> arg -two arg = - arg + two : arg -> arg + two arg = + arg -three : arg -> arg -three arg = - arg + three : arg -> arg + three arg = + arg -four : arg -> arg -four arg = - arg + four : arg -> arg + four arg = + arg -five : arg -> arg -five arg = - arg + five : arg -> arg + five arg = + arg -six : arg -> arg -six arg = - arg -""" + six : arg -> arg + six arg = + arg + """ , test "only include @docs if something is specifically exposed" <| \_ -> - Elm.Expect.fileContentAs - (Elm.file [ "Test" ] - [ placeholder "one" - |> Elm.expose - , placeholder "two" - , Elm.group - [ Elm.docs "## My group" - , Elm.docs "Here's how to use it" - , placeholder "three" - , placeholder "four" - |> Elm.expose - , placeholder "fourPointFive" - |> Elm.expose - ] - , placeholder "five" + Elm.file [ "Test" ] + [ placeholder "one" + |> Elm.expose + , placeholder "two" + , Elm.group + [ Elm.docs "## My group" + , Elm.docs "Here's how to use it" + , placeholder "three" + , placeholder "four" |> Elm.expose - , placeholder "six" + , placeholder "fourPointFive" |> Elm.expose ] - ) - """module Test exposing - ( one - , four, fourPointFive - , five, six - ) + , placeholder "five" + |> Elm.expose + , placeholder "six" + |> Elm.expose + ] + |> Elm.Expect.fileContentAs + """ + module Test exposing + ( one + , four, fourPointFive + , five, six + ) -{-| -@docs one + {-| + @docs one -## My group + ## My group -Here's how to use it + Here's how to use it -@docs four, fourPointFive + @docs four, fourPointFive -@docs five, six --} + @docs five, six + -} -one : arg -> arg -one arg = - arg + one : arg -> arg + one arg = + arg -two : arg -> arg -two arg = - arg + two : arg -> arg + two arg = + arg -three : arg -> arg -three arg = - arg + three : arg -> arg + three arg = + arg -four : arg -> arg -four arg = - arg + four : arg -> arg + four arg = + arg -fourPointFive : arg -> arg -fourPointFive arg = - arg + fourPointFive : arg -> arg + fourPointFive arg = + arg -five : arg -> arg -five arg = - arg + five : arg -> arg + five arg = + arg -six : arg -> arg -six arg = - arg -""" + six : arg -> arg + six arg = + arg + """ ] diff --git a/tests/Let.elm b/tests/Let.elm index 9f759905..353f0485 100644 --- a/tests/Let.elm +++ b/tests/Let.elm @@ -22,113 +22,108 @@ imports = describe "Import propagation" [ test "Simple" <| \_ -> - Elm.Expect.importAs - (Elm.Let.letIn - (\one two -> - Elm.Op.append one two - ) - |> Elm.Let.value "one" - (Elm.value - { importFrom = [ "Module" ] - , name = "constant" - , annotation = Nothing - } - ) - |> Elm.Let.value "two" (Elm.string "World!") - |> Elm.Let.toExpression + Elm.Let.letIn + (\one two -> + Elm.Op.append one two ) - "import Module" + |> Elm.Let.value "one" + (Elm.value + { importFrom = [ "Module" ] + , name = "constant" + , annotation = Nothing + } + ) + |> Elm.Let.value "two" (Elm.string "World!") + |> Elm.Let.toExpression + |> Elm.Expect.importAs + "import Module" , test "Second" <| \_ -> - Elm.Expect.importAs - (Elm.Let.letIn - (\one two -> - Elm.Op.append one two + Elm.Let.letIn + (\one two -> + Elm.Op.append one two + ) + |> Elm.Let.value "two" (Elm.string "World!") + |> Elm.Let.value "one" + (Elm.value + { importFrom = [ "Module" ] + , name = "constant" + , annotation = Nothing + } ) - |> Elm.Let.value "two" (Elm.string "World!") - |> Elm.Let.value "one" + |> Elm.Let.toExpression + |> Elm.Expect.importAs + "import Module" + , test "Tuple" <| + \_ -> + Elm.Let.letIn + (\( one, two ) -> + Elm.Op.append one two + ) + |> Elm.Let.unpack + (Arg.tuple (Arg.var "one") (Arg.var "two")) + (Elm.tuple (Elm.value { importFrom = [ "Module" ] , name = "constant" , annotation = Nothing } ) - |> Elm.Let.toExpression - ) - "import Module" - , test "Tuple" <| + (Elm.string "World!") + ) + |> Elm.Let.toExpression + |> Elm.Expect.importAs + "import Module" + , test "Record" <| \_ -> - Elm.Expect.importAs - (Elm.Let.letIn - (\( one, two ) -> - Elm.Op.append one two + Elm.Let.letIn + (\( one, two ) -> + Elm.Op.append one two + ) + |> Elm.Let.unpack + (Arg.record Tuple.pair + |> Arg.field "one" + |> Arg.field "two" ) - |> Elm.Let.unpack - (Arg.tuple (Arg.var "one") (Arg.var "two")) - (Elm.tuple - (Elm.value + (Elm.record + [ ( "one" + , Elm.value { importFrom = [ "Module" ] , name = "constant" , annotation = Nothing } - ) - (Elm.string "World!") - ) - |> Elm.Let.toExpression - ) - "import Module" - , test "Record" <| - \_ -> - Elm.Expect.importAs - (Elm.Let.letIn - (\( one, two ) -> - Elm.Op.append one two + ) + , ( "two", Elm.string "World!" ) + ] ) - |> Elm.Let.unpack - (Arg.record Tuple.pair - |> Arg.field "one" - |> Arg.field "two" - ) - (Elm.record - [ ( "one" - , Elm.value - { importFrom = [ "Module" ] - , name = "constant" - , annotation = Nothing - } - ) - , ( "two", Elm.string "World!" ) - ] - ) - |> Elm.Let.toExpression - ) - "import Module" + |> Elm.Let.toExpression + |> Elm.Expect.importAs + "import Module" , test "Nested" <| \_ -> - Elm.Expect.importAs - (Elm.Let.letIn - (\one two -> - Elm.Let.letIn - (\three four -> - Elm.tuple - (Elm.Op.append one two) - (Elm.Op.append three four) + Elm.Let.letIn + (\one two -> + Elm.Let.letIn + (\three four -> + Elm.tuple + (Elm.Op.append one two) + (Elm.Op.append three four) + ) + |> Elm.Let.value "three" (Elm.string "and") + |> Elm.Let.value "four" + (Elm.value + { importFrom = [ "Module" ] + , name = "constant" + , annotation = Nothing + } ) - |> Elm.Let.value "three" (Elm.string "and") - |> Elm.Let.value "four" - (Elm.value - { importFrom = [ "Module" ] - , name = "constant" - , annotation = Nothing - } - ) - |> Elm.Let.toExpression - ) - |> Elm.Let.value "one" (Elm.string "Hello") - |> Elm.Let.value "two" (Elm.string "World!") - |> Elm.Let.toExpression + |> Elm.Let.toExpression ) - "import Module" + |> Elm.Let.value "one" (Elm.string "Hello") + |> Elm.Let.value "two" (Elm.string "World!") + |> Elm.Let.toExpression + |> Elm.Expect.importAs + "import Module" ] @@ -137,162 +132,156 @@ generation = describe "Generation" [ test "Simple" <| \_ -> - Elm.Expect.renderedAs - (Elm.Let.letIn - (\one two -> - Elm.Op.append one two - ) - |> Elm.Let.value "one" (Elm.string "Hello") - |> Elm.Let.value "two" (Elm.string "World!") - |> Elm.Let.toExpression + Elm.Let.letIn + (\one two -> + Elm.Op.append one two ) - """ -let - one = - "Hello" + |> Elm.Let.value "one" (Elm.string "Hello") + |> Elm.Let.value "two" (Elm.string "World!") + |> Elm.Let.toExpression + |> Elm.Expect.renderedAs + """ + let + one = + "Hello" - two = - "World!" -in -one ++ two - """ + two = + "World!" + in + one ++ two + """ , test "Nested lets will merge in to one" <| \_ -> - Elm.Expect.renderedAs - (Elm.Let.letIn - (\one two -> - Elm.Let.letIn - (\three four -> - Elm.tuple - (Elm.Op.append one two) - (Elm.Op.append three four) - ) - |> Elm.Let.value "three" (Elm.string "and") - |> Elm.Let.value "four" (Elm.string "the moon") - |> Elm.Let.toExpression - ) - |> Elm.Let.value "one" (Elm.string "Hello") - |> Elm.Let.value "two" (Elm.string "World!") - |> Elm.Let.toExpression + Elm.Let.letIn + (\one two -> + Elm.Let.letIn + (\three four -> + Elm.tuple + (Elm.Op.append one two) + (Elm.Op.append three four) + ) + |> Elm.Let.value "three" (Elm.string "and") + |> Elm.Let.value "four" (Elm.string "the moon") + |> Elm.Let.toExpression ) - """ -let - one = - "Hello" + |> Elm.Let.value "one" (Elm.string "Hello") + |> Elm.Let.value "two" (Elm.string "World!") + |> Elm.Let.toExpression + |> Elm.Expect.renderedAs + """ + let + one = + "Hello" - two = - "World!" + two = + "World!" - three = - "and" + three = + "and" - four = - "the moon" -in -( one ++ two, three ++ four ) - """ + four = + "the moon" + in + ( one ++ two, three ++ four ) + """ , test "Tuples" <| \_ -> - Elm.Expect.renderedAs - (Elm.Let.letIn - (\( first, second ) -> - Elm.Op.append first second - ) - |> Elm.Let.unpack - (Arg.tuple - (Arg.var "first") - (Arg.var "second") - ) - (Elm.tuple (Elm.string "Hello") (Elm.string "World!")) - |> Elm.Let.toExpression + Elm.Let.letIn + (\( first, second ) -> + Elm.Op.append first second ) - """ -let - ( first, second ) = - ( "Hello", "World!" ) -in -first ++ second - """ + |> Elm.Let.unpack + (Arg.tuple + (Arg.var "first") + (Arg.var "second") + ) + (Elm.tuple (Elm.string "Hello") (Elm.string "World!")) + |> Elm.Let.toExpression + |> Elm.Expect.renderedAs + """ + let + ( first, second ) = + ( "Hello", "World!" ) + in + first ++ second + """ , test "Records" <| \_ -> - Elm.Expect.renderedAs - (Elm.Let.letIn - (\( first, second ) -> - Elm.Op.append first second - ) - |> Elm.Let.unpack - (Arg.record Tuple.pair - |> Arg.field "first" - |> Arg.field "second" - ) - (Elm.record - [ ( "first", Elm.string "Hello" ) - , ( "second", Elm.string "world!" ) - ] - ) - |> Elm.Let.toExpression + Elm.Let.letIn + (\( first, second ) -> + Elm.Op.append first second ) - """ -let - { first, second } = - { first = "Hello", second = "world!" } -in -first ++ second - """ + |> Elm.Let.unpack + (Arg.record Tuple.pair + |> Arg.field "first" + |> Arg.field "second" + ) + (Elm.record + [ ( "first", Elm.string "Hello" ) + , ( "second", Elm.string "world!" ) + ] + ) + |> Elm.Let.toExpression + |> Elm.Expect.renderedAs + """ + let + { first, second } = + { first = "Hello", second = "world!" } + in + first ++ second + """ , test "Elm.fn" <| \_ -> - Elm.Expect.renderedAs - (Elm.Let.letIn - (\myFn -> - Elm.apply myFn [ Elm.bool True ] - ) - |> Elm.Let.value "myFn" - (Elm.fn - (Arg.varWith "arg" Type.bool) - (\arg -> - Elm.ifThen arg - (Elm.string "True") - (Elm.string "False") - ) - ) - |> Elm.Let.toExpression + Elm.Let.letIn + (\myFn -> + Elm.apply myFn [ Elm.bool True ] ) - """ -let - myFn arg = - if arg then - "True" - - else - "False" -in -myFn True - """ - , test "Function" <| - \_ -> - Elm.Expect.renderedAs - (Elm.Let.letIn - (\myFn -> - myFn (Elm.bool True) - ) - |> Elm.Let.fn "myFn" + |> Elm.Let.value "myFn" + (Elm.fn (Arg.varWith "arg" Type.bool) (\arg -> Elm.ifThen arg (Elm.string "True") (Elm.string "False") ) - |> Elm.Let.toExpression + ) + |> Elm.Let.toExpression + |> Elm.Expect.renderedAs + """ + let + myFn arg = + if arg then + "True" + + else + "False" + in + myFn True + """ + , test "Function" <| + \_ -> + Elm.Let.letIn + (\myFn -> + myFn (Elm.bool True) ) - """ -let - myFn arg = - if arg then - "True" + |> Elm.Let.fn "myFn" + (Arg.varWith "arg" Type.bool) + (\arg -> + Elm.ifThen arg + (Elm.string "True") + (Elm.string "False") + ) + |> Elm.Let.toExpression + |> Elm.Expect.renderedAs + """ + let + myFn arg = + if arg then + "True" - else - "False" -in -myFn True - """ + else + "False" + in + myFn True + """ ] diff --git a/tests/Module.elm b/tests/Module.elm index e95e4a11..b9c5370a 100644 --- a/tests/Module.elm +++ b/tests/Module.elm @@ -6,7 +6,7 @@ import Elm import Elm.Annotation as Type import Elm.Arg as Arg import Elm.Declare as Declare -import Expect +import Elm.Expect import Test exposing (Test, describe, test) @@ -66,42 +66,42 @@ usageInsideModule = describe "Usage inside module is unqualified" [ test "Type reference" <| \_ -> - let - file = - Elm.file [ "Virtual", "Module" ] - [ Elm.declaration "test" - (Elm.fn - (Arg.varWith "a" mod.call.myType) - (\a -> - a - ) - ) - ] - in - Expect.equal file.contents """module Virtual.Module exposing (..) - - -test : MyType -> MyType -test a = - a""" + Elm.file [ "Virtual", "Module" ] + [ Elm.declaration "test" + (Elm.fn + (Arg.varWith "a" mod.call.myType) + (\a -> + a + ) + ) + ] + |> Elm.Expect.fileContentAs + """ + module Virtual.Module exposing (..) + + + test : MyType -> MyType + test a = + a + """ , test "Function" <| \_ -> - let - file = - Elm.file [ "Virtual", "Module" ] - [ Elm.declaration "test" - (Elm.fn - (Arg.var "a") - (\a -> mod.call.myFn a a) - ) - ] - in - Expect.equal file.contents """module Virtual.Module exposing (..) - - -test : Int -> Test -test a = - myFn a a""" + Elm.file [ "Virtual", "Module" ] + [ Elm.declaration "test" + (Elm.fn + (Arg.var "a") + (\a -> mod.call.myFn a a) + ) + ] + |> Elm.Expect.fileContentAs + """ + module Virtual.Module exposing (..) + + + test : Int -> Test + test a = + myFn a a + """ ] @@ -110,46 +110,45 @@ usageOutsideModule = describe "Usage outside module is qualified" [ test "Type reference" <| \_ -> - let - file = - Elm.file [ "My", "Module" ] - [ Elm.declaration "test" - (Elm.fn - (Arg.varWith "a" mod.call.myType) - (\a -> - a - ) - ) - ] - in - Expect.equal file.contents """module My.Module exposing (..) - -import Virtual.Module - - -test : Virtual.Module.MyType -> Virtual.Module.MyType -test a = - a""" + Elm.file [ "My", "Module" ] + [ Elm.declaration "test" + (Elm.fn + (Arg.varWith "a" mod.call.myType) + (\a -> + a + ) + ) + ] + |> Elm.Expect.fileContentAs + """ + module My.Module exposing (..) + + import Virtual.Module + + + test : Virtual.Module.MyType -> Virtual.Module.MyType + test a = + a""" , test "Function" <| \_ -> - let - file = - Elm.file [ "My", "Module" ] - [ Elm.declaration "test" - (Elm.fn - (Arg.var "a") - (\a -> mod.call.myFn a a) - ) - ] - in - Expect.equal file.contents """module My.Module exposing (..) - -import Virtual.Module - - -test : Int -> Test -test a = - Virtual.Module.myFn a a""" + Elm.file [ "My", "Module" ] + [ Elm.declaration "test" + (Elm.fn + (Arg.var "a") + (\a -> mod.call.myFn a a) + ) + ] + |> Elm.Expect.fileContentAs + """ + module My.Module exposing (..) + + import Virtual.Module + + + test : Int -> Test + test a = + Virtual.Module.myFn a a + """ ] @@ -157,32 +156,35 @@ fileGeneration : Test fileGeneration = test "File Generation" <| \_ -> - Expect.equal (Declare.toFile mod |> .contents) - (String.trim """module Virtual.Module exposing ( MyInt, MyType, myFn ) + Declare.toFile mod + |> Elm.Expect.fileContentAs + """ + module Virtual.Module exposing ( MyInt, MyType, myFn ) -{-| -@docs MyInt, MyType, myFn --} + {-| + @docs MyInt, MyType, myFn + -} -type alias MyInt = - Int + type alias MyInt = + Int -type MyType - = MyType + type MyType + = MyType -myFn : Int -> Int -> Test -myFn firstArg secondArg = - testFn - (Test - { one = firstArg - , two = firstArg - , three = firstArg - , four = firstArg - , five = firstArg - , six = firstArg - } - )""") + myFn : Int -> Int -> Test + myFn firstArg secondArg = + testFn + (Test + { one = firstArg + , two = firstArg + , three = firstArg + , four = firstArg + , five = firstArg + , six = firstArg + } + ) + """ diff --git a/tests/OperatorPrecedence.elm b/tests/OperatorPrecedence.elm index be55a6f5..a731a8ba 100644 --- a/tests/OperatorPrecedence.elm +++ b/tests/OperatorPrecedence.elm @@ -33,24 +33,24 @@ get = describe "Elm.get" [ test "Works correctly if the argument is a function call" <| \_ -> - Elm.Expect.renderedAs (Elm.get "field" (todo "todo")) - """(Debug.todo "todo").field""" + Elm.get "field" (todo "todo") + |> Elm.Expect.renderedAs """(Debug.todo "todo").field""" , test "Works correctly if the argument is a value" <| \_ -> - Elm.Expect.renderedAs (Elm.get "field" valueTodo) - """Debug.todo.field""" + Elm.get "field" valueTodo + |> Elm.Expect.renderedAs """Debug.todo.field""" , test "Works correctly if the argument is a record" <| \_ -> - Elm.Expect.renderedAs (Elm.get "field" (Elm.record [])) - """{}.field""" + Elm.get "field" (Elm.record []) + |> Elm.Expect.renderedAs """{}.field""" , test "Works correctly if the argument is a record update" <| \_ -> - Elm.Expect.renderedAs (Elm.get "field" (Elm.updateRecord [ ( "bar", Elm.int 3 ) ] (Elm.val "foo"))) - """{ foo | bar = 3 }.field""" + Elm.get "field" (Elm.updateRecord [ ( "bar", Elm.int 3 ) ] (Elm.val "foo")) + |> Elm.Expect.renderedAs """{ foo | bar = 3 }.field""" , test "Works correctly if the argument is a field" <| \_ -> - Elm.Expect.renderedAs (Elm.get "field2" (Elm.get "field1" (Elm.record []))) - """{}.field1.field2""" + Elm.get "field2" (Elm.get "field1" (Elm.record [])) + |> Elm.Expect.renderedAs """{}.field1.field2""" ] @@ -72,42 +72,42 @@ operators = describe "+ and *" [ test "(2 + 3) + 5" <| \_ -> - Elm.Expect.renderedAs (plus (plus two three) five) - """2 + 3 + 5""" + plus (plus two three) five + |> Elm.Expect.renderedAs """2 + 3 + 5""" , test "(2 * 3) * 5" <| \_ -> - Elm.Expect.renderedAs (multiply (multiply two three) five) - """2 * 3 * 5""" + multiply (multiply two three) five + |> Elm.Expect.renderedAs """2 * 3 * 5""" , test "(2 + 3) * 5" <| \_ -> - Elm.Expect.renderedAs (multiply (plus two three) five) - """(2 + 3) * 5""" + multiply (plus two three) five + |> Elm.Expect.renderedAs """(2 + 3) * 5""" , test "(2 * 3) + 5" <| \_ -> - Elm.Expect.renderedAs (plus (multiply two three) five) - """2 * 3 + 5""" + plus (multiply two three) five + |> Elm.Expect.renderedAs """2 * 3 + 5""" , test "2 + (3 * 5)" <| \_ -> - Elm.Expect.renderedAs (plus two (multiply three five)) - """2 + 3 * 5""" + plus two (multiply three five) + |> Elm.Expect.renderedAs """2 + 3 * 5""" , test "2 * (3 + 5)" <| \_ -> - Elm.Expect.renderedAs (multiply two (plus three five)) - """2 * (3 + 5)""" + multiply two (plus three five) + |> Elm.Expect.renderedAs """2 * (3 + 5)""" , test "(2 + 3) + (3 + 5)" <| \_ -> - Elm.Expect.renderedAs (plus (plus two three) (plus three five)) - "2 + 3 + (3 + 5)" + plus (plus two three) (plus three five) + |> Elm.Expect.renderedAs "2 + 3 + (3 + 5)" , test "(2 == 3) == (3 == 5)" <| \_ -> - Elm.Expect.renderedAs (equal (equal two three) (equal three five)) - "(2 == 3) == (3 == 5)" + equal (equal two three) (equal three five) + |> Elm.Expect.renderedAs "(2 == 3) == (3 == 5)" , test "2 || (3 || 5)" <| \_ -> - Elm.Expect.renderedAs (or two (or three five)) - "2 || 3 || 5" + or two (or three five) + |> Elm.Expect.renderedAs "2 || 3 || 5" , test "(2 || 3) || 5" <| \_ -> - Elm.Expect.renderedAs (or (or two three) five) - "(2 || 3) || 5" + or (or two three) five + |> Elm.Expect.renderedAs "(2 || 3) || 5" ] diff --git a/tests/PackageHelpers.elm b/tests/PackageHelpers.elm index 911de904..c5a9c49c 100644 --- a/tests/PackageHelpers.elm +++ b/tests/PackageHelpers.elm @@ -122,30 +122,27 @@ packageHelpers = |> Result.withDefault True in Expect.equal False cycles - , test "typevariables in functions within a record dont pollute each other" <| + , test "typevariables in functions within a record don't pollute each other" <| \_ -> - Expect.equal - (Elm.record - [ Elm.fn (Elm.Arg.var "arg") - (\arg -> - Elm.Op.plus arg (Elm.int 5) - ) - |> Tuple.pair "one" - , Elm.fn (Elm.Arg.var "arg") - (\arg -> - Elm.Op.append arg (Elm.string "World") - ) - |> Tuple.pair "two" - ] - |> Elm.declaration "test" - |> Elm.ToString.declaration - |> List.map .body - |> String.join "\n" - |> String.trim - ) - """test : { one : Int -> Int, two : String -> String } -test = - { one = \\arg -> arg + 5, two = \\arg -> arg ++ "World" }""" + Elm.record + [ Elm.fn (Elm.Arg.var "arg") + (\arg -> + Elm.Op.plus arg (Elm.int 5) + ) + |> Tuple.pair "one" + , Elm.fn (Elm.Arg.var "arg") + (\arg -> + Elm.Op.append arg (Elm.string "World") + ) + |> Tuple.pair "two" + ] + |> Elm.declaration "test" + |> Elm.Expect.declarationAs + """ + test : { one : Int -> Int, two : String -> String } + test = + { one = \\arg -> arg + 5, two = \\arg -> arg ++ "World" } + """ ] @@ -169,13 +166,12 @@ stringifying = ) |> Tuple.pair "one" ] - |> Elm.ToString.expressionWith + |> Elm.Expect.renderedAsWith { aliases = [ ( [ "Longer", "Module" ], "Ui" ) ] } - |> .body - |> Expect.equal "{ one = \\arg -> Ui.gorthalax arg }" + "{ one = \\arg -> Ui.gorthalax arg }" , test "expressionWith generates aliases as expected" <| \_ -> Elm.record @@ -193,43 +189,40 @@ stringifying = ) |> Tuple.pair "one" ] - |> Elm.ToString.expressionWith + |> Elm.Expect.importAsWith { aliases = [ ( [ "Longer", "Module" ], "Ui" ) ] } - |> .imports - |> Expect.equal "import Longer.Module as Ui" + "import Longer.Module as Ui" , test "declarations named `main` do not get sanitized" <| \_ -> - Elm.Expect.declarationAs - (Elm.declaration "main" - (Elm.int 5) - ) - "main : Int\nmain =\n 5" + Elm.declaration "main" + (Elm.int 5) + |> Elm.Expect.declarationAs + "main : Int\nmain =\n 5" , test "Pipelines are paren-ed correctly" <| \_ -> - Elm.Expect.renderedAs - (Elm.string "Hello" - |> Elm.Op.pipe + Elm.string "Hello" + |> Elm.Op.pipe + (Elm.value + { importFrom = [] + , name = "one" + , annotation = Nothing + } + ) + |> Elm.Op.pipe + (Elm.apply (Elm.value { importFrom = [] - , name = "one" + , name = "two" , annotation = Nothing } ) - |> Elm.Op.pipe - (Elm.apply - (Elm.value - { importFrom = [] - , name = "two" - , annotation = Nothing - } - ) - [ Elm.int 5 - , Elm.string "HELLOOOOOOOOOOOOOO" - ] - ) - ) - """"Hello" |> one |> two 5 "HELLOOOOOOOOOOOOOO\"""" + [ Elm.int 5 + , Elm.string "HELLOOOOOOOOOOOOOO" + ] + ) + |> Elm.Expect.renderedAs + """"Hello" |> one |> two 5 "HELLOOOOOOOOOOOOOO\"""" ] diff --git a/tests/Pattern.elm b/tests/Pattern.elm index 4006b42f..f98516df 100644 --- a/tests/Pattern.elm +++ b/tests/Pattern.elm @@ -14,266 +14,252 @@ suite = describe "Case expressions" [ test "Simple" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.maybe Elm.nothing - { nothing = Elm.unit - , just = ( "_", \_ -> Elm.unit ) - } - ) - """ - case Nothing of - Nothing -> - () + Elm.Case.maybe Elm.nothing + { nothing = Elm.unit + , just = ( "_", \_ -> Elm.unit ) + } + |> Elm.Expect.renderedAs + """ + case Nothing of + Nothing -> + () - Just _ -> - ()""" + Just _ -> + () + """ , test "tuple destructuring" <| \() -> - let - expression : Elm.Expression - expression = - Elm.Case.custom - (Elm.tuple - (Elm.just (Elm.char 'a')) - (Elm.just (Elm.char 'b')) + Elm.Case.custom + (Elm.tuple + (Elm.just (Elm.char 'a')) + (Elm.just (Elm.char 'b')) + ) + (Type.maybe Type.int) + [ Elm.Case.branch + (Arg.tuple + (Arg.customType "Just" identity + |> Arg.item (Arg.char 'a') ) - (Type.maybe Type.int) - [ Elm.Case.branch - (Arg.tuple - (Arg.customType "Just" identity - |> Arg.item (Arg.char 'a') - ) - (Arg.customType "Just" identity - |> Arg.item (Arg.char 'b') - ) - ) - (\_ -> - Elm.int 5 - ) - , Elm.Case.branch - (Arg.tuple - (Arg.customType "just" identity - |> Arg.item (Arg.var "left") - ) - (Arg.customType "Just" identity - |> Arg.item (Arg.var "right") - ) - ) - (\_ -> - Elm.int 8 - ) - , Elm.Case.branch Arg.ignore (\_ -> Elm.int 0) - ] - in - Elm.Expect.renderedAs - expression - """ - case ( Just 'a', Just 'b' ) of - ( Just 'a', Just 'b' ) -> - 5 + (Arg.customType "Just" identity + |> Arg.item (Arg.char 'b') + ) + ) + (\_ -> + Elm.int 5 + ) + , Elm.Case.branch + (Arg.tuple + (Arg.customType "just" identity + |> Arg.item (Arg.var "left") + ) + (Arg.customType "Just" identity + |> Arg.item (Arg.var "right") + ) + ) + (\_ -> + Elm.int 8 + ) + , Elm.Case.branch Arg.ignore (\_ -> Elm.int 0) + ] + |> Elm.Expect.renderedAs + """ + case ( Just 'a', Just 'b' ) of + ( Just 'a', Just 'b' ) -> + 5 - ( Just left, Just right ) -> - 8 + ( Just left, Just right ) -> + 8 - _ -> - 0 - """ + _ -> + 0 + """ , test "triple" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom (Elm.val "foo") - Type.unit - [ Elm.Case.branch - (Arg.triple Arg.unit (Arg.var "name") (Arg.char 'a')) - (\_ -> - Elm.unit - ) - ] - ) - """ - case foo of - ( (), name, 'a' ) -> - () - """ + Elm.Case.custom (Elm.val "foo") + Type.unit + [ Elm.Case.branch + (Arg.triple Arg.unit (Arg.var "name") (Arg.char 'a')) + (\_ -> + Elm.unit + ) + ] + |> Elm.Expect.renderedAs + """ + case foo of + ( (), name, 'a' ) -> + () + """ , test "record destructure" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom - (Elm.record - [ ( "first", Elm.string "Jane" ) - , ( "last", Elm.string "Doe" ) - ] - ) - Type.unit - [ Elm.Case.branch - (Arg.record Tuple.pair - |> Arg.field "first" - |> Arg.field "last" - ) - (\( first, last ) -> - Elm.Op.append first last - ) + Elm.Case.custom + (Elm.record + [ ( "first", Elm.string "Jane" ) + , ( "last", Elm.string "Doe" ) ] ) - """ - case { first = "Jane", last = "Doe" } of - { first, last } -> - first ++ last - """ + Type.unit + [ Elm.Case.branch + (Arg.record Tuple.pair + |> Arg.field "first" + |> Arg.field "last" + ) + (\( first, last ) -> + Elm.Op.append first last + ) + ] + |> Elm.Expect.renderedAs + """ + case { first = "Jane", last = "Doe" } of + { first, last } -> + first ++ last + """ , test "record destructure with as alias" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom - (Elm.record - [ ( "first", Elm.string "Jane" ) - , ( "last", Elm.string "Doe" ) - ] - ) - Type.unit - [ Elm.Case.branch - (Arg.record Tuple.pair - |> Arg.field "first" - |> Arg.field "last" - |> Arg.aliasAs "record" - ) - (\( ( first, second ), record ) -> - Elm.triple record first second - ) + Elm.Case.custom + (Elm.record + [ ( "first", Elm.string "Jane" ) + , ( "last", Elm.string "Doe" ) ] ) - """ - case { first = "Jane", last = "Doe" } of - { first, last } as record -> - ( record, first, last ) - """ + Type.unit + [ Elm.Case.branch + (Arg.record Tuple.pair + |> Arg.field "first" + |> Arg.field "last" + |> Arg.aliasAs "record" + ) + (\( ( first, second ), record ) -> + Elm.triple record first second + ) + ] + |> Elm.Expect.renderedAs + """ + case { first = "Jane", last = "Doe" } of + { first, last } as record -> + ( record, first, last ) + """ , test "custom type helpers" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom - Elm.nothing - (Type.maybe Type.char) - [ Elm.Case.branch - (Arg.customType "Just" identity - |> Arg.item (Arg.char 'c') - ) - (\_ -> Elm.string "There is 1 item") - , Elm.Case.branch - (Arg.customType "Just" identity - |> Arg.item (Arg.var "n") - ) - (\_ -> - Elm.string "There are more items" - ) - , Elm.Case.branch (Arg.customType "Nothing" ()) - (\_ -> Elm.string "Oh, it's nothing.") - ] - ) - """ - case Nothing of - Just 'c' -> - "There is 1 item" + Elm.Case.custom + Elm.nothing + (Type.maybe Type.char) + [ Elm.Case.branch + (Arg.customType "Just" identity + |> Arg.item (Arg.char 'c') + ) + (\_ -> Elm.string "There is 1 item") + , Elm.Case.branch + (Arg.customType "Just" identity + |> Arg.item (Arg.var "n") + ) + (\_ -> + Elm.string "There are more items" + ) + , Elm.Case.branch (Arg.customType "Nothing" ()) + (\_ -> Elm.string "Oh, it's nothing.") + ] + |> Elm.Expect.renderedAs + """ + case Nothing of + Just 'c' -> + "There is 1 item" - Just n -> - "There are more items" + Just n -> + "There are more items" - Nothing -> - "Oh, it's nothing." - """ + Nothing -> + "Oh, it's nothing." + """ , test "custom type helpers - with imports" <| \() -> - Elm.Expect.renderedAs - (Elm.fn + Elm.fn + (Arg.customTypeWith { importFrom = [ "From", "Here" ], variantName = "Variant", typeName = "SomeType" } identity + |> Arg.item (Arg.char 'c') + ) + (\_ -> Elm.string "There is 1 item") + |> Elm.Expect.renderedAs + """ + \\(From.Here.Variant 'c') -> "There is 1 item" + """ + , test "custom type helpers - type qualification" <| + \() -> + (Elm.declaration "fn" <| + Elm.fn (Arg.customTypeWith { importFrom = [ "From", "Here" ], variantName = "Variant", typeName = "SomeType" } identity |> Arg.item (Arg.char 'c') ) (\_ -> Elm.string "There is 1 item") - ) - """ - \\(From.Here.Variant 'c') -> "There is 1 item" - """ - , test "custom type helpers - type qualification" <| - \() -> - Elm.Expect.declarationAs - (Elm.declaration "fn" <| - Elm.fn - (Arg.customTypeWith { importFrom = [ "From", "Here" ], variantName = "Variant", typeName = "SomeType" } identity - |> Arg.item (Arg.char 'c') - ) - (\_ -> Elm.string "There is 1 item") - ) - """ - fn : From.Here.SomeType -> String - fn (From.Here.Variant 'c') = - "There is 1 item" - """ + ) + |> Elm.Expect.declarationAs + """ + fn : From.Here.SomeType -> String + fn (From.Here.Variant 'c') = + "There is 1 item" + """ , describe "literal patterns" [ test "unit" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom Elm.unit - Type.unit - [ Elm.Case.branch - Arg.unit - (\_ -> - Elm.unit - ) - ] - ) - """ - case () of - () -> - () - """ + Elm.Case.custom Elm.unit + Type.unit + [ Elm.Case.branch + Arg.unit + (\_ -> + Elm.unit + ) + ] + |> Elm.Expect.renderedAs + """ + case () of + () -> + () + """ , test "string" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom (Elm.string "Hi!") - Type.string - [ Elm.Case.branch (Arg.string "Hi!") (\_ -> Elm.string "Hello to you!") - , Elm.Case.branch Arg.ignore (\_ -> Elm.string "Excuse me?") - ] - ) - """ - case "Hi!" of - "Hi!" -> - "Hello to you!" + Elm.Case.custom (Elm.string "Hi!") + Type.string + [ Elm.Case.branch (Arg.string "Hi!") (\_ -> Elm.string "Hello to you!") + , Elm.Case.branch Arg.ignore (\_ -> Elm.string "Excuse me?") + ] + |> Elm.Expect.renderedAs + """ + case "Hi!" of + "Hi!" -> + "Hello to you!" - _ -> - "Excuse me?" - """ + _ -> + "Excuse me?" + """ , test "char" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom (Elm.char 'z') - Type.unit - [ Elm.Case.branch (Arg.char 'z') (\_ -> Elm.int 26) - , Elm.Case.branch Arg.ignore (\_ -> Elm.int 0) - ] - ) - """ - case 'z' of - 'z' -> - 26 + Elm.Case.custom (Elm.char 'z') + Type.unit + [ Elm.Case.branch (Arg.char 'z') (\_ -> Elm.int 26) + , Elm.Case.branch Arg.ignore (\_ -> Elm.int 0) + ] + |> Elm.Expect.renderedAs + """ + case 'z' of + 'z' -> + 26 - _ -> - 0 - """ + _ -> + 0 + """ , test "dots in names" <| \() -> - Elm.Expect.renderedAs - (Elm.Case.custom (Elm.char 'z') - Type.unit - [ Elm.Case.branch (Arg.var "a.b") - (\arg -> - arg - ) - ] - ) - """ - case 'z' of - ab -> - ab - """ + Elm.Case.custom (Elm.char 'z') + Type.unit + [ Elm.Case.branch (Arg.var "a.b") + (\arg -> + arg + ) + ] + |> Elm.Expect.renderedAs + """ + case 'z' of + ab -> + ab + """ ] , destructuringItems ] @@ -304,46 +290,41 @@ destructuringItems = , Elm.Case.branch Arg.ignore (\_ -> Elm.int 0) ] in - Elm.Expect.renderedAs - expression - """ + expression + |> Elm.Expect.renderedAs + """ case [ 'a' ] of [ first, second, third ] -> [ first, second, third ] _ -> 0 - """ + """ , test "type destructuring" <| \() -> - let - expression : Elm.Expression - expression = - Elm.Case.custom - (Elm.val "Test") - (Type.maybe Type.int) - [ Elm.Case.branch - (Arg.customType "MyType" identity - |> Arg.items - [ Arg.var "first" - , Arg.var "second" - , Arg.var "third" - ] - ) - (\allVars -> - Elm.list allVars - ) - , Elm.Case.branch Arg.ignore (\_ -> Elm.int 0) - ] - in - Elm.Expect.renderedAs - expression - """ + Elm.Case.custom + (Elm.val "Test") + (Type.maybe Type.int) + [ Elm.Case.branch + (Arg.customType "MyType" identity + |> Arg.items + [ Arg.var "first" + , Arg.var "second" + , Arg.var "third" + ] + ) + (\allVars -> + Elm.list allVars + ) + , Elm.Case.branch Arg.ignore (\_ -> Elm.int 0) + ] + |> Elm.Expect.renderedAs + """ case Test of MyType first second third -> [ first, second, third ] _ -> 0 - """ + """ ] diff --git a/tests/TypeChecking.elm b/tests/TypeChecking.elm index 3a5b0574..3d789b4e 100644 --- a/tests/TypeChecking.elm +++ b/tests/TypeChecking.elm @@ -10,7 +10,6 @@ import Elm.ToString import Expect import Internal.Compiler as Compiler import Internal.Index as Index -import String.Multiline import Test exposing (Test, describe, test) @@ -148,9 +147,9 @@ generatedCode = describe "Exact Output" [ test "Strings" <| \_ -> - Elm.Expect.renderedAs - (Elm.string "Hello!") - "\"Hello!\"" + Elm.string "Hello!" + |> Elm.Expect.renderedAs + "\"Hello!\"" , test "Function, arg order isn't reversed" <| \_ -> let @@ -170,40 +169,41 @@ generatedCode = Elm.unit ) in - Elm.Expect.declarationAs - (Elm.declaration "myFunc" exp) - "myFunc : String -> Int -> Bool -> ( String, Int, Bool )\nmyFunc str int bool =\n ( str, int, bool )" + Elm.declaration "myFunc" exp + |> Elm.Expect.declarationAs + """ + myFunc : String -> Int -> Bool -> ( String, Int, Bool ) + myFunc str int bool = + ( str, int, bool ) + """ , test "Simplified version of map generates the correct signature" <| \_ -> - Elm.Expect.declarationAs - (Elm.declaration "map" myMap2) - """ - map : (optional -> fn) -> optional -> Optional fn - map fn optional = - Present (fn optional) - """ + Elm.declaration "map" myMap2 + |> Elm.Expect.declarationAs + """ + map : (optional -> fn) -> optional -> Optional fn + map fn optional = + Present (fn optional) + """ , test "Map function generates corrections " <| \_ -> - (Elm.ToString.expression myMap + Elm.ToString.expression myMap |> .signature - ) - |> Expect.equal - (String.Multiline.here - """ - (a -> fn) -> Optional a -> Optional fn - """ - ) + |> Elm.Expect.expectEqualMultiline + "(a -> fn) -> Optional a -> Optional fn" , test "Multiply used type variable in record only appears once in signature" <| \_ -> - Elm.Expect.declarationAs - (Elm.alias "Record" - (Type.record - [ ( "a", Type.var "var" ) - , ( "b", Type.var "var" ) - ] - ) + Elm.alias "Record" + (Type.record + [ ( "a", Type.var "var" ) + , ( "b", Type.var "var" ) + ] ) - "type alias Record var =\n { a : var, b : var }" + |> Elm.Expect.declarationAs + """ + type alias Record var = + { a : var, b : var } + """ ]