Class: PatternMatcherTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/puppet/vendor/rgen/test/util/pattern_matcher_test.rb

Defined Under Namespace

Modules: TestMM

Instance Method Summary collapse

Instance Method Details

#modelAObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/vendor/rgen/test/util/pattern_matcher_test.rb', line 20

def modelA 
  env = RGen::Environment.new
  RGen::ModelBuilder.build(TestMM, env) do 
    node "A" do
      node "AA"
    end
    node "B" do
      node "B1"
      node "B2"
      node "B3"
    end
    node "C" do
      node "C1"
      node "C2"
    end
    node "D" do
      node "DD"
    end
  end
  env
end

#test_simpleObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/vendor/rgen/test/util/pattern_matcher_test.rb', line 42

def test_simple
  matcher = RGen::Util::PatternMatcher.new
  matcher.add_pattern("simple") do |env, c|
    TestMM::Node.new(:name => "A", :children => [
      TestMM::Node.new(:name => "AA")])
  end
  matcher.add_pattern("bad") do |env, c|
    TestMM::Node.new(:name => "X")
  end
  env = modelA

  match = matcher.find_pattern(env, "simple")
  assert_not_nil match
  assert_equal "A", match.root.name
  assert_equal env.find(:class => TestMM::Node, :name => "A").first.object_id, match.root.object_id
  assert_equal 2, match.elements.size
  assert_equal [nil], match.bound_values

  assert_nil matcher.find_pattern(env, "bad")
end

#test_value_bindingObject



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
# File 'lib/puppet/vendor/rgen/test/util/pattern_matcher_test.rb', line 63

def test_value_binding
  matcher = RGen::Util::PatternMatcher.new
  matcher.add_pattern("single_child") do |env, name, child|
    TestMM::Node.new(:name => name, :children => [ child ])
  end
  matcher.add_pattern("double_child") do |env, name, child1, child2|
    TestMM::Node.new(:name => name, :children => [ child1, child2 ])
  end
  matcher.add_pattern("child_pattern") do |env, child_name|
    TestMM::Node.new(:name => "A", :children => [
      TestMM::Node.new(:name => child_name)])
  end
  env = modelA

  match = matcher.find_pattern(env, "single_child")
  assert_not_nil match
  assert_equal "A", match.root.name
  assert_equal "AA", match.bound_values[1].name

  match = matcher.find_pattern(env, "single_child", "D")
  assert_not_nil match
  assert_equal "D", match.root.name
  assert_equal "DD", match.bound_values[0].name

  match = matcher.find_pattern(env, "double_child")
  assert_not_nil match
  assert_equal "C", match.root.name

  match = matcher.find_pattern(env, "child_pattern")
  assert_not_nil match
  assert_equal ["AA"], match.bound_values
end