on master help sign in
Move test sample code out to external file (#175)

* Move sample code out into into separate source file * Add test/data/GotoHover.hs to cabal extra-source-files * hlint: explicit module export list * hlint: implement and use readFileUtf8 * hlint: remove -Wmissing-signatures

commit5645a8030c30296b25ab5fd7357d58c41501f300 authorJacek Generowicz <jacg@my-post-office.net> dateTue 22 Oct 2019, 16:41 (7 years ago) parent38c2903
4 files changed+42−32
ghcide.cabal+3 src/Development/IDE/GHC/Util.hs+9−2 test/data/GotoHover.hs+21 test/exe/Main.hs+9−30
ghcide.cabal+3
@@ -15,6 +15,7 @@ homepage: https://github.com/digital-asset/ghcide#readme
1515bug-reports: https://github.com/digital-asset/ghcide/issues
1616tested-with: GHC==8.6.5
1717extra-source-files: include/ghc-api-version.h README.md CHANGELOG.md
18+ test/data/GotoHover.hs
1819
1920source-repository head
2021 type: git
@@ -172,6 +173,7 @@ test-suite ghcide-tests
172173 ghcide:ghcide
173174 build-depends:
174175 base,
176+ bytestring,
175177 containers,
176178 directory,
177179 extra,
@@ -184,6 +186,7 @@ test-suite ghcide-tests
184186 -- which works for now.
185187 ghc,
186188 --------------------------------------------------------------
189+ ghcide,
187190 haskell-lsp-types,
188191 lens,
189192 lsp-test >= 0.8,
src/Development/IDE/GHC/Util.hs+9−2
@@ -18,7 +18,8 @@ module Development.IDE.GHC.Util(
1818 runGhcEnv,
1919 textToStringBuffer,
2020 moduleImportPath,
21− HscEnvEq, hscEnv, newHscEnvEq
21+ HscEnvEq, hscEnv, newHscEnvEq,
22+ readFileUtf8
2223 ) where
2324
2425import Config
@@ -35,7 +36,10 @@ import FileCleanup
3536import Platform
3637import Data.Unique
3738import Development.Shake.Classes
38−import qualified Data.Text as T
39+import qualified Data.Text as T
40+import qualified Data.Text.Encoding as T
41+import qualified Data.Text.Encoding.Error as T
42+import qualified Data.ByteString as BS
3943import StringBuffer
4044import System.FilePath
4145
@@ -139,3 +143,6 @@ instance Eq HscEnvEq where
139143
140144instance NFData HscEnvEq where
141145 rnf (HscEnvEq a b) = rnf (hashUnique a) `seq` b `seq` ()
146+
147+readFileUtf8 :: FilePath -> IO T.Text
148+readFileUtf8 f = T.decodeUtf8With T.lenientDecode <$> BS.readFile f
test/data/GotoHover.hsnew file+21
@@ -0,0 +1,21 @@
1+
2+module Testing ( module Testing )where
3+import Data.Text (Text, pack)
4+data TypeConstructor = DataConstructor
5+ { fff :: Text
6+ , ggg :: Int }
7+aaa :: TypeConstructor
8+aaa = DataConstructor
9+ { fff = ""
10+ , ggg = 0
11+ }
12+bbb :: TypeConstructor
13+bbb = DataConstructor "" 0
14+ccc :: (Text, Int)
15+ccc = (fff bbb, ggg aaa)
16+ddd :: Num a => a -> a -> a
17+ddd vv ww = vv +! ww
18+a +! b = a - b
19+hhh (Just a) (><) = a >< a
20+iii a b = a `b` a
21+jjj s = pack $ s <> s
test/exe/Main.hs+9−30
@@ -12,6 +12,7 @@ import Control.Monad
1212import Control.Monad.IO.Class (liftIO)
1313import Data.Char (toLower)
1414import Data.Foldable
15+import Development.IDE.GHC.Util
1516import qualified Data.Text as T
1617import Development.IDE.Test
1718import Development.IDE.Test.Runfiles
@@ -694,7 +695,7 @@ findDefinitionAndHoverTests :: TestTree
694695findDefinitionAndHoverTests = let
695696
696697 tst (get, check) pos targetRange title = testSession title $ do
697− doc <- openDoc' sourceFilePath "haskell" source
698+ doc <- openTestDataDoc sourceFilePath
698699 found <- get doc pos
699700 check found targetRange
700701
@@ -751,8 +752,8 @@ findDefinitionAndHoverTests = let
751752 (T.unpack $ "failed to find: `" <> part <> "` in hover message:\n" <> whole)
752753 (part `T.isInfixOf` whole)
753754
754− sourceFilePath = "Testing.hs" -- TODO: convert from sourceFileName
755− sourceFileName = "Testing.hs"
755+ sourceFilePath = T.unpack sourceFileName
756+ sourceFileName = "GotoHover.hs"
756757
757758 mkFindTests tests = testGroup "get"
758759 [ testGroup "definition" $ mapMaybe fst tests
@@ -765,33 +766,6 @@ findDefinitionAndHoverTests = let
765766 hover = (getHover , checkHover)
766767 --type_ = (getTypeDefinitions, checkTDefs) -- getTypeDefinitions always times out
767768
768− source = T.unlines
769− -- 0123456789 123456789 123456789 123456789
770− [ "{-# OPTIONS_GHC -Wmissing-signatures #-}" -- 0
771− , "module Testing where" -- 1
772− , "import Data.Text (Text, pack)" -- 2
773− , "data TypeConstructor = DataConstructor" -- 3
774− , " { fff :: Text" -- 4
775− , " , ggg :: Int }" -- 5
776− , "aaa :: TypeConstructor" -- 6
777− , "aaa = DataConstructor" -- 7
778− , " { fff = \"\"" -- 8
779− , " , ggg = 0" -- 9
780− -- 0123456789 123456789 123456789 123456789
781− , " }" -- 10
782− , "bbb :: TypeConstructor" -- 11
783− , "bbb = DataConstructor \"\" 0" -- 12
784− , "ccc :: (Text, Int)" -- 13
785− , "ccc = (fff bbb, ggg aaa)" -- 14
786− , "ddd :: Num a => a -> a -> a" -- 15
787− , "ddd vv ww = vv +! ww" -- 16
788− , "a +! b = a - b" -- 17
789− , "hhh (Just a) (><) = a >< a" -- 18
790− , "iii a b = a `b` a" -- 19
791− , "jjj s = pack $ s <> s" -- 20
792− -- 0123456789 123456789 123456789 123456789
793− ]
794−
795769 -- search locations expectations on results
796770 fffL4 = _start fffR ; fffR = mkRange 4 4 4 7 ; fff = [ExpectRange fffR]
797771 fffL8 = Position 8 4 ;
@@ -888,3 +862,8 @@ run s = withTempDir $ \dir -> do
888862 -- If you uncomment this you can see all messages
889863 -- which can be quite useful for debugging.
890864 -- { logMessages = True, logColor = False, logStdErr = True }
865+
866+openTestDataDoc :: FilePath -> Session TextDocumentIdentifier
867+openTestDataDoc path = do
868+ source <- liftIO $ readFileUtf8 $ "test/data" </> path
869+ openDoc' path "haskell" source