kintama 0.1.9 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,147 +1,149 @@
1
1
  require 'test_helper'
2
2
 
3
- class VerboseReporterTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @reporter = Kintama::Reporter::Verbose.new(false)
7
- end
3
+ class VerboseReporterTest < KintamaIntegrationTest
4
+ report_with Kintama::Reporter::Verbose
8
5
 
9
6
  def test_should_print_out_test_names
10
- c = context "given something" do
11
- should "also pass" do
7
+ context "given something" do
8
+ should "pass" do
12
9
  assert true
13
10
  end
14
- should "pass" do
11
+ should "pass too" do
15
12
  assert true
16
13
  end
17
- end
18
- assert_output(/^given something\n should also pass: \.\n should pass: \./) do
19
- runner(c).run(@reporter)
20
- end
14
+ end.
15
+ should_output(%{
16
+ given something
17
+ should pass: .
18
+ should pass too: .
19
+ })
21
20
  end
22
21
 
23
22
  def test_should_print_out_Ps_beside_pending_test_names
24
- c = context "given something" do
23
+ context "given something" do
25
24
  should "not be implemented"
26
25
  should "pass" do
27
26
  assert true
28
27
  end
29
- end
30
- assert_output(/^given something\n should not be implemented: P\n should pass: \./) do
31
- runner(c).run(@reporter)
32
- end
28
+ end.
29
+ should_output(%{
30
+ given something
31
+ should not be implemented: P
32
+ should pass: .
33
+ })
33
34
  end
34
35
 
35
36
  def test_should_nest_printed_context_and_test_names
36
- c = context "given something" do
37
+ context "given something" do
37
38
  should "pass" do
38
39
  assert true
39
40
  end
40
- context "and then this" do
41
- should "also pass" do
41
+ context "and something else" do
42
+ should "pass" do
42
43
  assert true
43
44
  end
44
45
  end
45
- context "and something else" do
46
- should "pass" do
46
+ context "and then this" do
47
+ should "also pass" do
47
48
  assert true
48
49
  end
49
50
  end
50
- end
51
- assert_output(/^given something\n should pass: \.\n and something else\n should pass: \.\n and then this\n should also pass: \./) do
52
- runner(c).run(@reporter)
53
- end
51
+ end.
52
+ should_output(%{
53
+ given something
54
+ should pass: .
55
+ and something else
56
+ should pass: .
57
+ and then this
58
+ should also pass: .
59
+ })
54
60
  end
55
61
 
56
62
  def test_should_print_out_a_summary_of_the_failing_tests_if_some_fail
57
- c = context "given something" do
63
+ context "given something" do
58
64
  should "fail" do
59
65
  assert 1 == 2, "1 should equal 2"
60
66
  end
61
- end
62
- assert_output(/given something should fail:\n 1 should equal 2/) { runner(c).run(@reporter) }
67
+ end.
68
+ should_output(%{
69
+ given something should fail:
70
+ 1 should equal 2
71
+ })
63
72
  end
64
73
 
65
74
  def test_should_print_out_a_summary_of_the_failing_tests_if_an_exception_occurs_in_a_test
66
- c = context "given something" do
75
+ context "given something" do
67
76
  should "fail" do
68
77
  raise "unexpected issue!"
69
78
  end
70
- end
71
- assert_output(/given something should fail:\n unexpected issue!/) { runner(c).run(@reporter) }
79
+ end.
80
+ should_output(%{
81
+ given something should fail:
82
+ unexpected issue!
83
+ })
72
84
  end
73
85
 
74
86
  def test_should_print_out_a_summary_of_the_failing_tests_if_a_nested_test_fails
75
- c = context "given something" do
87
+ context "given something" do
76
88
  context "and something else" do
77
89
  should "fail" do
78
90
  assert 1 == 2, "1 should equal 2"
79
91
  end
80
92
  end
