Skip to content

Commit 95db9f9

Browse files
committed
Test RowGroup
1 parent a3edab0 commit 95db9f9

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/Text/Layout/Table/Spec/RowGroup.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ data ColumnSegment a
4040
= SingleValueSegment a
4141
| ColumnSegment (Col a)
4242
| NullableColumnSegment (Col (Maybe a))
43-
deriving (Functor, Foldable, Show)
43+
deriving (Functor, Foldable, Eq, Show)
4444

45-
newtype SegmentedColumn a = SegmentedColumn [ColumnSegment a] deriving (Functor, Foldable, Show)
45+
newtype SegmentedColumn a = SegmentedColumn [ColumnSegment a] deriving (Functor, Foldable, Eq, Show)
4646

4747
-- | Break down several 'RowGroups', which conceptually form a column by
4848
-- themselves, into a list of columns.

test-suite/TestSpec.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Text.Layout.Table.Spec.AlignSpec
2020
import Text.Layout.Table.Spec.CutMark
2121
import Text.Layout.Table.Spec.OccSpec
2222
import Text.Layout.Table.Spec.Position
23+
import Text.Layout.Table.Spec.RowGroup
2324
import Text.Layout.Table.Primitives.Basic
2425
import Text.Layout.Table.Primitives.AlignInfo
2526
import Text.Layout.Table.Justify
@@ -331,6 +332,35 @@ spec = do
331332
prop "gives the same result as wide string" $ \(Small n) x -> buildCell (dropLeft n . WideText $ T.pack x) `shouldBe` (buildCell . dropLeft n $ WideString x :: String)
332333
describe "dropRight" $ do
333334
prop "gives the same result as wide string" $ \(Small n) x -> buildCell (dropRight n . WideText $ T.pack x) `shouldBe` (buildCell . dropRight n $ WideString x :: String)
335+
336+
describe "row groups" $ do
337+
describe "rowGroupShape" $ do
338+
it "multi" $ rowGroupShape (MultiRowGroup [[0, 1], [2, 3]]) `shouldBe` [(), ()]
339+
it "multi only first row 1" $ rowGroupShape (MultiRowGroup [[0, 1], [2, 3, 4]]) `shouldBe` [(), ()]
340+
it "multi only first row 2" $ rowGroupShape (MultiRowGroup [[0, 1], []]) `shouldBe` [(), ()]
341+
it "multi empty" $ rowGroupShape (MultiRowGroup []) `shouldBe` []
342+
it "singleton empty" $ rowGroupShape (SingletonRowGroup []) `shouldBe` []
343+
it "singleton" $ rowGroupShape (SingletonRowGroup [1, 2]) `shouldBe` [(), ()]
344+
it "nullable empty" $ rowGroupShape (NullableRowGroup []) `shouldBe` []
345+
it "nullable one element" $ rowGroupShape (NullableRowGroup [[Just 4]]) `shouldBe` [()]
346+
it "nullable but no null" $ rowGroupShape (NullableRowGroup [[Just 1, Just 2]]) `shouldBe` [(), ()]
347+
it "nullable mixed" $ rowGroupShape (NullableRowGroup [[Just 1, Nothing, Nothing]]) `shouldBe` [(), (), ()]
348+
349+
let rgs = [rg1, rg2, rg3] :: [RowGroup Int]
350+
rg1 = MultiRowGroup [[0, 1, 2], [3, 4, 5]]
351+
rg2 = SingletonRowGroup [6, 7, 8]
352+
rg3 = NullableRowGroup [[Nothing, Just 9, Nothing]]
353+
it "transposeRowGroups" $
354+
transposeRowGroups rgs `shouldBe` [ SegmentedColumn [ColumnSegment [0, 3], SingleValueSegment 6, NullableColumnSegment [Nothing]]
355+
, SegmentedColumn [ColumnSegment [1, 4], SingleValueSegment 7, NullableColumnSegment [Just 9]]
356+
, SegmentedColumn [ColumnSegment [2, 5], SingleValueSegment 8, NullableColumnSegment [Nothing]]
357+
]
358+
describe "mapRowGroupColumns" $ do
359+
let mappers = [(negate 1, (+ 1)), (0, (* 2)), (negate 2, (`div` 2))]
360+
it "multi" $ mapRowGroupColumns mappers rg1 `shouldBe` [[1, 2, 1], [4, 8, 2]]
361+
it "singleton" $ mapRowGroupColumns mappers rg2 `shouldBe` [[7, 14, 4]]
362+
it "nullable" $ mapRowGroupColumns mappers rg3 `shouldBe` [[negate 1, 18, negate 2]]
363+
334364
where
335365
customCM = doubleCutMark "<.." "..>"
336366
unevenCM = doubleCutMark "<" "-->"

0 commit comments

Comments
 (0)