Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 1 | " Tests for the writefile() function. |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 3 | func Test_writefile() |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 4 | let f = tempname() |
| 5 | call writefile(["over","written"], f, "b") |
| 6 | call writefile(["hello","world"], f, "b") |
| 7 | call writefile(["!", "good"], f, "a") |
| 8 | call writefile(["morning"], f, "ab") |
| 9 | call writefile(["", "vimmers"], f, "ab") |
| 10 | let l = readfile(f) |
| 11 | call assert_equal("hello", l[0]) |
| 12 | call assert_equal("world!", l[1]) |
| 13 | call assert_equal("good", l[2]) |
| 14 | call assert_equal("morning", l[3]) |
| 15 | call assert_equal("vimmers", l[4]) |
| 16 | call delete(f) |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 17 | endfunc |
| 18 | |
| 19 | func Test_writefile_fails_gently() |
| 20 | call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:') |
| 21 | call assert_false(filereadable("Xfile")) |
| 22 | call delete("Xfile") |
| 23 | |
| 24 | call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:') |
| 25 | call assert_false(filereadable("Xfile")) |
| 26 | call delete("Xfile") |
| 27 | |
| 28 | call assert_fails('call writefile([], "Xfile", [])', 'E730:') |
| 29 | call assert_false(filereadable("Xfile")) |
| 30 | call delete("Xfile") |
| 31 | |
| 32 | call assert_fails('call writefile([], [])', 'E730:') |
| 33 | endfunc |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 34 | |
| 35 | func Test_writefile_fails_conversion() |
| 36 | if !has('multi_byte') || !has('iconv') |
| 37 | return |
| 38 | endif |
| 39 | set nobackup nowritebackup |
| 40 | new |
| 41 | let contents = ["line one", "line two"] |
| 42 | call writefile(contents, 'Xfile') |
| 43 | edit Xfile |
| 44 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
| 45 | call assert_fails('write ++enc=cp932') |
| 46 | call assert_equal(contents, readfile('Xfile')) |
| 47 | |
| 48 | call delete('Xfile') |
| 49 | bwipe! |
| 50 | set backup& writebackup& |
| 51 | endfunc |