Commit 960f7342 authored by Camil Staps's avatar Camil Staps 🐧

Rework FailReason

parent f4fed6f0
Pipeline #9567 passed with stage
in 1 minute and 49 seconds
......@@ -48,8 +48,33 @@ from Text.JSON import generic JSONEncode, generic JSONDecode, :: JSONNode, :: Ma
* Reasons for failing a test.
*/
:: FailReason
= CounterExample JSONNode //* A counter-example for a property was found
| ExpectedButGot JSONNode JSONNode //* An equality test failed
= FailedAssertions [FailedAssertion] //* Assertions that caused the test to fail
| CounterExamples [CounterExample] //* Example values for which the test failed
derive JSONEncode TestEvent, StartEvent, EndEvent, FailReason
derive JSONDecode TestEvent, StartEvent, EndEvent, FailReason
/**
* A counter-example to a test.
*/
:: CounterExample =
{ counterExample :: !JSONNode //* The value that disproves the property
, failedAssertions :: ![FailedAssertion] //* The assertions that failed in testing the property for that value
}
/**
* A failed test assertion.
*/
:: FailedAssertion
= ExpectedRelation JSONNode Relation JSONNode //* A relation test failed
/**
* A relation between two values.
*/
:: Relation
= Eq //* Equality
| Ne //* Negated equality
| Lt //* Lesser than
| Le //* Lesser than or equal to
| Gt //* Greater than
| Ge //* Greater than or equal to
derive JSONEncode TestEvent, StartEvent, EndEvent, FailReason, CounterExample, FailedAssertion, Relation
derive JSONDecode TestEvent, StartEvent, EndEvent, FailReason, CounterExample, FailedAssertion, Relation
......@@ -77,5 +77,46 @@ where
_ -> Nothing
JSONDecode{|EndEventType|} _ nodes = (Nothing, nodes)
derive JSONEncode FailReason
derive JSONDecode FailReason
JSONEncode{|FailedAssertion|} _ fa = [JSONArray arr]
where
arr = case fa of
ExpectedRelation x r y ->
[ JSONString "expected"
, x
, hd (JSONEncode{|*|} False r)
, y
]
JSONDecode{|FailedAssertion|} _ [JSONArray arr:rest] = (mbFA, rest)
where
mbFA = case arr of
[JSONString "expected":x:r:y:[]] -> case JSONDecode{|*|} False [r] of
(Just r, []) -> Just (ExpectedRelation x r y)
_ -> Nothing
_ -> Nothing
JSONEncode{|Relation|} _ r = [JSONString s]
where
s = case r of
Eq -> "=="
Ne -> "<>"
Lt -> "<"
Le -> "<="
Gt -> ">"
Ge -> ">="
JSONDecode{|Relation|} _ [JSONString s:rest] = (mbRel, rest)
where
mbRel = case s of
"==" -> Just Eq
"<>" -> Just Ne
"<" -> Just Lt
"<=" -> Just Le
">" -> Just Gt
">=" -> Just Ge
_ -> Nothing
derive JSONEncode FailReason, CounterExample
derive JSONDecode FailReason, CounterExample
Start = toString (toJSON {name="", message="", event=Failed (CounterExamples [{counterExample=JSONNull, failedAssertions=[ExpectedRelation (JSONInt 1) Eq (JSONInt 2)]}])})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment