Skip to content

Commit

Permalink
Merge pull request #23 from mateusfpleite/main
Browse files Browse the repository at this point in the history
Add tests to Install.Import
  • Loading branch information
jxxcarlson authored Jun 15, 2024
2 parents 2d52fd0 + 7ad7b23 commit 57fa163
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/Install/Import.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ To add the statement `import Foo.Bar as FB exposing (a, b, c)` to the `Frontend`
-}

import Elm.Syntax.Import exposing (Import)
import Elm.Syntax.Module exposing (Module)
import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Range as Range exposing (Range)
import Review.Fix as Fix
import Review.Rule as Rule exposing (Error, Rule)
import String.Extra


{-|
Expand Down Expand Up @@ -60,8 +62,8 @@ init hostModuleName_ importedModuleName_ =
{-| Add an alias to the imported module.
-}
withAlias : String -> Config -> Config
withAlias alias config =
{ config | importedModuleAlias = Just alias }
withAlias alias_ config =
{ config | importedModuleAlias = Just alias_ }


{-| Add an exposing list to the imported module.
Expand All @@ -78,6 +80,7 @@ makeRule : Config -> Rule
makeRule config =
Rule.newModuleRuleSchemaUsingContextCreator "Install.Import" initialContext
|> Rule.withImportVisitor (importVisitor config)
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|> Rule.withFinalModuleEvaluation (finalEvaluation config)
|> Rule.providesFixesForModuleRule
|> Rule.fromModuleRuleSchema
Expand Down Expand Up @@ -108,6 +111,12 @@ importVisitor config node context =
( [], { context | lastNodeRange = Node.range node } )


moduleDefinitionVisitor : Node Module -> Context -> ( List (Error {}), Context )
moduleDefinitionVisitor def context =
-- visit the module definition to set the module definition as the lastNodeRange in case the module has no imports yet
( [], { context | lastNodeRange = Node.range def } )


finalEvaluation : Config -> Context -> List (Rule.Error {})
finalEvaluation config context =
if context.moduleWasImported == False && config.hostModuleName == context.moduleName then
Expand All @@ -126,15 +135,16 @@ fixError config context =
++ " "
|> addAlias config.importedModuleAlias
|> addExposing config.exposedValues
|> String.Extra.clean

addAlias : Maybe String -> String -> String
addAlias mAlias str =
case mAlias of
Nothing ->
str

Just alias ->
str ++ " as " ++ alias
Just alias_ ->
str ++ " as " ++ alias_

addExposing : Maybe (List String) -> String -> String
addExposing mExposedValues str =
Expand Down
8 changes: 7 additions & 1 deletion tests/Install/FunctionTest.elm
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module Install.FunctionTest exposing (all)

import Install.Function
import Review.Rule exposing (Rule)
import Run
import Test exposing (Test, describe)


all : Test
all =
describe "Install.ClauseInCase"
describe "Install.Function"
[ Run.testFix test1
]

Expand All @@ -16,6 +17,7 @@ all =
-- TEST 1


test1 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String }
test1 =
{ description = "Test 1, replace function body of of Frontend.view"
, src = src1
Expand All @@ -26,6 +28,7 @@ test1 =
}


rule1 : Rule
rule1 =
Install.Function.init
[ "Frontend" ]
Expand All @@ -35,18 +38,21 @@ rule1 =
|> Install.Function.makeRule
src1 : String
src1 =
"""module Frontend exposing(..)

view model =
Html.div [] [ Html.text "Hello, World!" ]"""
under1 : String
under1 =
"""view model =
Html.div [] [ Html.text "Hello, World!" ]"""
fixed1 : String
fixed1 =
"""module Frontend exposing(..)

Expand Down
156 changes: 156 additions & 0 deletions tests/Install/ImportTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
module Install.ImportTest exposing (..)

import Install.Import
import Review.Rule exposing (Rule)
import Run
import Test exposing (Test, describe)


all : Test
all =
describe "Install.Import"
[ Run.testFix test1
, Run.testFix test1a
, Run.testFix test2
, Run.testFix test3
]



-- test 1 - add simple import


test1 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String }
test1 =
{ description = "add simple import"
, src = src1
, rule = rule1
, under = under1
, fixed = fixed1
, message = "moduleToImport: \"Dict\""
}


rule1 : Rule
rule1 =
Install.Import.init "Main" "Dict"
|> Install.Import.makeRule


src1 : String
src1 =
"""module Main exposing (..)
import Set
foo = 1"""


under1 : String
under1 =
"""import Set"""


fixed1 : String
fixed1 =
"""module Main exposing (..)
import Set
import Dict
foo = 1"""



-- test 1a - add simple import when there are no imports


test1a : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String }
test1a =
{ description = "add simple import when there are no imports"
, src = src1a
, rule = rule1
, under = under1a
, fixed = fixed1a
, message = "moduleToImport: \"Dict\""
}


src1a : String
src1a =
"""module Main exposing (..)
foo = 1"""


under1a : String
under1a =
"module Main exposing (..)"


fixed1a : String
fixed1a =
"""module Main exposing (..)
import Dict
foo = 1"""



-- test 2 - add import with alias


test2 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String }
test2 =
{ description = "add import with alias"
, src = src1
, rule = rule2
, under = under1
, fixed = fixed2
, message = "moduleToImport: \"Dict\""
}


rule2 : Rule
rule2 =
Install.Import.init "Main" "Dict"
|> Install.Import.withAlias "D"
|> Install.Import.makeRule


fixed2 : String
fixed2 =
"""module Main exposing (..)
import Set
import Dict as D
foo = 1"""



-- test 3 - add import exposing


test3 : { description : String, src : String, rule : Rule, under : String, fixed : String, message : String }
test3 =
{ description = "add import exposing"
, src = src1
, rule = rule3
, under = under1
, fixed = fixed3
, message = "moduleToImport: \"Dict\""
}


rule3 : Rule
rule3 =
Install.Import.init "Main" "Dict"
|> Install.Import.withExposedValues [ "Dict" ]
|> Install.Import.makeRule


fixed3 : String
fixed3 =
"""module Main exposing (..)
import Set
import Dict exposing (Dict)
foo = 1"""

0 comments on commit 57fa163

Please sign in to comment.