diff --git a/src/Lint/Test.elm b/src/Lint/Test.elm index 609fc54c..0ad5b2da 100644 --- a/src/Lint/Test.elm +++ b/src/Lint/Test.elm @@ -1,4 +1,4 @@ -module Lint.Test exposing (LintResult, errorWithoutRange, expectErrors, expectErrorsWithoutRange, location, ruleTester) +module Lint.Test exposing (LintResult, errorWithoutRange, expectErrors, expectErrorsWithoutRange, location, run) {-| Module that helps you test your linting rules, using [`elm-test`](https://package.elm-lang.org/packages/elm-explorations/test/latest). @@ -22,8 +22,33 @@ type alias LintResult = Result (List String) (List Error) -ruleTester : Rule -> String -> Result (List String) (List Error) -ruleTester rule str = +{-| Run a `Rule` on a string and get the errors reported by it. If the string is +not valid Elm code, this will return a `Result` of type `Err`. + +Note that to be valid, a code needs to start with a module definition followed by +a line break like `module A exposing (..)\n`. + + import Lint.Test exposing (LintResult) + import Test + + testRule : String -> LintResult + testRule string = + "module A exposing (..)\n\n" + ++ string + |> Lint.Test.run rule + + tests : List Test + tests = + [ test "should not report normal function calls" <| + \() -> + testRule "a = Debug.log" + |> Lint.Test.expectErrors + [ error (Lint.Test.location ( 3, 5 ) ( 3, 14 )) ] + ] + +-} +run : Rule -> String -> Result (List String) (List Error) +run rule str = lintSource [ ( Critical, rule ) ] str |> Result.map (List.map (\( severity, { message, range } ) -> Rule.error message range)) diff --git a/tests/DefaultPatternPositionTest.elm b/tests/DefaultPatternPositionTest.elm index c97a60bd..10955ce4 100644 --- a/tests/DefaultPatternPositionTest.elm +++ b/tests/DefaultPatternPositionTest.elm @@ -9,7 +9,7 @@ import Test exposing (Test, describe, test) testRule : PatternPosition -> String -> LintResult testRule patternPosition = - Lint.Test.ruleTester (rule patternPosition) + Lint.Test.run (rule patternPosition) error : String -> Error diff --git a/tests/NoDebugTest.elm b/tests/NoDebugTest.elm index 766a318e..a935131d 100644 --- a/tests/NoDebugTest.elm +++ b/tests/NoDebugTest.elm @@ -11,7 +11,7 @@ testRule : String -> LintResult testRule string = "module A exposing (..)\n\n" ++ string - |> Lint.Test.ruleTester rule + |> Lint.Test.run rule error : Range -> Error diff --git a/tests/NoExtraBooleanComparisonTest.elm b/tests/NoExtraBooleanComparisonTest.elm index 4a2ac3bd..b727c7da 100644 --- a/tests/NoExtraBooleanComparisonTest.elm +++ b/tests/NoExtraBooleanComparisonTest.elm @@ -11,7 +11,7 @@ testRule : String -> LintResult testRule string = "module A exposing (..)\n\n" ++ string - |> Lint.Test.ruleTester rule + |> Lint.Test.run rule tests : List Test diff --git a/tests/NoImportingEverythingTest.elm b/tests/NoImportingEverythingTest.elm index 4e48043d..407f3620 100644 --- a/tests/NoImportingEverythingTest.elm +++ b/tests/NoImportingEverythingTest.elm @@ -9,7 +9,7 @@ import Test exposing (Test, describe, test) testRule : Configuration -> String -> LintResult testRule options = - Lint.Test.ruleTester (rule options) + Lint.Test.run (rule options) error : String -> Error diff --git a/tests/NoUnusedVariablesTest.elm b/tests/NoUnusedVariablesTest.elm index 8875ff32..69806d35 100644 --- a/tests/NoUnusedVariablesTest.elm +++ b/tests/NoUnusedVariablesTest.elm @@ -9,7 +9,7 @@ import Test exposing (Test, describe, test) testRule : String -> LintResult testRule = - Lint.Test.ruleTester rule + Lint.Test.run rule tests : List Test