~jaro/balkon

13f4f8e765e5858c8869af76894b52d8a4d5bf6a — Jaro 1 year, 6 months ago 976fc30
Test paragraphSafeWidth on LTR paragraphs.
2 files changed, 78 insertions(+), 16 deletions(-)

M balkon.cabal
M test/Data/Text/ParagraphLayout/RichSpec.hs
M balkon.cabal => balkon.cabal +1 -1
@@ 116,6 116,7 @@ library balkon-internal
        Data.Text.ParagraphLayout.Internal.Paginable,
        Data.Text.ParagraphLayout.Internal.ParagraphAlignment,
        Data.Text.ParagraphLayout.Internal.ParagraphConstruction,
        Data.Text.ParagraphLayout.Internal.ParagraphLine,
        Data.Text.ParagraphLayout.Internal.ParagraphOptions,
        Data.Text.ParagraphLayout.Internal.Plain,
        Data.Text.ParagraphLayout.Internal.Plain.Paragraph,


@@ 139,7 140,6 @@ library balkon-internal
        Data.Text.ParagraphLayout.Internal.Layout,
        Data.Text.ParagraphLayout.Internal.LineNumbers,
        Data.Text.ParagraphLayout.Internal.ParagraphExtents,
        Data.Text.ParagraphLayout.Internal.ParagraphLine,
        Data.Text.ParagraphLayout.Internal.ProtoFragment,
        Data.Text.ParagraphLayout.Internal.ProtoLine,
        Data.Text.ParagraphLayout.Internal.ProtoRun,

M test/Data/Text/ParagraphLayout/RichSpec.hs => test/Data/Text/ParagraphLayout/RichSpec.hs +77 -15
@@ 9,6 9,7 @@ import System.FilePath ((</>))
import Data.Text.ParagraphLayout
import Data.Text.ParagraphLayout.FontLoader
import Data.Text.ParagraphLayout.Internal.Paginable (paginateAll)
import Data.Text.ParagraphLayout.Internal.ParagraphLine (forceLeftAlign)
import Data.Text.ParagraphLayout.Internal.Rich.ParagraphLayout (shapedRuns)
import Data.Text.ParagraphLayout.PrettyShow
import Data.Text.ParagraphLayout.PrettyShow.Golden


@@ 28,6 29,20 @@ glyphRuns = map (map (codepoint . fst) . fragmentGlyphs) . paragraphFragments
largeWidth :: Int32
largeWidth = 1000000000

-- | Expect two paragraph layouts to contain the same fragments,
-- except possibly shifted left or right.
shouldBeWrappedLike :: (Show d, Eq d) =>
    ParagraphLayout d -> ParagraphLayout d -> Expectation
shouldBeWrappedLike a b =
    forceLeftAlign a `shouldBe` forceLeftAlign b

-- | Expect two paragraph layouts to not contain the same fragments,
-- even if ignoring a possible shift to the left or to the right.
shouldNotBeWrappedLike :: (Show d, Eq d) =>
    ParagraphLayout d -> ParagraphLayout d -> Expectation
shouldNotBeWrappedLike a b =
    forceLeftAlign a `shouldNotBe` forceLeftAlign b

spec :: Spec
spec = do



@@ 40,32 55,79 @@ spec = do
            font <- runIO $ loadFont latinFont 0 testingOptions
            fontSmall <- runIO $ loadFont latinFont 0 testingOptionsSmall

            it "wraps lorem ipsum at 20em, left aligned" $ do
            let commonWrapWidth = 20000
            let expectedSafeWidth = 19791

            describe "lorem ipsum at 20em, left aligned" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignLeft
                        , paragraphMaxWidth = 20000
                        }
                let input = loremIpsumParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "loremIpsum20em"
                let input w = loremIpsumParagraph font $
                        opts { paragraphMaxWidth = w }
                let result20em = layoutRich $ input commonWrapWidth
                let resultSafe = layoutRich $ input expectedSafeWidth
                let resultUnsafe = layoutRich $ input (expectedSafeWidth - 1)

                it "wraps correctly" $
                    result20em `shouldBeGolden` "loremIpsum20em"

            it "wraps lorem ipsum at 20em, right aligned" $ do
                it "gives correct safe width" $
                    paragraphSafeWidth result20em `shouldBe` expectedSafeWidth

                it "wraps the same at safe width" $
                    resultSafe `shouldBeWrappedLike` result20em

                it "does not wrap the same at smaller width" $
                    resultUnsafe `shouldNotBeWrappedLike` result20em

            describe "lorem ipsum at 20em, right aligned" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignRight
                        , paragraphMaxWidth = 20000
                        }
                let input = loremIpsumParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "loremIpsum20emRight"
                let input w = loremIpsumParagraph font $
                        opts { paragraphMaxWidth = w }
                let result20em = layoutRich $ input commonWrapWidth
                let resultSafe = layoutRich $ input expectedSafeWidth
                let resultUnsafe = layoutRich $ input (expectedSafeWidth - 1)

            it "wraps lorem ipsum at 20em, centred" $ do
                it "wraps correctly" $
                    result20em `shouldBeGolden` "loremIpsum20emRight"

                it "gives correct safe width" $
                    paragraphSafeWidth result20em `shouldBe` expectedSafeWidth

                it "wraps the same at safe width" $
                    resultSafe `shouldBeWrappedLike` result20em

                it "does not wrap the same at smaller width" $
                    resultUnsafe `shouldNotBeWrappedLike` result20em

            describe "lorem ipsum at 20em, centred" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignCentreH
                        , paragraphMaxWidth = 20000
                        }
                let input = loremIpsumParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "loremIpsum20emCentre"
                let input w = loremIpsumParagraph font $
                        opts { paragraphMaxWidth = w }
                let result20em = layoutRich $ input commonWrapWidth
                let resultSafe = layoutRich $ input expectedSafeWidth
                let resultUnsafe = layoutRich $ input (expectedSafeWidth - 1)
                let resultUnsafe2 = layoutRich $ input (expectedSafeWidth - 2)

                it "wraps correctly" $
                    result20em `shouldBeGolden` "loremIpsum20emCentre"

                it "gives correct safe width" $
                    paragraphSafeWidth result20em `shouldBe` expectedSafeWidth

                it "wraps the same at safe width" $
                    resultSafe `shouldBeWrappedLike` result20em

                it "does not wrap the same at smaller width" $
                    resultUnsafe `shouldNotBeWrappedLike` result20em

                -- Cover for false negatives when halving odd numbers.
                it "does not wrap the same at 2 units smaller width" $
                    resultUnsafe2 `shouldNotBeWrappedLike` result20em

            it "handles mixed line height" $ do
                let opts = defaultParagraphOptions