patch 8.2.4770: cannot easily mix expression and heredoc
Problem: Cannot easily mix expression and heredoc.
Solution: Support in heredoc. (Yegappan Lakshmanan, closes #10138)
diff --git a/src/testdir/test_let.vim b/src/testdir/test_let.vim
index 5f8879b..2606fa2 100644
--- a/src/testdir/test_let.vim
+++ b/src/testdir/test_let.vim
@@ -1,5 +1,7 @@
" Tests for the :let command.
+import './vim9.vim' as v9
+
func Test_let()
" Test to not autoload when assigning. It causes internal error.
set runtimepath+=./sautest
@@ -379,7 +381,8 @@
call assert_equal(['Text', 'with', 'indent'], text)
endfunc
-" Test for the setting a variable using the heredoc syntax
+" Test for the setting a variable using the heredoc syntax.
+" Keep near the end, this messes up highlighting.
func Test_let_heredoc()
let var1 =<< END
Some sample text
@@ -495,4 +498,96 @@
call assert_equal([' x', ' \y', ' z'], [a, b, c])
endfunc
+" Test for evaluating Vim expressions in a heredoc using `=expr`
+" Keep near the end, this messes up highlighting.
+func Test_let_heredoc_eval()
+ let str = ''
+ let code =<< trim eval END
+ let a = `=5 + 10`
+ let b = `=min([10, 6])` + `=max([4, 6])`
+ `=str`
+ let c = "abc`=str`d"
+ END
+ call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code)
+ let $TESTVAR = "Hello"
+ let code =<< eval trim END
+ let s = "`=$TESTVAR`"
+ END
+ call assert_equal(['let s = "Hello"'], code)
+ let code =<< eval END
+ let s = "`=$TESTVAR`"
+END
+ call assert_equal([' let s = "Hello"'], code)
+ let a = 10
+ let data =<< eval END
+`=a`
+END
+ call assert_equal(['10'], data)
+ let x = 'X'
+ let code =<< eval trim END
+ let a = `abc`
+ let b = `=x`
+ let c = `
+ END
+ call assert_equal(['let a = `abc`', 'let b = X', 'let c = `'], code)
+ let code = 'xxx'
+ let code =<< eval trim END
+ let n = `=5 +
+ 6`
+ END
+ call assert_equal('xxx', code)
+ let code =<< eval trim END
+ let n = `=min([1, 2]` + `=max([3, 4])`
+ END
+ call assert_equal('xxx', code)
+
+ let lines =<< trim LINES
+ let text =<< eval trim END
+ let b = `=
+ END
+ LINES
+ call v9.CheckScriptFailure(lines, 'E1083:')
+
+ let lines =<< trim LINES
+ let text =<< eval trim END
+ let b = `=abc
+ END
+ LINES
+ call v9.CheckScriptFailure(lines, 'E1083:')
+
+ let lines =<< trim LINES
+ let text =<< eval trim END
+ let b = `=`
+ END
+ LINES
+ call v9.CheckScriptFailure(lines, 'E15:')
+
+ " Test for sourcing a script containing a heredoc with invalid expression.
+ " Variable assignment should fail, if expression evaluation fails
+ new
+ let g:Xvar = 'test'
+ let g:b = 10
+ let lines =<< trim END
+ let Xvar =<< eval CODE
+ let a = 1
+ let b = `=5+`
+ let c = 2
+ CODE
+ let g:Count += 1
+ END
+ call setline(1, lines)
+ let g:Count = 0
+ call assert_fails('source', 'E15:')
+ call assert_equal(1, g:Count)
+ call setline(3, 'let b = `=abc`')
+ call assert_fails('source', 'E121:')
+ call assert_equal(2, g:Count)
+ call setline(3, 'let b = `=abc` + `=min([9, 4])` + 2')
+ call assert_fails('source', 'E121:')
+ call assert_equal(3, g:Count)
+ call assert_equal('test', g:Xvar)
+ call assert_equal(10, g:b)
+ bw!
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim
index 7e382c2..02274b3 100644
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -740,7 +740,7 @@
enddef
def Test_extend_list()
- # using uninitilaized list assigns empty list
+ # using uninitialized list assigns empty list
var lines =<< trim END
var l1: list<number>
var l2 = l1
@@ -758,7 +758,7 @@
END
v9.CheckDefAndScriptSuccess(lines)
- # appending to uninitialzed list from a function works
+ # appending to uninitialized list from a function works
lines =<< trim END
vim9script
var list: list<string>
@@ -2637,6 +2637,56 @@
v9.CheckScriptSuccess(lines)
enddef
+let g:someVar = 'X'
+
+" Test for heredoc with Vim expressions.
+" This messes up highlighting, keep it near the end.
+def Test_heredoc_expr()
+ var code =<< trim eval END
+ var a = `=5 + 10`
+ var b = `=min([10, 6])` + `=max([4, 6])`
+ END
+ assert_equal(['var a = 15', 'var b = 6 + 6'], code)
+
+ code =<< eval trim END
+ var s = "`=$SOME_ENV_VAR`"
+ END
+ assert_equal(['var s = "somemore"'], code)
+
+ code =<< eval END
+ var s = "`=$SOME_ENV_VAR`"
+END
+ assert_equal([' var s = "somemore"'], code)
+
+ code =<< eval trim END
+ let a = `abc`
+ let b = `=g:someVar`
+ let c = `
+ END
+ assert_equal(['let a = `abc`', 'let b = X', 'let c = `'], code)
+
+ var lines =<< trim LINES
+ var text =<< eval trim END
+ let b = `=
+ END
+ LINES
+ v9.CheckDefAndScriptFailure(lines, 'E1083:')
+
+ lines =<< trim LINES
+ var text =<< eval trim END
+ let b = `=abc
+ END
+ LINES
+ v9.CheckDefAndScriptFailure(lines, 'E1083:')
+
+ lines =<< trim LINES
+ var text =<< eval trim END
+ let b = `=`
+ END
+ LINES
+ v9.CheckDefAndScriptFailure(lines, 'E15:')
+enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker