packages feed

itemfield-1.1.0.1: test/test_itemfield.hs

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Test.HUnit
import Test.QuickCheck
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import qualified Data.Text as T
import Data.Monoid
import Data.Maybe
import Control.Monad
import TextUI.ItemField
import TextUI.ItemField.Operations
import TestDataGen


main = defaultMain
       [ c1 , c2 , c3 , c4 , c5
       , s1 , s2
       , o1 , o2 , o3 , o4 , o5 , o6 , o7
       ]


c1 = testProperty "count items" $ \(BoundedItems i t) -> t == cntItems i

c2 = testProperty "count new field items" $
     \(BoundedItems i t) -> t == (cntItems $ items $ newItemField i Nothing)

c3 = testProperty "count field items with state" $
     \(BoundedItems i t) s c -> t == (cntItems $ items $ ItemFld c i s Nothing)

c4 = testProperty "count field items with mixed states" $
     \(BoundedStateItems i s t) c ->
         length s == t && t == (cntItems $ items $ ItemFld c i s Nothing)

c5 = testProperty "count generated field items" $
     \i -> length (itemst8 i) == cntItems (items i)

s1 = testProperty "item field no description showable" $
     \(BoundedStateItems i s t) c ->
         show (ItemFld c i s Nothing) ++ "ok" /= "ok"

s2 = testProperty "item field with description showable"  $
       \(BoundedStateItems i s t) c ->
           let f = (Just $ const $ const $ T.pack "item")
           in show (ItemFld c i s f) ++ "ok" /= "ok"

o1 = testProperty "change single item state is reversable" $
       \i s -> let n = curSel i
                   i' = changeItemState s i n
                   o_s = itemst8 i !! n
                   i'' = changeItemState o_s i n
               in and [ itemst8 i'' == itemst8 i
                      , null (itemst8 i') || s `elem` itemst8 i'
                      , length (itemst8 i) == length (itemst8 i')
                      , length (itemst8 i') == length (itemst8 i'')
                      ]

o2 = testProperty "change multiple item state is reversable" $
   \i s -> let ns = filter (\x -> x `mod` 3 == 0) [0 .. length (itemst8 i) - 1]
               i' = foldl (changeItemState s) i ns
               o_s = itemst8 i
               i'' = foldl (\f p -> changeItemState (o_s !! p) f p) i' ns
           in and [ itemst8 i'' == itemst8 i
                  , null (itemst8 i') || s `elem` itemst8 i'
                  , length (itemst8 i) == length (itemst8 i')
                  , length (itemst8 i') == length (itemst8 i'')
                  ]

o3 = testProperty "change all item state is reversable" $
   \i s -> let ns = [0 .. length (itemst8 i) - 1]
               i' = foldl (changeItemState s) i ns
               o_s = itemst8 i
               i'' = foldl (\f p -> changeItemState (o_s !! p) f p) i' ns
           in and [ itemst8 i'' == itemst8 i
                  , null (itemst8 i') || s `elem` itemst8 i'
                  , length (itemst8 i) == length (itemst8 i')
                  , length (itemst8 i') == length (itemst8 i'')
                  ]

o4 = testProperty "retrieve marked" $
   \i -> (length $ filter ((==) Marked) $ itemst8 i) == (length $ getMarked i)


o5 = testCase "group ranges" $
     let itms = [ Items 3
                , ItemGroup "g1" $ Items 5
                , Items 0
                , ItemGroup "g2" $ ItemGroup "g3" $ ItemGroup "g4" $ Items 2
                , ItemGroup "g2" $ ItemGroup "g3" $ Items 4
                , Items 9
                , ItemGroup "empty" $ Items 0
                , Items 5
                ]
         expected = replicate 3 (0,2) <>
                    replicate 5 (3,7) <>
                    replicate 2 (8,9) <>
                    replicate 4 (10,13) <>
                    replicate 9 (14,22) <>
                    replicate 5 (23,27) <>
                    replicate 2 (28,28)  -- just final loc if beyond end.
     in map (flip groupRange itms) [0 .. 29] @?= expected

o6 = testProperty "empty group ranges" $ \n -> groupRange n [] == (0,0)

o7 = testProperty "setting all to free state" $
     \i -> replicate (cntItems $ items i) Free == (itemst8 $ setAllFree i)