forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportsSpec.hs
130 lines (120 loc) · 5.55 KB
/
ImportsSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Language.PureScript.Ide.ImportsSpec where
import Protolude
import Data.Maybe (fromJust)
import qualified Language.PureScript as P
import Language.PureScript.Ide.Imports
import Language.PureScript.Ide.Types
import Test.Hspec
simpleFile :: [Text]
simpleFile =
[ "module Main where"
, "import Prelude"
, ""
, "myFunc x y = x + y"
]
splitSimpleFile :: (P.ModuleName, [Text], [Import], [Text])
splitSimpleFile = fromRight (sliceImportSection simpleFile)
where
fromRight = fromJust . rightToMaybe
withImports :: [Text] -> [Text]
withImports is =
take 2 simpleFile ++ is ++ drop 2 simpleFile
testParseImport :: Text -> Import
testParseImport = fromJust . parseImport
preludeImport, arrayImport, listImport, consoleImport, maybeImport :: Import
preludeImport = testParseImport "import Prelude"
arrayImport = testParseImport "import Data.Array (head, cons)"
listImport = testParseImport "import Data.List as List"
consoleImport = testParseImport "import Control.Monad.Eff.Console (log) as Console"
maybeImport = testParseImport "import Data.Maybe (Maybe(Just))"
wildcard :: P.Type
wildcard = P.TypeWildcard $ P.SourceSpan "" (P.SourcePos 0 0) (P.SourcePos 0 0)
spec :: Spec
spec = do
describe "determining the importsection" $ do
let moduleSkeleton imports =
Right (P.moduleNameFromString "Main", take 1 simpleFile, imports, drop 2 simpleFile)
it "finds a simple import" $
shouldBe (sliceImportSection simpleFile) (moduleSkeleton [preludeImport])
it "allows multiline import statements" $
shouldBe
(sliceImportSection (withImports [ "import Data.Array (head,"
, " cons)"
]))
(moduleSkeleton [preludeImport, arrayImport])
describe "pretty printing imports" $ do
it "pretty prints a simple import" $
shouldBe (prettyPrintImport' preludeImport) "import Prelude"
it "pretty prints an explicit import" $
shouldBe (prettyPrintImport' arrayImport) "import Data.Array (head, cons)"
it "pretty prints a qualified import" $
shouldBe (prettyPrintImport' listImport) "import Data.List as List"
it "pretty prints a qualified explicit import" $
shouldBe (prettyPrintImport' consoleImport) "import Control.Monad.Eff.Console (log) as Console"
it "pretty prints an import with a datatype (and PositionedRef's for the dtors)" $
shouldBe (prettyPrintImport' maybeImport) "import Data.Maybe (Maybe(Just))"
describe "import commands" $ do
let simpleFileImports = let (_, _, i, _) = splitSimpleFile in i
addValueImport i mn is =
prettyPrintImportSection (addExplicitImport' (IdeValue (P.Ident i) wildcard) mn is)
addOpImport op mn is =
prettyPrintImportSection (addExplicitImport' (IdeValueOperator op "" 2 P.Infix) mn is)
addDtorImport i t mn is =
prettyPrintImportSection (addExplicitImport' (IdeDataConstructor (P.ProperName i) t wildcard) mn is)
it "adds an implicit unqualified import" $
shouldBe
(addImplicitImport' simpleFileImports (P.moduleNameFromString "Data.Map"))
[ "import Prelude"
, "import Data.Map"
]
it "adds an explicit unqualified import" $
shouldBe
(addValueImport "head" (P.moduleNameFromString "Data.Array") simpleFileImports)
[ "import Prelude"
, "import Data.Array (head)"
]
it "doesn't add an import if the containing module is imported implicitly" $
shouldBe
(addValueImport "const" (P.moduleNameFromString "Prelude") simpleFileImports)
["import Prelude"]
let Right (_, _, explicitImports, _) = sliceImportSection (withImports ["import Data.Array (tail)"])
it "adds an identifier to an explicit import list" $
shouldBe
(addValueImport "head" (P.moduleNameFromString "Data.Array") explicitImports)
[ "import Prelude"
, "import Data.Array (head, tail)"
]
it "adds an operator to an explicit import list" $
shouldBe
(addOpImport (P.OpName "<~>") (P.moduleNameFromString "Data.Array") explicitImports)
[ "import Prelude"
, "import Data.Array ((<~>), tail)"
]
it "adds the type for a given DataConstructor" $
shouldBe
(addDtorImport "Just" (P.ProperName "Maybe") (P.moduleNameFromString "Data.Maybe") simpleFileImports)
[ "import Prelude"
, "import Data.Maybe (Maybe(Just))"
]
it "adds a dataconstructor to an existing type import" $ do
let Right (_, _, typeImports, _) = sliceImportSection (withImports ["import Data.Maybe (Maybe)"])
shouldBe
(addDtorImport "Just" (P.ProperName "Maybe") (P.moduleNameFromString "Data.Maybe") typeImports)
[ "import Prelude"
, "import Data.Maybe (Maybe(Just))"
]
it "doesn't add a dataconstructor to an existing type import with open dtors" $ do
let Right (_, _, typeImports, _) = sliceImportSection (withImports ["import Data.Maybe (Maybe(..))"])
shouldBe
(addDtorImport "Just" (P.ProperName "Maybe") (P.moduleNameFromString "Data.Maybe") typeImports)
[ "import Prelude"
, "import Data.Maybe (Maybe(..))"
]
it "doesn't add an identifier to an explicit import list if it's already imported" $
shouldBe
(addValueImport "tail" (P.moduleNameFromString "Data.Array") explicitImports)
[ "import Prelude"
, "import Data.Array (tail)"
]