jordi-xml_struct 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ require 'test/spec'
2
+
3
+ require 'test/spec/dox'
4
+
5
+ context "SpecDox" do
6
+ setup do
7
+ r = Test::Unit::UI::SpecDox::TestRunner.new(nil)
8
+ @unmangler = r.method(:unmangle)
9
+ end
10
+
11
+ specify "can unmangle Test::Unit names correctly" do
12
+ @unmangler["test_foo_bar(TestFoo)"].should.equal ["Foo", "foo bar"]
13
+ @unmangler["test_foo_bar(FooTest)"].should.equal ["Foo", "foo bar"]
14
+ @unmangler["test_he_he(Foo)"].should.equal ["Foo", "he he"]
15
+ @unmangler["test_heh(Foo)"].should.equal ["Foo", "heh"]
16
+
17
+ @unmangler["test_heh(Test::Unit::TC_Assertions)"].
18
+ should.equal ["Test::Unit::TC_Assertions", "heh"]
19
+
20
+ @unmangler["test_heh(Foo::Bar::Test)"].
21
+ should.equal ["Foo::Bar::Test", "heh"]
22
+ end
23
+
24
+ specify "can unmangle Test::Spec names correctly" do
25
+ @unmangler["test_spec {context} 007 [whee]()"].
26
+ should.equal ["context", "whee"]
27
+ @unmangler["test_spec {a bit longish context} 069 [and more text]()"].
28
+ should.equal ["a bit longish context", "and more text"]
29
+ @unmangler["test_spec {special chars !\"/&%$} 2 [special chars !\"/&%$]()"].
30
+ should.equal ["special chars !\"/&%$", "special chars !\"/&%$"]
31
+ @unmangler["test_spec {[]} 666666 [{}]()"].
32
+ should.equal ["[]", "{}"]
33
+ end
34
+
35
+ specify "has sensible fallbacks" do
36
+ @unmangler["weird"].should.equal [nil, nil]
37
+ end
38
+ end
39
+
@@ -0,0 +1,209 @@
1
+ # Adapted from flexmock (http://onestepback.org/software/flexmock).
2
+ #
3
+ # Copyright 2006 by Jim Weirich (jweirich@one.net).
4
+ # All rights reserved.
5
+
6
+ # Permission is granted for use, copying, modification, distribution,
7
+ # and distribution of modified versions of this work as long as the
8
+ # above copyright notice is included.
9
+
10
+
11
+ require 'test/spec'
12
+
13
+ begin
14
+ require 'flexmock'
15
+ rescue LoadError
16
+ context "flexmock" do
17
+ specify "can not be found. BAIL OUT!" do
18
+ end
19
+ end
20
+ else
21
+
22
+ context "flexmock" do
23
+ include FlexMock::TestCase
24
+
25
+ setup do
26
+ @mock = FlexMock.new
27
+ end
28
+
29
+ specify "should receive and return" do
30
+ args = nil
31
+ @mock.should_receive(:hi).and_return { |a, b| args = [a,b] }
32
+ @mock.hi(1,2)
33
+ args.should.equal [1,2]
34
+ end
35
+
36
+ specify "should receive without a block" do
37
+ lambda {
38
+ @mock.should_receive(:blip)
39
+ @mock.blip
40
+ }.should.not.raise
41
+ end
42
+
43
+ specify "should receive and return with a block" do
44
+ called = false
45
+ @mock.should_receive(:blip).and_return { |block| block.call }
46
+ @mock.blip { called = true }
47
+ called.should.be true
48
+ end
49
+
50
+ specify "should have a return value" do
51
+ @mock.should_receive(:blip).and_return { 10 }
52
+ @mock.blip.should.equal 10
53
+ end
54
+
55
+ specify "should handle missing methods" do
56
+ ex = lambda {
57
+ @mock.not_defined
58
+ }.should.raise(NoMethodError)
59
+ ex.message.should.match(/not_defined/)
60
+ end
61
+
62
+ specify "should ignore missing methods" do
63
+ lambda {
64
+ @mock.should_ignore_missing
65
+ @mock.blip
66
+ }.should.not.raise
67
+ end
68
+
69
+ specify "should count correctly" do
70
+ @mock.should_receive(:blip).times(3)
71
+ @mock.blip
72
+ @mock.blip
73
+ @mock.blip
74
+ lambda { @mock.flexmock_verify }.should.not.raise Test::Unit::AssertionFailedError
75
+ end
76
+
77
+ specify "should raise on bad counts" do
78
+ @mock.should_receive(:blip).times(3)
79
+ @mock.blip
80
+ @mock.blip
81
+ lambda { @mock.flexmock_verify }.should.raise Test::Unit::AssertionFailedError
82
+ end
83
+
84
+ specify "should handle undetermined counts" do
85
+ lambda {
86
+ FlexMock.use('fs') { |m|
87
+ m.should_receive(:blip)
88
+ m.blip
89
+ m.blip
90
+ m.blip
91
+ }
92
+ }.should.not.raise Test::Unit::AssertionFailedError
93
+ end
94
+
95
+ specify "should handle zero counts" do
96
+ lambda {
97
+ FlexMock.use { |m|
98
+ m.should_receive(:blip).never
99
+ m.blip
100
+ }
101
+ }.should.raise Test::Unit::AssertionFailedError
102
+ end
103
+
104
+ specify "should have file IO with use" do
105
+ file = FlexMock.use do |m|
106
+ filedata = ["line 1", "line 2"]
107
+ m.should_receive(:gets).times(3).and_return { filedata.shift }
108
+ count_lines(m).should.equal 2
109
+ end
110
+ end
111
+
112
+ def count_lines(stream)
113
+ result = 0
114
+ while line = stream.gets
115
+ result += 1
116
+ end
117
+ result
118
+ end
119
+
120
+ specify "should have use" do
121
+ lambda {
122
+ FlexMock.use do |m|
123
+ m.should_receive(:blip).times(2)
124
+ m.blip
125
+ end
126
+ }.should.raise Test::Unit::AssertionFailedError
127
+ end
128
+
129
+ specify "should handle failures during use" do
130
+ ex = lambda {
131
+ FlexMock.use do |m|
132
+ m.should_receive(:blip).times(2)
133
+ xyz
134
+ end
135
+ }.should.raise NameError
136
+ ex.message.should.match(/undefined local variable or method/)
137
+ end
138
+
139
+ specify "should deal with sequential values" do
140
+ values = [1,4,9,16]
141
+ @mock.should_receive(:get).and_return { values.shift }
142
+ @mock.get.should.equal 1
143
+ @mock.get.should.equal 4
144
+ @mock.get.should.equal 9
145
+ @mock.get.should.equal 16
146
+ end
147
+
148
+ specify "respond_to? should return false for non handled methods" do
149
+ @mock.should.not.respond_to :blah
150
+ end
151
+
152
+ specify "respond_to? should return true for explicit methods" do
153
+ @mock.should_receive(:xyz)
154
+ @mock.should.respond_to :xyz
155
+ end
156
+
157
+ specify "respond_to? should return true for missing_methods when should_ignore_missing" do
158
+ @mock.should_ignore_missing
159
+ @mock.should.respond_to :yada
160
+ end
161
+
162
+ specify "should raise error on unknown method proc" do
163
+ lambda {
164
+ @mock.method(:xyzzy)
165
+ }.should.raise NameError
166
+ end
167
+
168
+ specify "should return callable proc on method" do
169
+ got_it = false
170
+ @mock.should_receive(:xyzzy).and_return { got_it = true }
171
+ method_proc = @mock.method(:xyzzy)
172
+ method_proc.should.not.be.nil
173
+ method_proc.call
174
+ got_it.should.be true
175
+ end
176
+
177
+ specify "should return do nothing proc for missing methods" do
178
+ @mock.should_ignore_missing
179
+ method_proc = @mock.method(:plugh)
180
+ method_proc.should.not.be.nil
181
+ lambda { method_proc.call }.should.not.raise
182
+ end
183
+ end
184
+
185
+
186
+ class TemperatureSampler
187
+ def initialize(sensor)
188
+ @sensor = sensor
189
+ end
190
+
191
+ def average_temp
192
+ total = (0...3).collect { @sensor.read_temperature }.inject { |i, s| i + s }
193
+ total / 3.0
194
+ end
195
+ end
196
+
197
+ context "flexmock" do
198
+ include FlexMock::TestCase
199
+
200
+ specify "works with test/spec" do
201
+ sensor = flexmock("temp")
202
+ sensor.should_receive(:read_temperature).times(3).and_return(10, 12, 14)
203
+
204
+ sampler = TemperatureSampler.new(sensor)
205
+ sampler.average_temp.should.equal 12
206
+ end
207
+ end
208
+
209
+ end # if not rescue LoadError
@@ -0,0 +1,104 @@
1
+ # Adapted from mocha (http://mocha.rubyforge.org/).
2
+ #
3
+ # Copyright (C) 2006 Revieworld Ltd.
4
+ #
5
+ # You may use, copy and redistribute this library under the same terms
6
+ # as Ruby itself (see www.ruby-lang.org/en/LICENSE.txt) or under the
7
+ # MIT license (see MIT-LICENSE file).
8
+
9
+ require 'test/spec'
10
+
11
+ begin
12
+ require 'mocha'
13
+ rescue LoadError
14
+ context "mocha" do
15
+ specify "can not be found. BAIL OUT!" do
16
+ end
17
+ end
18
+ else
19
+
20
+ context "mocha" do
21
+ specify "works with test/spec" do
22
+ object = mock()
23
+ object.expects(:expected_method).with(:p1, :p2).returns(:result)
24
+ object.expected_method(:p1, :p2).should.equal :result
25
+ end
26
+ end
27
+
28
+ class Enterprise
29
+ def initialize(dilithium)
30
+ @dilithium = dilithium
31
+ end
32
+
33
+ def go(warp_factor)
34
+ warp_factor.times { @dilithium.nuke(:anti_matter) }
35
+ end
36
+ end
37
+
38
+ context "mocha" do
39
+ specify "works with test/spec and Enterprise example" do
40
+ dilithium = mock()
41
+ dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
42
+ enterprise = Enterprise.new(dilithium)
43
+ enterprise.go(2)
44
+ end
45
+ end
46
+
47
+ class Order
48
+ attr_accessor :shipped_on
49
+
50
+ def total_cost
51
+ line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
52
+ end
53
+
54
+ def total_weight
55
+ line_items.inject(0) { |total, line_item| total + line_item.weight }
56
+ end
57
+
58
+ def shipping_cost
59
+ total_weight * 5 + 10
60
+ end
61
+
62
+ class << self
63
+
64
+ def find_all
65
+ # Database.connection.execute('select * from orders...
66
+ end
67
+
68
+ def number_shipped_since(date)
69
+ find_all.select { |order| order.shipped_on > date }.size
70
+ end
71
+
72
+ def unshipped_value
73
+ find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ context "stubba" do
81
+ specify "works with test/spec and instance method stubbing" do
82
+ order = Order.new
83
+ order.stubs(:total_weight).returns(10)
84
+ order.shipping_cost.should.equal 60
85
+ end
86
+
87
+ specify "works with test/spec and class method stubbing" do
88
+ now = Time.now; week_in_secs = 7 * 24 * 60 * 60
89
+ order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
90
+ order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
91
+ Order.stubs(:find_all).returns([order_1, order_2])
92
+ Order.number_shipped_since(now - 2 * week_in_secs).should.equal 1
93
+ end
94
+
95
+ specify "works with test/spec and global instance method stubbing" do
96
+ Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
97
+ Order.any_instance.stubs(:shipped_on).returns(nil)
98
+ Order.any_instance.stubs(:total_cost).returns(10)
99
+ Order.unshipped_value.should.equal 30
100
+ end
101
+ end
102
+
103
+ end # if not rescue LoadError
104
+
@@ -0,0 +1,26 @@
1
+ require 'test/spec'
2
+
3
+ context "Empty context" do
4
+ # should.not.raise
5
+ end
6
+
7
+ context "Outer context" do
8
+ context "Inner context" do
9
+ specify "is nested" do
10
+ end
11
+ specify "has multiple empty specifications" do
12
+ end
13
+ end
14
+ context "Second Inner context" do
15
+ context "Inmost context" do
16
+ specify "works too!" do
17
+ end
18
+ specify "whoo!" do
19
+ end
20
+ end
21
+ specify "is indented properly" do
22
+ end
23
+ specify "still runs in order of definition" do
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,80 @@
1
+ describe_shared "A new-style shared description" do
2
+ it "should work as well with shared descriptions" do
3
+ true.should.be true
4
+ end
5
+ end
6
+
7
+ describe "A new-style description" do
8
+ before do
9
+ @before = true
10
+ @a = 2
11
+ end
12
+
13
+ before(:each) do
14
+ @before_each = true
15
+ end
16
+
17
+ before(:all) do
18
+ $before_all = true
19
+ end
20
+
21
+ it "should run before-clauses" do
22
+ $before_all.should.be true
23
+ @before.should.be true
24
+ @before_each.should.be true
25
+ end
26
+
27
+ it "should behave like context/specify" do
28
+ (1+1).should.equal 2
29
+ end
30
+
31
+ xit "this is disabled" do
32
+ bla
33
+ end
34
+
35
+ after do
36
+ @a.should.equal 2
37
+ @a = 3
38
+ end
39
+
40
+ after(:each) do
41
+ @a.should.equal 3
42
+ end
43
+
44
+ after(:all) do
45
+ @b = 1
46
+ end
47
+
48
+ after(:all) do
49
+ @b.should.equal 1
50
+ end
51
+
52
+ $describescope = self
53
+ it "should raise on unimplement{ed,able} before/after" do
54
+ lambda {
55
+ $describescope.before(:foo) {}
56
+ }.should.raise(ArgumentError)
57
+ lambda {
58
+ $describescope.after(:foo) {}
59
+ }.should.raise(ArgumentError)
60
+
61
+ lambda {
62
+ context "foo" do
63
+ end
64
+ }.should.raise(Test::Spec::DefinitionError)
65
+ end
66
+
67
+ describe "when nested" do
68
+ it "should work" do
69
+ end
70
+ end
71
+
72
+ behaves_like "A new-style shared description"
73
+ end
74
+
75
+ describe "An empty description" do
76
+ end
77
+
78
+ xdescribe "An disabled description" do
79
+ it "should not be run"
80
+ end