summaryrefslogtreecommitdiffstats
path: root/src/testdir/test_expr.vim
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2022-05-06 13:14:50 +0100
committerBram Moolenaar <Bram@vim.org>2022-05-06 13:14:50 +0100
commit2eaef106e4a7fc9dc74a7e672b5f550ec1f9786e (patch)
tree543832333e5ef90b9e51477457a7e65572274227 /src/testdir/test_expr.vim
parente7d6dbc5721342e3d6b04cf285e4510b5569e707 (diff)
patch 8.2.4883: string interpolation only works in heredocv8.2.4883
Problem: String interpolation only works in heredoc. Solution: Support interpolated strings. Use syntax for heredoc consistent with strings, similar to C#. (closes #10327)
Diffstat (limited to 'src/testdir/test_expr.vim')
-rw-r--r--src/testdir/test_expr.vim56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim
index 3c61d27036..3ea59eb267 100644
--- a/src/testdir/test_expr.vim
+++ b/src/testdir/test_expr.vim
@@ -890,4 +890,60 @@ func Test_float_compare()
call v9.CheckLegacyAndVim9Success(lines)
endfunc
+func Test_string_interp()
+ let lines =<< trim END
+ call assert_equal('', $"")
+ call assert_equal('foobar', $"foobar")
+ #" Escaping rules.
+ call assert_equal('"foo"{bar}', $"\"foo\"{{bar}}")
+ call assert_equal('"foo"{bar}', $'"foo"{{bar}}')
+ call assert_equal('foobar', $"{\"foo\"}" .. $'{''bar''}')
+ #" Whitespace before/after the expression.
+ call assert_equal('3', $"{ 1 + 2 }")
+ #" String conversion.
+ call assert_equal('hello from ' .. v:version, $"hello from {v:version}")
+ call assert_equal('hello from ' .. v:version, $'hello from {v:version}')
+ #" Paper over a small difference between VimScript behaviour.
+ call assert_equal(string(v:true), $"{v:true}")
+ call assert_equal('(1+1=2)', $"(1+1={1 + 1})")
+ #" Hex-escaped opening brace: char2nr('{') == 0x7b
+ call assert_equal('esc123ape', $"esc\x7b123}ape")
+ call assert_equal('me{}me', $"me{\x7b}\x7dme")
+ VAR var1 = "sun"
+ VAR var2 = "shine"
+ call assert_equal('sunshine', $"{var1}{var2}")
+ call assert_equal('sunsunsun', $"{var1->repeat(3)}")
+ #" Multibyte strings.
+ call assert_equal('say ハロー・ワールド', $"say {'ハロー・ワールド'}")
+ #" Nested.
+ call assert_equal('foobarbaz', $"foo{$\"{'bar'}\"}baz")
+ #" Do not evaluate blocks when the expr is skipped.
+ VAR tmp = 0
+ if v:false
+ echo "${ LET tmp += 1 }"
+ endif
+ call assert_equal(0, tmp)
+
+ #" Stray closing brace.
+ call assert_fails('echo $"moo}"', 'E1278:')
+ #" Undefined variable in expansion.
+ call assert_fails('echo $"{moo}"', 'E121:')
+ #" Empty blocks are rejected.
+ call assert_fails('echo $"{}"', 'E15:')
+ call assert_fails('echo $"{ }"', 'E15:')
+ END
+ call v9.CheckLegacyAndVim9Success(lines)
+
+ let lines =<< trim END
+ call assert_equal('5', $"{({x -> x + 1})(4)}")
+ END
+ call v9.CheckLegacySuccess(lines)
+
+ let lines =<< trim END
+ call assert_equal('5', $"{((x) => x + 1)(4)}")
+ call assert_fails('echo $"{ # foo }"', 'E1279:')
+ END
+ call v9.CheckDefAndScriptSuccess(lines)
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab