Scoop API

scoop:scoop.test:0.1.0

src/failure.scoop

Selected production source · src/failure.scoop

1package scoop.test
3/**
4 * The outcome of rendering an assertion operand for a failure report.
5 */
6public enum AssertionValue {
7 /**
8 * The operand has no available textual representation.
9 */
10 Unavailable,
11 /**
12 * The operand's successfully rendered text.
13 */
14 Text(String),
15 /**
16 * Rendering itself threw an exception, retained here without replacing the assertion failure.
17 */
18 RenderingFailed(Throwable)
21/**
22 * Structured assertion failure information used by the test runner. Operand-rendering failures are
23 * retained in [AssertionValue] so they do not hide the original comparison failure.
24 */
25public class AssertionFailure public constructor(
26 /**
27 * The assertion operation that failed.
28 */
29 public val kind: String,
30 /**
31 * Additional caller-supplied context, when provided.
32 */
33 public val message: String?,
34 /**
35 * The source location attributed to the assertion failure.
36 */
37 public val location: SourceLocation,
38 /**
39 * The expected operand's rendering result.
40 */
41 public val expected: AssertionValue = AssertionValue.Unavailable,
42 /**
43 * The actual operand's rendering result.
44 */
45 public val actual: AssertionValue = AssertionValue.Unavailable,
46 /**
47 * An underlying exception, such as the unexpected exception from assertFailsWith.
48 */
49 public val cause: Throwable? = None
50) : Throwable()
52internal fun renderAssertionValue(value: Any): AssertionValue {
53 if (value is ToString) {
54 return try {
55 AssertionValue.Text(value.toString())
56 } catch (error: Throwable) {
57 AssertionValue.RenderingFailed(error)
58 }
59 }
60 return AssertionValue.Unavailable
63internal fun comparisonFailure(
64 kind: String,
65 expected: Any,
66 actual: Any,
67 message: String?,
68 location: SourceLocation
69): Nothing {
70 throw AssertionFailure(
71 kind, message, location,
72 renderAssertionValue(expected), renderAssertionValue(actual)
73 )
76/**
77 * Always throws [AssertionFailure] with the optional message and caller location.
78 */
79public fun fail(
80 message: String? = None,
81 location: SourceLocation = currentSourceLocation()
82): Nothing {
83 throw AssertionFailure("fail", message, location)