kintama 0.1.9 → 0.2

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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +2 -2
  3. data/lib/kintama.rb +26 -12
  4. data/lib/kintama/assertions.rb +40 -1
  5. data/lib/kintama/context.rb +65 -50
  6. data/lib/kintama/mocha.rb +32 -10
  7. data/lib/kintama/no_conflict.rb +2 -0
  8. data/lib/kintama/reporter.rb +11 -10
  9. data/lib/kintama/runnable.rb +2 -2
  10. data/lib/kintama/runner.rb +5 -5
  11. data/lib/kintama/test.rb +2 -2
  12. data/test/integration/automatic_running_test.rb +22 -0
  13. data/test/integration/line_based_running_test.rb +129 -0
  14. data/test/reporters/base_reporter_test.rb +31 -101
  15. data/test/reporters/inline_reporter_test.rb +23 -35
  16. data/test/reporters/verbose_reporter_test.rb +78 -76
  17. data/test/test_helper.rb +159 -2
  18. data/test/{assertions_test.rb → unit/assertions_test.rb} +54 -5
  19. data/test/unit/context_test.rb +15 -0
  20. data/test/unit/runner_test.rb +87 -0
  21. data/test/{test_and_subcontext_access_test.rb → unit/test_and_subcontext_access_test.rb} +6 -33
  22. data/test/usage/01_basic_usage_test.rb +131 -0
  23. data/test/usage/02_setup_test.rb +98 -0
  24. data/test/usage/03_teardown_test.rb +121 -0
  25. data/test/usage/04_pending_tests_test.rb +16 -0
  26. data/test/usage/05_aliases_test.rb +73 -0
  27. data/test/usage/06_defining_methods_in_tests_test.rb +202 -0
  28. data/test/usage/07_exceptions_test.rb +42 -0
  29. data/test/usage/08_start_and_finish_test.rb +261 -0
  30. data/test/usage/09_expectations_and_mocking_test.rb +85 -0
  31. data/test/usage/10_let_and_subject_test.rb +134 -0
  32. data/test/usage/11_matcher_test.rb +148 -0
  33. data/test/usage/12_action_test.rb +118 -0
  34. metadata +55 -48
  35. data/test/aliases_test.rb +0 -26
  36. data/test/automatic_running_test.rb +0 -45
  37. data/test/exceptions_test.rb +0 -40
  38. data/test/kintama_test.rb +0 -114
  39. data/test/line_based_running_test.rb +0 -143
  40. data/test/matcher_test.rb +0 -80
  41. data/test/method_behaviour_test.rb +0 -176
  42. data/test/pending_test_and_context.rb +0 -20
  43. data/test/setup_test.rb +0 -107
  44. data/test/start_and_finish_test.rb +0 -94
  45. data/test/teardown_test.rb +0 -106
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+ require 'kintama/mocha'
3
+
4
+ class ExpectationsAndMockingTest < KintamaIntegrationTest
5
+
6
+ def setup
7
+ super
8
+ # This is normally called automatically when requiring 'kintama/mocha'
9
+ # but in these tests we totally reset Kintama, and so we need to
10
+ # ensure that the mocha integration is loaded properly.
11
+ Kintama::Mocha.setup
12
+ end
13
+
14
+ def test_should_allow_setting_of_expectations_in_tests
15
+ context "Given an expectation" do
16
+ setup do
17
+ @thing = stub('thing')
18
+ end
19
+
20
+ expect "blah to be called" do
21
+ @thing.expects(:blah)
22
+ @thing.blah
23
+ end
24
+ end.
25
+ should_output(%{
26
+ Given an expectation
27
+ expect blah to be called
28
+ }).
29
+ and_pass
30
+ end
31
+
32
+ def test_should_report_failed_expectations_as_failures
33
+ context "Given an expectation" do
34
+ setup do
35
+ @thing = stub('thing')
36
+ end
37
+
38
+ expect "blah to be called" do
39
+ @thing.expects(:blah)
40
+ end
41
+ end.
42
+ should_fail.
43
+ with_failure(%{
44
+ unsatisfied expectations:
45
+ - expected exactly once, invoked never: #<Mock:thing>.blah
46
+ })
47
+ end
48
+
49
+ def test_should_set_expectations_before_action_is_called
50
+ context "Given an action" do
51
+ setup do
52
+ @thing = stub('thing')
53
+ end
54
+
55
+ action do
56
+ @thing.go
57
+ end
58
+
59
+ expect "go to be called on thing" do
60
+ @thing.expects(:go)
61
+ end
62
+ end.
63
+ should_run_tests(1).
64
+ and_pass
65
+ end
66
+
67
+ def test_should_not_set_expectations_for_normal_tests_defined_near_the_expect
68
+ context "Given an expectation" do
69
+ setup do
70
+ @thing = [1,2,3]
71
+ end
72
+
73
+ expect "blah to be called" do
74
+ @thing.expects(:join)
75
+ @thing.join
76
+ end
77
+
78
+ it "should retain original behaviour in other tests" do
79
+ assert_equal "123", @thing.join
80
+ end
81
+ end.
82
+ should_run_tests(2).
83
+ and_pass
84
+ end
85
+ end
@@ -0,0 +1,134 @@
1
+ require 'test_helper'
2
+
3
+ class LetAndSubjectTest < KintamaIntegrationTest
4
+
5
+ def test_let_should_return_value_of_block_when_called_in_tests
6
+ context "Defining a `let` attribute with a given name in a context" do
7
+ let(:thing) do
8
+ "a thing"
9
+ end
10
+
11
+ should "allow that attribute to be called as a method within tests" do
12
+ assert_equal "a thing", thing
13
+ end
14
+ end.
15
+ should_run_tests(1).
16
+ and_pass
17
+ end
18
+
19
+ def test_let_should_return_the_same_instance_within_a_test
20
+ context "Defining a `let` attribute in a context" do
21
+ let(:thing) do
22
+ Object.new
23
+ end
24
+
25
+ should "memoize the returned object within a test" do
26
+ instance_a = thing()
27
+ instance_b = thing()
28
+ assert_equal instance_a.object_id, instance_b.object_id
29
+ end
30
+ end.
31
+ should_run_tests(1).
32
+ and_pass
33
+ end
34
+
35
+ def test_let_should_return_the_same_instance_within_a_test_and_its_setup
36
+ context "Defining a `let` attribute in a context" do
37
+ let(:thing) do
38
+ Object.new
39
+ end
40
+
41
+ setup do
42
+ $object_id = thing.object_id
43
+ end
44
+
45
+ should "memoize the returned object between setup and test" do
46
+ assert $object_id && $object_id == thing.object_id
47
+ end
48
+ end.
49
+ should_run_tests(1).
50
+ and_pass
51
+ end
52
+
53
+ def test_let_should_return_different_instances_in_different_tests
54
+ context "Defining a `let` attribute in a context" do
55
+ let(:thing) do
56
+ Object.new
57
+ end
58
+
59
+ should "return a one instance in one test" do
60
+ $object_id = thing.object_id
61
+ end
62
+
63
+ should "return some other instance in a different test" do
64
+ assert $object_id && $object_id != thing.object_id
65
+ end
66
+ end.
67
+ should_run_tests(2).
68
+ and_pass
69
+ end
70
+
71
+ def test_let_methods_should_be_callable_from_other_let_methods
72
+ context "Defining a `let` attribute in a context" do
73
+ let(:alpha) do
74
+ 123
75
+ end
76
+
77
+ let(:beta) do
78
+ alpha == 123
79
+ end
80
+
81
+ should "allow one `let` attribute to be referenced from another one" do
82
+ assert beta
83
+ end
84
+ end.
85
+ should_run_tests(1).
86
+ and_pass
87
+ end
88
+
89
+ def test_subject_should_work_just_like_lets
90
+ $object_id_in_test_one = nil
91
+ $object_id_in_test_two = nil
92
+
93
+ context "Defining a `subject` attribute in a context" do
94
+ subject do
95
+ Object.new
96
+ end
97
+
98
+ should "return one instance in one test" do
99
+ $object_id_in_test_one = subject.object_id
100
+ if $object_id_in_test_two # this test might run first
101
+ assert_not_equal $object_id_in_test_one, $object_id_in_test_two, "object ids should be different between tests"
102
+ end
103
+ end
104
+
105
+ should "return a different instance in a different test" do
106
+ $object_id_in_test_two = subject.object_id
107
+ if $object_id_in_test_one # or this test might run first
108
+ assert_not_equal $object_id_in_test_one, $object_id_in_test_two, "object ids should be different between tests"
109
+ end
110
+ end
111
+ end.
112
+ should_run_tests(2).
113
+ and_pass
114
+ end
115
+
116
+ def test_should_use_a_single_instance_of_the_subject_within_a_test
117
+ context "Given a context with a subject" do
118
+ subject do
119
+ Array.new
120
+ end
121
+
122
+ should "allow me to poke around with subject like it was a variable" do
123
+ subject << 1
124
+ assert_equal [1], subject
125
+ end
126
+
127
+ should "now be empty again because it's a new instance" do
128
+ assert subject.empty?
129
+ end
130
+ end.
131
+ should_run_tests(2).
132
+ and_pass
133
+ end
134
+ end
@@ -0,0 +1,148 @@
1
+ require "test_helper"
2
+
3
+ class MatcherTest < KintamaIntegrationTest
4
+
5
+ # As well as defining tests using blocks (see Basic Usage),
6
+ # the `should` method can define tests by accepting instances
7
+ # of "matcher" classes.
8
+ def test_should_allow_use_of_matchers_within_contexts
9
+ context "The number 123" do
10
+ subject do
11
+ 123
12
+ end
13
+
14
+ should EqualMatcher.new(123)
15
+ end.
16
+ should_run_tests(1).
17
+ and_pass
18
+ end
19
+
20
+ # A matcher is just an object that responds to four methods:
21
+ #
22
+ # * `matches?(provided_value)`, which will be called with the `subject` object
23
+ # as the argument. This is where you can compare aspects of the subject
24
+ # with some expected value or criteria. It should return true or false.
25
+ #
26
+ # * `description`, which should return a String to form part of the test name
27
+ #
28
+ # * `failure_message`, which returns a String describing what happened if
29
+ # `matches?` returned false
30
+ #
31
+ # * `negative_failure_message`, which returns a String describing what
32
+ # happened if the matcher was being used in a negated form (see below)
33
+ #
34
+ # This is an example Matcher object which can check the equality of the
35
+ # subject against a given value. It's used above, and in the tests below.
36
+ class EqualMatcher
37
+ def initialize(expected)
38
+ @expected = expected
39
+ end
40
+
41
+ def matches?(provided_value)
42
+ @actual = provided_value
43
+ @actual == @expected
44
+ end
45
+
46
+ def description
47
+ "be equal to #{@expected.inspect}"
48
+ end
49
+
50
+ def failure_message
51
+ "Expected #{@expected}, but got #{@actual}"
52
+ end
53
+
54
+ def negative_failure_message
55
+ "Didn't expect #{@expected}, but got it anyway"
56
+ end
57
+ end
58
+
59
+ def test_should_use_the_description_of_matchers_to_generate_test_names
60
+ context "The number 123" do
61
+ subject do
62
+ 123
63
+ end
64
+
65
+ should EqualMatcher.new(123)
66
+ end.
67
+ should_output(%{
68
+ The number 123
69
+ should be equal to 123: .
70
+ }).
71
+ and_pass
72
+ end
73
+
74
+ def test_should_use_the_failure_message_of_matchers_to_generate_failure_messages
75
+ context "The number 456" do
76
+ subject do
77
+ 456
78
+ end
79
+
80
+ should EqualMatcher.new(123)
81
+ end.
82
+ should_fail.
83
+ with_failure("Expected 123, but got 456")
84
+ end
85
+
86
+ def test_should_allow_negation_of_matchers
87
+ context "The number 123" do
88
+ subject do
89
+ 123
90
+ end
91
+
92
+ should_not EqualMatcher.new(456)
93
+ end.
94
+ should_run_tests(1).
95
+ and_pass
96
+ end
97
+
98
+ def test_should_generate_corresponding_test_names_for_negated_matchers
99
+ context "The number 123" do
100
+ subject do
101
+ 123
102
+ end
103
+
104
+ should_not EqualMatcher.new(456)
105
+ end.
106
+ should_output(%{
107
+ The number 123
108
+ should not be equal to 456: .
109
+ }).
110
+ and_pass
111
+ end
112
+
113
+ def test_should_use_the_negated_failure_message_of_negated_matchers_to_generate_failure_descriptions
114
+ context "The number 123" do
115
+ subject do
116
+ 123
117
+ end
118
+
119
+ should_not EqualMatcher.new(123)
120
+ end.
121
+ should_fail.
122
+ with_failure("Didn't expect 123, but got it anyway")
123
+ end
124
+
125
+ # You can also define methods to extend kintama for more readable
126
+ # tests; instead of `should EqualMatcher.new(value)` you could write
127
+ # `should be_equal_to(value)`
128
+ module MethodsWhichReturnMatcherInstances
129
+ def be_equal_to(expected)
130
+ EqualMatcher.new(expected)
131
+ end
132
+ end
133
+
134
+ def test_should_allow_definition_of_matchers_in_contexts
135
+ Kintama.extend(MethodsWhichReturnMatcherInstances)
136
+
137
+ context "The number 123" do
138
+ subject do
139
+ 123
140
+ end
141
+
142
+ should be_equal_to(123)
143
+ should_not be_equal_to(456)
144
+ end.
145
+ should_run_tests(2).
146
+ and_pass
147
+ end
148
+ end
@@ -0,0 +1,118 @@
1
+ require 'test_helper'
2
+
3
+ class ActionTest < KintamaIntegrationTest
4
+
5
+ def test_should_run_action_between_setup_and_test
6
+ context "Given a context with an `action`" do
7
+ setup do
8
+ @thing = 123
9
+ end
10
+
11
+ action do
12
+ @thing += 1
13
+ end
14
+
15
+ it "should have called the action between the `setup` and the test" do
16
+ assert @thing == 124
17
+ end
18
+ end.
19
+ should_output(%{
20
+ Given a context with an `action`
21
+ it should have called the action between the `setup` and the test: .
22
+ }).
23
+ and_pass
24
+ end
25
+
26
+ def test_should_run_action_after_all_setups
27
+ context "Given an action" do
28
+ setup do
29
+ @thing = 123
30
+ end
31
+
32
+ setup do
33
+ @thing *= 2
34
+ end
35
+
36
+ action do
37
+ @thing += 1
38
+ end
39
+
40
+ it "should have called the action after all setup blocks" do
41
+ assert @thing == 247
42
+ end
43
+ end.
44
+ should_output(%{
45
+ Given an action
46
+ it should have called the action after all setup blocks
47
+ }).
48
+ and_pass
49
+ end
50
+
51
+ def test_should_run_action_after_setups_in_nested_contexts
52
+ context "Given an action" do
53
+ setup do
54
+ @thing = 123
55
+ end
56
+
57
+ action do
58
+ @thing += 1
59
+ end
60
+
61
+ it "should have called the action after the first setup for outer tests" do
62
+ assert @thing == 124
63
+ end
64
+
65
+ context "and a setup in a nested context" do
66
+ setup do
67
+ @thing *= 2
68
+ end
69
+ it "should have called the action after both outer and inner setups" do
70
+ assert @thing == 247
71
+ end
72
+ end
73
+ end.
74
+ should_output(%{
75
+ Given an action
76
+ it should have called the action after the first setup for outer tests: .
77
+ and a setup in a nested context
78
+ it should have called the action after both outer and inner setups: .
79
+ }).
80
+ and_pass
81
+ end
82
+
83
+ def test_should_allow_subcontexts_to_change_parameters_used_in_action
84
+ context "A doubling action" do
85
+ action do
86
+ @result = @parameter * 2
87
+ end
88
+
89
+ context "when the parameter is 2" do
90
+ setup do
91
+ @parameter = 2
92
+ end
93
+
94
+ should "result in 4" do
95
+ assert_equal 4, @result
96
+ end
97
+ end
98
+
99
+ context "when the parameter is 3" do
100
+ setup do
101
+ @parameter = 3
102
+ end
103
+
104
+ should "result in 6" do
105
+ assert_equal 6, @result
106
+ end
107
+ end
108
+ end.
109
+ should_output(%{
110
+ A doubling action
111
+ when the parameter is 2
112
+ should result in 4: .
113
+ when the parameter is 3
114
+ should result in 6: .
115
+ }).
116
+ and_pass
117
+ end
118
+ end