Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit bcac2b5

Browse files
committed
Implement tests for importing constructors
1 parent 988dcff commit bcac2b5

File tree

1 file changed

+92
-2
lines changed

1 file changed

+92
-2
lines changed

test/functional/FunctionalCodeActionsSpec.hs

+92-2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,25 @@ spec = describe "code actions" $ do
153153
, " $ hPutStrLn stdout"
154154
, " $ fromMaybe \"Good night, World!\" (Just \"Hello, World!\")"
155155
]
156+
, -- Complex imports for Constructos and functions
157+
[ "{-# LANGUAGE NoImplicitPrelude #-}"
158+
, "import System.IO ( IO"
159+
, " , hPutStrLn"
160+
, " , stdout"
161+
, " )"
162+
, "import Prelude ( Bool(..) )"
163+
, "import Control.Monad ( when )"
164+
, "import Data.Maybe ( fromMaybe"
165+
, " , Maybe(Just)"
166+
, " )"
167+
, "import Data.Function ( ($) )"
168+
, "-- | Main entry point to the program"
169+
, "main :: IO ()"
170+
, "main ="
171+
, " when True"
172+
, " $ hPutStrLn stdout"
173+
, " $ fromMaybe \"Good night, World!\" (Just \"Hello, World!\")"
174+
]
156175
]
157176
hsImportSpec "floskell"
158177
[ -- Expected output for simple format.
@@ -178,6 +197,20 @@ spec = describe "code actions" $ do
178197
, " $ hPutStrLn stdout"
179198
, " $ fromMaybe \"Good night, World!\" (Just \"Hello, World!\")"
180199
]
200+
, -- Complex imports for Constructos and functions
201+
[ "{-# LANGUAGE NoImplicitPrelude #-}"
202+
, "import System.IO (IO, hPutStrLn, stdout)"
203+
, "import Prelude (Bool(..))"
204+
, "import Control.Monad (when)"
205+
, "import Data.Maybe (fromMaybe, Maybe(Just))"
206+
, "import Data.Function (($))"
207+
, "-- | Main entry point to the program"
208+
, "main :: IO ()"
209+
, "main ="
210+
, " when True"
211+
, " $ hPutStrLn stdout"
212+
, " $ fromMaybe \"Good night, World!\" (Just \"Hello, World!\")"
213+
]
181214
]
182215
describe "add package suggestions" $ do
183216
-- Only execute this test with ghc 8.4.4, below seems to be broken in the package.
@@ -504,7 +537,7 @@ spec = describe "code actions" $ do
504537
-- Parameterized HsImport Spec.
505538
-- ---------------------------------------------------------------------
506539
hsImportSpec :: T.Text -> [[T.Text]]-> Spec
507-
hsImportSpec formatterName [e1, e2, e3] =
540+
hsImportSpec formatterName [e1, e2, e3, e4] =
508541
describe ("Execute HsImport with formatter " <> T.unpack formatterName) $ do
509542
it "works with 3.8 code action kinds" $ runSession hieCommand fullCaps "test/testdata" $ do
510543
doc <- openDoc "CodeActionImport.hs" "haskell"
@@ -626,7 +659,7 @@ hsImportSpec formatterName [e1, e2, e3] =
626659
l3 `shouldBe` "main :: IO ()"
627660
l4 `shouldBe` "main = when True $ putStrLn \"hello\""
628661

629-
it ("import-list respects format config with " <> T.unpack formatterName) $ runSession hieCommand fullCaps "test/testdata" $ do
662+
it "import-list respects format config" $ runSession hieCommand fullCaps "test/testdata" $ do
630663
doc <- openDoc "CodeActionImportBrittany.hs" "haskell"
631664
_ <- waitForDiagnosticsSource "ghcmod"
632665

@@ -644,6 +677,63 @@ hsImportSpec formatterName [e1, e2, e3] =
644677
l2 `shouldBe` "import Control.Monad (when)"
645678
l3 `shouldBe` "main :: IO ()"
646679
l4 `shouldBe` "main = when True $ putStrLn \"hello\""
680+
681+
it "complex import-list" $ runSession hieCommand fullCaps "test/testdata" $ do
682+
doc <- openDoc "CodeActionImportListElaborate.hs" "haskell"
683+
_ <- waitForDiagnosticsSource "ghcmod"
684+
685+
let config = def { formatOnImportOn = False, formattingProvider = formatterName }
686+
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
687+
688+
let wantedCodeActionTitles = [ "Import module System.IO (hPutSetrLn)"
689+
, "Import module System.IO (stdout)"
690+
, "Import module Control.Monad (when)"
691+
, "Import module Data.Maybe (fromMaybe)"
692+
, "Import module Data.Function (($))"
693+
, "Import module Data.Maybe (Maybe(Just))"
694+
, "Import module Prelude (Bool(..))"
695+
]
696+
697+
executeAllCodeActions doc wantedCodeActionTitles
698+
699+
contents <- getDocumentEdit doc
700+
liftIO $
701+
T.lines contents `shouldBe` e4
702+
703+
it "complex import-list respects format config" $ runSession hieCommand fullCaps "test/testdata" $ do
704+
doc <- openDoc "CodeActionImportListElaborate.hs" "haskell"
705+
_ <- waitForDiagnosticsSource "ghcmod"
706+
707+
let config = def { formatOnImportOn = False, formattingProvider = formatterName }
708+
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
709+
710+
let wantedCodeActionTitles = [ "Import module System.IO (hPutStrLn)"
711+
, "Import module System.IO (stdout)"
712+
, "Import module Control.Monad (when)"
713+
, "Import module Data.Maybe (fromMaybe)"
714+
, "Import module Data.Function (($))"
715+
, "Import module Data.Maybe (Maybe(Just))"
716+
, "Import module Prelude (Bool(..))"
717+
]
718+
719+
executeAllCodeActions doc wantedCodeActionTitles
720+
721+
contents <- getDocumentEdit doc
722+
liftIO $ do
723+
let [l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12] = T.lines contents
724+
l1 `shouldBe` "{-# LANGUAGE NoImplicitPrelude #-}"
725+
l2 `shouldBe` "import System.IO (IO, hPutStrLn, stdout)"
726+
l3 `shouldBe` "import Prelude (Bool(..))"
727+
l4 `shouldBe` "import Control.Monad (when)"
728+
l5 `shouldBe` "import Data.Maybe (fromMaybe, Maybe(Just))"
729+
l6 `shouldBe` "import Data.Function (($))"
730+
l7 `shouldBe` "-- | Main entry point to the program"
731+
l8 `shouldBe` "main :: IO ()"
732+
l9 `shouldBe` "main ="
733+
l10 `shouldBe` " when True"
734+
l11 `shouldBe` " $ hPutStrLn stdout"
735+
l12 `shouldBe` " $ fromMaybe \"Good night, World!\" (Just \"Hello, World!\")"
736+
647737
where
648738
executeAllCodeActions :: TextDocumentIdentifier -> [T.Text] -> Session ()
649739
executeAllCodeActions doc names =

0 commit comments

Comments
 (0)