Commits

Stefan Saasen committed d5cfbe1

Add HUnit tests

  • Participants
  • Parent commits 8344091

Comments (0)

Files changed (3)

 flag                small_base
   description:      Choose the new, split-up base package.
 
+test-suite spy-testsuite
+  type:                     exitcode-stdio-1.0
+  main-is:                  Tests.hs
+  hs-source-dirs:           tests, src
+  build-depends:            base < 5 && >= 3,
+                            test-framework >= 0.3.3,
+                            test-framework-quickcheck2 >= 0.2.9,
+                            test-framework-hunit,
+                            HUnit,
+                            QuickCheck >= 2.4.0.1,
+                            hfsevents >= 0.1.3,
+                            cmdargs >= 0.10,
+                            filepath >= 1.3,
+                            filemanip >= 0.3.6.2,
+                            process >= 1.1,
+                            json >= 0.7,
+                            directory >= 1.1
+
 executable  spy
   main-is:          Main.hs
   other-modules:    Spy.Watcher

File src/Spy/Watcher.hs

 ,Format
 ,plainFormat
 ,Spy(..)
+,skipEvent
+,matchesFile
+,containsHiddenPathElement
 ) where
 
 import System.OSX.FSEvents

File tests/Tests.hs

+{-# LANGUAGE OverloadedStrings #-}
+
+module Main (main) where
+
+import qualified Test.HUnit as H
+import Data.Maybe
+import System.OSX.FSEvents
+import Spy.Watcher
+import Test.QuickCheck hiding ((.&.))
+import Test.Framework (Test, defaultMain, testGroup)
+import Test.Framework.Providers.QuickCheck2 (testProperty)
+import Test.Framework.Providers.HUnit
+
+
+test_globMatchesFile = H.assertBool
+  "*.hs should match a Haskell source file"
+  (matchesFile "/path/to/Main.hs" "*.hs")
+
+test_globDoesNotMatchFile = H.assertBool
+  "*.hs should not match a C source file"
+  (not $ matchesFile "/path/to/Main.c" "*.hs")
+
+test_skipEventHidden = H.assertBool
+  "Skip path if hidden directory and showing hidden files is not enabled"
+  (skipEvent (mockWatch {hidden = False}) (mockEvent "/a/b/.git/refs"))
+
+test_containsHiddenPathElement = H.assertBool
+  "Should identify hidden directory"
+  (containsHiddenPathElement "/a/b/.git/info/exclude")
+
+-- ===========================================================
+mockWatch :: Spy
+mockWatch = Watch { dir = ".", glob = Nothing, format = Nothing, hidden = False }
+
+mockEvent :: FilePath -> Event
+mockEvent path = Event path 0x00000001 0x00000001
+------------------------------------------------------------------------
+-- Test harness
+
+main :: IO ()
+main = defaultMain tests
+
+tests :: [Test]
+tests =
+    [ testGroup "Watcher"
+      [
+        testCase "watcher/shouldMatchFilePath" test_globMatchesFile,
+        testCase "watcher/shouldNotMatchFilePath" test_globDoesNotMatchFile
+      ],
+      testGroup "Watcher opts"
+      [
+        testCase "watcher/optHidden" test_skipEventHidden,
+        testCase "watcher/containsHiddenPathElement" test_containsHiddenPathElement
+      ]
+    ]
+