Commit d2c162ee authored by HiPhish's avatar HiPhish
Browse files

Translate Jinja detection tests Lua

parent cbe7343f
Loading
Loading
Loading
Loading

test/jinja-detect.vader

deleted100644 → 0
+0 −56
Original line number Diff line number Diff line
################################################################
# Test detection of various Jinja elements in a non-jinja file #
################################################################

Given jinja (A comment):
  {# A jinja comment #}

Execute:
  Assert jinja#DetectJinja(1, 1)


Given jinja (A statement):
  {% for item in items %}

Execute:
  Assert jinja#DetectJinja(1, 1)


Given jinja (An expression):
  {{ item }}

Execute:
  Assert jinja#DetectJinja(1, 1)


Given jinja (A line-comment):
  ## A jinja comment

Execute:
  Assert jinja#DetectJinja(1, 1)


Given jinja (A line statement):
  # for item in items

Execute:
  Assert jinja#DetectJinja(1, 1)


# We should test for more types of jinja elements
Given html (Try detecting auto-adjusting file type):
  {# A jinja comment #}
  <html>
    <head>
      <title>Sample HTML file</title>
    </head>
    <body>
      <p>Hello world.</p>
    </body>
  </html>

Execute:
  call jinja#AdjustFiletype()

Then:
  AssertEqual 'html.jinja', &filetype
+59 −0
Original line number Diff line number Diff line
local yd = require 'yo-dawg'

describe('Detection of various Jinja elements in non-Jinja files', function()
	local nvim

	local function set_content(...)
		nvim:buf_set_lines(0, 0, 0, true, {...})
	end

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

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

	it('detects comments', function()
		set_content('{# A jinja comment #}')
		assert.nvim(nvim).between_rows(1, 1).contains_jinja(1)
	end)

	it('detects statements', function()
		set_content('{% for item in items %}')
		assert.nvim(nvim).between_rows(1, 1).contains_jinja(1)
	end)

	it('detects expressions', function()
		set_content('{{ item }}')
		assert.nvim(nvim).between_rows(1, 1).contains_jinja(1)
	end)

	it('detects line comments', function()
		set_content('## A jinja comment')
		assert.nvim(nvim).between_rows(1, 1).contains_jinja(1)
	end)

	it('detects line statements', function()
		set_content('# for item in items')
		assert.nvim(nvim).between_rows(1, 1).contains_jinja(1)
	end)

	it('adjusts the file types', function()
		local content = [[
{# A jinja comment #}
<html>
    <head>
        <title>Sample HTML file</title>
    </head>
    <body>
        <p>Hello world.</p>
    </body>
</html>]]
		nvim:set_option_value('filetype', 'html', {})
		set_content(table.unpack(vim.split(content, '\n')))
		nvim:call_function('jinja#AdjustFiletype', {})
		assert.nvim(nvim).has_filetype('html.jinja')
	end)
end)
+27 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ local assert = require 'luassert'

---Globally unique keys to retrieve the values from the current test state.
local NVIM_STATE_KEY, ROW_STATE_KEY, COL_STATE_KEY = {}, {}, {}
local START_ROW_STATE_KEY, END_ROW_STATE_KEY = {}, {}

---Add the Neovim client to the current test state.
local function nvim_client(state, args, _level)
@@ -27,6 +28,13 @@ local function at_position(state, args, _level)
	return state
end

local function between_rows(state, args, _level)
	assert(args.n == 2, 'Wrong number of arguments given to modifier')
	rawset(state, START_ROW_STATE_KEY, args[1])
	rawset(state, END_ROW_STATE_KEY, args[2])
	return state
end

---Assertion that the current Neovim client at the current position has the
---given highlight group.
local function has_hlgroup(state, args)
@@ -46,6 +54,17 @@ local function has_hlgroup(state, args)
	return result == hlgroup
end

local function contains_jinja(state, args, _level)
	assert(args.n == 1, 'No row provided')
	local nvim = rawget(state, NVIM_STATE_KEY)
	local start_row = rawget(state, START_ROW_STATE_KEY)
	local end_row = rawget(state, END_ROW_STATE_KEY)
	local expected = args[1]

	local linenr = nvim:call_function('jinja#DetectJinja', {start_row, end_row})
	return linenr == expected
end

---Assert that the current buffer has the expected file type
local function has_filetype(state, args, _level)
	assert(args.n == 1, 'No file type provided')
@@ -61,8 +80,12 @@ say:set('assertion.hlgroup_at.negative', 'Unexpected highlight group %s')
say:set('assertion.has_filetype.positive', 'Expected file type %s')
say:set('assertion.has_filetype.negative', 'Unexpected file type %s')

say:set('assertion.contains_jina.positive', 'No Jinja code detected on line %s')
say:set('assertion.contains_jina.negative', 'Jinja code detected on line %s')

assert:register('modifier', 'nvim', nvim_client)
assert:register('modifier', 'at_position', at_position)
assert:register('modifier', 'between_rows', between_rows)
assert:register(
	'assertion', 'has_hlgroup', has_hlgroup,
	'assertion.hlgroup_at.positive', 'assertion.hlgroup_at.negative'
@@ -71,3 +94,7 @@ assert:register(
	'assertion', 'has_filetype', has_filetype,
	'assertion.has_filetype.positive', 'assertion.has_filetype.negative'
)
assert:register(
	'assertion', 'contains_jinja', contains_jinja,
	'assertion.contains_jinja.positive', 'assertion.contains_jinja.negative'
)