lue-bird/elm-review-mini/1.0.0

NOTE: the latest version is 1.0.1

Review.Test

Test your review using elm-explorations/test.

Implementing a review works really well in a Test-Driven loop:

  • write a failing test before writing any code. Run it to make sure that it is failing. Also write tests to ensure cases similar to what you want to report are not reported. For instance, if you wish to report uses of variables named foo, write a test that ensures that the use of variables named differently does not get reported
  • write simple (almost stupid) review code to make the test pass, using Debug.todo etc to cut corner cases
  • run the tests again and make sure that both new and previous tests are passing

Then repeat for every case that needs to be handled. Ideally, by only reading through the test titles, someone else should be able to recreate the rule you are testing.

Tests are cheap and it is better to have too many tests rather than too few, since the behavior of a review rarely changes drastically.

If you like putting Debug.logs in your code as much as I do, run your tests with elm-test-rs.

run :
{ files :
List
{ path : String
, source : String
}
, projectConfiguration :
{ elmJson : String
, directDependencies :
List
{ elmJson : String
, docsJson : String
}
}
, review : Review
, expectedErrors :
List
{ path : String
, message : String
, details : List String
, fixedFiles :
List
{ path : String
, source : String
}
}
}
-> Expectation

Run a Review on a project, matching all reported module errors, elm.json errors and extra file errors with your provided expected errors.

import Review.Test
import Test exposing (Test)
import DebugForbid


tests : Test
tests =
    Test.describe "DebugForbid"
        [ Test.test "report Debug.log use"
            (\() ->
                { files =
                    [ { path = "src/A.elm"
                      , source = """
                            module A exposing (a)
                            import Html
                            a =
                                Debug.log "some" "message"
                            """
                      }
                    ]
                , projectConfiguration =
                , review = DebugForbid.review
                , expectedErrors =
                    [ { path = "src/A.elm"
                      , message = "Remove the use of `Debug` before shipping to production"
                      , details = [ "Compiling elm code in optimized mode does not allow these helpers." ]
                      , range = Review.Test.Under "Debug.log"
                      , fixedFiles =
                          [ { path = "src/A.elm"
                            , source = """
                                module A exposing (a)
                                a =
                                    "message"
                                """
                            }
                          ]
                      }
                    ]
                }
                    |> Review.Test.run
            )
        ]

The extra indentation specified by the first line will be stripped of all the provided source strings.

You can also specify a custom project config in case you need to test a package or want to add dependencies (elm/core is automatically part of every tested project) by supplying the raw sources for the elm.json and the direct dependency docs.json and elm.json you can find in your elm root folder.

{ elmJson : String
, directDependencies :
List
{ elmJson : String
, docsJson : String
}
}

The default project after elm init minus the elm/html and elm/browser dependencies. Equivalent to (copy and adapt if necessary)

{ elmJson = """
    {
        "type": "application",
        "source-directories": [
            "src"
        ],
        "elm-version": "0.19.1",
        "dependencies": {
            "direct": {
                "elm/core": "1.0.5"
            },
            "indirect": {
                "elm/json": "1.1.3"
            }
        },
        "test-dependencies": {
            "direct": {},
            "indirect": {}
        }
    }
    """
, directDependencies = []
}

(elm/core is automatically part of every tested project)

= Under String
| UnderExactly
{ section : String
, startingAt : Location
}

An expectation for the reported error range. If the section to mark is unique in the source, use Under "your section source". If the section occurs multiple times in the source, use UnderExactly { section = "your section source", startingAt = { row = ..., column = ... } }