81
- end
82
- assert_output(/given something and something else should fail:\n 1 should equal 2/) { runner(c).run(@reporter) }
83
- end
84
-
85
- def test_should_nest_verbose_output_properly_when_running_tests_from_several_contexts
86
- c1 = context "given something" do
87
- should "pass" do
88
- assert true
89
- end
90
- end
91
- c2 = context "given another thing" do
92
- should "also pass" do
93
- assert true
94
- end
95
- end
96
- assert_output(/^given something\n should pass: \.\n\ngiven another thing\n should also pass: \./) do
97
- runner(c1, c2).run(@reporter)
98
- end
93
+ end.
94
+ should_output(%{
95
+ given something and something else should fail:
96
+ 1 should equal 2
97
+ })
99
98
  end
100
99
 
101
100
  def test_should_treat_a_context_as_transparent_if_it_has_no_name
102
- c1 = context "given something" do
101
+ context "given something" do
103
102
  context do
104
103
  should "pass" do
105
104
  assert true
106
105
  end
107
106
  end
108
- end
109
- assert_output(/^given something\n should pass: \./) do
110
- runner(c1).run(@reporter)
111
- end
107
+ end.
108
+ should_output(%{
109
+ given something
110
+ should pass: .
111
+ })
112
112
  end
113
113
 
114
114
  def test_should_print_out_test_names_in_colour_if_colour_is_set
115
- c = context "given something" do
116
- should "be red" do
115
+ use_colour = true
116
+ use_reporter Kintama::Reporter::Verbose.new(use_colour)
117
+
118
+ context "given tests reported in colour" do
119
+ should "show failures in red" do
117
120
  flunk
118
121
  end
119
- should "be green" do
122
+ should "show passes in green" do
120
123
  assert true
121
124
  end
