given 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ require 'test/test_helper'
2
+
3
+ require 'given'
4
+
5
+ class ThenTest < GivenTestCase
6
+ def test_then_passes_when_block_is_true
7
+ assert_all_pass do
8
+ Given do
9
+ Then { true }
10
+ end
11
+ end
12
+ end
13
+
14
+ def test_then_fails_if_block_is_false
15
+ tally = run_tests do
16
+ Given do
17
+ Then { false }
18
+ end
19
+ end
20
+ assert ! tally.passed?
21
+ end
22
+
23
+ def test_then_fails_if_block_fails
24
+ tally = run_tests do
25
+ Given do
26
+ Then { fail "OUCH" }
27
+ end
28
+ end
29
+ assert ! tally.passed?
30
+ end
31
+
32
+ def test_multiple_thens_create_multiple_tests
33
+ assert_all_pass(2) do
34
+ Given do
35
+ Then { true }
36
+ Then { true }
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_and_is_an_alias_for_then
42
+ assert_all_pass(3) do
43
+ Given do
44
+ Then { true }
45
+ And { true }
46
+ And { true }
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_multiple_thens_with_tested_givens_create_multiple_tests
52
+ assert_all_pass(3) do
53
+ Given do
54
+ Given do
55
+ Then { true }
56
+ end
57
+ Then { true }
58
+ Then { true }
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,43 @@
1
+ require 'test/test_helper'
2
+
3
+ require 'given'
4
+
5
+ class WhenTest < GivenTestCase
6
+ def test_whens_are_executed_after_all_setups_but_before_thens
7
+ assert_all_pass do
8
+ Given(:a, :b) do
9
+ When { @track << :c }
10
+ Then { @track == [:a, :b, :c] }
11
+ end
12
+ def a() @track = [:a] end
13
+ def b() @track << :b end
14
+ end
15
+ end
16
+
17
+ def test_whens_do_not_stack_linearly
18
+ assert_all_pass do
19
+ Given(:a_track) do
20
+ When { @track << :a }
21
+ Then { @track == [:a] }
22
+
23
+ When { @track << :b }
24
+ Then { @track == [:b] }
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_whens_cleanup_from_nested_whens
30
+ assert_all_pass do
31
+ Given(:a_track) do
32
+ When { @track << :a }
33
+
34
+ Given do
35
+ When { @track << :b }
36
+ Then { @track == [:b] }
37
+ end
38
+
39
+ Then { @track == [] }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+ require 'given/test_unit'
3
+ require 'given/anonymous_code'
4
+
5
+ class AnonymousCodeContract < Given::TestCase
6
+ Given(:an_anonymous_code_snippet) do
7
+ Then { @code.run(self) == :result }
8
+ Then { @code.line_marker.nil? }
9
+ Then { @code.file_line.nil? }
10
+ end
11
+
12
+ def an_anonymous_code_snippet
13
+ @code = Given::AnonymousCode.new(lambda { :result })
14
+ end
15
+ end
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'given/test_unit'
5
+
6
+ require 'given/expectation'
7
+
8
+ class ExpectationContract < Given::TestCase
9
+ Given do
10
+ Then { expect(1) == 1 }
11
+ Then { expect(2) > 1 }
12
+ Then { expect(2) >= 1 }
13
+ Then { expect(2) >= 2 }
14
+ Then { expect(2) < 3 }
15
+ Then { expect(2) <= 3 }
16
+ Then { expect(2) <= 2 }
17
+ Then { expect("abc") =~ /^a/ }
18
+ Then { expect("abc").not =~ /^b/ }
19
+ Then { expect(nil).nil? }
20
+ Then { expect([]).empty? }
21
+ Then { expect([1].size) == 1 }
22
+ end
23
+
24
+ Given do
25
+ When { expect([]).size }
26
+ FailsWith(Given::UsageError)
27
+ Then { expect(exception.message) =~ /x/ }
28
+ end
29
+
30
+ Given do
31
+ Then { expect(1).not == 2 }
32
+ Then { expect(2).not <= 1 }
33
+ Then { expect(2).not < 1 }
34
+ Then { expect(2).not < 2 }
35
+ Then { expect(2).not >= 3 }
36
+ Then { expect(2).not > 3 }
37
+ Then { expect(2).not > 2 }
38
+ Then { expect("abc").not =~ /^b/ }
39
+ Then { expect(1).not.nil? }
40
+ Then { expect([1]).not.empty? }
41
+ Then { expect([1,2].size).not == 1 }
42
+ end
43
+
44
+ Given do
45
+ When { expect(1) == 2 }
46
+ FailsWith(Given.assertion_failed_exception)
47
+ Then { expect(exception.message) =~ /<1> expected to be equal to.*<2>/m }
48
+
49
+ When { expect(1).not == 1 }
50
+ FailsWith(Given.assertion_failed_exception)
51
+ Then { expect(exception.message) =~ /<1> expected to not be equal to.*<1>/m }
52
+
53
+ When { expect(2) > 3 }
54
+ FailsWith(Given.assertion_failed_exception)
55
+ Then { expect(exception.message) =~ /<2> expected to be greater than.*<3>/m }
56
+
57
+ When { expect(3) < 2 }
58
+ FailsWith(Given.assertion_failed_exception)
59
+ Then { expect(exception.message) =~ /<3> expected to be less than.*<2>/m }
60
+
61
+ When { expect(3) <= 2 }
62
+ FailsWith(Given.assertion_failed_exception)
63
+ Then { expect(exception.message) =~ /<3> expected to be less than or equal to.*<2>/m }
64
+
65
+ When { expect(2) >= 3 }
66
+ FailsWith(Given.assertion_failed_exception)
67
+ Then { expect(exception.message) =~ /<2> expected to be greater than or equal to.*<3>/m }
68
+
69
+ When { expect("abc") =~ /^x/ }
70
+ FailsWith(Given.assertion_failed_exception)
71
+ Then { expect(exception.message) =~ /<abc> expected to be matched by.*\^x/m }
72
+
73
+ When { expect(1).nil? }
74
+ FailsWith(Given.assertion_failed_exception)
75
+ Then { expect(exception.message) =~ /<1> expected to be nil/m }
76
+
77
+ When { expect([1]).empty? }
78
+ FailsWith(Given.assertion_failed_exception)
79
+ Then { expect(exception.message) =~ /<1> expected to be empty\.?$/m }
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'given/test_unit'
5
+
6
+ class AdapterTest < Given::TestCase
7
+ include Given::TestUnit::Adapter
8
+
9
+ Code = Given::Code
10
+
11
+ def add_assertion
12
+ super
13
+ @assertion_counted = true
14
+ end
15
+
16
+ Given do
17
+ When { given_assert("Then", Code.new("T", lambda { true })) }
18
+ Then { @assertion_counted }
19
+
20
+ When {
21
+ @line = __LINE__ + 1
22
+ given_assert("Then", Code.new('T', lambda { false }))
23
+ }
24
+ FailsWith(Test::Unit::AssertionFailedError) do
25
+ Then {
26
+ exception.message =~ /#{__FILE__}:#{@line}/
27
+ }
28
+ end
29
+
30
+ When {
31
+ given_assert("Then", Code.new('T', lambda { fail "OUCH" }))
32
+ }
33
+ FailsWith(RuntimeError) do
34
+ Then { exception.message == "OUCH" }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,190 @@
1
+ require 'test/unit'
2
+ require 'given'
3
+ require 'given/test_unit/adapter'
4
+
5
+ class GivenTestCase < Test::Unit::TestCase
6
+ private
7
+
8
+ # Reach inside the tally object and return the first failure message
9
+ def failure_message(tally)
10
+ failures = tally.instance_eval { @failures }
11
+ failures.first.instance_eval { @message }
12
+ end
13
+
14
+ def assert_all_pass(run_count=nil, &block)
15
+ tally = run_tests(&block)
16
+ assert tally.passed?, tally.inspect
17
+ unless run_count.nil?
18
+ assert_equal(run_count, tally.run_count,
19
+ "Wrong number of test runs")
20
+ end
21
+ end
22
+
23
+ def run_tests(&block)
24
+ tests = test_class(&block)
25
+ suite = tests.suite
26
+ tally = Test::Unit::TestResult.new
27
+ suite.run(tally) { }
28
+ tally
29
+ end
30
+
31
+ def test_class(&block)
32
+ Class.new(GivenFauxTestCase, &block)
33
+ end
34
+
35
+ def default_test
36
+ end
37
+ end
38
+
39
+ # Fake TestCase for testing. This has everything the real
40
+ # Test::Unit::TestCase has, except that it won't trigger auto-testing.
41
+ # We use this to construct test cases.
42
+ class FauxTestCase
43
+ include Test::Unit::Assertions
44
+ include Test::Unit::Util::BacktraceFilter
45
+ include Given::DSL::TestHelper
46
+ extend Given::DSL
47
+
48
+ attr_reader :method_name
49
+
50
+ STARTED = name + "::STARTED"
51
+ FINISHED = name + "::FINISHED"
52
+
53
+ ##
54
+ # These exceptions are not caught by #run.
55
+
56
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt,
57
+ SystemExit]
58
+
59
+ # Creates a new instance of the fixture for running the
60
+ # test represented by test_method_name.
61
+ def initialize(test_method_name)
62
+ unless(respond_to?(test_method_name) and
63
+ (method(test_method_name).arity == 0 ||
64
+ method(test_method_name).arity == -1))
65
+ throw :invalid_test
66
+ end
67
+ @method_name = test_method_name
68
+ @test_passed = true
69
+ end
70
+
71
+ # Rolls up all of the test* methods in the fixture into
72
+ # one suite, creating a new instance of the fixture for
73
+ # each method.
74
+ def self.suite
75
+ method_names = public_instance_methods(true)
76
+ tests = method_names.delete_if {|method_name| method_name !~ /^test./}
77
+ suite = Test::Unit::TestSuite.new(name)
78
+ tests.sort.each do
79
+ |test|
80
+ catch(:invalid_test) do
81
+ suite << new(test)
82
+ end
83
+ end
84
+ if (suite.empty?)
85
+ catch(:invalid_test) do
86
+ suite << new("default_test")
87
+ end
88
+ end
89
+ return suite
90
+ end
91
+
92
+ # Runs the individual test method represented by this
93
+ # instance of the fixture, collecting statistics, failures
94
+ # and errors in result.
95
+ def run(result)
96
+ yield(STARTED, name)
97
+ @_result = result
98
+ begin
99
+ setup
100
+ __send__(@method_name)
101
+ rescue Test::Unit::AssertionFailedError => e
102
+ add_failure(e.message, e.backtrace)
103
+ rescue Exception
104
+ raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
105
+ add_error($!)
106
+ ensure
107
+ begin
108
+ teardown
109
+ rescue Test::Unit::AssertionFailedError => e
110
+ add_failure(e.message, e.backtrace)
111
+ rescue Exception
112
+ raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
113
+ add_error($!)
114
+ end
115
+ end
116
+ result.add_run
117
+ yield(FINISHED, name)
118
+ end
119
+
120
+ # Called before every test method runs. Can be used
121
+ # to set up fixture information.
122
+ def setup
123
+ end
124
+
125
+ # Called after every test method runs. Can be used to tear
126
+ # down fixture information.
127
+ def teardown
128
+ end
129
+
130
+ def default_test
131
+ flunk("No tests were specified")
132
+ end
133
+
134
+ # Returns whether this individual test passed or
135
+ # not. Primarily for use in teardown so that artifacts
136
+ # can be left behind if the test fails.
137
+ def passed?
138
+ return @test_passed
139
+ end
140
+ private :passed?
141
+
142
+ def size
143
+ 1
144
+ end
145
+
146
+ def add_assertion
147
+ @_result.add_assertion
148
+ end
149
+ private :add_assertion
150
+
151
+ def add_failure(message, all_locations=caller())
152
+ @test_passed = false
153
+ @_result.add_failure(Test::Unit::Failure.new(name, filter_backtrace(all_locations), message))
154
+ end
155
+ private :add_failure
156
+
157
+ def add_error(exception)
158
+ @test_passed = false
159
+ @_result.add_error(Test::Unit::Error.new(name, exception))
160
+ end
161
+ private :add_error
162
+
163
+ # Returns a human-readable name for the specific test that
164
+ # this instance of TestCase represents.
165
+ def name
166
+ "#{@method_name}(#{self.class.name})"
167
+ end
168
+
169
+ # Overridden to return #name.
170
+ def to_s
171
+ name
172
+ end
173
+
174
+ # It's handy to be able to compare TestCase instances.
175
+ def ==(other)
176
+ return false unless(other.kind_of?(self.class))
177
+ return false unless(@method_name == other.method_name)
178
+ self.class == other.class
179
+ end
180
+ end
181
+
182
+
183
+ class GivenFauxTestCase < FauxTestCase
184
+ include Given::TestUnit::Adapter
185
+
186
+ # A track array is used by many tests to record the order of events.
187
+ def a_track
188
+ @track = []
189
+ end
190
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: given
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jim Weirich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-17 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: |
17
+ Given is a specification framework that allows explicit definition of the
18
+ pre and post-conditions for code under test. Given is an alternative
19
+ to the traditional RSpec and Test::Unit frameworks.
20
+
21
+ email: jim.weirich@gmail.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - Rakefile
30
+ - README.textile
31
+ - lib/given/anonymous_code.rb
32
+ - lib/given/code.rb
33
+ - lib/given/dsl.rb
34
+ - lib/given/errors.rb
35
+ - lib/given/expectation.rb
36
+ - lib/given/framework.rb
37
+ - lib/given/test_unit/adapter.rb
38
+ - lib/given/test_unit.rb
39
+ - lib/given/version.rb
40
+ - lib/given.rb
41
+ - test/functional/fails_with_test.rb
42
+ - test/functional/invalid_use_test.rb
43
+ - test/functional/invariant_test.rb
44
+ - test/functional/setup_test.rb
45
+ - test/functional/then_test.rb
46
+ - test/functional/when_test.rb
47
+ - test/given/anonymous_code_contract.rb
48
+ - test/given/expectation_test.rb
49
+ - test/given/test_unit/adapter_contract.rb
50
+ - test/test_helper.rb
51
+ - examples/stack.rb
52
+ - examples/stack_test.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/jimweirich/Given
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --line-numbers
60
+ - --inline-source
61
+ - --main
62
+ - README.rdoc
63
+ - --title
64
+ - Rake -- Ruby Make
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: given
82
+ rubygems_version: 1.3.5
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Given/When/Then Specification Framework.
86
+ test_files: []
87
+