Commit b7f6660e authored by HiPhish's avatar HiPhish
Browse files

Translate syntax tests to Lua

parent 7622c138
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
{# This should be highlighted like a comment #}

{# Variables #}
{{ variable }}
{{ variable | with | filter }}
{{ filtering | with(arg, args) }}

{# Statements #}
{% jinja_statement %}
{% statement with arguments %}
{% function_call() %}

{# Control of command #}
{% if foo or bar %}
{% else if baz %}
{% endif %}

{% for foo in foos %}
{% endfor %}

test/spec/syntax.lua

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

describe('Syntax highlighting', function()
	local nvim

	before_each(function()
		nvim = yd.start()
		nvim:cmd({cmd = 'edit', args = {'test/files/sample.jinja'}}, {})
		nvim:set_option_value('filetype', 'jinja', {buf = 0})
	end)

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

	it('highlights comments', function()
		assert.nvim(nvim).at_position(1,  1).has_hlgroup('jinjaCommentDelim')
		assert.nvim(nvim).at_position(1,  2).has_hlgroup('jinjaCommentDelim')
		assert.nvim(nvim).at_position(1,  5).has_hlgroup('jinjaComment')
		assert.nvim(nvim).at_position(1, 46).has_hlgroup('jinjaCommentDelim')
		assert.nvim(nvim).at_position(1, 47).has_hlgroup('jinjaCommentDelim')
	end)

	it('highlights variable blocks', function()
		assert.nvim(nvim).at_position(4,  1).has_hlgroup('jinjaVarDelim')
		assert.nvim(nvim).at_position(4,  2).has_hlgroup('jinjaVarDelim')
		assert.nvim(nvim).at_position(4,  3).has_hlgroup('jinjaVarBlock')
		assert.nvim(nvim).at_position(4,  4).has_hlgroup('jinjaVariable')
		assert.nvim(nvim).at_position(4, 13).has_hlgroup('jinjaVarDelim')
		assert.nvim(nvim).at_position(4, 14).has_hlgroup('jinjaVarDelim')
	end)

	it('highlights operators', function()
		assert.nvim(nvim).at_position(5, 13).has_hlgroup('jinjaOperator')
	end)

	it('highlights nested arguments', function()
		assert.nvim(nvim).at_position(6, 25).has_hlgroup('jinjaNested')
	end)

	it('highlights filters', function()
		assert.nvim(nvim).at_position(5, 15).has_hlgroup('jinjaFilter')
	end)

	it('highlights statement blocks', function()
		assert.nvim(nvim).at_position(9, 1).has_hlgroup('jinjaTagDelim')
		assert.nvim(nvim).at_position(9, 2).has_hlgroup('jinjaTagDelim')
		assert.nvim(nvim).at_position(9, 3).has_hlgroup('jinjaTagBlock')
		assert.nvim(nvim).at_position(9, 4).has_hlgroup('jinjaStatement')
	end)
end)

test/syntax.vader

deleted100644 → 0
+0 −51
Original line number Diff line number Diff line
# ##########################################
# Syntax detection of various Jinja elements
# ##########################################

Given jinja (Sample of Jinja syntax):
  {# This should be highlighted like a comment #}

  {# Variables #}
  {{ variable }}
  {{ variable | with | filter }}
  {{ filtering | with(arg, args) }}

  {# Statements #}
  {% jinja_statement %}
  {% statement with arguments %}
  {% function_call() %}

  {# Control of command #}
  {% if foo or bar %}
  {% else if baz %}
  {% endif %}

  {% for foo in foos %}
  {% endfor %}

Execute (Verify comments):
  AssertEqual 'jinjaCommentDelim', SyntaxAt(1,  1)
  AssertEqual 'jinjaCommentDelim', SyntaxAt(1,  2)
  AssertEqual 'jinjaComment'     , SyntaxAt(1,  4)
  AssertEqual 'jinjaCommentDelim', SyntaxAt(1, 46)
  AssertEqual 'jinjaCommentDelim', SyntaxAt(1, 47)

Execute (Verify variable blocks):
  AssertEqual 'jinjaVarDelim'    , SyntaxAt(4,  1)
  AssertEqual 'jinjaVarDelim'    , SyntaxAt(4,  2)
  AssertEqual 'jinjaVarBlock'    , SyntaxAt(4,  3)
  AssertEqual 'jinjaVariable'    , SyntaxAt(4,  4)
  AssertEqual 'jinjaVarDelim'    , SyntaxAt(4, 13)
  AssertEqual 'jinjaVarDelim'    , SyntaxAt(4, 14)

  AssertEqual 'jinjaOperator'    , SyntaxAt(5, 13)
  AssertEqual 'jinjaFilter'      , SyntaxAt(5, 15)

  " I don't know why this one is failing
  AssertEqual 'jinjaNested'      , SyntaxAt(6, 25)

Execute (Verify statements blocks):
  AssertEqual 'jinjaTagDelim'    , SyntaxAt(9,  1)
  AssertEqual 'jinjaTagDelim'    , SyntaxAt(9,  2)
  AssertEqual 'jinjaTagBlock'    , SyntaxAt(9,  3)
  AssertEqual 'jinjaStatement'   , SyntaxAt(9,  4)
+73 −0
Original line number Diff line number Diff line
-- Custom configuration for busted

-- If busted is not available this configuration is not running as part of a
-- test, so there is nothing to do.
local success, say = pcall(require, 'say')
if not success then
	return
end
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 = {}, {}, {}

---Add the Neovim client to the current test state.
local function nvim_client(state, args, _level)
	assert(args.n > 0, "No Neovim channel provided to the modifier")
	assert(rawget(state, NVIM_STATE_KEY) == nil, "Neovim already set")
	rawset(state, NVIM_STATE_KEY, args[1])
	return state
end

---Adds a (row, column) position pair (1-indexed) to the current test state.
local function at_position(state, args, _level)
	assert(args.n == 2, 'Wrong number of arguments given to modifier')
	rawset(state, ROW_STATE_KEY, args[1])
	rawset(state, COL_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)
	assert(args.n == 1, 'No highlight group provided')
	local nvim = rawget(state, NVIM_STATE_KEY)
	local row = rawget(state, ROW_STATE_KEY) - 1
	local column = rawget(state, COL_STATE_KEY) - 1
	local hlgroup = args[1]

	local syntax = nvim
		:exec_lua('return vim.inspect_pos(...)', {0, row, column, {syntax=true}})
		.syntax
	local result = vim.iter(syntax)
		:map(function(x) return x.hl_group end)
		:find(hlgroup)

	return result == hlgroup
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')
	local nvim = rawget(state, NVIM_STATE_KEY)
	local filetype = args[1]

	return filetype == nvim:get_option_value('filetype', {})
end

say:set('assertion.hlgroup_at.positive', 'Expected highlight group %s')
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')

assert:register('modifier', 'nvim', nvim_client)
assert:register('modifier', 'at_position', at_position)
assert:register(
	'assertion', 'has_hlgroup', has_hlgroup,
	'assertion.hlgroup_at.positive', 'assertion.hlgroup_at.negative'
)
assert:register(
	'assertion', 'has_filetype', has_filetype,
	'assertion.has_filetype.positive', 'assertion.has_filetype.negative'
)