Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incremental mode #14

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

106 changes: 85 additions & 21 deletions e2e/E2E.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,119 @@ module E2E exposing (suite)

-- Please edit this only in the e2e directory. The point of this is that the code is generated.

import Standard exposing (..)
import Csv.Decode
import Expect
import Fuzz exposing (Fuzzer)
import Json.Decode exposing (Decoder)
import Json.Encode exposing (Value)
import Fuzz exposing (Fuzzer)
import Serialize exposing (Codec)
import Standard exposing (..)
import Test exposing (Test)
import Expect


decoder : Decoder (A Int)
decoder =
Debug.todo ""

encode : A Int -> Value

encode : A Int -> Value
encode =
Debug.todo ""


serialize : Codec e (A Int)
serialize =
Debug.todo ""


fuzz : Fuzzer (A Int)
fuzz =
Debug.todo ""

type Foo
= X | Y | Z

all : List Foo
type Foo
= X
| Y
| Z


all : List Foo
all =
Debug.todo ""

toString : Foo -> String

toString : Foo -> String
toString =
Debug.todo ""

fromString : String -> Maybe Foo

fromString : String -> Maybe Foo
fromString =
Debug.todo ""


type alias FlatData =
{ a : Int
, b : String
, c : Bool
, d : Maybe Float
, f : Foo
}


csvDecoder : Csv.Decode.Decoder FlatData
csvDecoder =
Debug.todo ""


random : Random.Generator FlatData
random =
Debug.todo ""


