v2 / vlib / v / gen / js / tests / interp.v
188 lines · 173 sloc · 5.54 KB · 2332ecff4811b8c97dfda8e825170e9397962519
Raw
1fn test_fn(s1 string, s2 string) {
2 print(if s1 == s2 { 'true' } else { 'false' })
3 print('\t=> ')
4 println('"${s1}", "${s2}"')
5}
6
7fn simple_string_interpolation() {
8 a := 'Hello'
9 b := 'World'
10 res := '${a} ${b}'
11 test_fn(res, 'Hello World')
12}
13
14fn mixed_string_interpolation() {
15 num := 7
16 str := 'abc'
17 s1 := 'number=${num}'
18 test_fn(s1, 'number=7')
19 s2 := 'string=${str}'
20 test_fn(s2, 'string=abc')
21 s3 := 'a: ${num} | b: ${str}'
22 test_fn(s3, 'a: 7 | b: abc')
23}
24
25fn formatted_string_interpolation() {
26 x := 'abc'
27 axb := 'a:${x}:b'
28 test_fn(axb, 'a:abc:b')
29 x_10 := 'a:${x:10s}:b'
30 x10_ := 'a:${x:-10s}:b'
31 test_fn(x_10, 'a: abc:b')
32 test_fn(x10_, 'a:abc :b')
33 i := 23
34 si_right := '${i:10d}'
35 si__left := '${i:-10d}'
36 test_fn(si_right, ' 23')
37 test_fn(si__left, '23 ')
38}
39
40/*
41escape_dollar_in_string()
42fn escape_dollar_in_string() {
43 i := 42
44 test_fn('(${i})', '(42)')
45 println('(\${i})'.contains('i') && !'(\${i})'.contains('42'))
46 println(!'(\\${i})'.contains('i') && '(\\${i})'.contains('42') && '(\\${i})'.contains('\\'))
47 println('(\\\${i})'.contains('i') && !'(\\\${i})'.contains('42') && '(\\${i})'.contains('\\'))
48 println(!'(\\\\${i})'.contains('i') && '(\\\\${i})'.contains('42') && '(\\\\${i})'.contains('\\\\'))
49 test_fn('(${i})', '(42)')
50 println('(\${i})'.contains('i') && !'(\${i})'.contains('42'))
51 println(!'(\\${i})'.contains('i') && '(\\${i})'.contains('42') && '(\\${i})'.contains('\\'))
52 println('(\\\${i})'.contains('i') && !'(\\\${i})'.contains('42') && '(\\${i})'.contains('\\'))
53 println(!'(\\\\${i})'.contains('i') && '(\\\\${i})'.contains('42') && '(\\\\${i})'.contains('\\\\'))
54 test_fn(i, 42)
55}
56*/
57
58fn implicit_str() {
59 i := 42
60 test_fn('int ${i}', 'int 42')
61 test_fn('${i}', '42')
62 check := '${i}' == '42'
63 // println(check)
64 text := '${i}' + '42'
65 test_fn(text, '4242')
66}
67
68fn string_interpolation_percent_escaping() {
69 test := 'hello'
70 hello := 'world'
71 x := '%.*s${hello}${test} |${hello:-30s}|'
72 test_fn(x, '%.*sworldhello |world |')
73}
74
75fn string_interpolation_string_prefix() {
76 // `r`, `c` and `js` are also used as a string prefix.
77 r := 'r'
78 rr := '${r}${r}'
79 test_fn(rr, 'rr')
80 c := 'c'
81 cc := '${c}${c}'
82 test_fn(cc, 'cc')
83 js := 'js'
84 jsjs := '${js}${js}'
85 test_fn(jsjs, 'jsjs')
86}
87
88fn interpolation_string_prefix_expr() {
89 r := 1
90 c := 2
91 js := 1
92 test_fn('>${3 + r}<', '>4<')
93 test_fn('${r == js} ${js}', 'true 1')
94 test_fn('>${js + c} ${js + r == c}<', '>3 true<')
95}
96
97/*
98inttypes_string_interpolation()
99fn inttypes_string_interpolation() {
100 c := i8(-103)
101 uc := u8(217)
102 uc2 := u8(13)
103 s := i16(-23456)
104 us := u16(54321)
105 i := -1622999040
106 ui := u32(3421958087)
107 vp := voidptr(ui)
108 bp := byteptr(15541149836)
109 l := i64(-7694555558525237396)
110 ul := u64(17234006112912956370)
111 test_fn('${s} ${us}', '-23456 54321')
112 test_fn('${ui} ${i}', '3421958087 -1622999040')
113 test_fn('${l} ${ul}', '-7694555558525237396 17234006112912956370')
114 test_fn('>${s:11}:${us:-13}<', '> -23456:54321 <')
115 test_fn('0x${ul:-19x}:${l:22d}', '0xef2b7d4001165bd2 : -7694555558525237396')
116 test_fn('${c:5}${uc:-7}x', ' -103217 x')
117 test_fn('${c:x}:${uc:x}:${uc2:02X}', '99:d9:0D')
118 test_fn('${s:X}:${us:x}:${u16(uc):04x}', 'A460:d431:00d9')
119 test_fn('${i:x}:${ui:X}:${int(s):x}', '9f430000:CBF6EFC7:ffffa460')
120 test_fn('${l:x}:${ul:X}', '9537727cad98876c:EF2B7D4001165BD2')
121 // default pointer format is platform dependent, so try a few
122 println("platform pointer format: '${vp:p}:${bp}'")
123 test_fn('${vp:p}:${bp}', '0xcbf6efc7:0x39e53208c' ||
124 '${vp:p}:${bp}' == 'CBF6EFC7:39E53208C' ||
125 '${vp:p}:${bp}' == 'cbf6efc7:39e53208c' ||
126 '${vp:p}:${bp}' == '00000000CBF6EFC7:000000039E53208C')
127}
128*/
129
130fn utf8_string_interpolation() {
131 a := 'à-côté'
132 st := 'Sträßle'
133 m := '10€'
134 test_fn('${a} ${st} ${m}', 'à-côté Sträßle 10€')
135 zz := '>${a:10}< >${st:-8}< >${m:5}<-'
136 zz_expected := '> à-côté< >Sträßle < > 10€<-'
137 // println(' zz: ${zz}')
138 // println('zz_expected: ${zz_expected}')
139 test_fn(zz, zz_expected)
140 // e := '\u20AC' // Eurosign doesn' work with MSVC and tcc
141 e := '€'
142 test_fn('100.00 ${e}', '100.00 €')
143 m2 := 'Москва́' // cyrillic а́: combination of U+0430 and U+0301, UTF-8: d0 b0 cc 81
144 d := 'Antonín Dvořák' // latin á: U+00E1, UTF-8: c3 a1
145 test_fn(':${m2:7}:${d:-15}:', ': Москва́:Antonín Dvořák :')
146 g := 'Πελοπόννησος'
147 test_fn('>${g:-13}<', '>Πελοπόννησος <')
148}
149
150struct Sss {
151 v1 int
152 v2 f64
153}
154
155fn (s Sss) str() string {
156 return '[${s.v1}, ${s.v2:.3f}]'
157}
158
159fn string_interpolation_str_evaluation() {
160 mut x := Sss{17, 13.455893}
161 test_fn('${x}', '[17, 13.456]')
162}
163
164/*
165string_interpolation_with_negative_format_width_should_compile_and_run_without_segfaulting()
166fn string_interpolation_with_negative_format_width_should_compile_and_run_without_segfaulting() {
167 // discovered during debugging VLS
168 i := 3
169 input := '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'
170 println('---------------------------------------------------------------------------------------------')
171 println('+60 ${i:10} | input.len: ${input.len:10} | ${input.hex():60} | ${input}')
172 println('-60 ${i:10} | input.len: ${input.len:10} | ${input.hex():-60} | ${input}')
173 println('---------------------------------------------------------------------------------------------')
174 println(true)
175}
176*/
177
178fn main() {
179 simple_string_interpolation()
180 mixed_string_interpolation()
181 formatted_string_interpolation()
182 implicit_str()
183 string_interpolation_percent_escaping()
184 string_interpolation_string_prefix()
185 interpolation_string_prefix_expr()
186 utf8_string_interpolation()
187 string_interpolation_str_evaluation()
188}
189