Skip to content

Commit 650b8ba

Browse files
committed
fix add / remove backslash and add tests
1 parent 9bcf5c8 commit 650b8ba

File tree

2 files changed

+78
-44
lines changed

2 files changed

+78
-44
lines changed

src/StringLiteral.hs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import qualified System.Exit as Exit
1616

1717
usage :: String
1818
usage =
19-
"StrLit [ --wrapped --{add,remove,toggle}-{backslash,lines} ]\n\
19+
"string-literal [ --wrapped --{add,remove,toggle}-{backslash,lines} ]\n\
2020
\\n\
2121
\Convert between plain text and either backslash-continued string\n\
2222
\literals, or list of lines style strings. This is to work around\n\
@@ -108,7 +108,6 @@ collectNewlines = filter (not . Text.null) . snd . List.mapAccumL collect 0
108108
| Text.strip line == "" = (newlines+1, "")
109109
| otherwise = (0, Text.replicate newlines "\\n" <> line)
110110

111-
112111
{- | Invert 'addBackslashWrapped'. Drop a leading space unless there was
113112
a leading \n.
114113
-}
@@ -130,21 +129,18 @@ removeBackslashWrapped =
130129
| otherwise = s
131130

132131
addBackslash :: [Text] -> [Text]
133-
addBackslash = indent . map3 add1 addn end . map quote . dedent
132+
addBackslash = indent
133+
. mapSurround ("\"", nl) ("\\", nl) ("\\", "\\n\"") ("\"", "\\n\"")
134+
. map quote . dedent
134135
where
135136
nl = "\\n\\"
136-
add1 s = "\"" <> s <> nl
137-
addn s = "\\" <> s <> nl
138-
end s = "\\" <> s <> "\\n\""
139137

140138
removeBackslash :: [Text] -> [Text]
141-
removeBackslash = indent . map unquote . map3 start middle end . dedent
139+
removeBackslash = indent . map unquote
140+
. mapStrip ("\"", nl) ("\\", nl) ("\\", "\\n\"") ("\"", "\\n\"")
141+
. dedent
142142
where
143-
start = stripPrefix "\"" . spaces . nl
144-
middle = stripPrefix "\\" . spaces . nl
145-
end = stripPrefix "\\" . spaces . stripSuffix "\\n\""
146-
spaces = Text.dropWhile (==' ')
147-
nl = stripSuffix "\\n\\"
143+
nl = "\\n\\"
148144

149145
-- * lines
150146

@@ -212,6 +208,18 @@ mapAround start middle end only xs = case xs of
212208
[x] -> [end x]
213209
x : xs -> middle x : go xs
214210

211+
mapSurround :: (Text, Text) -> (Text, Text) -> (Text, Text) -> (Text, Text)
212+
-> [Text] -> [Text]
213+
mapSurround start middle end only =
214+
mapAround (uncurry surround start) (uncurry surround middle)
215+
(uncurry surround end) (uncurry surround only)
216+
217+
mapStrip :: (Text, Text) -> (Text, Text) -> (Text, Text) -> (Text, Text)
218+
-> [Text] -> [Text]
219+
mapStrip start middle end only =
220+
mapAround (uncurry strip start) (uncurry strip middle)
221+
(uncurry strip end) (uncurry strip only)
222+
215223
surround :: Text -> Text -> Text -> Text
216224
surround s e x = s <> x <> e
217225

src/StringLiteral_test.hs

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ main :: IO ()
1111
main = Tasty.defaultMain $ Tasty.testGroup "tests"
1212
[ test_backslashWrapped
1313
, test_backslashWrapped_roundTrip
14+
, test_backslash
15+
, test_backslash_roundTrip
1416
]
1517

