gotest.vim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. " Write a Go file to a temporary directory and append this directory to $GOPATH.
  2. "
  3. " The file will written to a:path, which is relative to the temporary directory,
  4. " and this file will be loaded as the current buffer.
  5. "
  6. " The cursor will be placed on the character before any 0x1f byte.
  7. "
  8. " The full path to the created directory is returned, it is the caller's
  9. " responsibility to clean that up!
  10. fun! gotest#write_file(path, contents) abort
  11. let l:dir = go#util#tempdir("vim-go-test/testrun/")
  12. let $GOPATH .= ':' . l:dir
  13. let l:full_path = l:dir . '/src/' . a:path
  14. call mkdir(fnamemodify(l:full_path, ':h'), 'p')
  15. call writefile(a:contents, l:full_path)
  16. exe 'cd ' . l:dir . '/src'
  17. silent exe 'e! ' . a:path
  18. " Set cursor.
  19. let l:lnum = 1
  20. for l:line in a:contents
  21. let l:m = match(l:line, "\x1f")
  22. if l:m > -1
  23. call setpos('.', [0, l:lnum, l:m, 0])
  24. call setline('.', substitute(getline('.'), "\x1f", '', ''))
  25. break
  26. endif
  27. let l:lnum += 1
  28. endfor
  29. return l:dir
  30. endfun
  31. " Load a fixture file from test-fixtures.
  32. "
  33. " The file will be copied to a new GOPATH-compliant temporary directory and
  34. " loaded as the current buffer.
  35. fun! gotest#load_fixture(path) abort
  36. let l:dir = go#util#tempdir("vim-go-test/testrun/")
  37. let $GOPATH .= ':' . l:dir
  38. let l:full_path = l:dir . '/src/' . a:path
  39. call mkdir(fnamemodify(l:full_path, ':h'), 'p')
  40. exe 'cd ' . l:dir . '/src'
  41. silent exe 'noautocmd e ' . a:path
  42. silent exe printf('read %s/test-fixtures/%s', g:vim_go_root, a:path)
  43. silent noautocmd w!
  44. return l:dir
  45. endfun
  46. " Diff the contents of the current buffer to a:want, which should be a list.
  47. " If a:skipHeader is true we won't bother with the package and import
  48. " declarations; so e.g.:
  49. "
  50. " let l:diff = s:diff_buffer(1, ['_ = mail.Address{}'])
  51. "
  52. " will pass, whereas otherwise you'd have to:
  53. "
  54. " let l:diff = s:diff_buffer(0, ['package main', 'import "net/mail", '_ = mail.Address{}'])
  55. fun! gotest#assert_buffer(skipHeader, want) abort
  56. let l:buffer = go#util#GetLines()
  57. if a:skipHeader
  58. for l:lnum in range(0, len(l:buffer) - 1)
  59. " Bit rudimentary, but works reasonably well.
  60. if match(l:buffer[l:lnum], '^\v(func|var|const|import \(|\))') > -1
  61. " vint bug: https://github.com/Kuniwak/vint/issues/179
  62. " vint: -ProhibitUsingUndeclaredVariable
  63. let l:buffer = l:buffer[l:lnum:len(l:buffer)]
  64. break
  65. endif
  66. endfor
  67. endif
  68. " Using ' is often easier so we don't have to escape ".
  69. let l:want = map(a:want, 'substitute(v:val, "\\\\t", "\t", "")')
  70. let l:tmp = go#util#tempdir('assert_buffer')
  71. try
  72. call writefile(l:buffer, l:tmp . '/have')
  73. call writefile(l:want, l:tmp . '/want')
  74. call go#fmt#run('gofmt', l:tmp . '/have', l:tmp . '/have')
  75. call go#fmt#run('gofmt', l:tmp . '/want', l:tmp . '/want')
  76. let [l:out, l:err] = go#util#Exec(["diff", "-u", l:tmp . '/have', l:tmp . '/want'])
  77. finally
  78. call delete(l:tmp . '/have')
  79. call delete(l:tmp . '/want')
  80. call delete(l:tmp, 'd')
  81. endtry
  82. if l:err || l:out != ''
  83. let v:errors = extend(v:errors, split(l:out, "\n"))
  84. endif
  85. endfun
  86. " Diff the contents of the current buffer to the fixture file in a:path.
  87. fun! gotest#assert_fixture(path) abort
  88. let l:want = readfile(printf('%s/test-fixtures/%s', g:vim_go_root, a:path))
  89. call gotest#assert_buffer(0, l:want)
  90. endfun
  91. func! gotest#assert_quickfix(got, want) abort
  92. call assert_equal(len(a:want), len(a:got), "number of errors")
  93. if len(a:want) != len(a:got)
  94. call assert_equal(a:want, a:got)
  95. return
  96. endif
  97. let i = 0
  98. while i < len(a:want)
  99. let want_item = a:want[i]
  100. let got_item = a:got[i]
  101. let i += 1
  102. call assert_equal(want_item.bufnr, got_item.bufnr, "bufnr")
  103. call assert_equal(want_item.lnum, got_item.lnum, "lnum")
  104. call assert_equal(want_item.col, got_item.col, "col")
  105. call assert_equal(want_item.vcol, got_item.vcol, "vcol")
  106. call assert_equal(want_item.nr, got_item.nr, "nr")
  107. call assert_equal(want_item.pattern, got_item.pattern, "pattern")
  108. call assert_equal(want_item.text, got_item.text, "text")
  109. call assert_equal(want_item.type, got_item.type, "type")
  110. call assert_equal(want_item.valid, got_item.valid, "valid")
  111. endwhile
  112. endfunc
  113. " vim: sw=2 ts=2 et