Модуль:Песочница/D6194c-1cc/WDFormatV2/testcases

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация

✔ Все тесты пройдены.

Название Ожидается Фактически
✔ test_format_array
✔ test_format_arrayCapitalize
✔ test_format_arrayForceCapitalize
✔ test_format_capitalize
✔ test_format_conflicts
✔ test_format_conflicts_recursive
✔ test_format_date
✔ test_format_delimiter_function
✔ test_format_depends
✔ test_format_depends_fieldPath
✔ test_format_depends_recursive
✔ test_format_ensureEndsAndDelimiter
✔ test_format_fieldPath
✔ test_format_group_childDelimiter_and_childEnsureEnds
✔ test_format_group_passthrough
✔ test_format_group_prefix_with_delimiter
✔ test_format_group_prefix_with_delimiter_inside_another_group
✔ test_format_groups_delimiter
✔ test_format_innerTags
✔ test_format_isStatic_hidden
✔ test_format_isStatic_hidden_in_array
✔ test_format_isStatic_hidden_in_array_in_additional_group
✔ test_format_isStatic_visible
✔ test_format_link
✔ test_format_linkOrder
✔ test_format_nested_array
✔ test_format_oneField
✔ test_format_person
✔ test_format_person_multipleNames
✔ test_format_prefixAndSuffix
✔ test_format_recurseGroups
✔ test_format_rootTag
✔ test_format_squareBrackets
✔ test_format_tableTag
✔ test_format_wikilink
✔ test_format_wikisource
local suite = require('Модуль:ScribuntoUnit'):new()
local formatter = require('Модуль:Песочница/D6194c-1cc/WDFormatV2')
local f = formatter.f

