PageRenderTime 28ms CodeModel.GetById 8ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/PortletReviewer/Test/ValidationTest.hs

http://hdbc.googlecode.com/
Haskell | 79 lines | 69 code | 10 blank | 0 comment | 1 complexity | e417e66e187665a8e3ae8846bbc84e1a MD5 | raw file
 1
 2module Test.ValidationTest where
 3
 4import Validation
 5import Test.HUnit
 6import Text.Regex
 7
 8allTests :: Test
 9allTests = TestList [TestLabel "testValidateLine" testValidateLine,
10                     TestLabel "testValidateAnd" testValidateAnd,
11                     TestLabel "testAddValidationError" testAddValidationError,
12                     TestLabel "testAddValidationError2" testAddValidationError2,
13                    TestLabel "testValidateAtLeastOneLineMatches" testValidateAtLeastOneLineMatches,
14                    TestLabel "testValidateAtLeastOneLineMatches2" testValidateAtLeastOneLineMatches2]
15
16testValidateLine :: Test
17testValidateLine = let Vldtion c e v = validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
18         in TestCase (do assertBool "expecting no match for bb" (not v)
19                         assertEqual "expecting c = 1" 1 c)
20
21testValidateAnd :: Test
22testValidateAnd = let Vldtion c e v = do aa <- validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
23                                         bb <- validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
24                                         cc <- validateAnd aa bb
25                                         dd <- validateLine (mkRegex "aa") (mkRegex "bb") "aaabba"
26                                         validateAnd cc dd
27         in TestCase (do assertBool "expecting overall false result" (not v)
28                         assertEqual "expecting c = 3" 3 c
29                         assertEqual "expecting no errors" 0 (length e)
30                         )
31
32testAddValidationError :: Test
33testAddValidationError = let Vldtion c e v = do aa <- validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
34                                                addValidationError "this is an error\n" "aaaaa" False
35                                                bb <- validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
36                                                cc <- validateAnd aa bb
37                                                dd <- validateLine (mkRegex "aa") (mkRegex "bb") "aaabba"
38                                                validateAnd cc dd
39         in TestCase (do assertBool "expecting overall false result" (not v)
40                         assertEqual "expecting c = 3" 3 c
41                         assertEqual "expecting one error" 1 (length e)
42                         assertEqual "expecting error listing"
43                                     "this is an error\nline is: aaaaa\n" (head e)
44                         )
45
46testAddValidationError2 :: Test
47testAddValidationError2 = let Vldtion c e v = do aa <- validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
48                                                 addValidationError "this is error 1\n" "aaaaaa" aa
49                                                 bb <- validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
50                                                 addValidationError "this is error 2\n" "aaaaaa" bb
51                                                 cc <- validateAnd aa bb
52                                                 dd <- validateLine (mkRegex "aa") (mkRegex "bb") "aaaaaa"
53                                                 addValidationError "this is error 3\n" "aaaaaa" aa
54                                                 validateAnd cc dd
55         in TestCase (do assertBool "expecting overall false result" (not v)
56                         assertEqual "expecting c = 3" 3 c
57                         assertEqual "expecting three errors" 3 (length e)
58                         assertEqual "expecting line number indicator in error msg"
59                                     "this is error 1\nline is: aaaaaa\n" (e!!0)
60                         assertEqual "expecting line number indicator in error msg"
61                                     "this is error 2\nline is: aaaaaa\n" (e!!1)
62                         assertEqual "expecting line number indicator in error msg"
63                                     "this is error 3\nline is: aaaaaa\n" (e!!2)
64                         )
65
66testValidateAtLeastOneLineMatches :: Test
67testValidateAtLeastOneLineMatches = 
68        let Vldtion c e v = validateAtLeastOneLineMatches (mkRegex "a", mkRegex "a", "yo") []
69        in TestCase (do 
70           assertEqual "expecting false result" False v
71           assertEqual "expecting one error" 1 (length e))
72
73testValidateAtLeastOneLineMatches2 :: Test
74testValidateAtLeastOneLineMatches2 =
75        let lines = ["line1", "line2", "bonus", "line3"]
76            Vldtion c e v = validateAtLeastOneLineMatches (mkRegex "bonus", mkRegex "bonus", "yo") lines
77        in TestCase (do assertEqual "expecting to find a match" True v
78                        assertEqual "expecting empty error list" [] e)
79