v2 / vlib / v / tests / printing / dump_fns_test.v
79 lines · 65 sloc · 1.5 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1fn foo() int {
2 return 1
3}
4
5fn zoo() int {
6 return 123
7}
8
9fn test_dump_of_functions() {
10 x := dump(foo)
11 y := dump(zoo)
12 dump(foo())
13 dump(zoo())
14 dump(x)
15 dump(y)
16 dump(x())
17 dump(y())
18 assert voidptr(x) != 0
19 assert voidptr(y) != 0
20 assert foo == x
21 assert y == zoo
22}
23
24//
25
26struct StructWithStrMethodTakingReference {
27 x int
28}
29
30pub fn (t &StructWithStrMethodTakingReference) str() string {
31 return 'StructWithStrMethodTakingReference{x: ${t.x}}'
32}
33
34fn test_dump_of_type_that_has_custom_str_method_with_reference_parameter() {
35 s := StructWithStrMethodTakingReference{123}
36 assert dump(s).x == 123
37 ps := &StructWithStrMethodTakingReference{456}
38 assert dump(ps).x == 456
39}
40
41//
42
43struct StructWithNormalStrMethod {
44 x int
45}
46
47pub fn (t StructWithNormalStrMethod) str() string {
48 return 'StructWithNormalStrMethod{x: ${t.x}}'
49}
50
51fn test_dump_of_type_that_has_normal_custom_str_method() {
52 s := StructWithNormalStrMethod{123}
53 assert dump(s).x == 123
54 ps := &StructWithNormalStrMethod{456}
55 assert dump(ps).x == 456
56}
57
58//
59
60struct StructWithoutStrMethod {
61 x int
62}
63
64fn test_dump_of_type_that_has_no_custom_str_method() {
65 s := StructWithoutStrMethod{123}
66 assert dump(s).x == 123
67 ps := &StructWithoutStrMethod{456}
68 assert dump(ps).x == 456
69}
70
71fn test_nil_values_and_voidptr_values_can_be_dumped_in_the_same_program() {
72 // Note, that nil is its own type in the main v repo,
73 // while dump() generates `_v_dump_expr_voidptr` for *both* `nil` and `voidptr` values.
74 a := unsafe { nil }
75 b := voidptr(456)
76 dump(a)
77 dump(b)
78 assert true
79}
80