v2 / vlib / v / tests / builtin_arrays / array_to_string_test.v
57 lines · 44 sloc · 1.25 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1fn array_array_array[T](len int, value T) [][][]T {
2 return [][][]T{len: len, init: [][]T{len: len, init: []T{len: len, init: value}}}
3}
4
5fn test_array_to_string_conversion() {
6 a := ['1', '2', '3', '4']
7 assert a.str() == "['1', '2', '3', '4']"
8
9 b := [1, 2, 3, 4]
10 assert b.str() == '[1, 2, 3, 4]'
11
12 c := [1.1, 2.2, 3.3, 4.4]
13 assert c.str() == '[1.1, 2.2, 3.3, 4.4]'
14
15 d := [i16(1), 2, 3]
16 assert d.str() == '[1, 2, 3]'
17
18 e := [i64(1), 2, 3]
19 assert e.str() == '[1, 2, 3]'
20
21 f := [u8(66), 32, 126, 10, 13, 5, 18, 127, 255]
22 assert f.str() == '[66, 32, 126, 10, 13, 5, 18, 127, 255]'
23
24 // https://github.com/vlang/v/issues/8036
25 g := array_array_array[int](2, 2)
26 assert g.str() == '[[[2, 2], [2, 2]], [[2, 2], [2, 2]]]'
27}
28
29fn test_interpolation_array_to_string() {
30 a := ['1', '2', '3']
31 assert '${a}' == "['1', '2', '3']"
32
33 b := [1, 2, 3, 4]
34 assert '${b}' == '[1, 2, 3, 4]'
35
36 c := [1.1, 2.2, 3.3, 4.4]
37 assert '${c}' == '[1.1, 2.2, 3.3, 4.4]'
38
39 d := [i16(1), 2, 3]
40 assert '${d}' == '[1, 2, 3]'
41
42 e := [i64(1), 2, 3]
43 assert '${e}' == '[1, 2, 3]'
44}
45
46fn test_interpolation_array_of_map_to_string() {
47 mut ams := []map[string]string{}
48 ams << {
49 'a': 'b'
50 'c': 'd'
51 }
52 ams << {
53 'e': 'f'
54 'g': 'h'
55 }
56 assert '${ams}' == "[{'a': 'b', 'c': 'd'}, {'e': 'f', 'g': 'h'}]"
57}
58