/PortletReviewer/Test/ValidationTest.hs

http://hdbc.googlecode.com/ · Haskell · 79 lines · 69 code · 10 blank · 0 comment · 0 complexity · e417e66e187665a8e3ae8846bbc84e1a MD5 · raw file

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