on develop help sign in
fix bug in wrapWithOffsetAndAlignment and add tests
commit494a52073aabd23414e827b5db96e81636703115 authorpdlla <chippermonky@gmail.com> dateSat 12 Jun 2021, 20:43 (5 years ago) parent97e0c65
2 files changed+13−3
src/Data/Text/Zipper.hs+1−1 test/Data/Text/ZipperSpec.hs+12−2
src/Data/Text/Zipper.hs+1−1
@@ -367,7 +367,7 @@ wrapWithOffsetAndAlignment
367367 -> [WrappedLine] -- (words on that line, hidden space char, offset from beginning of line)
368368wrapWithOffsetAndAlignment _ maxWidth _ _ | maxWidth <= 0 = []
369369wrapWithOffsetAndAlignment alignment maxWidth n txt = assert (n <= maxWidth) r where
370− r' = splitWordsAtDisplayWidth maxWidth $ T.replicate n " " : wordsWithWhitespace txt
370+ r' = splitWordsAtDisplayWidth maxWidth $ wordsWithWhitespace ( T.replicate n "." <> txt)
371371 fmapfn (t,b) = case alignment of
372372 TextAlignment_Left -> WrappedLine t b 0
373373 TextAlignment_Right -> WrappedLine t b (maxWidth-l)
test/Data/Text/ZipperSpec.hs+12−2
@@ -9,14 +9,15 @@ import Prelude
99import Test.Hspec
1010
1111import qualified Data.Map as Map
12−import Data.Text
12+import qualified Data.Text as T
13+import Control.Monad
1314
1415import Data.Text.Zipper
1516
1617
1718someSentence = "12345 1234 12"
1819
19−splitSentenceAtDisplayWidth :: Int -> Text -> [(Text, Bool)]
20+splitSentenceAtDisplayWidth :: Int -> T.Text -> [(T.Text, Bool)]
2021splitSentenceAtDisplayWidth w t = splitWordsAtDisplayWidth w (wordsWithWhitespace t)
2122
2223spec :: Spec
@@ -38,6 +39,7 @@ spec = do
3839 wrapWithOffsetAndAlignment TextAlignment_Left 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 0)]
3940 wrapWithOffsetAndAlignment TextAlignment_Right 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 1), (WrappedLine "12" False 3)]
4041 wrapWithOffsetAndAlignment TextAlignment_Center 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 1)]
42+ wrapWithOffsetAndAlignment TextAlignment_Left 5 1 someSentence `shouldBe` [(WrappedLine "1234" False 0), (WrappedLine "5" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 0)]
4143 it "eolSpacesToLogicalLines" $ do
4244 eolSpacesToLogicalLines
4345 [
@@ -83,3 +85,11 @@ spec = do
8385 _displayLines_cursorPos dl4 `shouldBe` (5,0)
8486 _displayLines_cursorPos dl5 `shouldBe` (4,0)
8587 _displayLines_cursorPos dl6 `shouldBe` (4,0)
88+ it "displayLinesWithAlignment - spans" $ do
89+ let
90+ someText = top $ fromText "0123456789abcdefgh"
91+ -- outer span length should be invariant when changing TextAlignment and CursorPosition
92+ forM_ [0..4] $ \x -> do
93+ forM_ [TextAlignment_Left, TextAlignment_Center, TextAlignment_Right] $ \ta -> do
94+ length (_displayLines_spans $ (displayLinesWithAlignment ta 5 () () someText) {_displayLines_cursorPos = (x,x)}) `shouldBe` 4
95+ length (_displayLines_spans $ (displayLinesWithAlignment ta 10 () () someText) {_displayLines_cursorPos = (x,x)}) `shouldBe` 2