From 0f5dfa9a2b6865dc46ef926983c64b2ab11fa3d1 Mon Sep 17 00:00:00 2001 From: Jaro Date: Tue, 21 Feb 2023 22:01:12 +0100 Subject: [PATCH] Use infix operators for constructing test data. --- balkon.cabal | 1 + .../ParagraphLayout/ParagraphConstruction.hs | 64 +++++++++++++++++++ .../Text/ParagraphLayout/ParagraphData.hs | 51 ++------------- 3 files changed, 72 insertions(+), 44 deletions(-) create mode 100644 test/Data/Text/ParagraphLayout/ParagraphConstruction.hs diff --git a/balkon.cabal b/balkon.cabal index c8e1d89..2475a4a 100644 --- a/balkon.cabal +++ b/balkon.cabal @@ -130,6 +130,7 @@ test-suite balkon-test other-modules: Data.Text.ParagraphLayoutSpec, Data.Text.ParagraphLayout.FontLoader, + Data.Text.ParagraphLayout.ParagraphConstruction, Data.Text.ParagraphLayout.ParagraphData, Data.Text.ParagraphLayout.PlainSpec, Data.Text.ParagraphLayout.RectSpec, diff --git a/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs b/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs new file mode 100644 index 0000000..a9af880 --- /dev/null +++ b/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs @@ -0,0 +1,64 @@ +-- | Infix operators for readable construction of paragraphs as testing input. +-- +-- Example construction: +-- @"ignored prefix" |< "en"~"one two " >|< "ja"~"三四" >| "ignored suffix"@ +-- +-- Special syntax for paragraphs with no contents: +-- @"ignored prefix" |<>| "ignored suffix"@ +-- +-- Please note that this form of construction is inefficient for longer text. +module Data.Text.ParagraphLayout.ParagraphConstruction + ((>|) + ,(>|<) + ,(|<) + ,(|<>|) + ,(~) + ) +where + +import Data.Text (append, pack) +import Data.Text.Foreign (lengthWord8) +import Data.Text.Internal (Text(Text)) +import Data.Text.ParagraphLayout.Plain + (Paragraph(Paragraph) + ,ParagraphOptions() + ,Span(Span) + ) + +-- | Create first span with optional ignored suffix. +infixr 5 >| +(>|) :: (String, String) -> String -> (Text, [Span]) +(spanText, spanLanguage) >| ignoredSuffix = (newText, newSpans) + where + newSpans = [Span (fromIntegral $ lengthWord8 packedSpanText) spanLanguage] + newText = append packedSpanText packedSuffix + packedSpanText = pack spanText + packedSuffix = pack ignoredSuffix + +-- Create next span. +infixr 5 >|< +(>|<) :: (String, String) -> (Text, [Span]) -> (Text, [Span]) +(spanText, spanLanguage) >|< (oldText, oldSpans) = (newText, newSpans) + where + newSpans = Span (fromIntegral $ lengthWord8 packedText) spanLanguage : oldSpans + newText = append packedText oldText + packedText = pack spanText + +-- Add optional ignored prefix and wrap in a `Paragraph`. +infixr 5 |< +(|<) :: String -> (Text, [Span]) -> ParagraphOptions -> Paragraph +ignoredPrefix |< (oldText, spans) = Paragraph arr (fromIntegral off) spans + where + (Text arr offPrefix _) = append packedPrefix oldText + off = offPrefix + (lengthWord8 packedPrefix) + packedPrefix = pack ignoredPrefix + +-- Create a `Paragraph` with no spans, just two ignored texts. +infixr 5 |<>| +(|<>|) :: String -> String -> ParagraphOptions -> Paragraph +ignoredPrefix |<>| ignoredSuffix = ignoredPrefix |< (pack ignoredSuffix, []) + +-- Combine language with text. +infix 6 ~ +(~) :: String -> String -> (String, String) +lang ~ txt = (txt, lang) diff --git a/test/Data/Text/ParagraphLayout/ParagraphData.hs b/test/Data/Text/ParagraphLayout/ParagraphData.hs index 7b7d0a8..672149b 100644 --- a/test/Data/Text/ParagraphLayout/ParagraphData.hs +++ b/test/Data/Text/ParagraphLayout/ParagraphData.hs @@ -7,57 +7,20 @@ module Data.Text.ParagraphLayout.ParagraphData ) where -import Data.Text (pack) -import Data.Text.Internal (Text(Text)) -import Data.Text.ParagraphLayout.Plain - (Paragraph(Paragraph) - ,ParagraphOptions - ,Span(Span) - ) +import Data.Text.ParagraphLayout.ParagraphConstruction +import Data.Text.ParagraphLayout.Plain (Paragraph, ParagraphOptions) emptyParagraph :: ParagraphOptions -> Paragraph -emptyParagraph opts = - let (Text arr off _) = pack "" - in Paragraph - arr - (fromIntegral off) - [] - opts +emptyParagraph = "" |<>| "" emptySpanParagraph :: ParagraphOptions -> Paragraph -emptySpanParagraph opts = - let (Text arr off _) = pack "" - in Paragraph - arr - (fromIntegral off) - [Span 0 "en"] - opts +emptySpanParagraph = "" |< "en"~"" >| "" czechHelloParagraph :: ParagraphOptions -> Paragraph -czechHelloParagraph opts = - let (Text arr off len) = pack "Ahoj, světe!" - in Paragraph - arr - (fromIntegral off) - [Span (fromIntegral len) "cs"] - opts +czechHelloParagraph = "" |< "cs"~"Ahoj, světe!" >| "" mixedScriptSerbianParagraph :: ParagraphOptions -> Paragraph -mixedScriptSerbianParagraph opts = - let (Text arr off len) = pack "Vikipedija (Википедија)" - in Paragraph - arr - (fromIntegral off) - [Span (fromIntegral len) "sr"] - opts +mixedScriptSerbianParagraph = "" |< "sr"~"Vikipedija (Википедија)" >| "" mixedLanguageLTRParagraph :: ParagraphOptions -> Paragraph -mixedLanguageLTRParagraph opts = - let (Text arr off _) = pack "Tak jsem tady, 世界!" - in Paragraph - arr - (fromIntegral off + 4) - [Span 11 "cs" -- this will contain the text "jsem tady, " - ,Span 7 "ja" -- this will contain the text "世界!" - ] - opts +mixedLanguageLTRParagraph = "Tak " |< "cs"~"jsem tady, " >|< "ja"~"世界!" >| "" -- 2.30.2