suite : Test
suite =
Test.describe "E2E test" [
Test.fuzz fuzz "JSON encoding roundtrips on fuzzed value" <|
Test.describe "E2E test"
[ Test.fuzz fuzz "JSON encoding roundtrips on fuzzed value" <|
\val ->
encode val
encode val
|> Json.Decode.decodeValue decoder
|> Expect.equal (Ok val)

, List.map (\foo ->
Test.test (toString foo ++ " roundtrips from string") <|
\() ->
toString foo
|> fromString
|> Expect.equal (Just foo)
) all
, List.map
(\foo ->
Test.test (toString foo ++ " roundtrips from string") <|
\() ->
toString foo
|> fromString
|> Expect.equal (Just foo)
)
all
|> Test.describe "toString/fromString"
]
, Test.fuzz fuzz "serialize roundtrips on fuzzed value" <|
\val ->
Serialize.encodeToBytes serialize val
|> Serialize.decodeFromBytes serialize
|> Expect.equal (Ok val)
, Test.test "CSV decoding works" <|
\() ->
"""a,b,c_tag,d,f_tag
1,foo,True,1.0,X
2,bar,False,,Y
3,baz,True,3.0,Z"""
|> Csv.Decode.decodeCsv Csv.Decode.FieldNamesFromFirstRow csvDecoder
|> Result.mapError Csv.Decode.errorToString
|> Expect.equal
(Ok
[ { a = 1, b = "foo", c = True, d = Just 1.0, f = X }
, { a = 2, b = "bar", c = False, d = Nothing, f = Y }
, { a = 3, b = "baz", c = True, d = Just 3.0, f = Z }
]
)
, Test.test "Random generates a random-ish value" <|
\() ->
Random.initialSeed 42
|> Random.step random
|> Tuple.first
|> Expect.equal
{ a = -848567307, b = "TODO: Define string options", c = False, d = Nothing, f = X }
]
59 changes: 32 additions & 27 deletions elm.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
{
"type": "package",
"name": "gampleman/elm-review-derive",
"summary": "Generate code for json encoders/decoders, codecs, fuzzers, generators, and more",
"license": "MIT",
"version": "1.0.0",
"exposed-modules": [
"NoDebug.TodoItForMe",
"CodeGenerator",
"CodeGenerator.Test",
"TypePattern",
"ResolvedType"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.2 <= v < 2.0.0",
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/project-metadata-utils": "1.0.1 <= v < 2.0.0",
"elm-explorations/test": "2.0.0 <= v < 3.0.0",
"elmcraft/core-extra": "2.0.0 <= v < 3.0.0",
"erlandsona/assoc-set": "1.1.0 <= v < 2.0.0",
"jfmengels/elm-review": "2.4.0 <= v < 3.0.0",
"pzp1997/assoc-list": "1.0.0 <= v < 2.0.0",
"stil4m/elm-syntax": "7.2.1 <= v < 8.0.0",
"the-sett/elm-pretty-printer": "3.0.0 <= v < 4.0.0",
"the-sett/elm-syntax-dsl": "6.0.1 <= v < 7.0.0"
},
"test-dependencies": {}
"type": "package",
"name": "gampleman/elm-review-derive",
"summary": "Generate code for json encoders/decoders, codecs, fuzzers, generators, and more",
"license": "MIT",
"version": "1.0.0",
"exposed-modules": [
"NoDebug.TodoItForMe",
"CodeGenerator",
"CodeGenerator.Test",
"TypePattern",
"ResolvedType"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.2 <= v < 2.0.0",
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/project-metadata-utils": "1.0.1 <= v < 2.0.0",
"elm-explorations/test": "2.0.0 <= v < 3.0.0",
"elmcraft/core-extra": "2.0.0 <= v < 3.0.0",
"erlandsona/assoc-set": "1.1.0 <= v < 2.0.0",
"jfmengels/elm-review": "2.4.0 <= v < 3.0.0",
"pzp1997/assoc-list": "1.0.0 <= v < 2.0.0",
"stil4m/elm-syntax": "7.2.1 <= v < 8.0.0",
"the-sett/elm-pretty-printer": "3.0.0 <= v < 4.0.0",
"the-sett/elm-syntax-dsl": "6.0.1 <= v < 7.0.0"
},
"test-dependencies": {
"BrianHicks/elm-csv": "4.0.0 <= v < 5.0.0",
"MartinSStewart/elm-serialize": "1.3.1 <= v < 2.0.0",
"elm/random": "1.0.0 <= v < 2.0.0",
"elm-community/random-extra": "3.2.0 <= v < 4.0.0"
}
}
52 changes: 26 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"dependencies": {
"elm": "0.19.1-5",
"elm-doc-preview": "^5.0.5",
"elm-format": "^0.8.7",
"elm-review": "^2.10.2",
"elm-test": "0.19.1-revision12",
"elm-verify-examples": "^5.2.0",
"npm-run-all": "^4.1.5"
},
"scripts": {
"test": "npm-run-all --print-name --sequential test:make test:format test:e2e test:run test:review",
"test:make": "elm make --docs=docs.json",
"test:format": "elm-format src/ tests/*.elm review preview --validate",
"test:examples": "elm-verify-examples",
"test:e2e": "./e2e/test.sh",
"test:run": "elm-test",
"test:review": "elm-review",
"fix": "npm-run-all --print-name --sequential fix:format fix:review",
"fix:format": "elm-format src/ tests/*.elm review preview --yes",
"fix:review": "elm-review --fix",
"start": "npm-run-all -p -l start:docs",
"start:docs": "elm-doc-preview",
"elm-bump": "npm-run-all --print-name --sequential test bump-version 'test:review -- --fix-all-without-prompt'",
"bump-version": "(yes | elm bump)"
}
}
"dependencies": {
"elm": "0.19.1-5",
"elm-doc-preview": "^5.0.5",
"elm-format": "^0.8.7",
"elm-review": "^2.10.2",
"elm-test": "0.19.1-revision12",
"elm-verify-examples": "^5.2.0",
"npm-run-all": "^4.1.5"
},
"scripts": {
"test": "npm-run-all --print-name --sequential test:make test:format test:e2e test:run test:review",
"test:make": "elm make --docs=docs.json",
"test:format": "elm-format src/ tests/*.elm review preview --validate",
"test:examples": "elm-verify-examples",
"test:e2e": "./e2e/test.sh",
"test:run": "elm-test",
"test:review": "elm-review",
"fix": "npm-run-all --print-name --sequential fix:format fix:review",
"fix:format": "elm-format src/ tests/*.elm review preview e2e/E2E.elm --yes",
"fix:review": "elm-review --fix",
"start": "npm-run-all -p -l start:docs",
"start:docs": "elm-doc-preview",
"elm-bump": "npm-run-all --print-name --sequential test bump-version 'test:review -- --fix-all-without-prompt'",
"bump-version": "(yes | elm bump)"
}
}
2 changes: 1 addition & 1 deletion preview/src/ReviewConfig.elm
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ import Review.Rule exposing (Rule)

