public
Description: Various quizes and tricks that we use in our WNYRuby.com group for mutual self-education
Homepage: http://wnyruby.com/
Clone URL: git://github.com/kcbaird/wnyruby.com-homework.git
wnyruby.com-homework / for_2008_07_08 / rev_moon_spec.rb
100644 75 lines (67 sloc) 2.252 kb
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
#!/usr/bin/env ruby
# rev_moon_spec.rb
 
if (__FILE__ == $0)
  require %q[rubygems]
  require %q[spec]
  require %q[rev_moon]
 
  describe RevMoon do
    
    before(:each) do
      @rev = RevMoon.new.clear!
    end
 
    describe %q[RevMoon#clear!] do
      it %q[should clear the matches] do
        @rev.unify( 1, 1 ).should == { 1 => 1 }
        @rev.clear!
        @rev.unify( 2, 2 ).should == { 2 => 2 }
      end
    end
 
    describe %q[RevMoon#unify] do
      it %q[should make a simple match] do
        sample = [ :name, :first, :last ]
        data = [ :name, %q[Mark], %q[Josef] ]
        expected_match = { :first => %q[Mark], :last => %q[Josef] }
        @rev.unify(sample, data).should == expected_match
      end
      it %q[should make a compound match] do
        @rev.clear!
        sample = :shape
        data = [ :circle, :radius ]
        expected_match = { :shape => data }
        @rev.unify(sample, data).should == expected_match
      end
      describe %q[it should fail to make an impossible match due to] do
        it %q[an atom needing to have two values] do
          sample = [ :x, :y, :y ]
          data = [ 1, 2, 3 ]
          @rev.unify(sample, data).should be_nil
        end
        it %q[differing literal atoms] do
          sample = [ :x, :y, 4 ]
          data = [ 1, 2, 3 ]
          @rev.unify(sample, data).should be_nil
        end
        it %q[differing literal atoms2] do
          sample = [ :x, :y, %q[z] ]
          data = [ 1, 2, 3 ]
          @rev.unify(sample, data).should be_nil
        end
      end
      describe %q[should raise a ReassignmentError on an attempted reassignment] do
        it %q[simple] do
          sample = [ :name, :first, :last ]
          data = [ :name, %q[Mark], %q[Josef] ]
          data2 = [ :name, %q[Kevin], %q[Baird] ]
          @rev.unify(sample, data)
          lambda { @rev.unify(sample, data2) }.should raise_error(ReassignmentError)
        end
        it %q[compound] do
          sample = :shape
          data = [ :circle, :radius ]
          data2 = [ :square, :length ]
          @rev.unify(sample, data)
          lambda { @rev.unify(sample, data2) }.should raise_error(ReassignmentError)
        end
      end
    end
  
  end
 
end