File: Main.hs

package info (click to toggle)
haskell-text-builder 1.0.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 124 kB
  • sloc: haskell: 622; makefile: 3
file content (153 lines) | stat: -rw-r--r-- 6,500 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module Main where

import qualified Data.ByteString as ByteString
import Data.Char (intToDigit)
import Data.Int
import Data.Proxy
import Data.String
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Word
import Numeric
import Numeric.Natural (Natural)
import Test.QuickCheck.Classes
import Test.QuickCheck.Instances ()
import Test.Tasty
import Test.Tasty.QuickCheck
import TextBuilder
import Util.ExtraInstances ()
import Util.TestTrees
import Prelude

main :: IO ()
main = (defaultMain . testGroup "All") tests

tests :: [TestTree]
tests =
  [ testGroup "Instances" $
      [ followsLaws $ showLaws (Proxy @TextBuilder),
        followsLaws $ eqLaws (Proxy @TextBuilder),
        followsLaws $ semigroupLaws (Proxy @TextBuilder),
        followsLaws $ monoidLaws (Proxy @TextBuilder)
      ],
    testGroup "Functions" $
      [ testGroup "decimal" $
          [ testGroup "Int" $
              [ mapsToMonoid @Int decimal,
                testProperty "Encodes the same as show" $ \(x :: Int) ->
                  (fromString . show) x === toText (decimal x)
              ],
            testGroup "Int8" $
              [ mapsToMonoid @Int8 decimal,
                testProperty "Encodes the same as show" $ \(x :: Int8) ->
                  (fromString . show) x === toText (decimal x)
              ],
            testGroup "Word" $
              [ mapsToMonoid @Word decimal,
                testProperty "Encodes the same as show" $ \(x :: Word) ->
                  (fromString . show) x === toText (decimal x)
              ],
            testGroup "Word8" $
              [ mapsToMonoid @Word8 decimal,
                testProperty "Encodes the same as show" $ \(x :: Word8) ->
                  (fromString . show) x === toText (decimal x)
              ],
            testGroup "Integer" $
              [ mapsToMonoid @Integer decimal,
                testProperty "Encodes the same as show" $ \(x :: Integer) ->
                  (fromString . show) x === toText (decimal x)
              ],
            testGroup "Natural" $
              [ mapsToMonoid @Natural decimal,
                testProperty "Encodes the same as show" $ \(x :: Natural) ->
                  (fromString . show) x === toText (decimal x)
              ]
          ],
        testGroup "fixedLengthDecimal" $
          [ testGroup "Word" $
              [ mapsToMonoid @Word (fixedLengthDecimal 42)
              ],
            testGroup "Natural" $
              [ mapsToMonoid @Natural (fixedLengthDecimal 42),
                testProperty "Encodes the same as printf" $ \(size :: Word8, val :: Natural) ->
                  let rendered = show val
                      renderedLength = length rendered
                      intSize = fromIntegral size
                      padded =
                        if renderedLength > intSize
                          then drop (renderedLength - intSize) rendered
                          else replicate (intSize - renderedLength) '0' <> rendered
                   in fromString padded
                        === toText (fixedLengthDecimal (fromIntegral size) val)
              ]
          ],
        testGroup "thousandSeparatedDecimal" $
          [ testGroup "Int" $
              [ mapsToMonoid @Int (thousandSeparatedDecimal ','),
                testProperty "Encodes the same as show" $ \(x :: Int) ->
                  (fromString . show) x === Text.filter (/= ',') (toText (thousandSeparatedDecimal ',' x))
              ],
            testGroup "Int8" $
              [ mapsToMonoid @Int8 (thousandSeparatedDecimal ','),
                testProperty "Encodes the same as show" $ \(x :: Int8) ->
                  (fromString . show) x === Text.filter (/= ',') (toText (thousandSeparatedDecimal ',' x))
              ],
            testGroup "Integer" $
              [ mapsToMonoid @Integer (thousandSeparatedDecimal ','),
                testProperty "Encodes the same as show" $ \(x :: Integer) ->
                  (fromString . show) x === Text.filter (/= ',') (toText (thousandSeparatedDecimal ',' x))
              ]
          ],
        testGroup "binary" $
          [ testGroup "Int" $
              [ mapsToMonoid @Int binary,
                testProperty "Encodes the same as showIntAtBase" $ \(x :: Word32) ->
                  (Text.justifyRight 32 '0' . Text.pack . showIntAtBase 2 intToDigit x) "" === toText (binary x)
              ]
          ],
        testGroup "octal" $
          [ testGroup "Int" $
              [ mapsToMonoid @Int octal,
                testProperty "Encodes the same as showIntAtBase" $ \(x :: Word32) ->
                  (Text.justifyRight 11 '0' . Text.pack . showIntAtBase 8 intToDigit x) "" === toText (octal x)
              ]
          ],
        testGroup "hexadecimal" $
          [ testGroup "Int" $
              [ mapsToMonoid @Int hexadecimal,
                testProperty "Encodes the same as showHex" $ \(x :: Word32) ->
                  (Text.justifyRight 8 '0' . Text.pack . showHex x) "" === toText (hexadecimal x)
              ]
          ],
        testGroup "unsafeUtf8ByteString" $
          [ mapsToMonoid (unsafeUtf8ByteString . Text.encodeUtf8),
            testProperty "Works on ASCII" $
              let gen = listOf do
                    list <- listOf (choose (0, 127))
                    return (ByteString.pack list)
               in forAll gen \chunks ->
                    mconcat chunks
                      === Text.encodeUtf8 (toText (foldMap unsafeUtf8ByteString chunks))
          ],
        testGroup "intercalate" $
          [ customGenMonoid do
              sep <- arbitrary
              texts <- listOf arbitrary
              return (intercalate (text sep) (fmap text texts)),
            testProperty "Has the same effect as in Text" $
              \separator texts ->
                Text.intercalate separator texts
                  === toText (intercalate (text separator) (fmap text texts))
          ],
        testGroup "intercalateMap" $
          [ customGenMonoid do
              sep <- arbitrary
              texts <- listOf arbitrary
              return (intercalateMap (text sep) text texts),
            testProperty "intercalateMap sep mapper == intercalate sep . fmap mapper" $
              \separator ints ->
                Text.intercalate separator (fmap (fromString . show @Int) ints)
                  === toText (intercalateMap (text separator) decimal ints)
          ]
      ]
  ]