/ test / test_writefile_function.vader
test_writefile_function.vader
  1  Before:
  2    call ale#test#SetDirectory('/testplugin/test')
  3  
  4    let g:new_line_test_file = tempname()
  5  
  6  After:
  7    noautocmd :e! ++ff=unix
  8    setlocal buftype=nofile
  9  
 10    if filereadable(g:new_line_test_file)
 11      call delete(g:new_line_test_file)
 12    endif
 13  
 14    unlet! g:new_line_test_file
 15  
 16    call ale#test#RestoreDirectory()
 17  
 18  Given(A file with Windows line ending characters):
 19    first
 20    second
 21    third
 22  
 23  Execute(Carriage returns should be included for ale#util#Writefile):
 24    call ale#test#SetFilename(g:new_line_test_file)
 25  
 26    setlocal buftype=
 27    noautocmd :w
 28    noautocmd :e! ++ff=dos
 29  
 30    call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file)
 31  
 32    AssertEqual
 33    \ ["first\r", "second\r", "third\r", ''],
 34    \ readfile(g:new_line_test_file, 'b')
 35  
 36  Given(A file with extra carriage returns):
 37    first

 38    second


 39    third
 40    fourth
 41  
 42  Execute(Carriage returns should be de-depulicated):
 43    call ale#test#SetFilename(g:new_line_test_file)
 44  
 45    setlocal buftype=
 46    noautocmd :w
 47    noautocmd :e! ++ff=dos
 48  
 49    call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file)
 50  
 51    AssertEqual
 52    \ ["first\r", "second\r", "third\r", "fourth\r", ''],
 53    \ readfile(g:new_line_test_file, 'b')
 54  
 55  Given(A file with Unix line ending characters):
 56    first
 57    second
 58    third
 59  
 60  Execute(Unix file lines should be written as normal):
 61    call ale#test#SetFilename(g:new_line_test_file)
 62  
 63    setlocal buftype=
 64    noautocmd :w
 65    noautocmd :e! ++ff=unix
 66  
 67    call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file)
 68  
 69    AssertEqual
 70    \ ['first', 'second', 'third', ''],
 71    \ readfile(g:new_line_test_file, 'b')
 72  
 73  Execute(Newline at end of file should be preserved even when nofixeol):
 74    call ale#test#SetFilename(g:new_line_test_file)
 75  
 76    setlocal buftype=
 77    noautocmd :w
 78    noautocmd :e! ++ff=unix
 79    set eol
 80    set nofixeol
 81  
 82    call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file)
 83  
 84    AssertEqual
 85    \ ['first', 'second', 'third', ''],
 86    \ readfile(g:new_line_test_file, 'b')
 87  
 88  Execute(Newline should not be appended on write when noeol and nofixeol):
 89    call ale#test#SetFilename(g:new_line_test_file)
 90  
 91    setlocal buftype=
 92    noautocmd :w
 93    noautocmd :e! ++ff=unix
 94    set noeol
 95    set nofixeol
 96  
 97    call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file)
 98  
 99    AssertEqual
100    \ ['first', 'second', 'third'],
101    \ readfile(g:new_line_test_file, 'b')
102  
103  Execute(Newline should be appended on write when noeol and fixeol):
104    call ale#test#SetFilename(g:new_line_test_file)
105  
106    setlocal buftype=
107    noautocmd :w
108    noautocmd :e! ++ff=unix
109    set noeol
110    set fixeol
111  
112    call ale#util#Writefile(bufnr(''), getline(1, '$'), g:new_line_test_file)
113  
114    AssertEqual
115    \ ['first', 'second', 'third', ''],
116    \ readfile(g:new_line_test_file, 'b')
117