aboutsummaryrefslogtreecommitdiff
path: root/server/test/Thermoprint/Server/Printer/GenericSpec.hs
blob: e5357545d65c778564b4963402fff2eb54eafd56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE OverloadedStrings, OverloadedLists #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ViewPatterns #-}

module Thermoprint.Server.Printer.GenericSpec (spec) where

import Test.Hspec
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (Property, Discard(..), property)
import Test.QuickCheck.Instances

import Thermoprint.Server.Printer.Generic
import Thermoprint.Printout

import Data.Text (Text)
import qualified Data.Text.Lazy as TL (pack)

import qualified Data.ByteString.Lazy as L (ByteString)
import qualified Data.ByteString.Lazy as L.BS

import Data.String (IsString(..))

import Control.Monad (zipWithM_, join)
import Data.Monoid ((<>))
import Data.Function (on)

import Data.Word

import Data.Sequence (Seq)
import qualified Data.Sequence as Seq

instance IsString Line where
  fromString = (\(Right l) -> l) . text . TL.pack

spec :: Spec
spec = do
  describe "example printouts" $
    zipWithM_ example [1..] examples
  where
    example n (s, ts) = let str = show s
                        in specify str $ (mkPrintout . Printout . fmap (Paragraph . Seq.singleton) . fmap Cooked $ s) == ts

final, initial :: L.ByteString
initial  = esc [64] <> esc [64]
final = "\n\n\n \n"

esc :: [Word8] -> L.ByteString
esc = L.BS.pack . (27:)

p :: L.ByteString -> L.ByteString
p = (<> final) . (initial <>)

examples :: [(Seq Block, L.ByteString)]
examples = [ ( [Line (JuxtaPos ["Hello", HSpace 1, "World!"])]
             , p "Hello World!")
           , ( [Line (JuxtaPos ["Hello", HSpace 4, "World!"])]
             , p "Hello    World!")
           , ( [Line ("Par1"), Line ("Par2"), Line (JuxtaPos ["Par3", HSpace 1, "Word2"])]
             , p "Par1\n\nPar2\n\nPar3 Word2")
           , ( [Line (JuxtaPos ["Par1", HSpace 4]), NewlSep [VSpace 2, Line ("Par2")], Line (JuxtaPos ["Par3", HSpace 1, "Word2"])]
             , p "Par1    \n\n\n\n\nPar2\n\nPar3 Word2")
           , ( [Line (JuxtaPos [Markup [Bold] (JuxtaPos ["B",HSpace 1,Markup [Underline] "BM"]),HSpace 1,Markup [Bold,Underline] "BM"])]
             , p $ esc [33, 8] <> "B " <> esc [33, 128 + 8] <> "BM" <> esc [33, 8] <> esc [33, 0] <> " " <> esc [33, 128 + 8] <> "BM" <> esc [33, 0])
           ]