1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
$:.unshift File.join(File.dirname(__FILE__),"..","lib")
require 'minitest/autorun'
require 'rgen/metamodel_builder'
require 'rgen/instantiator/qualified_name_resolver'
class QualifiedNameResolverTest < Minitest::Test
class TestNode < RGen::MetamodelBuilder::MMBase
has_attr 'name', String
has_one 'nextSibling', TestNode
contains_many 'children', TestNode, "parent"
end
class TestNode2 < RGen::MetamodelBuilder::MMBase
has_attr 'cname', String
has_one 'nextSibling', TestNode2
contains_many 'children', TestNode2, "parent"
end
class TestNode3 < RGen::MetamodelBuilder::MMBase
has_attr 'name', String
contains_one 'child', TestNode3, "parent"
end
def testModel
[TestNode.new(:name => "Root1", :children => [
TestNode.new(:name => "Sub11"),
TestNode.new(:name => "Sub12", :children => [
TestNode.new(:name => "Sub121")])]),
TestNode.new(:name => "Root2", :children => [
TestNode.new(:name => "Sub21", :children => [
TestNode.new(:name => "Sub211")])]),
TestNode.new(:name => "Root3"),
TestNode.new(:name => "Root3")
]
end
def testModel2
[TestNode2.new(:cname => "Root1", :children => [
TestNode2.new(:cname => "Sub11")])]
end
def testModel3
[TestNode3.new(:name => "Root1", :child =>
TestNode3.new(:name => "Sub11", :child =>
TestNode3.new(:name => "Sub111")))]
end
def test_customNameAttribute
model = testModel2
res = RGen::Instantiator::QualifiedNameResolver.new(model, :nameAttribute => "cname")
assert_equal model[0], res.resolveIdentifier("/Root1")
assert_equal model[0].children[0], res.resolveIdentifier("/Root1/Sub11")
end
def test_customSeparator
model = testModel
res = RGen::Instantiator::QualifiedNameResolver.new(model, :separator => "|")
assert_equal model[0], res.resolveIdentifier("|Root1")
assert_nil res.resolveIdentifier("/Root1")
assert_equal model[0].children[0], res.resolveIdentifier("|Root1|Sub11")
end
def test_noLeadingSeparator
model = testModel
res = RGen::Instantiator::QualifiedNameResolver.new(model, :leadingSeparator => false)
assert_equal model[0], res.resolveIdentifier("Root1")
assert_nil res.resolveIdentifier("/Root1")
assert_equal model[0].children[0], res.resolveIdentifier("Root1/Sub11")
end
def test_resolve
model = testModel
res = RGen::Instantiator::QualifiedNameResolver.new(model)
assert_equal model[0], res.resolveIdentifier("/Root1")
# again
assert_equal model[0], res.resolveIdentifier("/Root1")
assert_equal model[0].children[0], res.resolveIdentifier("/Root1/Sub11")
# again
assert_equal model[0].children[0], res.resolveIdentifier("/Root1/Sub11")
assert_equal model[0].children[1], res.resolveIdentifier("/Root1/Sub12")
assert_equal model[0].children[1].children[0], res.resolveIdentifier("/Root1/Sub12/Sub121")
assert_equal model[1], res.resolveIdentifier("/Root2")
assert_equal model[1].children[0], res.resolveIdentifier("/Root2/Sub21")
assert_equal model[1].children[0].children[0], res.resolveIdentifier("/Root2/Sub21/Sub211")
# duplicate name yields two result elements
assert_equal [model[2], model[3]], res.resolveIdentifier("/Root3")
assert_nil res.resolveIdentifier("/RootX")
assert_nil res.resolveIdentifier("/Root1/SubX")
end
def test_oneChild
model = testModel3
res = RGen::Instantiator::QualifiedNameResolver.new(model)
assert_equal model[0], res.resolveIdentifier("/Root1")
assert_equal model[0].child, res.resolveIdentifier("/Root1/Sub11")
assert_equal model[0].child.child, res.resolveIdentifier("/Root1/Sub11/Sub111")
end
end
|