File size: 9,675 Bytes
a10b189 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | {-|
Module : Test.Verify.ErrorReportTests
Description : Tests for error categorization and formatting
Copyright : (c) 2026 Ahmad Ali Parr
License : BSL-1.0
Tests covering:
- Error categorization from messages
- Error formatting and display
- Source location handling
- Fix suggestion generation
- Context extraction
-}
module Test.Verify.ErrorReportTests (tests) where
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase, (@?=), Assertion)
import Assertica.Verify.ErrorReport
( categorizeError
, formatError
, formatErrorLocation
, suggestFix
, suggestParseError
, suggestTypeError
, suggestProofError
, suggestModuleError
, suggestTerminationError
, suggestPositivityError
, buildContext
)
import Assertica.Verify.Report
( ErrorDetail (..)
, ErrorCategory (..)
, SourceLocation (..)
)
-- ============================================================================
-- ERROR CATEGORIZATION TESTS
-- ============================================================================
tests :: TestTree
tests = testGroup "Assertica.Verify.ErrorReport"
[ testGroup "Error Categorization"
[ testCase "parse error message categorized as SyntaxError" $
testCategorizeParseError
, testCase "type error message categorized as TypeError" $
testCategorizeTypeError
, testCase "proof error categorized as ProofObligation" $
testCategorizeProofError
, testCase "circular dependency categorized as ModuleError" $
testCategorizeModuleError
, testCase "termination error categorized correctly" $
testCategorizeTerminationError
, testCase "positivity error categorized correctly" $
testCategorizePositivityError
, testCase "codegen error categorized correctly" $
testCategorizeCodeGenError
, testCase "unknown error defaults to UnknownError" $
testCategorizeUnknownError
]
, testGroup "Error Formatting"
[ testCase "format error includes category" $
testFormatErrorIncludesCategory
, testCase "format error includes message" $
testFormatErrorIncludesMessage
, testCase "format error includes location when present" $
testFormatErrorIncludesLocation
, testCase "format error deterministic" $
testFormatErrorDeterministic
]
, testGroup "Location Formatting"
[ testCase "location format includes file" $
testLocationFormatIncludesFile
, testCase "location format includes line" $
testLocationFormatIncludesLine
, testCase "location format includes column" $
testLocationFormatIncludesColumn
]
, testGroup "Fix Suggestions"
[ testCase "parse error suggests fix" $
testParseSuggestsFix
, testCase "type error suggests fix" $
testTypeSuggestsFix
, testCase "proof error suggests fix" $
testProofSuggestsFix
, testCase "module error suggests fix" $
testModuleSuggestsFix
, testCase "termination error suggests fix" $
testTerminationSuggestsFix
, testCase "positivity error suggests fix" $
testPositivitySuggestsFix
]
, testGroup "Context Extraction"
[ testCase "build context with valid location" $
testBuildContextValid
, testCase "build context with valid source" $
testBuildContextValidSource
]
, testGroup "Determinism"
[ testCase "same error produces same category" $
testCategorizeDeterministic
, testCase "same error produces same format" $
testFormatDeterministic
]
]
-- ============================================================================
-- ERROR CATEGORIZATION TESTS
-- ============================================================================
testCategorizeParseError :: Assertion
testCategorizeParseError =
categorizeError "parse error at token" Nothing @?= SyntaxError
testCategorizeTypeError :: Assertion
testCategorizeTypeError =
categorizeError "type mismatch: expected Int but got Bool" Nothing @?= TypeError
testCategorizeProofError :: Assertion
testCategorizeProofError =
categorizeError "proof obligation: assertion not proven" Nothing @?= ProofObligation
testCategorizeModuleError :: Assertion
testCategorizeModuleError =
categorizeError "circular dependency detected" Nothing @?= ModuleError
testCategorizeTerminationError :: Assertion
testCategorizeTerminationError =
categorizeError "termination: decreasing argument not found" Nothing @?= TerminationError
testCategorizePositivityError :: Assertion
testCategorizePositivityError =
categorizeError "positivity: type parameter in negative position" Nothing @?= PositivityError
testCategorizeCodeGenError :: Assertion
testCategorizeCodeGenError =
categorizeError "codegen failed: invalid Haskell output" Nothing @?= CodeGenError
testCategorizeUnknownError :: Assertion
testCategorizeUnknownError =
categorizeError "something went wrong" Nothing @?= UnknownError
-- ============================================================================
-- ERROR FORMATTING TESTS
-- ============================================================================
testFormatErrorIncludesCategory :: Assertion
testFormatErrorIncludesCategory =
let err = ErrorDetail SyntaxError "Test error" Nothing Nothing Nothing
formatted = formatError err
in ("SYNTAX" `elem` formatted) @?= True
testFormatErrorIncludesMessage :: Assertion
testFormatErrorIncludesMessage =
let err = ErrorDetail SyntaxError "Test error" Nothing Nothing Nothing
formatted = formatError err
in ("Test error" `elem` formatted) @?= True
testFormatErrorIncludesLocation :: Assertion
testFormatErrorIncludesLocation =
let loc = SourceLocation "test.as" 1 5
err = ErrorDetail SyntaxError "Test error" (Just loc) Nothing Nothing
formatted = formatError err
in ("test.as" `elem` formatted) @?= True
testFormatErrorDeterministic :: Assertion
testFormatErrorDeterministic =
let err = ErrorDetail SyntaxError "Test error" Nothing Nothing Nothing
formatted1 = formatError err
formatted2 = formatError err
in formatted1 @?= formatted2
-- ============================================================================
-- LOCATION FORMATTING TESTS
-- ============================================================================
testLocationFormatIncludesFile :: Assertion
testLocationFormatIncludesFile =
let loc = SourceLocation "test.as" 1 1
formatted = formatErrorLocation loc
in ("test.as" `elem` formatted) @?= True
testLocationFormatIncludesLine :: Assertion
testLocationFormatIncludesLine =
let loc = SourceLocation "test.as" 42 1
formatted = formatErrorLocation loc
in ("42" `elem` formatted) @?= True
testLocationFormatIncludesColumn :: Assertion
testLocationFormatIncludesColumn =
let loc = SourceLocation "test.as" 1 5
formatted = formatErrorLocation loc
in ("5" `elem` formatted) @?= True
-- ============================================================================
-- FIX SUGGESTION TESTS
-- ============================================================================
testParseSuggestsFix :: Assertion
testParseSuggestsFix =
suggestParseError "unexpected token" @?= Just "Check for missing operators, mismatched brackets, or incorrect syntax."
testTypeSuggestsFix :: Assertion
testTypeSuggestsFix =
case suggestTypeError "cannot unify Int with Bool" of
Just msg -> ("Add explicit type annotation" `elem` msg) @?= True
Nothing -> fail "Type error should suggest fix"
testProofSuggestsFix :: Assertion
testProofSuggestsFix =
case suggestProofError "missing proof" of
Just msg -> ("proof term" `elem` msg) @?= True
Nothing -> fail "Proof error should suggest fix"
testModuleSuggestsFix :: Assertion
testModuleSuggestsFix =
case suggestModuleError "circular dependency" of
Just msg -> ("circular" `elem` msg) @?= True
Nothing -> fail "Module error should suggest fix"
testTerminationSuggestsFix :: Assertion
testTerminationSuggestsFix =
case suggestTerminationError "decreasing argument" of
Just msg -> ("structural" `elem` msg) @?= True
Nothing -> fail "Termination error should suggest fix"
testPositivitySuggestsFix :: Assertion
testPositivitySuggestsFix =
case suggestPositivityError "negative occurrence" of
Just msg -> ("negative" `elem` msg) @?= True
Nothing -> fail "Positivity error should suggest fix"
-- ============================================================================
-- CONTEXT EXTRACTION TESTS
-- ============================================================================
testBuildContextValid :: Assertion
testBuildContextValid =
let source = "assert x = x\nassert y = y\nassert z = z"
loc = SourceLocation "test.as" 2 1
ctx = buildContext source loc 1
in length ctx > 0 @?= True
testBuildContextValidSource :: Assertion
testBuildContextValidSource =
let source = "line 1\nline 2\nline 3"
loc = SourceLocation "test.as" 2 1
ctx = buildContext source loc 1
in ("2" `elem` ctx) @?= True
-- ============================================================================
-- DETERMINISM TESTS
-- ============================================================================
testCategorizeDeterministic :: Assertion
testCategorizeDeterministic =
let msg = "parse error at position"
in categorizeError msg Nothing @?= categorizeError msg Nothing
testFormatDeterministic :: Assertion
testFormatDeterministic =
let err = ErrorDetail SyntaxError "Test" Nothing Nothing Nothing
in formatError err @?= formatError err
|