kintama 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ require 'test_helper'
2
+
3
+ class VerboseRunnerTest < Test::Unit::TestCase
4
+ def test_should_print_out_test_names
5
+ c = context "given something" do
6
+ should "also pass" do
7
+ assert true
8
+ end
9
+ should "pass" do
10
+ assert true
11
+ end
12
+ end
13
+ assert_output(/^given something\n should also pass: \.\n should pass: \./) do
14
+ runner(c).run(false)
15
+ end
16
+ end
17
+
18
+ def test_should_print_out_Ps_beside_pending_test_names
19
+ c = context "given something" do
20
+ should "not be implemented"
21
+ should "pass" do
22
+ assert true
23
+ end
24
+ end
25
+ assert_output(/^given something\n should not be implemented: P\n should pass: \./) do
26
+ runner(c).run(false)
27
+ end
28
+ end
29
+
30
+ def test_should_nest_printed_context_and_test_names
31
+ c = context "given something" do
32
+ should "pass" do
33
+ assert true
34
+ end
35
+ context "and then this" do
36
+ should "also pass" do
37
+ assert true
38
+ end
39
+ end
40
+ context "and something else" do
41
+ should "pass" do
42
+ assert true
43
+ end
44
+ end
45
+ end
46
+ assert_output(/^given something\n should pass: \.\n and something else\n should pass: \.\n and then this\n should also pass: \./) do
47
+ runner(c).run(false)
48
+ end
49
+ end
50
+
51
+ def test_should_print_out_a_summary_of_the_failing_tests_if_some_fail
52
+ c = context "given something" do
53
+ should "fail" do
54
+ assert 1 == 2, "1 should equal 2"
55
+ end
56
+ end
57
+ assert_output(/given something should fail:\n 1 should equal 2/) { runner(c).run(false) }
58
+ end
59
+
60
+ def test_should_print_out_a_summary_of_the_failing_tests_if_an_exception_occurs_in_a_test
61
+ c = context "given something" do
62
+ should "fail" do
63
+ raise "unexpected issue!"
64
+ end
65
+ end
66
+ assert_output(/given something should fail:\n unexpected issue!/) { runner(c).run(false) }
67
+ end
68
+
69
+ def test_should_print_out_a_summary_of_the_failing_tests_if_a_nested_test_fails
70
+ c = context "given something" do
71
+ context "and something else" do
72
+ should "fail" do
73
+ assert 1 == 2, "1 should equal 2"
74
+ end
75
+ end
76
+ end
77
+ assert_output(/given something and something else should fail:\n 1 should equal 2/) { runner(c).run(false) }
78
+ end
79
+
80
+ def test_should_nest_verbose_output_properly_when_running_tests_from_several_contexts
81
+ c1 = context "given something" do
82
+ should "pass" do
83
+ assert true
84
+ end
85
+ end
86
+ c2 = context "given another thing" do
87
+ should "also pass" do
88
+ assert true
89
+ end
90
+ end
91
+ assert_output(/^given something\n should pass: \.\n\ngiven another thing\n should also pass: \./) do
92
+ runner(c1, c2).run(false)
93
+ end
94
+ end
95
+
96
+ def test_should_print_out_test_names_in_colour_if_colour_is_set
97
+ c = context "given something" do
98
+ should "be red" do
99
+ flunk
100
+ end
101
+ should "be green" do
102
+ assert true
103
+ end
104
+ should "be yellow"
105
+ end
106
+ assert_output(/^given something\n\e\[32m should be green\e\[0m\n\e\[31m should be red\e\[0m\n\e\[33m should be yellow\e\[0m/) do
107
+ runner(c).run(colour=true)
108
+ end
109
+ end
110
+
111
+ def test_should_print_appropriate_test_names_when_given_and_it_aliases_are_used
112
+ c = context "In a world without hope" do
113
+ given "a massive gun" do
114
+ it "should work out well in the end" do
115
+ assert true
116
+ end
117
+ end
118
+ end
119
+ assert_output(/^In a world without hope\n given a massive gun\n it should work out well in the end: \./) do
120
+ runner(c).run(false)
121
+ end
122
+ end
123
+
124
+ private
125
+
126
+ def runner(*args)
127
+ Kintama::Runner::Verbose.new(*args)
128
+ end
129
+ end
@@ -0,0 +1,107 @@
1
+ require 'test_helper'
2
+
3
+ class SetupTest < Test::Unit::TestCase
4
+
5
+ def test_should_allow_setup_to_provide_instance_variables
6
+ x = context "Given something" do
7
+ setup do
8
+ @name = "james"
9
+ end
10
+ should "work" do
11
+ assert_equal "james", @name
12
+ end
13
+ end
14
+ x.run
15
+ assert x.passed?
16
+ end
17
+
18
+ def test_should_run_setup_before_every_test
19
+ x = context "Given something" do
20
+ setup do
21
+ @name = "james"
22
+ end
23
+ should "work" do
24
+ @name += " is awesome"
25
+ assert_equal "james is awesome", @name
26
+ end
27
+ should "also work" do
28
+ @name += " is the best"
29
+ assert_equal "james is the best", @name
30
+ end
31
+ end
32
+ x.run
33
+ assert x.passed?, x.failures.join(", ")
34
+ end
35
+
36
+ def test_should_allow_call_all_setup_methods_when_running_tests_in_a_nested_context
37
+ x = context "Given something" do
38
+ setup do
39
+ @name = "james"
40
+ end
41
+ context "and another thing" do
42
+ setup do
43
+ @name += " is amazing"
44
+ end
45
+ should "work" do
46
+ assert_equal "james is amazing", @name
47
+ end
48
+ end
49
+ end
50
+ x.run
51
+ assert x.passed?
52
+ end
53
+
54
+ def test_should_only_run_necessary_setups_where_tests_at_different_nestings_exist
55
+ x = context "Given something" do
56
+ setup do
57
+ @name = "james"
58
+ end
59
+ context "and another thing" do
60
+ setup do
61
+ @name += " is amazing"
62
+ end
63
+ should "work" do
64
+ assert_equal "james is amazing", @name
65
+ end
66
+ end
67
+ should "work" do
68
+ assert_equal "james", @name
69
+ end
70
+ end
71
+ x.run
72
+ assert x.passed?
73
+ end
74
+
75
+ def test_should_run_setup_defined_on_kintama_itself_before_other_setups
76
+ Kintama.setup do
77
+ @thing = 'well then'
78
+ end
79
+ c = context "Given a context" do
80
+ setup do
81
+ assert_equal 'well then', @thing
82
+ @thing = 'now then'
83
+ end
84
+ should "have run the setup defined in the default behaviour" do
85
+ assert_equal 'now then', @thing
86
+ end
87
+ end
88
+ c.run
89
+ assert c.passed?, "@thing was not defined!"
90
+ end
91
+
92
+ def test_should_allow_multiple_setups_to_be_registered
93
+ Kintama.setup do
94
+ @thing = 1
95
+ end
96
+ Kintama.setup do
97
+ @thing += 1
98
+ end
99
+ c = context "Given multiple setups" do
100
+ should "run them all" do
101
+ assert_equal 2, @thing
102
+ end
103
+ end
104
+ c.run
105
+ assert c.passed?, "both setups didn't run - #{c.failures.inspect}"
106
+ end
107
+ end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class TeardownTest < Test::Unit::TestCase
4
+
5
+ def test_should_run_teardown_after_the_test_finishes
6
+ $called = false
7
+ x = context "Given a teardown" do
8
+ teardown do
9
+ raise "Argh" unless @result == 123
10
+ $called = true
11
+ end
12
+ should "run teardown after this test" do
13
+ @result = 123
14
+ end
15
+ end
16
+ x.run
17
+ assert x.passed?
18
+ assert $called
19
+ end
20
+
21
+ def test_should_run_all_teardowns_in_proximity_of_nesting_order_after_a_nested_test_finishes
22
+ $called = false
23
+ x = context "Given a teardown" do
24
+ teardown do
25
+ raise "Argh" unless @result == 123
26
+ $called = true
27
+ end
28
+ context "with a subcontext with another teardown" do
29
+ teardown do
30
+ raise "Oh no" unless @result == 456
31
+ @result = 123
32
+ end
33
+ should "run teardown after this test" do
34
+ @result = 456
35
+ end
36
+ end
37
+ end
38
+ x.run
39
+ assert x.passed?
40
+ assert $called
41
+ end
42
+
43
+ def test_should_run_teardown_defined_on_kintama_itself_after_other_teardowns
44
+ ran = false
45
+ Kintama.teardown do
46
+ ran = true
47
+ assert_equal 'blah', @thing
48
+ end
49
+ c = context "Given a context" do
50
+ should "have run the setup defined in the default behaviour" do
51
+ # nothing
52
+ end
53
+ teardown do
54
+ @thing = 'blah'
55
+ end
56
+ end
57
+ c.run
58
+ assert c.passed?, "@thing was not redefined!"
59
+ assert ran
60
+ end
61
+
62
+ def test_should_allow_multiple_teardowns_to_be_registered
63
+ Kintama.teardown do
64
+ $ran = 1
65
+ end
66
+ Kintama.teardown do
67
+ $ran += 1
68
+ end
69
+ c = context "Given multiple setups" do
70
+ should "run them all" do
71
+ assert true
72
+ end
73
+ end
74
+ c.run
75
+ assert_equal 2, $ran, "both teardowns didn't run"
76
+ end
77
+
78
+ def test_should_run_teardowns_even_after_exceptions
79
+ ran = false
80
+ c = context "Given a test that fails" do
81
+ should "still run teardown" do
82
+ raise "argh"
83
+ end
84
+ teardown do
85
+ ran = true
86
+ end
87
+ end
88
+ c.run
89
+ assert !c.passed?
90
+ assert ran
91
+ end
92
+ end
@@ -0,0 +1,110 @@
1
+ require 'test_helper'
2
+
3
+ class TestAndSubcontextAccessTest < Test::Unit::TestCase
4
+
5
+ def test_should_stash_all_defined_contexts_so_they_can_be_accessed_later
6
+ c1 = context "Given some context" do
7
+ should "stash this" do
8
+ end
9
+ end
10
+ c2 = context "Given some other context" do
11
+ should "also stash this" do
12
+ end
13
+ end
14
+ assert_equal [c1, c2], Kintama.default_context.subcontexts
15
+ end
16
+
17
+ def test_should_allow_running_of_specific_subcontexts
18
+ x = context "Given something" do
19
+ should "not be run" do
20
+ flunk
21
+ end
22
+ context "and another thing" do
23
+ should "pass" do
24
+ assert true
25
+ end
26
+ end
27
+ end
28
+ inner_context = x.and_another_thing
29
+ inner_context.run
30
+ assert inner_context.passed?
31
+ end
32
+
33
+ def test_should_allow_running_of_specific_tests
34
+ x = context "Given something" do
35
+ should "fail when run" do
36
+ flunk
37
+ end
38
+ end
39
+ t = x.should_fail_when_run.new
40
+ t.run
41
+ assert !t.passed?
42
+ end
43
+
44
+ def test_should_allow_running_of_specific_subcontexts_using_hashlike_syntax
45
+ x = context "Given something" do
46
+ should "not be run" do
47
+ flunk
48
+ end
49
+ context "and another thing" do
50
+ should "pass" do
51
+ assert true
52
+ end
53
+ end
54
+ end
55
+ inner_context = x["and another thing"]
56
+ inner_context.run
57
+ assert inner_context.passed?
58
+ end
59
+
60
+ def test_should_allow_running_of_specific_tests_using_hashlike_syntax
61
+ x = context "Given something" do
62
+ should "fail when run" do
63
+ flunk
64
+ end
65
+ end
66
+ t = x["should fail when run"].new
67
+ t.run
68
+ assert !t.passed?
69
+ end
70
+
71
+ def test_should_return_true_if_running_a_subcontext_passes
72
+ x = context "Given something" do
73
+ context "and another thing" do
74
+ should "pass" do
75
+ assert true
76
+ end
77
+ end
78
+ end
79
+ assert_equal true, x.and_another_thing.run
80
+ end
81
+
82
+ def test_should_return_true_if_running_a_test_passes
83
+ x = context "Given something" do
84
+ should "pass when run" do
85
+ assert true
86
+ end
87
+ end
88
+ assert_equal true, x.should_pass_when_run.run
89
+ end
90
+
91
+ def test_should_return_false_if_running_a_subcontext_fails
92
+ x = context "Given something" do
93
+ context "and another thing" do
94
+ should "fail" do
95
+ flunk
96
+ end
97
+ end
98
+ end
99
+ assert_equal false, x.and_another_thing.run
100
+ end
101
+
102
+ def test_should_return_false_if_running_a_test_fails
103
+ x = context "Given something" do
104
+ should "fail when run" do
105
+ flunk
106
+ end
107
+ end
108
+ assert_equal false, x.should_fail_when_run.run
109
+ end
110
+ end
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require 'test/unit'
3
+
4
+ ENV["KINTAMA_EXPLICITLY_DONT_RUN"] = "true"
5
+ require 'kintama'
6
+
7
+ require 'stringio'
8
+
9
+ class Test::Unit::TestCase
10
+ def setup
11
+ Kintama.reset
12
+ end
13
+
14
+ private
15
+
16
+ module ::Kernel
17
+ def capture_stdout
18
+ out = StringIO.new
19
+ $stdout = out
20
+ yield
21
+ out.rewind
22
+ return out
23
+ ensure
24
+ $stdout = STDOUT
25
+ end
26
+ end
27
+
28
+ def assert_output(expected, &block)
29
+ output = capture_stdout(&block).read
30
+ if expected.is_a?(Regexp)
31
+ assert_match expected, output
32
+ else
33
+ assert_equal expected, output
34
+ end
35
+ end
36
+ end