summaryrefslogtreecommitdiff
path: root/spec/unit/pops/visitor_spec.rb
blob: c5858c2c2259e21c2f240cd16c1a77ea3fb89d02 (plain)
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
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/pops'

describe Puppet::Pops::Visitor do
  describe "A visitor and a visitable in a configuration with min and max args set to 0" do
    class DuckProcessor
      def initialize
        @friend_visitor = Puppet::Pops::Visitor.new(self, "friend", 0, 0)
      end

      def hi(o, *args)
        @friend_visitor.visit(o, *args)
      end

      def friend_Duck(o)
        "Hi #{o.class}"
      end

      def friend_Numeric(o)
        "Howdy #{o.class}"
      end
    end

    class Duck
      include Puppet::Pops::Visitable
    end

    it "should select the expected method when there are no arguments" do
      duck = Duck.new
      duck_processor = DuckProcessor.new
      duck_processor.hi(duck).should == "Hi Duck"
    end

    it "should fail if there are too many arguments" do
      duck = Duck.new
      duck_processor = DuckProcessor.new
      expect { duck_processor.hi(duck, "how are you?") }.to raise_error(/^Visitor Error: Too many.*/)
    end

    it "should select method for superclass" do
      duck_processor = DuckProcessor.new
      duck_processor.hi(42).should == "Howdy Fixnum"
    end

    it "should select method for superclass" do
      duck_processor = DuckProcessor.new
      duck_processor.hi(42.0).should == "Howdy Float"
    end

    it "should fail if class not handled" do
      duck_processor = DuckProcessor.new
      expect { duck_processor.hi("wassup?") }.to raise_error(/Visitor Error: the configured.*/)
    end
  end

  describe "A visitor and a visitable in a configuration with min =1, and max args set to 2" do
    class DuckProcessor2
      def initialize
        @friend_visitor = Puppet::Pops::Visitor.new(self, "friend", 1, 2)
      end

      def hi(o, *args)
        @friend_visitor.visit(o, *args)
      end

      def friend_Duck(o, drink, eat="grain")
        "Hi #{o.class}, drink=#{drink}, eat=#{eat}"
      end
    end

    class Duck
      include Puppet::Pops::Visitable
    end

    it "should select the expected method when there are is one arguments" do
      duck = Duck.new
      duck_processor = DuckProcessor2.new
      duck_processor.hi(duck, "water").should == "Hi Duck, drink=water, eat=grain"
    end

    it "should fail if there are too many arguments" do
      duck = Duck.new
      duck_processor = DuckProcessor2.new
      expect { duck_processor.hi(duck, "scotch", "soda", "peanuts") }.to raise_error(/^Visitor Error: Too many.*/)
    end

    it "should fail if there are too few arguments" do
      duck = Duck.new
      duck_processor = DuckProcessor2.new
      expect { duck_processor.hi(duck) }.to raise_error(/^Visitor Error: Too few.*/)
    end
  end
end