1618
run :: Tasty.TestTree -> IO ()
@@ -51,49 +53,73 @@ test_backslashWrapped = Tasty.testGroup "backslashWrapped"
5153
-- it goes from reasonable input to the right haskell, and round trip will
5254
-- ensure it goes back to the original state.
5355
test_backslashWrapped_roundTrip :: Tasty.TestTree
54-
test_backslashWrapped_roundTrip = Tasty.testGroup "roundTrip"
55-
[ backslashWrapped [" one line"]
56-
, backslashWrapped [" two", " lines"]
57-
, backslashWrapped
56+
test_backslashWrapped_roundTrip = Tasty.testGroup "backslashWrapped_roundTrip" $
57+
map trip
58+
[ [" one line"]
59+
, [" two", " lines"]
60+
,
5861
[ " with an"
5962
, " explicit"
6063
, ""
6164
, " newline"
6265
]
6366
]
6467
where
65-
backslashWrapped = trip
68+
trip = roundTrip
6669
StringLiteral.addBackslashWrapped StringLiteral.removeBackslashWrapped
6770

68-
trip :: (Stack.HasCallStack, Show a, Eq a) => (a -> b) -> (b -> a) -> a
71+
test_backslash :: Tasty.TestTree
72+
test_backslash = Tasty.testGroup "backslash"
73+
[ [" one line"] ==> [" \"one line\\n\""]
74+
, [" two", " lines"] ==> [" \"two\\n\\", " \\lines\\n\""]
75+
,
76+
[ " with an"
77+
, " explicit"
78+
, ""
79+
, " newline"
80+
] ==>
81+
[ " \"with an\\n\\"
82+
, " \\explicit\\n\\"
83+
, " \\\\n\\"
84+
, " \\newline\\n\""
85+
]
86+
,
87+
[ " with"
88+
, " explicit"
89+
, " indent"
90+
] ==>
91+
[ " \"with\\n\\"
92+
, " \\ explicit\\n\\"
93+
, " \\indent\\n\""
94+
]
95+
]
96+
where
97+
(==>) :: Stack.HasCallStack => [Text] -> [Text] -> Tasty.TestTree
98+
(==>) = test StringLiteral.addBackslash
99+
100+
test_backslash_roundTrip :: Tasty.TestTree
101+
test_backslash_roundTrip = Tasty.testGroup "backslash_roundTrip" $ map trip
102+
[ [" one line"]
103+
, [" two", " lines"]
104+
,
105+
[ " with an"
106+
, " explicit"
107+
, ""
108+
, " newline"
109+
]
110+
,
111+
[ " with"
112+
, " explicit"
113+
, " indent"
114+
]
115+
]
116+
where
117+
trip = roundTrip StringLiteral.addBackslash StringLiteral.removeBackslash
118+
119+
roundTrip :: (Stack.HasCallStack, Show a, Eq a) => (a -> b) -> (b -> a) -> a
69120
-> Tasty.TestTree
70-
trip f g x = HUnit.testCase (take 70 $ show x) $ g (f x) HUnit.@?= x
121+
roundTrip f g x = HUnit.testCase (take 70 $ show x) $ g (f x) HUnit.@?= x
71122

72123
test :: (Stack.HasCallStack, Show a, Eq b, Show b) => (a -> b) -> a -> b
73124
-> Tasty.TestTree
74125
test f x expected = HUnit.testCase (take 70 $ show x) $ f x HUnit.@?= expected
75-
76-
-- _test_addBackslash = Text.IO.putStr $ Text.unlines $ addBackslash
77-
-- [ " foo"
78-
-- , ""
79-
-- , " bar"
80-
-- ]
81-
82-
-- test1 = HUnit.testCase "test1"
83-
-- [ [1, 2, 3] `compare` [1,2] === GT
84-
-- , [1, 2, 3] `compare` [1,2,2] === LT
85-
-- ]
86-
87-
-- unitTests :: Tasty.TestTree
88-
-- unitTests = Tasty.testGroup "Unit tests"
89-
-- [ HUnit.testCase "blah" $
90-
-- [1, 2, 3] `compare` [1,2] @?= GT
91-
--
92-
-- -- the following test does not hold
93-
-- , HUnit.testCase "List comparison (same length)" $
94-
-- [1, 2, 3] `compare` [1,2,2] @?= LT
95-
-- ]
96-
97-
98-
(===) :: (Eq a, Show a) => a -> a -> HUnit.Assertion
99-
(===) = (HUnit.@?=)

0 commit comments

Comments
 (0)