Skip to content

Commit eb7f8a4

Browse files
Add test for non-duplication; better name.
1 parent 8585551 commit eb7f8a4

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

Text.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module Text (
1010
displayWidth, toMaxWidth, toWidth, byteLength,
1111
fromBS, isLineSafe,
1212
encodeFS,
13-
drawText, setXtermTitle
13+
drawText, setXtermTitle,
14+
toBS, -- only used in test suite
1415
) where
1516

1617
import Base
@@ -35,12 +36,12 @@ newtype SText = SText ByteString
3536
deriving newtype (Semigroup, Monoid)
3637

3738
-- | Convenient internal combinator.
38-
unSText :: SText -> ByteString
39-
unSText (SText bs) = bs
39+
toBS :: SText -> ByteString
40+
toBS (SText bs) = bs
4041

4142
-- | Can be used in lieu of 'displayWidth' for known 1-width-character text.
4243
byteLength :: SText -> Int
43-
byteLength = P.length . unSText
44+
byteLength = P.length . toBS
4445

4546
instance IsString SText where
4647
fromString = SText . UTF8.fromString . toPrintable
@@ -55,7 +56,7 @@ matches s = match' <$> makeRegexOptsM (compIgnoreCase + compExtended + compNoSub
5556

5657
-- | Possible number.
5758
readIntM :: SText -> Maybe Int
58-
readIntM = fmap fst . P.readInt . unSText
59+
readIntM = fmap fst . P.readInt . toBS
5960

6061
showInt :: Int -> SText
6162
showInt = SText . P.pack . show
@@ -111,7 +112,7 @@ encodeFS str = do
111112

112113
-- | Sum of the column widths of every codepoint.
113114
displayWidth :: SText -> Int
114-
displayWidth = UTF8.foldl (\acc c -> acc + charWidth c) 0 . unSText
115+
displayWidth = UTF8.foldl (\acc c -> acc + charWidth c) 0 . toBS
115116

116117
-- | These functions truncate with ellipses if needed to get width ≤'w'.
117118
-- 'toWidth' adds padding as needed so the width is exactly 'w'.
@@ -125,7 +126,7 @@ sizer pad w s@(SText bs)
125126
| True = walk 0 bs
126127
where
127128
dw = displayWidth s
128-
byteTake i = SText . P.take i . unSText
129+
byteTake i = SText . P.take i . toBS
129130
walk !l rest
130131
| l' >= w = byteTake (byteLength s - P.length rest) s
131132
<> mconcat (replicate (w-l) "")
@@ -146,7 +147,7 @@ foreign import ccall unsafe
146147
-- | Set xterm title with ANSI escape sequence.
147148
setXtermTitle :: [SText] -> IO ()
148149
setXtermTitle strs = do
149-
traverse_ (P.hPut stderr) (before : map unSText strs ++ [after])
150+
traverse_ (P.hPut stderr) (before : map toBS strs ++ [after])
150151
hFlush stderr
151152
where
152153
before = "\ESC]0;"

test/TextSpec.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Test.Tasty.HUnit
55

66
import Text
77
import Data.ByteString.UTF8 as UTF8
8+
import Data.ByteString.Unsafe qualified as P
89

910
-- These tests depend on wcwidth's behavior under a UTF-8 locale and on a
1011
-- handful of codepoints whose canonical widths are well-known:
@@ -90,13 +91,20 @@ tests = testGroup "Text"
9091
]
9192
, testGroup "fromBS"
9293
[ testCase "Unicode" $ fromBS (UTF8.fromString "encöde") @?= "encöde"
93-
, testCase "bad bytes" $ fromBS ("no\130b\8y") @?= "no�b�y"
94-
, testCase "bad bytes" $ fromBS ("no\130bsy") @?= "no�bsy"
95-
, testCase "control" $ fromBS ("nob\8dy") @?= "nob�dy"
94+
, testCase "bad bytes" $ fromBS "no\130b\8y" @?= "no�b�y"
95+
, testCase "bad bytes" $ fromBS "no\130bsy" @?= "no�bsy"
96+
, testCase "control" $ fromBS "nob\8dy" @?= "nob�dy"
97+
, testCase "no dupe" $
98+
let bs = UTF8.fromString "schőn" in eqRef bs (toBS $ fromBS bs)
9699
]
97100
]
98101

99102

100103
m :: Maybe Bool -> String -> String -> SText -> TestTree
101104
m b tag pat str = testCase tag $ ($ str) <$> matches (UTF8.fromString pat) @?= b
102105

106+
-- Test memory reuse.
107+
eqRef :: ByteString -> ByteString -> Assertion
108+
eqRef a b =
109+
P.unsafeUseAsCStringLen a \a' -> P.unsafeUseAsCStringLen b \b' -> a' @?= b'
110+

0 commit comments

Comments
 (0)