config : List Rule
config =
[ NoDebug.TodoItForMe.rule []
[ NoDebug.TodoItForMe.rule True []
]
2 changes: 0 additions & 2 deletions review/src/ReviewConfig.elm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import NoSimpleLetBody
import NoUnoptimizedRecursion
import NoUnused.CustomTypeConstructorArgs
import NoUnused.CustomTypeConstructors
import NoUnused.Dependencies
import NoUnused.Exports
import NoUnused.Parameters
import NoUnused.Patterns
Expand Down Expand Up @@ -60,7 +59,6 @@ config =
-- , NoPrematureLetComputation.rule
, NoUnused.CustomTypeConstructors.rule []
, NoUnused.CustomTypeConstructorArgs.rule
, NoUnused.Dependencies.rule
, NoUnused.Exports.rule
, NoUnused.Parameters.rule
, NoUnused.Patterns.rule
Expand Down
36 changes: 31 additions & 5 deletions src/CodeGenerator/Test.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module CodeGenerator.Test exposing (codeGenTest, codeGenTestFailsWith, fakeDependency, FakeDependency)
module CodeGenerator.Test exposing (codeGenTest, codeGenTestFailsWith, codeGenIncrementalTest, fakeDependency, FakeDependency)

{-| Testing code generators can be tricky, but very rewarding as it makes developing CodeGenerators much easier.

@docs codeGenTest, codeGenTestFailsWith, fakeDependency, FakeDependency
@docs codeGenTest, codeGenTestFailsWith, codeGenIncrementalTest, fakeDependency, FakeDependency

-}

Expand Down Expand Up @@ -39,8 +39,8 @@ type FakeDependency
| FailedDep String


codeGenTestHelper : String -> List FakeDependency -> List CodeGenerator -> List String -> ({ module_ : String, under : String } -> Review.Test.ReviewResult -> Expectation) -> Test
codeGenTestHelper description dependencies codeGens modules fn =
codeGenTestHelper : String -> List FakeDependency -> List CodeGenerator -> List String -> Bool -> ({ module_ : String, under : String } -> Review.Test.ReviewResult -> Expectation) -> Test
codeGenTestHelper description dependencies codeGens modules incremental fn =
Test.test description <|
\_ ->
let
Expand Down Expand Up @@ -78,7 +78,7 @@ codeGenTestHelper description dependencies codeGens modules fn =
project =
List.foldl Review.Project.addDependency Review.Test.Dependencies.projectWithElmCore validDependencies
in
fn result (Review.Test.runOnModulesWithProjectData project (NoDebug.TodoItForMe.rule codeGens) inputModules)
fn result (Review.Test.runOnModulesWithProjectData project (NoDebug.TodoItForMe.rule incremental codeGens) inputModules)

else
Expect.fail ("Found issues in the following dependencies: \n\n" ++ String.join "\n" failedDeps)
Expand All @@ -95,6 +95,7 @@ codeGenTestFailsWith description dependencies codeGens modules expectedFailureDe
dependencies
codeGens
modules
False
(\result ->
Review.Test.expectErrorsForModules
[ ( result.module_
Expand Down Expand Up @@ -144,6 +145,31 @@ codeGenTest description dependencies codeGens modules expected =
dependencies
codeGens
modules
False
(\result ->
Review.Test.expectErrorsForModules
[ ( result.module_
, [ Review.Test.error
{ message = "Remove the use of `Debug.todo` before shipping to production"
, details = [ "`Debug.todo` can be useful when developing, but is not meant to be shipped to production or published in a package. I suggest removing its use before committing and attempting to push to production." ]
, under = result.under
}
|> Review.Test.whenFixed (String.replace "\u{000D}" "" expected)
]
)
]
)


{-| Like `codeGenTest`, but runs in incremental mode. Only looks for a fix for the first `Debug.todo` found.
-}
codeGenIncrementalTest : String -> List FakeDependency -> List CodeGenerator -> List String -> String -> Test
codeGenIncrementalTest description dependencies codeGens modules expected =
codeGenTestHelper description
dependencies
codeGens
modules
True
(\result ->
Review.Test.expectErrorsForModules
[ ( result.module_
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Builtin/Random.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import TypePattern exposing (TypePattern(..))

random =
{ int = \min max -> CG.apply [ CG.fqFun [ "Random" ] "int", min, max ]
, float = \min max -> CG.apply [ CG.fqFun [ "Random" ] "float", min, max ]
, minInt = CG.fqVal [ "Random" ] "minInt"
, maxInt = CG.fqVal [ "Random" ] "maxInt"
, uniform = \fst rest -> CG.apply [ CG.fqFun [ "Random" ] "uniform", fst, rest ]
Expand Down Expand Up @@ -37,6 +38,7 @@ codeGen =
}
[ CodeGenerator.int (random.int random.minInt random.maxInt)
, CodeGenerator.int randomExtra.anyInt |> CodeGenerator.ifUserHasDependency "elm-community/random-extra"
, CodeGenerator.float (random.float (CG.float 0) (CG.float 1))
, CodeGenerator.string (random.uniform (CG.string "TODO: Define string options") (CG.list []))
, CodeGenerator.list random.list
, CodeGenerator.pipeline random.constant (\arg -> CG.apply [ CG.fqFun [ "Random" ] "map2", CG.parens (CG.binOp CG.piper), arg ])
Expand Down
Loading
Loading