test4requirements 0.1.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ =begin rdoc
2
+ Support of shoulda.
3
+
4
+ Details on usage see examples/example_test4requirements_shoulda.rb
5
+
6
+ Fixmes:
7
+ * should_not is not supported yet
8
+ =end
9
+
10
+ require_relative '../test4requirements'
11
+ require 'shoulda' #tested with shoulda 2.11.3
12
+
13
+ #
14
+ module Test
15
+ module Unit
16
+ class TestCase
17
+ class << self
18
+ #remember original definition.
19
+ alias :should_old :should
20
+ =begin rdoc
21
+ Extend #should to support assignment to requirement.
22
+
23
+ Usage:
24
+ should 'fullfill request 1', requirement: :req1 do
25
+ assert_equal(2,1+1)
26
+ end
27
+ =end
28
+ def should(name_or_matcher, options = {}, &blk)
29
+ if ! self.name
30
+ raise RuntimeError, "Shoulda not defined for anonymous classes"
31
+ end
32
+ if options[:requirement]
33
+ #testmethodname must fit self.name of the later test (TestCase#run_test)
34
+ testmethodname = 'test: '
35
+ testmethodname << self.name.gsub(/Test/, "")
36
+ testmethodname << ' should '
37
+ testmethodname << name_or_matcher
38
+ testmethodname << '. '
39
+ testmethodname << "(#{self.name})"
40
+ raise RuntimeError, "No requirements defined for #{self}" unless get_requirements
41
+ get_requirements.assign_test(options[:requirement], testmethodname)
42
+ end
43
+ should_old(name_or_matcher, options = {}, &blk)
44
+ end #should
45
+ end #eigenclass TestCase
46
+ end #class TestCase
47
+ end #module Unit
48
+ end #module Test
49
+
50
+ module Shoulda
51
+ class Context
52
+ #remember original definition.
53
+ alias :should_old :should
54
+ =begin rdoc
55
+ Extend #should inside a context to support assignment to requirement.
56
+
57
+ Usage:
58
+ should 'fullfill request 1', requirement: :req1 do
59
+ assert_equal(2,1+1)
60
+ end
61
+ =end
62
+ def should(name_or_matcher, options = {}, &blk)
63
+ if options[:requirement]
64
+ #testmethodname must fit self.name of the later test (TestCase#run_test)
65
+ testmethodname = 'test: '
66
+ testmethodname << self.name
67
+ testmethodname << ' should '
68
+ testmethodname << name_or_matcher
69
+ testmethodname << '. '
70
+ testmethodname << "(#{self.parent.name})"
71
+ raise RuntimeError, "No requirements defined for #{self}" unless get_requirements
72
+ get_requirements.assign_test(options[:requirement], testmethodname)
73
+ end
74
+ should_old(name_or_matcher, options = {}, &blk)
75
+ end #should
76
+ end# class Context
77
+ end #Shoulda
@@ -0,0 +1,59 @@
1
+ =begin rdoc
2
+ Extend TestCase to handle requirements.
3
+ =end
4
+ gem 'test-unit'
5
+ require 'test/unit'
6
+
7
+ #
8
+ module Test
9
+ module Unit
10
+ class TestCase
11
+ class << self
12
+ =begin rdoc
13
+ Define requirements, handled by the TestCase-object.
14
+ =end
15
+ def requirements=(req)
16
+ raise ArgumentError, "#{self.class}: Expected RequirementList, received #{req.inspect}" unless req.is_a?(Test4requirements::RequirementList)
17
+ raise ArgumentError, "#{self.class}: Get two RequirementLists" if defined? @requirements
18
+ @requirements = req
19
+ end
20
+ #for usage inside class definition.
21
+ alias :requirements :'requirements='
22
+
23
+ def get_requirements; @requirements; end
24
+ end #eigenclass TestCase
25
+ =begin rdoc
26
+ Get Requirements for TestCases
27
+ =end
28
+ def requirements
29
+ self.class.get_requirements
30
+ end
31
+ =begin rdoc
32
+ Assign a requirement.
33
+ To be called inside a test_*-method.
34
+
35
+ Example:
36
+ def test_1()
37
+ assign_requirement(:req1) #this test is testing requirement 1
38
+ assert_equal(2,1+1)
39
+ end
40
+ =end
41
+ def assign_requirement(req)
42
+ raise RuntimeError, "No requirements defined for #{self}" unless requirements
43
+ requirements.assign_test(req, self.name)
44
+ end
45
+ alias :run_test_old :run_test
46
+ =begin rdoc
47
+ Add info about a successfull test to the assigned requirement.
48
+ =end
49
+ def run_test
50
+ run_test_old
51
+ #this code is left if a problem occured.
52
+ #in other words: if we reach this place, then the test was sucesfull
53
+ if requirements
54
+ requirements.test_successfull(self.name)
55
+ end #if requirements
56
+ end #def run_test
57
+ end #class TestCase
58
+ end #module Unit
59
+ end #module Test
@@ -0,0 +1,65 @@
1
+ =begin rdoc
2
+ Some check routines to check the installation
3
+ =end
4
+
5
+ puts RUBY_DESCRIPTION
6
+
7
+
8
+ gem 'test-unit'
9
+ #~ require 'test/unit'
10
+ require 'test/unit/version'
11
+
12
+ =begin rdoc
13
+
14
+ Encoding#ascii_compatible?
15
+
16
+ Test::Unit::Assertions contains:
17
+ if string.respond_to?(:encoding) and
18
+ !string.encoding.ascii_compatible?
19
+ string = string.dup.force_encoding("ASCII-8BIT")
20
+ end
21
+ Probably added with test-unit 2.2.0
22
+ http://rubyforge.org/forum/forum.php?forum_id=38792
23
+ support not ASCII compatible string diff.
24
+
25
+ Encoding#ascii_compatible? was added with:
26
+ http://redmine.ruby-lang.org/issues/show/1900
27
+ Applied in changeset r24480.
28
+
29
+ http://www.ruby-forum.com/topic/192885
30
+
31
+
32
+ =end
33
+ if Test::Unit::VERSION >= '2.3.1'
34
+ if ! 'string'.encoding.respond_to?(:'ascii_compatible?')
35
+ puts "Encoding#ascii_compatible? not defined - Test::Unit #{Test::Unit::VERSION} needs it"
36
+ end
37
+ end
38
+
39
+ #~ gem 'test-unit', '= 2.3.1'
40
+ #~ require 'test/unit'
41
+ #~ class Test_Requirement < Test::Unit::TestCase
42
+ #~ def test_interface_ok()
43
+ #~ assert_equal('a', 'b')
44
+ #~ end
45
+ #~ end
46
+
47
+
48
+ __END__
49
+ RUBY_VERSION
50
+ RUBY_PATCHLEVEL
51
+ RUBY_RELEASE_DATE
52
+
53
+ Worked fine:
54
+ gem 'test-unit', '= 2.1.1'
55
+
56
+
57
+ gem 'test-unit', '= 2.3.1'
58
+ ruby 1.9.1
59
+
60
+
61
+ if string.respond_to?(:encoding) and
62
+ !string.encoding.ascii_compatible?
63
+ string = string.dup.force_encoding("ASCII-8BIT")
64
+ end
65
+
@@ -0,0 +1,82 @@
1
+ $:.unshift('../lib')
2
+ gem 'test-unit'
3
+ require 'test/unit'
4
+ require 'test4requirements/requirement.rb'
5
+ include Test4requirements
6
+
7
+ class Test_Requirement < Test::Unit::TestCase
8
+
9
+ def test_interface_ok()
10
+ req = nil
11
+ assert_nothing_raised { req = Requirement.new(1) }
12
+ assert_equal(1, req.key)
13
+ end
14
+ def test_interface_empty()
15
+ assert_raise(ArgumentError) { Requirement.new() }
16
+ end
17
+ def test_interface_option_description()
18
+ req = nil
19
+ assert_nothing_raised { req = Requirement.new(1, :description => 'desc')}
20
+ assert_equal('desc', req.description)
21
+ end
22
+ def test_interface_option_reference()
23
+ req = nil
24
+ assert_nothing_raised { req = Requirement.new(1, :reference => 'ref')}
25
+ assert_equal('ref', req.reference)
26
+ end
27
+ def test_interface_option()
28
+ assert_raise(ArgumentError) { Requirement.new(1, :undefined_key => 'desc') }
29
+ end
30
+
31
+ def test_no_test_assignment()
32
+ req = Requirement.new(1)
33
+ assert_false(req.tested?)
34
+ assert_nil(req.successfull?)
35
+ end
36
+
37
+ def test_test_assignment()
38
+ req = Requirement.new(1)
39
+ req.test='loc'
40
+ assert_equal(1,req.tested?)
41
+ req.test='loc'
42
+ assert_equal(1,req.tested?, "No duplicate definition")
43
+ req.test='loc2'
44
+ assert_equal(2,req.tested?, "Two TestCases")
45
+
46
+ assert_false(req.successfull?)
47
+ end
48
+
49
+ def test_test_success()
50
+ req = Requirement.new(1)
51
+ req.test='loc'
52
+ assert_equal(1,req.tested?)
53
+ assert_false(req.successfull?)
54
+
55
+ req.successfull_test('loc')
56
+ assert_equal(1,req.tested?)
57
+ assert_true(req.successfull?)
58
+ end
59
+
60
+ def test_test_success_without_assignment()
61
+ req = Requirement.new(1)
62
+ assert_false(req.tested?)
63
+ assert_nil(req.successfull?)
64
+
65
+ req.successfull_test('loc')
66
+ assert_equal(1,req.tested?)
67
+ assert_true(req.successfull?)
68
+ end
69
+
70
+ def test_2test_success()
71
+ req = Requirement.new(1)
72
+ req.test='loc'
73
+ assert_equal(1,req.tested?)
74
+ assert_false(req.successfull?)
75
+
76
+ req.successfull_test('loc2')
77
+ assert_equal(2,req.tested?)
78
+ assert_false(req.successfull?, "One of the tests was unsuccessfull")
79
+ end
80
+
81
+
82
+ end
@@ -0,0 +1,84 @@
1
+ $:.unshift('../lib')
2
+ gem 'test-unit'
3
+ require 'test/unit'
4
+ require 'log4r'
5
+
6
+ require 'test4requirements/requirement.rb'
7
+ require 'test4requirements/requirementlist.rb'
8
+ include Test4requirements
9
+
10
+ RequirementList.report_type_default = nil
11
+
12
+
13
+ class Test_RequirementList < Test::Unit::TestCase
14
+
15
+ def test_interface()
16
+ assert_nothing_raised { RequirementList.new()}
17
+ assert_nothing_raised { RequirementList.new(1)}
18
+ assert_nothing_raised { RequirementList.new(Requirement.new(1))}
19
+ end
20
+
21
+ def test_add_requirement()
22
+ req = RequirementList.new()
23
+ assert_nothing_raised { req << Requirement.new(1) }
24
+ assert_raise(ArgumentError) { req << 1 }
25
+ end
26
+
27
+ def test_interface_double_definition()
28
+ assert_raise(ArgumentError) { RequirementList.new(1, 1) }
29
+ assert_raise(ArgumentError) { RequirementList.new(Requirement.new(1),1)}
30
+ assert_raise(ArgumentError) { RequirementList.new(Requirement.new(1),Requirement.new(1))}
31
+ end
32
+
33
+ def test_unassigned_test()
34
+ req = RequirementList.new()
35
+ assert_raise(ArgumentError, "Unkwown req. can't be assigned") { req.assign_test(1, 'loc')}
36
+ assert_equal(nil, req[1], "Unassigned test returns nil")
37
+ #~ assert_false(req[1].tested?)
38
+ #~ assert_false(req[1].successfull?)
39
+ end
40
+ def test_assign_test()
41
+ req = RequirementList.new()
42
+ assert_nothing_raised("Assign requirement") { req << Requirement.new(1) }
43
+ assert_nothing_raised("Assign test") { req.assign_test(1, 'loc') }
44
+ assert_instance_of(Requirement, req[1])
45
+ assert_equal(false, req[1]['loc'])
46
+ assert_equal(1,req[1].tested?)
47
+ assert_false(req[1].successfull?)
48
+ end
49
+
50
+ def test_successfull_test()
51
+ req = RequirementList.new()
52
+ req << Requirement.new(1)
53
+ req.assign_test(1, 'loc') #not neccesary
54
+ assert_false(req[1]['loc'])
55
+
56
+ assert_nothing_raised("Successfull test") { req.test_successfull('loc') }
57
+ assert_instance_of(Requirement, req[1])
58
+ assert_equal(1,req[1].tested?)
59
+ assert_true(req[1]['loc'])
60
+ assert_true(req[1].successfull?)
61
+ end
62
+
63
+ def test_successfull_test_without_assignment()
64
+ req = RequirementList.new()
65
+ req << Requirement.new(1)
66
+
67
+ assert_nothing_raised("Successfull test") { req.test_successfull('loc') }
68
+ assert_instance_of(Requirement, req[1])
69
+ assert_false(req[1].tested?)
70
+ assert_nil(req[1].successfull?)
71
+ end
72
+
73
+ def test_unsuccessfull_test()
74
+ req = RequirementList.new()
75
+ req << Requirement.new(1)
76
+ req.assign_test(1, 'loc')
77
+ assert_instance_of(Requirement, req[1])
78
+ assert_equal(false, req[1]['loc'])
79
+ assert_equal(1,req[1].tested?)
80
+ assert_false(req[1].successfull?)
81
+ end
82
+
83
+ end
84
+
@@ -0,0 +1,199 @@
1
+ $:.unshift('../lib')
2
+ #~ gem 'test-unit'
3
+ #~ require 'test/unit'
4
+ require 'test4requirements.rb'
5
+ include Test4requirements
6
+
7
+ RequirementList.report_type_default = nil
8
+
9
+ #IO to catch stdout in TestRunner
10
+ IO_Catcher = Class.new(IO){ def write(arg);end }.new(2, 'w')
11
+
12
+ class Test_RequirementList_Overview_without_test < Test::Unit::TestCase
13
+ def setup
14
+ @reqs = RequirementList.new(1,2,3)
15
+ @testcase = Class.new(Test::Unit::TestCase)
16
+ @testcase.requirements= @reqs
17
+ #~ @testcase.suite() #run tests
18
+ end
19
+
20
+ def test_overview_default()
21
+ assert_kind_of(Hash, @reqs.overview)
22
+ assert_equal([1,2,3], @reqs.overview.keys)
23
+ @reqs.overview.each{|key,req| assert_nil(req) }
24
+ end
25
+
26
+ #same as default
27
+ def test_overview_nil()
28
+ @reqs.report_type = nil
29
+ assert_kind_of(Hash, @reqs.overview)
30
+ assert_equal([1,2,3], @reqs.overview.keys)
31
+ end
32
+
33
+ def test_overview_txt()
34
+ @reqs.report_type = :txt
35
+ assert_kind_of(Array, @reqs.overview)
36
+ assert_equal(3, @reqs.overview.size)
37
+ assert_equal(["Requirement 1 was not tested",
38
+ "Requirement 2 was not tested",
39
+ "Requirement 3 was not tested"], @reqs.overview)
40
+ end
41
+
42
+ def test_overview_unknown()
43
+ assert_raise(ArgumentError){@reqs.report_type = :unknown}
44
+ assert_nothing_raised{@reqs.overview}
45
+ assert_raise(ArgumentError){@reqs.overview(:unknown)}
46
+ end
47
+ end
48
+
49
+
50
+ class Test_RequirementList_Overview_with_test < Test::Unit::TestCase
51
+ def setup
52
+ @reqs = RequirementList.new(:req1,:req2,:req3)
53
+ @testcase = Class.new(Test::Unit::TestCase) do
54
+ #Set class name. Normally class name.
55
+ def self.name;'Test_ReqList';end
56
+
57
+ def test_1()
58
+ assign_requirement(:req1) #this test is testing requirement req1
59
+ assert_equal(2,1+1)
60
+ end
61
+ def test_2()
62
+ assign_requirement(:req2) #this test is testing requirement req2 without success
63
+ assert_equal(3,1+1)
64
+ end
65
+ end
66
+ @testcase.requirements= @reqs
67
+
68
+ suite = @testcase.suite #run tests
69
+ Test::Unit::UI::Console::TestRunner.run(suite, :output => IO_Catcher)
70
+ end
71
+
72
+ def test_testcase_definition()
73
+ assert_true(@testcase.instance_methods.include?(:test_1))
74
+ assert_true(@testcase.instance_methods.include?(:test_2))
75
+ #~ assert_true(@testcase.respond_to?(:test_1))
76
+ #~ assert_true(@testcase.respond_to?(:test_2))
77
+ end
78
+
79
+ def test_overview_default()
80
+ assert_kind_of(Hash, @reqs.overview)
81
+ assert_equal([:req1,:req2,:req3], @reqs.overview.keys)
82
+ assert_equal([true,false,nil], @reqs.overview.values)
83
+ end
84
+
85
+ #same as default
86
+ def test_overview_nil()
87
+ @reqs.report_type = nil
88
+ assert_kind_of(Hash, @reqs.overview)
89
+ assert_equal([true,false,nil], @reqs.overview.values)
90
+ end
91
+
92
+ def test_overview_txt()
93
+ @reqs.report_type = :txt
94
+ assert_kind_of(Array, @reqs.overview)
95
+ assert_equal(3, @reqs.overview.size)
96
+ assert_equal(["Requirement req1 was successfull tested (test_1(Test_ReqList))",
97
+ "Requirement req2 was unsuccessfull tested (test_2(Test_ReqList))",
98
+ "Requirement req3 was not tested"
99
+ ], @reqs.overview)
100
+ end
101
+ end
102
+
103
+
104
+
105
+ class Test_two_tests_for_one_requirement_successfull < Test::Unit::TestCase
106
+ def setup
107
+ @reqs = reqs = RequirementList.new(:req1)
108
+ testcase = Class.new(Test::Unit::TestCase) do
109
+ #Set class name. Normally class name.
110
+ def self.name;'Test_ReqList';end
111
+ self.requirements= reqs
112
+
113
+ def test_1()
114
+ assign_requirement(:req1) #this test is testing requirement req1
115
+ assert_equal(2,1+1)
116
+ end
117
+ def test_2()
118
+ assign_requirement(:req1) #this test is testing requirement req2 without success
119
+ assert_equal(2,1+1)
120
+ end
121
+ end
122
+
123
+ suite = testcase.suite #run tests
124
+ Test::Unit::UI::Console::TestRunner.run(suite, :output => IO_Catcher)
125
+ end
126
+ def test_duplicate_test
127
+ assert_equal([:req1], @reqs.overview.keys)
128
+ assert_equal([true], @reqs.overview.values)
129
+ end
130
+ def test_overview_txt()
131
+ @reqs.report_type = :txt
132
+ assert_equal(["Requirement req1 was successfull tested (test_1(Test_ReqList), test_2(Test_ReqList))",], @reqs.overview)
133
+ end
134
+ end
135
+
136
+ class Test_two_tests_for_one_requirement_unsuccessfull < Test::Unit::TestCase
137
+ def setup
138
+ @reqs = reqs = RequirementList.new(:req1)
139
+ testcase = Class.new(Test::Unit::TestCase) do
140
+ #Set class name. Normally class name.
141
+ def self.name;'Test_ReqList';end
142
+ self.requirements= reqs
143
+ #Unsuccessfull
144
+ def test_1()
145
+ assign_requirement(:req1) #this test is testing requirement req1
146
+ assert_equal(3,1+1)
147
+ end
148
+ #Unsuccessfull
149
+ def test_2()
150
+ assign_requirement(:req1) #this test is testing requirement req2 without success
151
+ assert_equal(3,1+1)
152
+ end
153
+ end
154
+
155
+ suite = testcase.suite #run tests
156
+ Test::Unit::UI::Console::TestRunner.run(suite, :output => IO_Catcher)
157
+ end
158
+ def test_duplicate_test
159
+ assert_equal([:req1], @reqs.overview.keys)
160
+ assert_equal([false], @reqs.overview.values)
161
+ end
162
+ def test_overview_txt()
163
+ @reqs.report_type = :txt
164
+ assert_equal(["Requirement req1 was unsuccessfull tested (test_1(Test_ReqList), test_2(Test_ReqList))",], @reqs.overview)
165
+ end
166
+ end
167
+
168
+ class Test_two_tests_for_one_requirement_mixed_result < Test::Unit::TestCase
169
+ def setup
170
+ @reqs = reqs = RequirementList.new(:req1)
171
+ testcase = Class.new(Test::Unit::TestCase) do
172
+ #Set class name. Normally class name.
173
+ def self.name;'Test_ReqList';end
174
+ self.requirements= reqs
175
+ #Successfull
176
+ def test_1()
177
+ assign_requirement(:req1) #this test is testing requirement req1
178
+ assert_equal(2,1+1)
179
+ end
180
+ #Unsuccessfull
181
+ def test_2()
182
+ assign_requirement(:req1) #this test is testing requirement req2 without success
183
+ assert_equal(3,1+1)
184
+ end
185
+ end
186
+
187
+ suite = testcase.suite #run tests
188
+ Test::Unit::UI::Console::TestRunner.run(suite, :output => IO_Catcher)
189
+ end
190
+ def test_duplicate_test
191
+ assert_equal([:req1], @reqs.overview.keys)
192
+ assert_equal([false], @reqs.overview.values)
193
+ end
194
+ def test_overview_txt()
195
+ @reqs.report_type = :txt
196
+ assert_equal(["Requirement req1 was unsuccessfull tested (OK: test_1(Test_ReqList), Failure: test_2(Test_ReqList))",], @reqs.overview)
197
+ end
198
+ end
199
+