kintama 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ module Kintama
12
12
  assert actual == expected, message
13
13
  end
14
14
 
15
- def assert_not_equal(expected, actual, message)
15
+ def assert_not_equal(expected, actual, message="Expected #{expected.inspect} to not be equal to #{actual.inspect}")
16
16
  assert actual != expected, message
17
17
  end
18
18
 
@@ -24,15 +24,37 @@ module Kintama
24
24
  assert_not_equal nil, object, message
25
25
  end
26
26
 
27
+ def assert_match(regexp, string, message="expected #{string.inspect} to match #{regexp.inspect}")
28
+ assert (string =~ regexp), message
29
+ end
30
+
27
31
  def assert_kind_of(klass, thing, message="should be a kind of #{klass}")
28
- assert thing.is_a?(klass)
32
+ assert thing.is_a?(klass), message
33
+ end
34
+
35
+ def assert_nothing_raised(message="should not raise anything", &block)
36
+ yield
37
+ rescue Exception => e
38
+ raise Kintama::TestFailure, message + " (#{e} was raised)"
29
39
  end
30
40
 
31
- def assert_raises(message="should raise an exception", &block)
41
+ def assert_raises(klass_or_message=Exception, message="should raise an exception", &block)
42
+ if klass_or_message.respond_to?(:ancestors)
43
+ klass = klass_or_message
44
+ else
45
+ message = klass_or_message
46
+ klass = Exception
47
+ end
32
48
  yield
33
- raise Kintama::TestFailure, message
34
- rescue
35
- # do nothing, we expected this, but now no TestFailure was raised.
49
+ raised = false
50
+ rescue => e
51
+ if e.class.ancestors.include?(klass)
52
+ raised = true
53
+ else
54
+ raised = false
55
+ end
56
+ ensure
57
+ raise Kintama::TestFailure, message unless raised
36
58
  end
37
59
  end
38
60
  end
@@ -12,14 +12,47 @@ module Kintama
12
12
 
13
13
  module ClassMethods
14
14
 
15
+ def find_definition_1_8
16
+ line = caller.find { |line| line =~ /^[^:]+:(\d+)$/ }
17
+ if line
18
+ parts = line.split(":")
19
+ parts[1] = parts[1].to_i
20
+ parts
21
+ end
22
+ end
23
+
24
+ def find_definition_1_9(&block)
25
+ block.source_location if block
26
+ end
27
+
28
+ def find_definition_rbx(&block)
29
+ if block
30
+ m = block.block.code
31
+ [m.file, m.first_line]
32
+ end
33
+ end
34
+
35
+ def find_definition(&block)
36
+ if defined? RUBY_ENGINE
37
+ case RUBY_ENGINE
38
+ when "ruby"
39
+ RUBY_VERSION == "1.9.2" ? find_definition_1_9(&block) : find_definition_1_8
40
+ when "rbx"
41
+ find_definition_rbx(&block)
42
+ end
43
+ else
44
+ find_definition_1_8
45
+ end
46
+ end
47
+
15
48
  # Create a new context. If this is called within a context, a new subcontext
16
49
  # will be created. Aliases are 'testcase' and 'describe'
17
50
  def context(name=nil, parent=self, &block)
18
51
  c = Class.new(parent)
19
52
  c.send(:include, Kintama::Context)
20
53
  c.name = name.to_s if name
