packages feed

unbeliever-0.10.0.1 / tests / CheckRopeBehaviour.hs

{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE QuasiQuotes #-}{-# OPTIONS_GHC -fno-warn-missing-signatures #-}module CheckRopeBehaviour whereimport Data.Char (isSpace)import qualified Data.FingerTree as Fimport qualified Data.List as Listimport qualified Data.Text as Timport qualified Data.Text.Encoding as Timport qualified Data.Text.Lazy as Uimport qualified Data.Text.Short as Simport Test.Hspecimport Core.Text.Ropeimport Core.Text.Utilitieshydrogen = "H₂" :: Ropesulfate = "SO₄" :: Ropesulfuric_acid = hydrogen <> sulfatecompound = "3" <> "-" <> "ethyl" <> "-" <> "4" <> "-" <> "methyl" <> "hexane" :: RopecheckRopeBehaviour :: SpeccheckRopeBehaviour = do    describe "Rope data type" $      do        it "IsString instance behaves" $ do            unRope ("Hello" :: Rope) `shouldBe` F.singleton (S.pack "Hello")        it "calculates length accurately" $ do            widthRope hydrogen `shouldBe` 2            widthRope sulfate `shouldBe` 3            widthRope (hydrogen <> sulfate) `shouldBe` 5        it "Eq instance behaves" $ do             ("" :: Rope) == ("" :: Rope) `shouldBe` True             ("C" :: Rope) /= ("" :: Rope) `shouldBe` True             ("" :: Rope) /= ("F" :: Rope) `shouldBe` True             ("O" :: Rope) == ("O" :: Rope) `shouldBe` True             ("H₂" :: Rope) == ("H₂" :: Rope) `shouldBe` True             ("H₂" :: Rope) /= ("SO₄" :: Rope)  `shouldBe` True        -- depended on Textual instance for String being fixed and        -- the Eq instance being customized to ignore tree structure        it "concatonates two Ropes correctly (Monoid)" $ do             ("H₂" :: Rope) <> ("SO₄" :: Rope)  `shouldBe` ("H₂SO₄" :: Rope)        it "concatonates two Ropes correctly (Textual)" $ do             appendRope ("SO₄" :: Rope) ("H₂" :: Rope) `shouldBe` ("H₂SO₄" :: Rope)        it "exports to ByteString" $          let            expected = T.encodeUtf8 (T.pack "H₂SO₄")          in do            fromRope sulfuric_acid `shouldBe` expected        it "exports to Text (Strict)" $ do            fromRope sulfuric_acid `shouldBe` T.pack "H₂SO₄"        it "exports to Text (Lazy)" $ do            fromRope sulfuric_acid `shouldBe` U.pack "H₂SO₄"        it "does the splits" $ do            -- compare behaviour on Haskell lists            List.splitAt 0 ("123456789" :: String) `shouldBe` ("", "123456789")            List.splitAt 3 ("123456789" :: String) `shouldBe` ("123", "456789")            -- expect same behaviour of Rope            splitRope 0 ("123456789" :: Rope) `shouldBe` ("", "123456789")            splitRope 3 ("123456789" :: Rope) `shouldBe` ("123", "456789")            splitRope 9 ("123456789" :: Rope) `shouldBe` ("123456789","")            splitRope 10 ("123456789" :: Rope) `shouldBe` ("123456789","")            splitRope (-1) ("123456789" :: Rope) `shouldBe` ("", "123456789")            -- exercise splitRopeting at and between piece boundaries            splitRope 0 compound `shouldBe` ("", "3-ethyl-4-methylhexane")            splitRope 1 compound `shouldBe` ("3", "-ethyl-4-methylhexane")            splitRope 2 compound `shouldBe` ("3-", "ethyl-4-methylhexane")            splitRope 4 compound `shouldBe` ("3-et", "hyl-4-methylhexane")            --                             1234567890            splitRope 10 compound `shouldBe` ("3-ethyl-4-", "methylhexane")            splitRope 11 compound `shouldBe` ("3-ethyl-4-m", "ethylhexane")            splitRope 16 compound `shouldBe` ("3-ethyl-4-methyl", "hexane")            splitRope 21 compound `shouldBe` ("3-ethyl-4-methylhexan", "e")            widthRope compound `shouldBe` 22            splitRope 22 compound `shouldBe` ("3-ethyl-4-methylhexane", "")            splitRope 23 compound `shouldBe` ("3-ethyl-4-methylhexane", "")            splitRope (-1) compound `shouldBe` ("", "3-ethyl-4-methylhexane")        it "does insertion correctly" $ do            insertRope 3 "two" "onethree" `shouldBe` "onetwothree"            insertRope 3 "Con" "Def 1" `shouldBe` "DefCon 1"            insertRope 0 "one" "twothree" `shouldBe` "onetwothree"            insertRope 6 "three" "onetwo" `shouldBe` "onetwothree"    describe "QuasiQuoted string literals" $      do        it "string literal is IsString" $ do            [quote|Hello|] `shouldBe` ("Hello" :: String)            [quote|Hello|] `shouldBe` ("Hello" :: Rope)        it "trims multi-line string literal" $ do            [quote|Hello            |] `shouldBe` ("Hello\n" :: Rope)            [quote|HelloWorld            |] `shouldBe` ("Hello\nWorld\n" :: Rope)    describe "Splitting into words" $ do        it "breaks short text into chunks" $ do            intoChunks isSpace "" `shouldBe` []            intoChunks isSpace "Hello" `shouldBe` ["Hello"]            intoChunks isSpace "Hello World" `shouldBe` ["Hello","World"]            intoChunks isSpace "Hello " `shouldBe` ["Hello",""]            intoChunks isSpace " Hello" `shouldBe` ["","Hello"]            intoChunks isSpace " Hello " `shouldBe` ["","Hello",""]        it "breaks consecutive short texts into chunks" $ do            intoPieces isSpace "Hello" (Nothing,[]) `shouldBe`                (Just "Hello",[])            intoPieces isSpace "" (Nothing,[]) `shouldBe`                (Nothing,[])            intoPieces isSpace "" (Nothing,["World"]) `shouldBe`                (Nothing,["World"])            intoPieces isSpace "This is" (Nothing,["","a","","test."]) `shouldBe`                (Just "This",["is","","a","","test."])            intoPieces isSpace "This i" (Just "s",["","a","","test."]) `shouldBe`                (Just "This",["is","","a","","test."])        it "single piece containing multiple words splits correctly" $          let            text = "This is a test"          in do            breakWords text `shouldBe` ["This","is","a","test"]        it "single piece, long run of whitespace splits correctly" $          let            text = "This is\na    test"          in do            breakWords text `shouldBe` ["This","is","a","test"]        it "text spanning two pieces can be split into words" $          let            text = "This is " <> "a test"          in do            breakWords text `shouldBe` ["This","is","a","test"]        it "text spanning many pieces can be split into words" $          let            text = "st" <> "" <> "op" <> "" <> " " <> " " <> "and go" <> "op"          in do            breakWords text `shouldBe` ["stop","and","goop"]        it "empty and whitespace-only corner cases handled correctly" $          let            text = "  " <> "" <> "stop" <> "" <> "  "          in do            breakWords text `shouldBe` ["stop"]    describe "Splitting into lines" $ do        it "preconditions are met" $ do            breakLines "" `shouldBe` []            breakLines "Hello" `shouldBe` ["Hello"]            breakLines "Hello\nWorld" `shouldBe` ["Hello","World"]            breakLines "Hello\n" `shouldBe` ["Hello"]            breakLines "\nHello" `shouldBe` ["","Hello"]            breakLines "\nHello\n" `shouldBe` ["","Hello"]            breakLines "Hello\nWorld\n" `shouldBe` ["Hello","World"]            breakLines "Hello\n\nWorld\n" `shouldBe` ["Hello","","World"]            breakLines "Hello\n\nWorld\n\n" `shouldBe` ["Hello","","World",""]        it "single piece containing multiple lines splits correctly" $          let            para = [quote|This is a testof the EmergencyBroadcastSystem, beeeeep|]          in do            breakLines para `shouldBe`                [ "This is a test"                , "of the Emergency"                , "Broadcast"                , "System, beeeeep"                ]        it "preserves blank lines" $          let            para = [quote|First line.Third line.|]          in do            breakLines para `shouldBe`                [ "First line."                , ""                , "Third line."                ]    describe "Formatting paragraphs" $ do        it "multi-line paragraph rewraps correctly" $          let            para = [quote|Hello this isa test of the Emergency Broadcast System            |]          in            wrap 20 para `shouldBe` [quote|Hello this is a testof the EmergencyBroadcast System|]