function suite:test_format_oneField()
	local profile = {
		{
			{
				field = 'name',
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		name = { value = 'Some name' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>Some name</span>', wikitext)
end

function suite:test_format_ensureEndsAndDelimiter()
	local profile = {
		groups = {
			{
				{
					field = 'title',
				},
				{
					ensureEnds = ':',
					delimiter = ' ',
					field = 'subtitle',
				},
			},
		},
		ensureEnds = '.',
		tag = {
			name = 'span',
		},
	}
	local source = {
		title = { value = 'some name' },
		subtitle = { value = 'some other text' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>Some name: some other text.</span>', wikitext)
end

function suite:test_format_delimiter_function()
	local profile = {
		{
			groups = {
				{
					field = 'title',
				},
				{
					delimiter = function()
						return ': '
					end,
					field = 'subtitle',
				},
			},
		},
		ensureEnds = '.',
		tag = {
			name = 'span',
		},
	}
	local source = {
		title = { value = 'some name' },
		subtitle = { value = 'some other text' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>Some name: some other text.</span>', wikitext)
end

function suite:test_format_capitalize()
	local profile = {
		{
			{
				field = 'title',
				capitalize = false,
			},
			{
				ensureEnds = '.',
				delimiter = ' ',
				field = 'subtitle',
				capitalize = true,
			},
		},
		ensureEnds = '.',
		tag = {
			name = 'span',
		},
	}
	local source = {
		title = { value = 'some name' },
		subtitle = { value = 'some other text' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>some name. Some other text.</span>', wikitext)
end

function suite:test_format_wikilink()
	local profile = {
		{
			{
				field = 'title',
				format = { f.wikilink },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		title = { entity = 'Q9377' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>[[Почка]]</span>', wikitext)
end

function suite:test_format_link()
	local profile = {
		{
			{
				field = 'title',
				urlField = 'url',
				format = { f.link },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		title = { value = 'Example' },
		url = { value = 'http://example.com' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>[http://example.com Example]</span>', wikitext)
end

function suite:test_format_wikisource()
	local profile = {
		{
			{
				field = 'title',
				urlField = 'url',
				format = { f.wikisource },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		title = { entity = 'Q19147272' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>[[s:ГОСТ Р 7.0.5—2008|ГОСТ Р 7.0.5—2008]]</span>', wikitext)
end

function suite:test_format_linkOrder()
	local profile = {
		{
			{
				field = 'title',
				wikilink = 'ГОСТ',
				urlField = 'url',
				format = { f.wikisource, f.wikilink, f.link },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		title = { entity = 'Q19147272' },
		url = { value = 'http://example.com' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>[[s:ГОСТ Р 7.0.5—2008|ГОСТ Р 7.0.5—2008]]</span>', wikitext)
end

function suite:test_format_person()
	local profile = {
		{
			{
				field = 'author1',
				format = { f.person },
			},
			{
				delimiter = '; ',
				field = 'author1',
				format = { f.personReversed },
			},
			{
				delimiter = '; ',
				field = 'author2',
				format = { f.person },
			},
			{
				delimiter = '; ',
				field = 'author2',
				format = { f.personReversedNoComma },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		author1 = {
			value = 'Name Patronym Family',
			components = {
				givenName = { value = 'Name' },
				familyName = { value = 'Family' },
				ancestorName = { value = 'Patronym' },
			},
		},
		author2 = {
			value = 'Name Family',
			components = {
				givenName = { value = 'Name' },
				familyName = { value = 'Family' },
			},
		},
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>N.&nbsp;P.&nbsp;Family; Family,&nbsp;N.&nbsp;P.; N.&nbsp;Family; Family&nbsp;N.</span>', wikitext)
end

function suite:test_format_person_multipleNames()
	local profile = {
		{
			{
				field = 'author',
				format = { f.person },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		author = {
			value = 'FirstName SecondName ThirdName Patronym Family',
			components = {
				givenName = { { value = 'FirstName' }, { value = 'SecondName' }, { value = 'ThirdName' } },
				familyName = { value = 'Family' },
				ancestorName = { value = 'Patronym' },
			},
		},
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>F.&nbsp;S.&nbsp;T.&nbsp;P.&nbsp;Family</span>', wikitext)
end

function suite:test_format_rootTag()
	local profile = {
		{
			{
				field = 'title',
			},
		},
		tag = {
			name = 'span',
			classes = {
				'class1',
				'class2',
			},
			attr = {
				id = 'test',
				style = 'font-weight:bold;',
			},
		},
	}
	local source = {
		title = { value = 'Text' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span class="class1 class2" id="test" style="font-weight:bold;">Text</span>', wikitext)
end

function suite:test_format_tableTag()
	local profileGroup = {
		tag = {
			name = 'tr',
		},
		{
			tag = {
				name = 'th',
			},
			field = 'title',
		},
		{
			tag = {
				name = 'td',
			},
			field = 'value',
			capitalize = true,
		},
	}

	local profile = {
		tag = {
			name = 'table',
			classes = { 'infobox' },
		},
		profileGroup,
		profileGroup,
	}
	local source = {
		title = { value = 'caption' },
		value = { value = 'text' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<table class="infobox"><tr><th>Caption</th><td>Text</td></tr><tr><th>Caption</th><td>Text</td></tr></table>', wikitext)
end

function suite:test_format_innerTags()
	local profileGroup = {
		tag = {
			name = 'tbody',
			attr = { style='background-color: #ddd;' },
			tag = {
				name = 'tr',
			},
		},
		{
			tag = {
				name = 'th',
			},
			field = 'title',
		},
		{
			tag = {
				name = 'td',
				tag = {
					name = 'div',
					attr = { style='font-size: 10pt;' }
				},
			},
			field = 'value',
			capitalize = true,
		},
	}

	local profile = {
		tag = {
			name = 'div',
			tag = {
				name = 'table',
				classes = { 'infobox' },
			},
		},
		profileGroup,
	}
	local source = {
		title = { value = 'caption' },
		value = { value = 'text' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<div><table class="infobox"><tbody style="background-color: #ddd;"><tr><th>Caption</th><td><div style="font-size: 10pt;">Text</div></td></tr></tbody></table></div>', wikitext)
end

function suite:test_format_recurseGroups()
	local profileGroup = {
		tag = {
			name = 'tr',
		},
		{
			tag = {
				name = 'th',
			},
			field = 'title',
		},
		{
			tag = {
				name = 'td',
				tag = {
					name = 'ul'
				},
			},
			{
				tag = {
					name = 'li',
				},
				{
					tag = { name = 'b' },
					field = 'name1',
					capitalize = false,
				},
				{
					ensureEnds = ':',
					delimiter = ' ',
					field = 'value1',
				},
			},
			{
				tag = {
					name = 'li',
				},
				{
					tag = { name = 'b' },
					field = 'name2',
					capitalize = false,
				},
				{
					ensureEnds = ':',
					delimiter = ' ',
					field = 'value2',
				},
			},
		},
	}

	local profile = {
		tag = {
			name = 'table',
			classes = { 'infobox' },
		},
		profileGroup,
	}
	local source = {
		title = { value = 'caption' },
		name1 = { value = 'title1' },
		value1 = { value = 'text1' },
		name2 = { value = 'title2' },
		value2 = { value = 'text2' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<table class="infobox"><tr><th>Caption</th><td><ul><li><b>title1</b>: text1</li><li><b>title2</b>: text2</li></ul></td></tr></table>', wikitext)
end

function suite:test_format_array()
	local profile = {
		tag = {
			name = 'span',
		},
		{
			{
				field = 'title',
				itemsDelimiter = '/',
			},
			{
				delimiter = '; ',
				field = { 'title', 2 },
			},
		},
	}
	local source = {
		title = { { value = 1 }, { value = 2 }, { value = 3 } },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>1/2/3; 2</span>', wikitext)
end

function suite:test_format_date()
	local profile = {
		tag = {
			name = 'span',
		},
		{
			{
				field = { 'date', sub='year' },
			},
			{
				delimiter = '/',
				field = { 'date', sub='month' },
			},
			{
				delimiter = '/',
				field = { 'date', sub='day' },
			},
		},
		{
			delimiter = '; ',
			{
				field = { 'date' },
				format = { f.date },
			},
		},
	}
	local source = {
		date = { value = { year=2022, month=12, day=4, timestamp='2022-12-04' } },
	}
	local wikitext = formatter.format(profile, source, 'en')

	self:assertEquals('<span>2022/12/4; 4 December 2022</span>', wikitext)
end

function suite:test_format_groups_delimiter()
	local profile = {
		tag = {
			name = 'span',
		},
		{
			{
				value = '1',
			},
		},
		{
			delimiter = '; ',
			{
				value = '2',
			},
		},
	}
	local wikitext = formatter.format(profile, {})

	self:assertEquals('<span>1; 2</span>', wikitext)
end

function suite:test_format_prefixAndSuffix()
	local profile = {
		tag = {
			name = 'table',
		},
		{
			prefix = '<tr>',
			{
				prefix = '<td>',
				field = { 'title' },
				suffix = '</td>',
			},
			suffix = '</tr>',
		},
	}
	local source = {
		title = { value = 'Do not ever do like this! It could break ensureEnds! Use tag instead.' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<table><tr><td>Do not ever do like this! It could break ensureEnds! Use tag instead.</td></tr></table>', wikitext)
end

function suite:test_format_arrayCapitalize()
	local profile = {
		tag = {
			name = 'span',
		},
		{
			{
				field = 'title',
				itemsDelimiter = ', ',
			},
		},
	}
	local source = {
		title = { { value='first' }, { value='second' }, { value='third' } },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>First, second, third</span>', wikitext)
end

function suite:test_format_arrayForceCapitalize()
	local profile = {
		tag = {
			name = 'span',
		},
		{
			{
				field = 'empty',
			},
			{
				field = 'title',
				itemsDelimiter = ', ',
				capitalize = true,
			},
		},
	}
	local source = {
		empty = { value = '' },
		title = { { value='first' }, { value='second' }, { value='third' } },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>First, second, third</span>', wikitext)
end

function suite:test_format_squareBrackets()
	local profile = {
		{
			{
				field = 'name',
				format = { f.squareBrackets },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		name = { value = 'Some name' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>&lsqb;Some name&rsqb;</span>', wikitext)
end

function suite:test_format_conflicts()
	local profile = {
		{
			childDelimiter = ', ',
			{
				field = 'conflTrue1',
				conflicts = { 'n1', 'NoN1', 'NoN2' },
			},
			{
				field = 'conflTrue2',
				conflicts = { 'n1', { 'n3', 'noN2', 'noN3' }, 'noN1' },
			},
			{
				field = 'conflFalse1',
				conflicts = { 'noN1', 'noN2', 'noN3' },
				capitalize = false,
			},
			{
				field = 'conflFalse2',
				conflicts = { 'noN1', { 'n1', 'noN' }, 'noN2' },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		n1 = { value = 'n1' },
		n2 = { value = 'n2' },
		n3 = { value = 'n3' },
		n4 = { value = 'n4' },
		n5 = { value = 'n5' },
		conflTrue1 = { value = 'conflTrue1' },
		conflTrue2 = { value = 'conflTrue2' },
		conflFalse1 = { value = 'conflFalse1' },
		conflFalse2 = { value = 'conflFalse2' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>conflFalse1, conflFalse2</span>', wikitext)
end

function suite:test_format_depends()
	local profile = {
		{
			childDelimiter = ', ',
			{
				field = 'depTrue1',
				depends = { 'n1', 'n3', 'n2' },
				capitalize = false,
			},
			{
				field = 'depTrue2',
				depends = { 'n1', { 'n2', 'noN', 'noOtherN' }, 'n2' },
			},
			{
				field = 'depFalse1',
				depends = { 'n1', 'noN', 'n2' },
			},
			{
				field = 'depFalse2',
				depends = { 'n1', { 'noN', 'noOtherN' }, 'n2' },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		n1 = { value = 'n1' },
		n2 = { value = 'n2' },
		n3 = { value = 'n3' },
		n4 = { value = 'n4' },
		n5 = { value = 'n5' },
		depTrue1 = { value = 'depTrue1' },
		depTrue2 = { value = 'depTrue2' },
		depFalse1 = { value = 'depFalse1' },
		depFalse2 = { value = 'depFalse2' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>depTrue1, depTrue2</span>', wikitext)
end

function suite:test_format_depends_recursive()
	local profile = {
		{
			childDelimiter = ', ',
			{
				field = 'depTrue',
				depends = { 'n1', { { 'n3', 'n4' }, 'noN', 'noOtherN' }, 'n2' },
				capitalize = false,
			},
			{
				field = 'depFalse',
				depends = { 'n1', { { 'n3', 'noN' }, 'noN', 'noOtherN' }, 'n2' },
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		n1 = { value = 'n1' },
		n2 = { value = 'n2' },
		n3 = { value = 'n3' },
		n4 = { value = 'n4' },
		n5 = { value = 'n5' },
		depTrue = { value = 'depTrue' },
		depFalse = { value = 'depFalse' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>depTrue</span>', wikitext)
end

function suite:test_format_conflicts_recursive()
	local profile = {
		{
			delimiter = ', ',
			{
				field = 'conflTrue',
				conflicts = { 'noN1', { { 'n3', 'noN3' }, 'n1', 'n2' }, 'noN2' },
			},
			{
				field = 'conflFalse',
				conflicts = { 'noN1', { { 'noN3', 'noN4' }, 'n1', 'n2' }, 'noN2' },
				capitalize = false,
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		n1 = { value = 'n1' },
		n2 = { value = 'n2' },
		n3 = { value = 'n3' },
		n4 = { value = 'n4' },
		n5 = { value = 'n5' },
		conflTrue = { value = 'conflTrue' },
		conflFalse = { value = 'conflFalse' },
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>conflFalse</span>', wikitext)
end

function suite:test_format_isStatic_visible()
	local profile = {
		{
			childDelimiter = ', ',
			{
				isStatic = true,
				{
					value = 'static',
					capitalize = false,
				},
			},
			{
				value = 'non-static',
				capitalize = false,
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>static, non-static</span>', wikitext)
end

function suite:test_format_isStatic_hidden()
	local profile = {
		{
			delimiter = ', ',
			{
				isStatic = true,
				{
					value = 'static',
					capitalize = false,
				},
			},
			{
				value = nil,
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('', wikitext)
end

function suite:test_format_isStatic_hidden_in_array()
	local profile = {
		{
			isStatic = true,
			{
				value = 'title',
				capitalize = false,
			},
		},
		{
			delimiter = ' : ',
			field = 'name',
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		{
		},
		{
			name = { value = 't2' },
		},
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>title : t2</span>', wikitext)
end

function suite:test_format_isStatic_hidden_in_array_in_additional_group()
	local profile = {
		{
			childDelimiter = ' : ',
			{
				isStatic = true,
				{
					value = 'title',
					capitalize = false,
				},
			},
			{
				field = 'name',
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		{
		},
		{
			name = { value = 't2' },
		},
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>title : t2</span>', wikitext)
end

function suite:test_format_nested_array()
	local profile = {
		{
			childDelimiter = ', ',
			field = 'tests',
			{
				field = 'title',
				capitalize = false,
			},
			{
				delimiter = ': ',
				field = 'value',
				capitalize = false,
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		tests = {
			{
				title = { value = 't1' },
				value = { value = '1' },
			},
			{
				title = { value = 't2' },
				value = { value = '2' },
			},
		},
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>t1: 1, t2: 2</span>', wikitext)
end

function suite:test_format_group_prefix_with_delimiter()
	local profile = {
		{
			value = 'A',
		},
		{
			ensureEnds = '.',
			delimiter = ' — ',
			prefix = '(',
			{
				value = 'B',
			},
			{
				delimiter = ', ',
				value = 'C',
			},
			suffix = ')',
		},
		tag = {
			name = 'span',
		},
	}
	local source = {}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>A. — (B, C)</span>', wikitext)
end

function suite:test_format_group_prefix_with_delimiter_inside_another_group()
	local profile = {
		{
			value = 'A',
		},
		{
			{
				ensureEnds = '.',
				delimiter = ' — ',
				prefix = '(',
				{
					value = 'B',
				},
				{
					delimiter = ', ',
					value = 'C',
				},
				suffix = ')',
			},
		},
		tag = {
			name = 'span',
		},
	}
	local source = {}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>A. — (B, C)</span>', wikitext)
end

function suite:test_format_group_childDelimiter_and_childEnsureEnds()
	local profile = {
		{
			value = 'A',
		},
		{
			ensureEnds = '.',
			delimiter = ' — ',
			childEnsureEnds = '.',
			childDelimiter = ' ',
			prefix = '(',
			{
				value = 'B',
			},
			{
				value = 'C',
			},
			{
				value = 'D',
			},
			suffix = ')',
		},
		tag = {
			name = 'span',
		},
	}
	local source = {}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>A. — (B. C. D)</span>', wikitext)
end

function suite:test_format_group_passthrough()
	local profile = {
		{
			value = 'a',
		},
		{
			passthrough = true,
			{
				value = 'b',
			},
			{
				value = 'c',
			},
			{
				value = 'd',
			},
		},
		{
			value = 'e',
		},
		tag = {
			name = 'span',
		},
	}
	local source = {}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>Abcde</span>', wikitext)
end

function suite:test_format_depends_fieldPath()
	local profile = {
		{
			depends = { isPath=true, 'f1', 'sub2' },
			field = 'f1',
		},
		{
			depends = { isPath=true, 'f1', 'sub3' },
			field = 'f1',
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		f1 = {
			value = 'Ok',
			components = {
				sub1 = {
					value = 'Ok',
				},
				sub2 = {
					value = 'Not ok',
				},
			},
		},
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>Ok</span>', wikitext)
end

function suite:test_format_fieldPath()
	local profile = {
		{
			field = { isPath=true, 'parent', 'child1' },
		},
		{
			field = { isPath=true, 'parent', 'child2' },
		},
		tag = {
			name = 'span',
		},
	}
	local source = {
		parent = {
			value = 'Missed',
			components = {
				child1 = {
					value = 'Ok',
				},
			},
		},
	}
	local wikitext = formatter.format(profile, source)

	self:assertEquals('<span>Ok</span>', wikitext)
end

return suite