Adding a failing test for the gap handling discussed in #29
[darcs-mirror-arbtt.git] / tests / test.hs
1 {-# LANGUAGE DeriveDataTypeable, OverloadedStrings #-}
2
3 import Test.Tasty hiding (defaultMain)
4 import Test.Tasty.Golden.Manage
5 import Test.Tasty.Golden
6 import Test.Tasty.HUnit
7 import System.Process.ByteString.Lazy
8 import qualified Data.ByteString.Lazy as B
9 import Control.Monad
10 import Control.Exception
11 import Data.Typeable
12 import System.Exit
13 import System.Posix.Env
14
15 import Categorize
16 import TimeLog
17 import Data
18 import Data.Time.Clock
19
20 main = do
21     putEnv "TZ=UTC" -- to make tests reproducible
22     defaultMain tests
23
24 tests :: TestTree
25 tests = testGroup "Tests" [goldenTests, regressionTests]
26
27 regressionTests :: TestTree
28 regressionTests = testGroup "Regression tests"
29     [ testCase "Issue #4" $ do
30         cat <- readCategorizer "tests/issue4.cfg"
31         let sample = TimeLogEntry undefined 0 (CaptureData [(True, "aa", "program")] 0 "")
32         let [TimeLogEntry _ _ (_,acts)] = cat [sample]
33         [Activity (Just "Cat") "aa"] @=? acts
34         return ()
35     , testCase "Issue #5" $ do
36         cat <- readCategorizer "tests/issue5.cfg"
37         let sample = TimeLogEntry undefined 0 (CaptureData [(True, "aa", "program")] 0 "")
38         let [TimeLogEntry _ _ (_,acts)] = cat [sample]
39         [Activity Nothing "A2"] @=? acts
40         return ()
41     , testCase "Issue #14" $ do
42         cat <- readCategorizer "tests/issue14.cfg"
43         now <- getCurrentTime
44         let backThen = (-60*60*101) `addUTCTime` now
45
46         let sample = TimeLogEntry backThen 0 (CaptureData [(True, "aa", "program")] 0 "")
47         let [TimeLogEntry _ _ (_,acts)] = cat [sample]
48         [Activity Nothing "old"] @=? acts
49         return ()
50     ]
51
52
53 goldenTests :: TestTree
54 goldenTests = testGroup "Golden tests"
55     [ goldenVsString "dump small"
56         "tests/small_dump.out" $
57         run "dist/build/arbtt-dump/arbtt-dump" ["-f","tests/small.log", "-t", "Show"] B.empty
58     , goldenVsFile "import small"
59         "tests/small_import.out" "tests/small_import.out.actual" $ void $
60         B.readFile "tests/small_import.in" >>=
61         run "dist/build/arbtt-import/arbtt-import" ["-f","tests/small_import.out.actual"]
62     , goldenVsFile "recover small"
63         "tests/small_borked_recover.out" "tests/small_borked_recover.out.actual" $ void $
64         run "dist/build/arbtt-recover/arbtt-recover" ["-i","tests/small_borked_recover.out", "-o", "tests/small_borked_recover.out.actual"] B.empty
65     , goldenVsString "stats small"
66         "tests/small_stats.out" $
67         run "dist/build/arbtt-stats/arbtt-stats" ["--logfile", "tests/small.log", "--categorize", "tests/small.cfg"] B.empty
68     , goldenVsString "stats small csv"
69         "tests/small_stats_csv.out" $
70         run "dist/build/arbtt-stats/arbtt-stats" ["--logfile", "tests/small.log", "--categorize", "tests/small.cfg", "--output-format", "csv"] B.empty
71     , goldenVsString "stats small unicode"
72         "tests/unicode_stats.out" $
73         run "dist/build/arbtt-stats/arbtt-stats" ["--logfile", "tests/unicode.log", "--categorize", "tests/unicode.cfg"] B.empty
74     , goldenVsString "stats gap handling"
75         "tests/gap-handling.out" $
76         run "dist/build/arbtt-stats/arbtt-stats" ["--logfile", "tests/gap-handling.log", "--categorize", "tests/gap-handling.cfg", "--intervals", "Program:"] B.empty
77     ]
78
79
80 run :: FilePath -> [FilePath] -> B.ByteString -> IO B.ByteString
81 run cmd args stdin = do
82    (ex,stdout,stderr) <- readProcessWithExitCode cmd args stdin
83    unless (B.null stderr) $ throwIO $ StderrException stderr
84    case ex of
85      ExitSuccess   -> return stdout
86      ExitFailure r -> throwIO $ ExitCodeException r
87
88 data StderrException = StderrException B.ByteString
89      deriving (Show, Typeable)
90 data ExitCodeException = ExitCodeException Int
91      deriving (Show, Typeable)
92
93 instance Exception StderrException
94 instance Exception ExitCodeException