123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- fun! gotest#write_file(path, contents) abort
- let l:dir = go#util#tempdir("vim-go-test/testrun/")
- let $GOPATH .= ':' . l:dir
- let l:full_path = l:dir . '/src/' . a:path
- call mkdir(fnamemodify(l:full_path, ':h'), 'p')
- call writefile(a:contents, l:full_path)
- exe 'cd ' . l:dir . '/src'
- silent exe 'e! ' . a:path
-
- let l:lnum = 1
- for l:line in a:contents
- let l:m = match(l:line, "\x1f")
- if l:m > -1
- call setpos('.', [0, l:lnum, l:m, 0])
- call setline('.', substitute(getline('.'), "\x1f", '', ''))
- break
- endif
- let l:lnum += 1
- endfor
- return l:dir
- endfun
- fun! gotest#load_fixture(path) abort
- let l:dir = go#util#tempdir("vim-go-test/testrun/")
- let $GOPATH .= ':' . l:dir
- let l:full_path = l:dir . '/src/' . a:path
- call mkdir(fnamemodify(l:full_path, ':h'), 'p')
- exe 'cd ' . l:dir . '/src'
- silent exe 'noautocmd e ' . a:path
- silent exe printf('read %s/test-fixtures/%s', g:vim_go_root, a:path)
- silent noautocmd w!
- return l:dir
- endfun
- " let l:diff = s:diff_buffer(0, ['package main', 'import "net/mail
- fun! gotest#assert_buffer(skipHeader, want) abort
- let l:buffer = go#util#GetLines()
- if a:skipHeader
- for l:lnum in range(0, len(l:buffer) - 1)
-
- if match(l:buffer[l:lnum], '^\v(func|var|const|import \(|\))') > -1
-
-
- let l:buffer = l:buffer[l:lnum:len(l:buffer)]
- break
- endif
- endfor
- endif
- " Using ' is often easier so we don't have to escape ".
- let l:want = map(a:want, 'substitute(v:val, "\\\\t", "\t", "")')
- let l:tmp = go#util#tempdir('assert_buffer')
- try
- call writefile(l:buffer, l:tmp . '/have')
- call writefile(l:want, l:tmp . '/want')
- call go#fmt#run('gofmt', l:tmp . '/have', l:tmp . '/have')
- call go#fmt#run('gofmt', l:tmp . '/want', l:tmp . '/want')
- let [l:out, l:err] = go#util#Exec(["diff", "-u", l:tmp . '/have', l:tmp . '/want'])
- finally
- call delete(l:tmp . '/have')
- call delete(l:tmp . '/want')
- call delete(l:tmp, 'd')
- endtry
- if l:err || l:out != ''
- let v:errors = extend(v:errors, split(l:out, "\n"))
- endif
- endfun
- fun! gotest#assert_fixture(path) abort
- let l:want = readfile(printf('%s/test-fixtures/%s', g:vim_go_root, a:path))
- call gotest#assert_buffer(0, l:want)
- endfun
- func! gotest#assert_quickfix(got, want) abort
- call assert_equal(len(a:want), len(a:got), "number of errors")
- if len(a:want) != len(a:got)
- call assert_equal(a:want, a:got)
- return
- endif
- let i = 0
- while i < len(a:want)
- let want_item = a:want[i]
- let got_item = a:got[i]
- let i += 1
- call assert_equal(want_item.bufnr, got_item.bufnr, "bufnr")
- call assert_equal(want_item.lnum, got_item.lnum, "lnum")
- call assert_equal(want_item.col, got_item.col, "col")
- call assert_equal(want_item.vcol, got_item.vcol, "vcol")
- call assert_equal(want_item.nr, got_item.nr, "nr")
- call assert_equal(want_item.pattern, got_item.pattern, "pattern")
- call assert_equal(want_item.text, got_item.text, "text")
- call assert_equal(want_item.type, got_item.type, "type")
- call assert_equal(want_item.valid, got_item.valid, "valid")
- endwhile
- endfunc
|