v2 / vlib / builtin / meta_assert.v
53 lines · 50 sloc · 1.89 KB · 90fa8bc56fd4c3870a9041e39fc156bfacdcd1b1
Raw
1module builtin
2
3// VAssertMetaInfo is used during assertions. An instance of it is filled in by compile time generated code, when an assertion fails.
4pub struct VAssertMetaInfo {
5pub:
6 fpath string // the source file path of the assertion
7 line_nr int // the line number of the assertion
8 fn_name string // the function name in which the assertion is
9 src string // the actual source line of the assertion
10 op string // the operation of the assertion, i.e. '==', '<', 'call', etc ...
11 llabel string // the left side of the infix expressions as source
12 rlabel string // the right side of the infix expressions as source
13 lvalue string // the stringified *actual value* of the left side of a failed assertion
14 rvalue string // the stringified *actual value* of the right side of a failed assertion
15 message string // the value of the `message` from `assert cond, message`
16 has_msg bool // false for assertions like `assert cond`, true for `assert cond, 'oh no'`
17}
18
19// free frees the memory occupied by the assertion meta data. It is called automatically by
20// the code, that V's test framework generates, after all other callbacks have been called.
21@[manualfree; unsafe]
22pub fn (ami &VAssertMetaInfo) free() {
23 unsafe {
24 ami.fpath.free()
25 ami.fn_name.free()
26 ami.src.free()
27 ami.op.free()
28 ami.llabel.free()
29 ami.rlabel.free()
30 ami.lvalue.free()
31 ami.rvalue.free()
32 ami.message.free()
33 }
34}
35
36fn __print_assert_failure(i &VAssertMetaInfo) {
37 eprintln('${i.fpath}:${i.line_nr + 1}: FAIL: fn ${i.fn_name}: assert ${i.src}')
38 if i.op.len > 0 && i.op != 'call' {
39 if i.llabel == i.lvalue {
40 eprintln(' left value: ${i.llabel}')
41 } else {
42 eprintln(' left value: ${i.llabel} = ${i.lvalue}')
43 }
44 if i.rlabel == i.rvalue {
45 eprintln(' right value: ${i.rlabel}')
46 } else {
47 eprintln(' right value: ${i.rlabel} = ${i.rvalue}')
48 }
49 }
50 if i.has_msg {
51 eprintln(' message: ${i.message}')
52 }
53}
54