~jaro/balkon

ff1079899e99faf229e747c333f34b5be822877c — Jaro 1 year, 10 months ago e1e85a2
Add unit tests for spanToRuns.
2 files changed, 64 insertions(+), 7 deletions(-)

M balkon.cabal
M test/ParagraphLayoutTest.hs
M balkon.cabal => balkon.cabal +5 -5
@@ 52,13 52,13 @@ extra-doc-files:    CHANGELOG.md

library
    -- Modules exported by the library.
    exposed-modules:  Data.Text.ParagraphLayout
    exposed-modules:
        Data.Text.ParagraphLayout,
        Data.Text.ParagraphLayout.Run,
        Data.Text.ParagraphLayout.Span

    -- Modules included in this library but not exported.
    other-modules:
        Data.Text.ParagraphLayout.Run,
        Data.Text.ParagraphLayout.Span,
        Data.Text.Script
    other-modules:    Data.Text.Script

    -- LANGUAGE extensions used by modules in this package.
    -- other-extensions:

M test/ParagraphLayoutTest.hs => test/ParagraphLayoutTest.hs +59 -2
@@ 2,16 2,31 @@ module Main (main) where

import qualified Data.ByteString as BS
import Data.List (intersperse)
import Data.Text.Glyphize (Font, GlyphInfo, GlyphPos, createFace, createFont)
import Data.Text.Glyphize
    (Direction(..)
    ,Font
    ,GlyphInfo
    ,GlyphPos
    ,createFace
    ,createFont
    )
import Data.Text.Lazy (pack)

import Test.Hspec
import Test.Hspec.Golden
import System.FilePath ((</>))
import Data.Text.ParagraphLayout
import Data.Text.ParagraphLayout.Run

type LayoutOutput = [[(GlyphInfo,GlyphPos)]]

emptySpan :: Font -> Span
emptySpan font = Span
    { spanText = pack ""
    , spanFont = font
    , spanLanguage = Nothing
    }

czechHello :: Font -> Span
czechHello font = Span
    { spanText = pack "Ahoj, světe!"


@@ 19,6 34,13 @@ czechHello font = Span
    , spanLanguage = Just "cs"
    }

serbianMixedScript :: Font -> Span
serbianMixedScript font = Span
    { spanText = pack "Vikipedija (Википедија)"
    , spanFont = font
    , spanLanguage = Just "sr"
    }

prettyShow :: LayoutOutput -> String
prettyShow = showOutput
    where


@@ 47,8 69,43 @@ main = do
    let face = createFace ttf 0
    let font = createFont face
    hspec $ do
        describe "spanToRuns" $ do
            it "handles span with no text" $ do
                spanToRuns (emptySpan font) `shouldBe` []
            it "handles Czech hello" $ do
                let inputSpan = czechHello font
                let runs = spanToRuns inputSpan
                runs `shouldBe`
                    [ Run
                        { runText = spanText inputSpan
                        , runDirection = Just DirLTR
                        , runScript = Just "Latn"
                        , runOriginalSpan = inputSpan
                        }
                    ]
            it "handles Serbian with mixed script" $ do
                let inputSpan = serbianMixedScript font
                let runs = spanToRuns inputSpan
                runs `shouldBe`
                    [ Run
                        -- TODO: We might want both parentheses in the same run.
                        { runText = pack "Vikipedija ("
                        , runDirection = Just DirLTR
                        , runScript = Just "Latn"
                        , runOriginalSpan = inputSpan
                        }
                    , Run
                        { runText = pack "Википедија)"
                        , runDirection = Just DirLTR
                        , runScript = Just "Cyrl"
                        , runOriginalSpan = inputSpan
                        }
                    ]

        describe "layout" $ do
            it "handles empty input" $ do
            it "handles input with no spans" $ do
                layout [] `shouldBe` []
            it "handles one span with no text" $ do
                layout [emptySpan font] `shouldBe` []
            it "handles Czech hello" $ do
                layout [czechHello font] `shouldBeGolden` "czechHello"