122
- should "be yellow"
123
- end
124
- 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
125
- runner(c).run(Kintama::Reporter::Verbose.new(true))
126
- end
125
+ should "show pending tests in yellow"
126
+ end.
127
+ should_output(%{
128
+ given tests reported in colour
129
+ \e\[31m should show failures in red\e\[0m
130
+ \e\[32m should show passes in green\e\[0m
131
+ \e\[33m should show pending tests in yellow\e\[0m
132
+ })
127
133
  end
128
134
 
129
135
  def test_should_print_appropriate_test_names_when_given_and_it_aliases_are_used
130
- c = context "In a world without hope" do
136
+ context "In a world without hope" do
131
137
  given "a massive gun" do
132
138
  it "should work out well in the end" do
133
139
  assert true
134
140
  end
135
141
  end
136
- end
137
- assert_output(/^In a world without hope\n given a massive gun\n it should work out well in the end: \./) do
138
- runner(c).run(@reporter)
139
- end
140
- end
141
-
142
- private
143
-
144
- def runner(*args)
145
- Kintama::Runner::Default.new.with(*args)
142
+ end.
143
+ should_output(%{
144
+ In a world without hope
145
+ given a massive gun
146
+ it should work out well in the end: .
147
+ })
146
148
  end
147
- end
149
+ end
@@ -1,12 +1,14 @@
1
1
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
- require 'test/unit'
2
+ require 'bundler/setup'
3
+ require 'minitest/autorun'
3
4
 
4
5
  ENV["KINTAMA_EXPLICITLY_DONT_RUN"] = "true"
5
6
  require 'kintama'
6
7
 
7
8
  require 'stringio'
9
+ require 'mocha/minitest'
8
10
 
9
- class Test::Unit::TestCase
11
+ class Minitest::Test
10
12
  def setup
11
13
  Kintama.reset
12
14
  end
@@ -23,6 +25,13 @@ class Test::Unit::TestCase
23
25
  ensure
24
26
  $stdout = STDOUT
25
27
  end
28
+
29
+ def silence_stdout
30
+ $stdout = StringIO.new
31
+ return yield
32
+ ensure
33
+ $stdout = STDOUT
34
+ end
26
35
  end
27
36
 
28
37
  def assert_output(expected, &block)
@@ -34,3 +43,151 @@ class Test::Unit::TestCase
34
43
  end
35
44
  end
36
45
  end
46
+
47
+ class KintamaIntegrationTest < Minitest::Test
48
+ class << self
49
+ def reporter_class
50
+ @reporter_class ||= Kintama::Reporter::Verbose
51
+ end
52
+
53
+ def report_with(reporter_class)
54
+ @reporter_class = reporter_class
55
+ end
56
+ end
57
+
58
+ attr_reader :reporter
59
+
60
+ private
61
+
62
+ def use_reporter(reporter)
63
+ @reporter = reporter
64
+ end
65
+
66
+ def context(name, &block)
67
+ ContextTestRunner.new(Kintama.context(name, &block), self)
68
+ end
69
+
70
+ def running_default_context(name, &block)
71
+ Kintama.context(name, &block)
72
+ ContextTestRunner.new(Kintama.default_context, self)
73
+ end
74
+
75
+ class ContextTestRunner
76
+ def initialize(context, test_unit_test)
77
+ @test_unit_test = test_unit_test
78
+ @context = context
79
+ @result = nil
80
+ use_colour = false
81
+ @reporter = @test_unit_test.reporter || test_unit_test.class.reporter_class.new(use_colour)
82
+ @output = capture_stdout do
83
+ @result = Kintama::Runner.default.with(context).run(@reporter)
84
+ end.read
85
+ end
86
+
87
+ def should_output(expected_output)
88
+ if expected_output.is_a?(Regexp)
89
+ processed_output = expected_output
90
+ else
91
+ processed_output = deindent_string_argument(expected_output)
92
+ end
93
+ @test_unit_test.assert_match processed_output, @output
94
+ self
95
+ end
96
+ alias_method :and_output, :should_output
97
+
98
+ def should_pass(message=nil)
99
+ @test_unit_test.assert(@result == true, message || "Expected a pass, but failed: #{@context.failures.map { |f| f.failure.message }.join(", ")}")
100
+ self
101
+ end
102
+ alias_method :and_pass, :should_pass
103
+
104
+ def should_fail(message=nil)
105
+ @test_unit_test.assert(@result == false, message || "Expected a failure, but passed!")
106
+ self
107
+ end
108
+ alias_method :and_fail, :should_fail
109
+
110
+ def with_failure(failure)
111
+ @test_unit_test.assert_match deindent_string_argument(failure), @output
112
+ self
113
+ end
114
+
115
+ def should_run_tests(number)
116
+ @test_unit_test.assert_equal number, @reporter.test_count, "Expected #{number} tests to run, but #{@reporter.test_count} actually did"
117
+ self
118
+ end
119
+
120
+ private
121
+
122
+ def deindent_string_argument(string)
123
+ initial_indent = string.gsub(/^\n/, '').match(/^(\s+)/)
124
+ initial_indent = initial_indent ? initial_indent[1] : ""
125
+ string.gsub("\n#{initial_indent}", "\n").gsub(/^\n/, '').gsub(/\s+$/, '')
126
+ end
127
+ end
128
+
129
+ def test_with_content(content)
130
+ IntegrationTestRunner.new(self, content)
131
+ end
132
+
133
+ class IntegrationTestRunner
134
+ def initialize(test_unit_test, test_body)
135
+ @test_unit_test = test_unit_test
136
+ @test_body = test_body
137
+ end
138
+
139
+ def run(options = nil, &block)
140
+ path = write_test(@test_body)
141
+ prev = ENV["KINTAMA_EXPLICITLY_DONT_RUN"]
142
+ ENV["KINTAMA_EXPLICITLY_DONT_RUN"] = nil
143
+ @output = `ruby #{path} #{options}`
144
+ ENV["KINTAMA_EXPLICITLY_DONT_RUN"] = prev
145
+ @exit_status = $?
146
+ instance_eval(&block) if block_given?
147
+ self
148
+ end
149
+
150
+ def should_have_passing_exit_status
151
+ @test_unit_test.assert_equal 0, @exit_status.exitstatus
152
+ end
153
+
154
+ def should_have_failing_exit_status
155
+ @test_unit_test.assert_equal 1, @exit_status.exitstatus
156
+ end
157
+
158
+ private
159
+
160
+ def assert_output(match)
161
+ @test_unit_test.assert_match(match, @output)
162
+ end
163
+
164
+ def refute_output(match)
165
+ @test_unit_test.refute_match(match, @output)
166
+ end
167
+
168
+ def passing(test_name)
169
+ if $stdin.tty?
170
+ /\e\[32m\s*#{test_name}\e\[0m/
171
+ else
172
+ /\s*#{test_name}: ./
173
+ end
174
+ end
175
+
176
+ def failing(test_name)
177
+ if $stdin.tty?
178
+ /\e\[31m\s*#{test_name}\e\[0m/
179
+ else
180
+ /\s*#{test_name}: F/
181
+ end
182
+ end
183
+
184
+ def write_test(string)
185
+ path = "/tmp/kintama_tmp_test.rb"
186
+ File.open(path, "w") do |f|
187
+ f.puts %|$LOAD_PATH.unshift "#{File.expand_path("../../lib", __FILE__)}"; require "kintama"|
188
+ f.puts string.strip
189
+ end
190
+ path
191
+ end
192
+ end
193
+ end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class AssertionsTest < Test::Unit::TestCase
3
+ class AssertionsTest < Minitest::Test
4
4
 
5
5
  class PseudoTest
6
6
  include Kintama::Assertions
@@ -40,7 +40,7 @@ class AssertionsTest < Test::Unit::TestCase
40
40
  end
41
41
 
42
42
  def test_should_provide_assert_kind_of
43
- assert_passed { @test.assert_kind_of Fixnum, 1 }
43
+ assert_passed { @test.assert_kind_of Integer, 1 }
44
44
  assert_passed { @test.assert_kind_of Object, 1 }
45
45
  assert_passed { @test.assert_kind_of String, "hello" }
46
46
  assert_failed("pa!") { @test.assert_kind_of String, 1, "pa!" }
@@ -61,8 +61,13 @@ class AssertionsTest < Test::Unit::TestCase
61
61
  end
62
62
 
63
63
  def test_should_provide_assert_match
64
- assert_passed { @test.assert_match /jam/, "bluejam" }
65
- assert_failed(%|expected "blah" to match /mm/|) { @test.assert_match /mm/, "blah" }
64
+ assert_passed { @test.assert_match(/jam/, "bluejam") }
65
+ assert_failed(%|expected "blah" to match /mm/|) { @test.assert_match(/mm/, "blah") }
66
+ end
67
+
68
+ def test_should_provide_assert_no_match
69
+ assert_passed { @test.assert_no_match(/jam/, "bluejay") }
70
+ assert_failed(%|expected "blah" not to match /ah/|) { @test.assert_no_match(/ah/, "blah") }
66
71
  end
67
72
 
68
73
  def test_should_provide_assert_same_elements_to_compare_arrays
@@ -73,6 +78,50 @@ class AssertionsTest < Test::Unit::TestCase
73
78
  end
74
79
  end
75
80
 
81
+ def test_should_provide_assert_same
82
+ expected, actual = 'foo', 'foo'
83
+ assert_passed { @test.assert_same expected, expected }
84
+ assert_failed("Expected #{expected.inspect} (oid=#{expected.object_id}) to be the same as #{actual.inspect} (oid=#{actual.object_id})") do
85
+ @test.assert_same expected, actual
86
+ end
87
+ end
88
+
89
+ def test_should_provide_assert_output
90
+ assert_passed do
91
+ @test.assert_output 'foobar' do
92
+ puts 'foobar'
93
+ end
94
+ end
95
+ assert_passed do
96
+ @test.assert_output(/oba/) do
97
+ puts 'foobar'
98
+ end
99
+ end
100
+ assert_failed('Expected output to match "foobar"') do
101
+ @test.assert_output 'foobar' do
102
+ puts 'whambam'
103
+ end
104
+ end
105
+ end
106
+
107
+ def test_should_provide_assert_not_output
108
+ assert_passed do
109
+ @test.assert_not_output 'foobar' do
110
+ puts 'whambam'
111
+ end
112
+ end
113
+ assert_passed do
114
+ @test.assert_not_output(/oba/) do
115
+ puts 'whambam'
116
+ end
117
+ end
118
+ assert_failed('Expected output not to match "foobar"') do
119
+ @test.assert_not_output 'foobar' do
120
+ puts 'foobar'
121
+ end
122
+ end
123
+ end
124
+
76
125
  private
77
126
 
78
127
  def assert_passed
@@ -87,4 +136,4 @@ class AssertionsTest < Test::Unit::TestCase
87
136
  assert_equal message, e.message, "assertion failure message didn't match"
88
137
  end
89
138
  end
90
- end
139
+ end