-- -- Tests for the doclayout module -- local doclayout = require 'doclayout' local tasty = require 'tasty' local group = tasty.test_group local test = tasty.test_case local assert = tasty.assert -- Check existence static fields return { group 'constructors' { test('empty', function () assert.are_equal(type(doclayout.empty), 'userdata') end), test('blankline', function () assert.are_equal(type(doclayout.blankline), 'userdata') end), test('cr', function () assert.are_equal(type(doclayout.cr), 'userdata') end), test('space', function () assert.are_equal(type(doclayout.space), 'userdata') end), group 'concat' { test('without sep', function () local list = { doclayout.literal 'one', doclayout.literal 'two', doclayout.literal 'three' } local sep = doclayout.cr assert.are_equal( doclayout.concat(list, sep), list[1] .. sep .. list[2] .. sep .. list[3]) end), test('without sep', function () local list = { doclayout.literal 'one', doclayout.literal 'two', doclayout.literal 'three' } assert.are_equal(doclayout.concat(list), list[1] .. list[2] .. list[3]) end), } }, group 'render' { test('empty doc', function () assert.are_equal(doclayout.render(doclayout.empty), '') end) }, group 'document querying' { test('is_empty', function () assert.is_truthy(doclayout.is_empty(doclayout.empty)) assert.is_truthy(doclayout.is_empty('')) assert.is_falsy(doclayout.is_empty('non-empty')) end), test('height', function () assert.are_equal(doclayout.height(doclayout.empty), 1) assert.are_equal(doclayout.height('line'), 1) assert.are_equal(doclayout.height(doclayout.literal 'p' / 'q'), 2) assert.are_equal(doclayout.height(doclayout.literal 'p' // 'q'), 3) end), test('min_offset', function () assert.are_equal(doclayout.min_offset 'four', 4) assert.are_equal( doclayout.min_offset(doclayout.literal 'four' + 'radio'), 5 ) end), test('offset', function () assert.are_equal(doclayout.offset 'four', 4) assert.are_equal(doclayout.offset(doclayout.literal 'four' / 'radio'), 5) end), test('real_length', function () assert.are_equal(doclayout.real_length(''), 0) assert.are_equal(doclayout.real_length('a'), 1) assert.are_equal(doclayout.real_length('❄'), 1) assert.are_equal(doclayout.real_length('シ'), 2) end), }, group 'Doc type' { test('empty strings equal the empty Doc', function () assert.are_equal(doclayout.empty .. '', doclayout.empty) end), test('strings can be used as Doc values', function () assert.are_equal(doclayout.render('hello world'), 'hello world') end), test('numbers can be used as Doc values', function () assert.are_equal(doclayout.render(42), '42') end), test('equality', function () assert.is_truthy(doclayout.literal "true", doclayout.literal "true") end), test('concatenate docs', function () assert.are_equal( tostring(doclayout.literal 'Rock-' .. doclayout.literal 'Ola'), 'Rock-Ola' ) end), test('has tostring method', function () local str = 'just a literal string for now' assert.are_equal(tostring(str), str) end), test('adding concatenates with space', function () local helloworld = doclayout.literal 'Hello,' .. doclayout.space .. doclayout.literal 'World' assert.are_equal(doclayout.literal 'Hello,' + 'World', helloworld) end), test('dividing sets the first above the second', function () local first = doclayout.literal 'first' local second = doclayout.literal 'second' assert.are_equal(first / second, first .. doclayout.cr .. second) end), test('// separates docs with blank line', function () local first = doclayout.literal 'first' local second = doclayout.literal 'second' assert.are_equal(first // second, first .. doclayout.blankline .. second) end), } }