21
- c.definition = caller.find { |line| line =~ /^#{block.__file__}:(\d+)$/ }
22
- c.class_eval(&block)
54
+ c.definition = find_definition(&block)
55
+ c.class_eval(&block) if block
23
56
  c
24
57
  end
25
58
  alias_method :testcase, :context
@@ -94,7 +127,7 @@ module Kintama
94
127
  c = Class.new(self)
95
128
  c.send(:include, Kintama::Test)
96
129
  c.name = name
97
- c.definition = caller.find { |line| line =~ /^[^:]+:(\d+)$/ }
130
+ c.definition = find_definition(&block)
98
131
  c.block = block if block_given?
99
132
  end
100
133
 
@@ -207,8 +240,9 @@ module Kintama
207
240
  end
208
241
 
209
242
  def runnable_on_line(line)
210
- sorted_runnables = all_runnables.delete_if { |r| r.line_defined.nil? }.sort_by { |r| r.line_defined }
211
- if line >= sorted_runnables.first.line_defined
243
+ known_runnables = all_runnables.delete_if { |r| r.line_defined.nil? }
244
+ sorted_runnables = known_runnables.sort_by { |r| r.line_defined }
245
+ if sorted_runnables.first && line >= sorted_runnables.first.line_defined
212
246
  next_runnable = sorted_runnables.find { |r| r.line_defined > line }
213
247
  index = sorted_runnables.index(next_runnable)
214
248
  if index != nil && index > 0
@@ -30,7 +30,7 @@ module Kintama
30
30
  end
31
31
 
32
32
  def line_defined
33
- definition ? definition.split(":").last.to_i : nil
33
+ definition ? definition.last : nil
34
34
  end
35
35
  end
36
36
  end
@@ -32,7 +32,7 @@ module Kintama
32
32
  begin
33
33
  teardown
34
34
  rescue Exception => e
35
- @failure = e
35
+ @failure ||= e
36
36
  end
37
37
  end
38
38
  end
@@ -10,33 +10,73 @@ class AssertionsTest < Test::Unit::TestCase
10
10
  @test = PseudoTest.new
11
11
  end
12
12
 
13
+ def test_should_provide_assert
14
+ assert_passed { @test.assert true }
15
+ assert_failed("waaa") { @test.assert false, "waaa" }
16
+ end
17
+
18
+ def test_should_provide_flunk
19
+ assert_failed(":(") { @test.flunk ":(" }
20
+ end
21
+
22
+ def test_should_provide_assert_equal
23
+ assert_passed { @test.assert_equal 1, 1 }
24
+ assert_failed("blurgh") { @test.assert_equal 1, 2, "blurgh" }
25
+ end
26
+
27
+ def test_should_provide_assert_not_equal
28
+ assert_passed { @test.assert_not_equal 1, 2 }
29
+ assert_failed("sadface") { @test.assert_not_equal 1, 1, "sadface" }
30
+ end
31
+
13
32
  def test_should_provide_assert_nil
14
- assert_assertion_fails { @test.assert_nil Object.new }
15
- assert_assertion_passes { @test.assert_nil nil }
33
+ assert_failed("bums") { @test.assert_nil Object.new, "bums" }
34
+ assert_passed { @test.assert_nil nil }
16
35
  end
17
36
 
18
37
  def test_should_provide_assert_not_nil
19
- assert_assertion_passes { @test.assert_not_nil Object.new }
20
- assert_assertion_fails { @test.assert_not_nil nil }
38
+ assert_passed { @test.assert_not_nil Object.new }
39
+ assert_failed("fiddlesticks!") { @test.assert_not_nil nil, "fiddlesticks!" }
21
40
  end
22
41
 
23
42
  def test_should_provide_assert_kind_of
24
- assert_assertion_passes { @test.assert_kind_of Fixnum, 1 }
25
- assert_assertion_passes { @test.assert_kind_of Object, 1 }
26
- assert_assertion_passes { @test.assert_kind_of String, "hello" }
27
- assert_assertion_fails { @test.assert_kind_of String, 1 }
43
+ assert_passed { @test.assert_kind_of Fixnum, 1 }
44
+ assert_passed { @test.assert_kind_of Object, 1 }
45
+ assert_passed { @test.assert_kind_of String, "hello" }
46
+ assert_failed("pa!") { @test.assert_kind_of String, 1, "pa!" }
47
+ end
48
+
49
+ def test_should_provide_assert_nothing_raised
50
+ assert_passed { @test.assert_nothing_raised { true } }
51
+ assert_passed { @test.assert_nothing_raised { false } }
52
+ assert_failed("ouch (oh no was raised)") { @test.assert_nothing_raised("ouch") { raise "oh no" } }
53
+ end
54
+
55
+ def test_should_provide_assert_raises
56
+ assert_passed { @test.assert_raises { raise "urgh" } }
57
+ assert_passed { @test.assert_raises(StandardError) { raise StandardError, "urgh" } }
58
+ assert_failed("no way") { @test.assert_raises("no way") { false } }
59
+ assert_failed { @test.assert_raises(RuntimeError) { raise StandardError, "urgh" } }
60
+ assert_passed { @test.assert_raises("woah") { this_method_doesnt_exist } }
61
+ end
62
+
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" }
28
66
  end
29
67
 
30
68
  private
31
69
 
32
- def assert_assertion_passes
70
+ def assert_passed
33
71
  yield
34
72
  end
35
73
 
36
- def assert_assertion_fails
74
+ def assert_failed(message=nil)
37
75
  yield
38
- raise "assertion did not fail!"
39
- rescue Kintama::TestFailure
40
- # nothing
76
+ raise "assertion did not fail!" if failed
77
+ rescue Kintama::TestFailure => e
78
+ if message
79
+ assert_equal message, e.message, "assertion failure message didn't match"
80
+ end
41
81
  end
42
82
  end
@@ -84,6 +84,8 @@ class LineBasedRunningTest < Test::Unit::TestCase
84
84
  end
85
85
 
86
86
  def test_should_be_able_to_target_a_top_level_context
87
+ end
88
+
87
89
  def test_should_not_show_pending_tests_in_the_same_context_as_pending_when_not_targeted
88
90
  test_file = %{
89
91
  context "given a context with a pending test" do
@@ -94,7 +96,6 @@ class LineBasedRunningTest < Test::Unit::TestCase
94
96
  end}
95
97
  assert_match /2 tests/, run_test(test_file, "--line 2")
96
98
  end
97
- end
98
99
 
99
100
  def test_should_report_if_nothing_runnable_can_be_found_for_that_line
100
101
  test_file = %{
@@ -105,22 +106,6 @@ class LineBasedRunningTest < Test::Unit::TestCase
105
106
  assert_match /Nothing runnable found on line 1/, run_test(test_file, "--line 1")
106
107
  end
107
108
 
108
- def test_should_run_startup_blocks_from_outer_parents
109
- test_file = %{
110
- context "outer" do
111
- on_start { $started = '123' }
112
- context "inner" do
113
- context "deeply inner" do
114
- should "have run startup" do
115
- assert_equal '123', $started
116
- end
117
- end
118
- end
119
- end}
120
- assert_match /#{passing("should have run startup")}/, run_test(test_file, "--line 9")
121
- assert_match /^1 tests, 0 failures/, run_test(test_file, "--line 9")
122
- end
123
-
124
109
  private
125
110
 
126
111
  def write_test(string, path)
@@ -10,4 +10,11 @@ class PendingTest < Test::Unit::TestCase
10
10
  assert c.passed?
11
11
  end
12
12
 
13
+ def test_should_ignore_empty_contexts
14
+ c = context "Given an empty context" do
15
+ context "should ignore this"
16
+ end
17
+ c.run
18
+ assert c.passed?
19
+ end
13
20
  end
@@ -89,4 +89,18 @@ class TeardownTest < Test::Unit::TestCase
89
89
  assert !c.passed?
90
90
  assert ran
91
91
  end
92
+
93
+ def test_should_not_mask_exceptions_in_tests_with_ones_in_teardown
94
+ c = context "Given a test that fails" do
95
+ should "report this error" do
96
+ raise "this"
97
+ end
98
+ teardown do
99
+ raise "that"
100
+ end
101
+ end
102
+ c.run
103
+ assert !c.passed?
104
+ assert_equal "this", c.failures.first.failure.to_s
105
+ end
92
106
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kintama
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - James Adam
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-29 00:00:00 +01:00
18
+ date: 2011-06-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -37,7 +37,7 @@ files:
37
37
  - test/line_based_running_test.rb
38
38
  - test/matcher_test.rb
39
39
  - test/method_behaviour_test.rb
40
- - test/pending_test.rb
40
+ - test/pending_test_and_context.rb
41
41
  - test/reporters/base_reporter_test.rb
42
42
  - test/reporters/inline_reporter_test.rb
43
43
  - test/reporters/verbose_reporter_test.rb