Skip to content

Commit f5af2d3

Browse files
committed
Add golden tests for underscore conversions
1 parent a68382a commit f5af2d3

File tree

7 files changed

+65
-40
lines changed

7 files changed

+65
-40
lines changed

plugins/hls-alternate-number-format-plugin/test/Main.hs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import Data.List (find)
66
import Data.Text (Text)
77
import qualified Data.Text as T
88
import qualified Ide.Plugin.AlternateNumberFormat as AlternateNumberFormat
9-
import qualified Ide.Plugin.Conversion as Conversion
10-
import Properties.Conversion (conversions)
9+
import qualified Properties.Conversion as Conversion
1110
import System.FilePath ((<.>), (</>))
1211
import Test.Hls
1312
import Text.Regex.TDFA ((=~))
@@ -23,29 +22,32 @@ alternateNumberFormatPlugin = mkPluginTestDescriptor AlternateNumberFormat.descr
2322
-- to do with how
2423
test :: TestTree
2524
test = testGroup "alternateNumberFormat" [
26-
codeActionHex "TIntDtoH" 3 13
27-
, codeActionOctal "TIntDtoO" 3 13
28-
, codeActionBinary "TIntDtoB" 4 13
29-
, codeActionNumDecimal "TIntDtoND" 5 13
30-
, codeActionFracExp "TFracDtoE" 3 13
31-
, codeActionFloatHex "TFracDtoHF" 4 13
32-
, codeActionDecimal "TIntHtoD" 3 13
33-
, codeActionDecimal "TFracHFtoD" 4 13
25+
codeActionHex 0 "TIntDtoH" 3 13
26+
, codeActionOctal 0 "TIntDtoO" 3 13
27+
, codeActionBinary 0 "TIntDtoB" 4 13
28+
, codeActionNumDecimal 0 "TIntDtoND" 5 13
29+
, codeActionFracExp 0 "TFracDtoE" 3 13
30+
, codeActionFloatHex 0 "TFracDtoHF" 4 13
31+
, codeActionDecimal 0 "TIntHtoD" 3 13
32+
, codeActionDecimal 0 "TFracHFtoD" 4 13
33+
, codeActionDecimal 3 "TFracDtoDU0toU3" 3 13
34+
, codeActionDecimal 2 "TFracDtoDU3toU4" 3 13
35+
, codeActionDecimal 3 "TFracDtoDU3toU0" 3 13
3436
-- to test we don't duplicate pragmas
35-
, codeActionFloatHex "TFracDtoHFWithPragma" 4 13
37+
, codeActionFloatHex 0 "TFracDtoHFWithPragma" 4 13
3638
, codeActionProperties "TFindLiteralIntPattern" [(4, 25), (5,25)] $ \actions -> do
3739
liftIO $ length actions @?= 8
3840
, codeActionProperties "TFindLiteralIntCase" [(4, 29)] $ \actions -> do
39-
liftIO $ length actions @?= 4
41+
liftIO $ length actions @?= 5
4042
, codeActionProperties "TFindLiteralIntCase2" [(5, 21)] $ \actions -> do
41-
liftIO $ length actions @?= 4
43+
liftIO $ length actions @?= 5
4244
, codeActionProperties "TFindLiteralDoReturn" [(6, 10)] $ \actions -> do
43-
liftIO $ length actions @?= 4
45+
liftIO $ length actions @?= 5
4446
, codeActionProperties "TFindLiteralDoLet" [(6, 13), (7, 13)] $ \actions -> do
45-
liftIO $ length actions @?= 8
47+
liftIO $ length actions @?= 12
4648
, codeActionProperties "TFindLiteralList" [(4, 28)] $ \actions -> do
47-
liftIO $ length actions @?= 4
48-
, conversions
49+
liftIO $ length actions @?= 5
50+
, Conversion.conversions
4951
]
5052

5153
codeActionProperties :: TestName -> [(Int, Int)] -> ([CodeAction] -> Session ()) -> TestTree
@@ -81,26 +83,26 @@ codeActionTest filter' fp line col = goldenAlternateFormat fp $ \doc -> do
8183
Just (InR x) -> executeCodeAction x
8284
_ -> liftIO $ assertFailure "Unable to find CodeAction"
8385

84-
codeActionDecimal :: FilePath -> Int -> Int -> TestTree
85-
codeActionDecimal = codeActionTest isDecimalCodeAction
86+
codeActionDecimal :: Int -> FilePath -> Int -> Int -> TestTree
87+
codeActionDecimal nrUnderscores = codeActionTest (isDecimalCodeAction nrUnderscores)
8688

87-
codeActionHex :: FilePath -> Int -> Int -> TestTree
88-
codeActionHex = codeActionTest isHexCodeAction
89+
codeActionHex :: Int -> FilePath -> Int -> Int -> TestTree
90+
codeActionHex nrUnderscores = codeActionTest (isHexCodeAction nrUnderscores)
8991

