Skip to content

Commit 3060535

Browse files
committed
Add tests
1 parent e418ac4 commit 3060535

16 files changed

+261
-4
lines changed

plugins/hls-explicit-record-fields-plugin/hls-explicit-record-fields-plugin.cabal

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,9 @@ test-suite hls-explicit-record-fields-plugin-test
5050
hs-source-dirs: test
5151
main-is: Main.hs
5252
build-depends:
53-
base ^>=4.15.1.0,
54-
hls-explicit-record-fields-plugin
53+
, base ^>=4.15.1.0
54+
, filepath
55+
, text
56+
, hls-explicit-record-fields-plugin
57+
, lsp-test
58+
, hls-test-utils
Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,65 @@
1-
module Main (main) where
1+
{-# LANGUAGE DuplicateRecordFields #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE TypeOperators #-}
5+
6+
module Main ( main ) where
7+
8+
import Data.Either (rights)
9+
import qualified Data.Text as T
10+
import qualified Ide.Plugin.ExplicitFields as ExplicitFields
11+
import System.FilePath ((<.>), (</>))
12+
import Test.Hls
13+
214

315
main :: IO ()
4-
main = putStrLn "Test suite not yet implemented."
16+
main = defaultTestRunner test
17+
18+
plugin :: PluginDescriptor IdeState
19+
plugin = ExplicitFields.descriptor mempty "explicit-fields"
20+
21+
test :: TestTree
22+
test = testGroup "explicit-fields"
23+
[ mkTest "WildcardOnly" "WildcardOnly" 12 10 12 20
24+
, mkTest "WithPun" "WithPun" 13 10 13 25
25+
, mkTest "WithExplicitBind" "WithExplicitBind" 12 10 12 32
26+
, mkTest "Mixed" "Mixed" 13 10 13 37
27+
, mkTest "Construction" "Construction" 16 5 16 15
28+
, mkTestNoAction "ExplicitBinds" "ExplicitBinds" 11 10 11 52
29+
, mkTestNoAction "Puns" "Puns" 12 10 12 31
30+
, mkTestNoAction "Infix" "Infix" 11 11 11 31
31+
, mkTestNoAction "Prefix" "Prefix" 10 11 10 28
32+
]
33+
34+
mkTestNoAction :: TestName -> FilePath -> UInt -> UInt -> UInt -> UInt -> TestTree
35+
mkTestNoAction title fp x1 y1 x2 y2 =
36+
testCase title $
37+
runSessionWithServer plugin (testDataDir </> "noop") $ do
38+
doc <- openDoc (fp <.> "hs") "haskell"
39+
actions <- getExplicitFieldsActions doc x1 y1 x2 y2
40+
liftIO $ actions @?= []
41+
42+
mkTest :: TestName -> FilePath -> UInt -> UInt -> UInt -> UInt -> TestTree
43+
mkTest title fp x1 y1 x2 y2 =
44+
goldenWithHaskellDoc plugin title testDataDir fp "expected" "hs" $ \doc -> do
45+
(act:_) <- getExplicitFieldsActions doc x1 y1 x2 y2
46+
executeCodeAction act
47+
48+
getExplicitFieldsActions
49+
:: TextDocumentIdentifier
50+
-> UInt -> UInt -> UInt -> UInt
51+
-> Session [CodeAction]
52+
getExplicitFieldsActions doc x1 y1 x2 y2 =
53+
findExplicitFieldsAction <$> getCodeActions doc range
54+
where
55+
range = Range (Position x1 y1) (Position x2 y2)
56+
57+
findExplicitFieldsAction :: [a |? CodeAction] -> [CodeAction]
58+
findExplicitFieldsAction = filter isExplicitFieldsCodeAction . rights . map toEither
59+
60+
isExplicitFieldsCodeAction :: CodeAction -> Bool
61+
isExplicitFieldsCodeAction CodeAction {_title} =
62+
"Expand record wildcard" `T.isPrefixOf` _title
63+
64+
testDataDir :: FilePath
65+
testDataDir = "test" </> "testdata"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module Construction where
6+
7+
data MyRec = MyRec
8+
{ foo :: Int
9+
, bar :: Int
10+
, baz :: Char
11+
}
12+
13+
convertMe :: () -> MyRec
14+
convertMe _ =
15+
let foo = 3
16+
bar = 5
17+
baz = 'a'
18+
in MyRec {foo, bar, baz}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
4+
module Construction where
5+
6+
data MyRec = MyRec
7+
{ foo :: Int
8+
, bar :: Int
9+
, baz :: Char
10+
}
11+
12+
convertMe :: () -> MyRec
13+
convertMe _ =
14+
let foo = 3
15+
bar = 5
16+
baz = 'a'
17+
in MyRec {..}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module Mixed where
6+
7+
data MyRec = MyRec
8+
{ foo :: Int
9+
, bar :: Int
10+
, baz :: Char
11+
}
12+
13+
convertMe :: MyRec -> String
14+
convertMe MyRec {foo, bar = bar', baz} = show foo ++ show bar' ++ show baz
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module Mixed where
6+
7+
data MyRec = MyRec
8+
{ foo :: Int
9+
, bar :: Int
10+
, baz :: Char
11+
}
12+
13+
convertMe :: MyRec -> String
14+
convertMe MyRec {foo, bar = bar', ..} = show foo ++ show bar' ++ show baz
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module WildcardOnly where
6+
7+
data MyRec = MyRec
8+
{ foo :: Int
9+
, bar :: Int
10+
, baz :: Char
11+
}
12+
13+
convertMe :: MyRec -> String
14+
convertMe MyRec {foo, bar, baz} = show foo ++ show bar ++ show baz
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
4+
module WildcardOnly where
5+
6+
data MyRec = MyRec
7+
{ foo :: Int
8+
, bar :: Int
9+
, baz :: Char
10+
}
11+
12+
convertMe :: MyRec -> String
13+
convertMe MyRec {..} = show foo ++ show bar ++ show baz
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module WithExplicitBind where
6+
7+
data MyRec = MyRec
8+
{ foo :: Int
9+
, bar :: Int
10+
, baz :: Char
11+
}
12+
13+
convertMe :: MyRec -> String
14+
convertMe MyRec {foo = foo', bar, baz} = show foo' ++ show bar ++ show baz
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
4+
module WithExplicitBind where
5+
6+
data MyRec = MyRec
7+
{ foo :: Int
8+
, bar :: Int
9+
, baz :: Char
10+
}
11+
12+
convertMe :: MyRec -> String
13+
convertMe MyRec {foo = foo', ..} = show foo' ++ show bar ++ show baz
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module WithPun where
6+
7+
data MyRec = MyRec
8+
{ foo :: Int
9+
, bar :: Int
10+
, baz :: Char
11+
}
12+
13+
convertMe :: MyRec -> String
14+
convertMe MyRec {foo, bar, baz} = show foo ++ show bar ++ show baz
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module WithPun where
6+
7+
data MyRec = MyRec
8+
{ foo :: Int
9+
, bar :: Int
10+
, baz :: Char
11+
}
12+
13+
convertMe :: MyRec -> String
14+
convertMe MyRec {foo, ..} = show foo ++ show bar ++ show baz
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
3+
module ExplicitBinds where
4+
5+
data MyRec = MyRec
6+
{ foo :: Int
7+
, bar :: Int
8+
, baz :: Char
9+
}
10+
11+
convertMe :: MyRec -> String
12+
convertMe MyRec {foo = foo', bar = bar', baz = baz'} = show foo' ++ show bar' ++ show baz'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
3+
module Infix where
4+
5+
data MyRec = MyRec
6+
{ foo :: Int
7+
, bar :: Int
8+
}
9+
10+
convertMe :: MyRec -> String
11+
convertMe (foo' `MyRec` bar') = show foo' ++ show bar'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
3+
module Prefix where
4+
5+
data MyRec = MyRec
6+
{ foo :: Int
7+
, bar :: Int
8+
}
9+
10+
convertMe :: MyRec -> String
11+
convertMe (foo' `MyRec` bar') = show foo' ++ show bar'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
3+
4+
module Puns where
5+
6+
data MyRec = MyRec
7+
{ foo :: Int
8+
, bar :: Int
9+
, baz :: Char
10+
}
11+
12+
convertMe :: MyRec -> String
13+
convertMe MyRec {foo, bar, baz} = show foo ++ show bar ++ show baz

0 commit comments

Comments
 (0)