Commit cbe7343f authored by HiPhish's avatar HiPhish
Browse files

Translate file type detection tests to Lua

parent b7f6660e
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -21,9 +21,7 @@
"   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}

augroup filetypedetect
autocmd! BufRead,BufNewFile *.jinja,*jinja2,*.j2 call <SID>extension(expand('<afile>'))
augroup END

" Detect a normal or compound file extension (like 'foo.html.jinja')
function! s:extension(fname)
@@ -32,9 +30,9 @@ function! s:extension(fname)
	" Try to detect the file type without the Jinja extension first. This will
	" fail setting the file type of file extension like 'foo.xxxx.jinja',
	" which is what we want.
	noautocmd silent exe 'file' fnameescape(fnamemodify(a:fname, ':r'))
	call nvim_buf_set_name(0, fnamemodify(a:fname, ':r'))
	filetype detect
	noautocmd silent exe 'file' fnameescape(a:fname)
	call nvim_buf_set_name(0, a:fname)
	" Using ':file' has dissociated the buffer from its file, but executing
	" ':edit' fixes this
	noautocmd silent edit

test/ftdetect.vader

deleted100644 → 0
+0 −132
Original line number Diff line number Diff line
####################################################
# Detection of Jinja files based of file extension #
####################################################

Given (A simple file extension):
  Hello world

Execute:
  silent file foo.jinja
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Given (File extension j2):
  Hello world

Execute:
  silent file foo.j2
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Given (File extension jinja2):
  Hello world

Execute:
  silent file foo.jinja2
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Given (Two file extensions):
  Hello world

Execute:
  silent file foo.html.jinja
  filetype detect

Then:
  AssertEqual 'html.jinja', &filetype


Given (Three file extensions):
  Hello world

Execute:
  silent file foo.tex.html.jinja
  filetype detect

Then:
  AssertEqual 'html.jinja', &filetype


Given (Nonsense file extensions):
  Hello world

# I really hope there is no 'nonsense' file format out there
Execute:
  silent file foo.nonsense.jinja
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Given (File name with percent character):
  Hello world

Execute:
  silent execute 'file' fnameescape('foo%bar.html.jinja')
  filetype detect

Then:
  AssertEqual 'html.jinja', &filetype
  AssertEqual 'foo%bar.html.jinja', bufname()


Given (Do not double-detect jinja):
  {# This is a Jinja comment #}

Execute:
  silent file foo.html.jinja
  filetype detect

Then:
  AssertEqual 'html.jinja', &filetype


Given (Do not append Jinja multiple times):
  Hello world

# This will fail because the filetype remains 'html.jinja'.
Execute:
  silent file foo.html.jinja
  filetype detect
  silent file foo.nonsense.jinja
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Given (Add new file type where necessary):
  Hello world

Execute:
  silent file foo.nonsense.jinja
  filetype detect
  silent file foo.html.jinja
  filetype detect

Then:
  AssertEqual 'html.jinja', &filetype


Given (change file type when necessary):
  Hello world

Execute:
  silent file foo.html.jinja
  filetype detect
  silent file foo.xml.jinja
  filetype detect

Then:
  AssertEqual 'xml.jinja', &filetype

test/spec/ftdetect.lua

0 → 100644
+101 −0
Original line number Diff line number Diff line
local yd = require 'yo-dawg'

describe('File type detection', function()
	local nvim

	before_each(function()
		nvim = yd.start()
	end)

	after_each(function()
		yd.stop(nvim)
	end)

	describe('Based on file extension', function()
		it('recognizes the file extension `.jinja`', function()
			nvim:command('file foo.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('jinja')
		end)

		it('recognizes the file extension `.jinja2`', function()
			nvim:command('file foo.jinja2')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('jinja')
		end)

		it('recognizes the file extension `.j2`', function()
			nvim:command('file foo.j2')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('jinja')
		end)

		it('recognizes two file extension', function()
			nvim:command('file foo.html.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('html.jinja')
		end)

		it('handles three file extension', function()
			nvim:command('file foo.tex.html.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('html.jinja')
		end)

		it('ignores unknown file extensions', function()
			nvim:command('file foo.nonsense.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('jinja')
		end)

		it('handles file names with percent character', function()
			nvim:command('file ' ..  vim.fn.fnameescape('foo%bar.html.jinja'))
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('html.jinja')
		end)
	end)

	describe('Idemptence', function()
		it('detects Jinja only once', function()
			-- NOTE: we use XML instead of HTML as our other file type because
			-- out of the box Neovim would detect the file content as
			-- `htmldjango` instead of `html`
			local tempname = nvim:eval('tempname() .. ".xml.jinja"')
			nvim:cmd({cmd = 'write', args = {tempname}}, {})
			nvim:buf_set_lines(0, 0, 0, true, {'{# This is a Jinja comment #}'})
			nvim:cmd({cmd = 'write', args = {tempname}}, {})
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('xml.jinja')
		end)

		it('appends `jinja` only once', function()
			nvim:command('file foo.html.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('html.jinja')
			nvim:command('file foo.nonsense.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('jinja')
		end)
	end)

	describe('Adjustment', function()
		it('adds new file type when necessary', function()
			nvim:command('file foo.nonsense.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('jinja')
			nvim:command('file foo.html.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('html.jinja')
		end)

		it('changes file type when necessary', function()
			-- TODO
			nvim:command('file foo.html.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('html.jinja')
			nvim:command('file foo.xml.jinja')
			nvim:command('filetype detect')
			assert.nvim(nvim).has_filetype('xml.jinja')
		end)
	end)
end)