90-
codeActionOctal :: FilePath -> Int -> Int -> TestTree
91-
codeActionOctal = codeActionTest isOctalCodeAction
92+
codeActionOctal :: Int -> FilePath -> Int -> Int -> TestTree
93+
codeActionOctal nrUnderscores = codeActionTest (isOctalCodeAction nrUnderscores)
9294

93-
codeActionBinary :: FilePath -> Int -> Int -> TestTree
94-
codeActionBinary = codeActionTest isBinaryCodeAction
95+
codeActionBinary :: Int -> FilePath -> Int -> Int -> TestTree
96+
codeActionBinary nrUnderscores = codeActionTest (isBinaryCodeAction nrUnderscores)
9597

96-
codeActionNumDecimal :: FilePath -> Int -> Int -> TestTree
97-
codeActionNumDecimal = codeActionTest isNumDecimalCodeAction
98+
codeActionNumDecimal :: Int -> FilePath -> Int -> Int -> TestTree
99+
codeActionNumDecimal nrUnderscores = codeActionTest (isNumDecimalCodeAction nrUnderscores)
98100

99-
codeActionFracExp :: FilePath -> Int -> Int -> TestTree
100-
codeActionFracExp = codeActionTest isNumDecimalCodeAction
101+
codeActionFracExp :: Int -> FilePath -> Int -> Int -> TestTree
102+
codeActionFracExp nrUnderscores = codeActionTest (isNumDecimalCodeAction nrUnderscores)
101103

102-
codeActionFloatHex :: FilePath -> Int -> Int -> TestTree
103-
codeActionFloatHex = codeActionTest isHexFloatCodeAction
104+
codeActionFloatHex :: Int -> FilePath -> Int -> Int -> TestTree
105+
codeActionFloatHex nrUnderscores = codeActionTest (isHexFloatCodeAction nrUnderscores)
104106

105107
codeActionTitle :: (Command |? CodeAction) -> Maybe Text
106108
codeActionTitle (InR CodeAction {_title}) = Just _title
@@ -123,26 +125,31 @@ octalRegex = intoInfix <> Conversion.octalRegex <> maybeExtension
123125
numDecimalRegex = intoInfix <> Conversion.numDecimalRegex <> maybeExtension
124126
decimalRegex = intoInfix <> Conversion.decimalRegex <> maybeExtension
125127

126-
isCodeAction :: Text -> Maybe Text -> Bool
127-
isCodeAction userRegex (Just txt) = txt =~ Conversion.matchLineRegex (convertPrefix <> userRegex)
128-
isCodeAction _ _ = False
128+
isCodeAction :: Text -> Int -> Maybe Text -> Bool
129+
isCodeAction userRegex nrUnderscores (Just txt)
130+
| matchesUnderscores txt nrUnderscores
131+
= txt =~ Conversion.matchLineRegex (convertPrefix <> userRegex)
132+
isCodeAction _ _ _ = False
133+
134+
matchesUnderscores :: Text -> Int -> Bool
135+
matchesUnderscores txt nrUnderscores = T.count "_" txt == nrUnderscores
129136

130-
isHexCodeAction :: Maybe Text -> Bool
137+
isHexCodeAction :: Int -> Maybe Text -> Bool
131138
isHexCodeAction = isCodeAction hexRegex
132139

133-
isHexFloatCodeAction :: Maybe Text -> Bool
140+
isHexFloatCodeAction :: Int -> Maybe Text -> Bool
134141
isHexFloatCodeAction = isCodeAction hexFloatRegex
135142

136-
isBinaryCodeAction :: Maybe Text -> Bool
143+
isBinaryCodeAction :: Int -> Maybe Text -> Bool
137144
isBinaryCodeAction = isCodeAction binaryRegex
138145

139-
isOctalCodeAction :: Maybe Text -> Bool
146+
isOctalCodeAction :: Int -> Maybe Text -> Bool
140147
isOctalCodeAction = isCodeAction octalRegex
141148

142149
-- This can match EITHER an integer as NumDecimal extension or a Fractional
143150
-- as in 1.23e-3 (so anything with an exponent really)
144-
isNumDecimalCodeAction :: Maybe Text -> Bool
151+
isNumDecimalCodeAction :: Int -> Maybe Text -> Bool
145152
isNumDecimalCodeAction = isCodeAction numDecimalRegex
146153

147-
isDecimalCodeAction :: Maybe Text -> Bool
154+
isDecimalCodeAction :: Int -> Maybe Text -> Bool
148155
isDecimalCodeAction = isCodeAction decimalRegex
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoDUnderscores3 where
2+
3+
convertMe = 12_345.678_912_3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoDUnderscores3 where
2+
3+
convertMe = 12345.6789123
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoDUnderscores3 where
2+
3+
convertMe = 12345.6789123
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoDUnderscores3 where
2+
3+
convertMe = 12_345.678_912_3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoDUnderscores3 where
2+
3+
convertMe = 1_2345.6789_123
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoDUnderscores3 where
2+
3+
convertMe = 12345.6789123

0 commit comments

Comments
 (0)