Module:PropertyPath/testcases

Documentation for this module may be created at Module:PropertyPath/testcases/doc

local p = require('Module:UnitTests')
local wdpath = require ('Module:PropertyPath')
local parser = require ('Module:PropertyPath/parser')
local path = require ('Module:PropertyPath/Path')


local function gather_items(start_point, path)
	
end

function p:some_parse_test(name, to_parse, expected, custom_parser)
	local parser_to_use = custom_parser or parser.pathFirstAlternative
	local plop=parser.parse(to_parse, parser_to_use)
	local escaped = to_parse:gsub("|","!")
    self:equals_deep(name .. ' Parser «' .. escaped .. '» operator ', plop, expected)
end

function p:test_parser()

	
	local instance_node = path.PropertyNode:create("P31")
	local subclass_node = path.PropertyNode:create("P279")
	local subclass_q_node = path.QualifierSnakNode:create(subclass_node)
	local qualified_instance = path.QualifiedStatementNode:create(instance_node, subclass_q_node)
	local part_of_node = path.PropertyNode:create("part of")
	local four_part_of = path.SequenceNode:create({part_of_node, part_of_node, part_of_node, part_of_node})
	 --local alternative_qualifiers = path.QualifiedStatementNode:create(instance_node, path.QualifierSnakNode(path.PropertySetNode:create({subclass_node, instance_node})))
	-- local 
	
	local instances = path.SequenceNode:create(
		{
			path.PropertyNode:create("P31"),
			path.StarNode:create(path.PropertyNode:create("P279"))
		}
	)
	
	self:some_parse_test("Parser Property by Pid (elem)",  "P31", instance_node, parser.pathElt)
	self:some_parse_test("Parser Property by Pid (full)",  "P31", instance_node)
	
	self:some_parse_test("Parser Property by Pid with trailing spaces","P31   ", instance_node)
    
    self:some_parse_test("Parser Property by name (pathElt)", "instance of", instance_node, parser.pathElt)
    
	self:some_parse_test("Parser Property qualif (pathElt)", "P31>P279", qualified_instance, parser.pathElt)
	self:some_parse_test("Parser Property qualif", "P31>P279", qualified_instance)


	self:some_parse_test("Parser alternative qualifier (Elt)", "P31>(P279|P31)", alternative_qualifiers, parser.pathElt)
	
	self:some_parse_test("Parser « a » operator with leading and trailing spaces", "  a ", instances)
    self:some_parse_test("Parser « a » operator with trailing space", "a ", instances, parser.pathElt)
    
    self:some_parse_test("", "P31/(P279*)", instances)
    
    self:some_parse_test("", "P31/P279*", instances)
    
    self:some_parse_test("iterated part of", "part of*", path.StarNode:create(part_of_node))
    
    self:some_parse_test("4 part of", "part of/part of/part of/part of", four_part_of)
	
end

local function flatten(iterator)
	local ta = {}
	for x in iterator do
		table.insert(ta, x:item_value())
	end
	table.sort(ta)
	return ta
end
function p:all_in(name, query, start, results)
	table.sort(results)
	self:equals_deep(name .. "«".. query.. "»", flatten(wdpath.iterate(start, query)), results)
end

function p:test_query()
	self:all_in("{{Q|Q4328}} instance of person"  ,"P31" ,"Q4328", {"Q5"})	
	self:all_in("person replaced by {{Q|Q4328}}"  ,"P39>P1365" ,"Q4328", {"Q58685920", "Q58684914"})
	
end

return p