2020-03-26 21:17:14 +03:00
|
|
|
fn test_simple_string_interpolation() {
|
2020-01-21 01:04:26 +03:00
|
|
|
a := 'Hello'
|
|
|
|
b := 'World'
|
|
|
|
res := '$a $b'
|
|
|
|
assert res == 'Hello World'
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:17:14 +03:00
|
|
|
fn test_mixed_string_interpolation() {
|
|
|
|
num := 7
|
|
|
|
str := 'abc'
|
|
|
|
s1 := 'number=$num'
|
|
|
|
assert s1 == 'number=7'
|
|
|
|
s2 := 'string=$str'
|
|
|
|
assert s2 == 'string=abc'
|
|
|
|
s3 := 'a: $num | b: $str'
|
|
|
|
assert s3 == 'a: 7 | b: abc'
|
|
|
|
}
|
2019-08-15 12:41:23 +03:00
|
|
|
|
2020-03-26 21:17:14 +03:00
|
|
|
fn test_formatted_string_interpolation() {
|
|
|
|
x := 'abc'
|
|
|
|
axb := 'a:$x:b'
|
|
|
|
assert axb == 'a:abc:b'
|
|
|
|
x_10 := 'a:${x:10s}:b'
|
|
|
|
x10_ := 'a:${x:-10s}:b'
|
|
|
|
assert x_10 == 'a: abc:b'
|
|
|
|
assert x10_ == 'a:abc :b'
|
|
|
|
i := 23
|
|
|
|
si_right := '${i:10d}'
|
|
|
|
si__left := '${i:-10d}'
|
|
|
|
assert si_right == ' 23'
|
|
|
|
assert si__left == '23 '
|
|
|
|
}
|
2019-08-15 12:41:23 +03:00
|
|
|
|
2020-03-26 21:17:14 +03:00
|
|
|
fn test_excape_dollar_in_string() {
|
|
|
|
i := 42
|
|
|
|
assert '($i)' == '(42)'
|
|
|
|
assert '(\$i)'.contains('i') && !'(\$i)'.contains('42')
|
|
|
|
assert !'(\\$i)'.contains('i') && '(\\$i)'.contains('42') && '(\\$i)'.contains('\\')
|
|
|
|
assert '(\\\$i)'.contains('i') && !'(\\\$i)'.contains('42') && '(\\$i)'.contains('\\')
|
|
|
|
assert !'(\\\\$i)'.contains('i') && '(\\\\$i)'.contains('42') && '(\\\\$i)'.contains('\\\\')
|
|
|
|
assert '(${i})' == '(42)'
|
|
|
|
assert '(\${i})'.contains('i') && !'(\${i})'.contains('42')
|
|
|
|
assert !'(\\${i})'.contains('i') && '(\\${i})'.contains('42') && '(\\${i})'.contains('\\')
|
|
|
|
assert '(\\\${i})'.contains('i') && !'(\\\${i})'.contains('42') && '(\\${i})'.contains('\\')
|
|
|
|
assert !'(\\\\${i})'.contains('i') && '(\\\\${i})'.contains('42') && '(\\\\${i})'.contains('\\\\')
|
|
|
|
assert i == 42
|
2019-08-15 12:41:23 +03:00
|
|
|
}
|
2019-08-16 17:06:27 +03:00
|
|
|
|
|
|
|
fn test_implicit_str() {
|
2020-03-26 21:17:14 +03:00
|
|
|
i := 42
|
|
|
|
assert 'int $i' == 'int 42'
|
|
|
|
assert '$i' == '42'
|
|
|
|
check := '$i' == '42'
|
|
|
|
assert check
|
|
|
|
text := '$i' + '42'
|
|
|
|
assert text == '4242'
|
2019-08-16 17:06:27 +03:00
|
|
|
}
|