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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0585d375e075683687b7a47da3323ace4468f9a9f61c4fdb2c8931144a879d55'
4
+ data.tar.gz: 63325f6c21c46bec7432996421fe43ac54316a5b96c12d0c89851dd2f9fd88ea
5
+ SHA512:
6
+ metadata.gz: d90b1a3eb60ff446e2ebeea8e9e251384750db13dde88986aa86164da6595bbbac6ccff9dec4ad44f36d915a86e84bce64dd9fc0075ee478462236b39a828396
7
+ data.tar.gz: 317f355a6141242af6c6d5efb388b8bfa8fbc9ab14a5d56f02e1413485470901b970ae4565645adf0d7c8604bff956edc63163d6e68e78c4c33cc6c090e96aea
data/README.md CHANGED
@@ -30,7 +30,7 @@ Probably the closest thing I've seen is [baretest][]. If you look around the cod
30
30
 
31
31
  Another alternative test framework is [riot][], which claims to be fast, but also appears to constrain the way that tests are written by avoiding instance variables in setups, for example.
32
32
 
33
- [Testy][] is interesting - it looks like its output is YAML!. [Tryouts][] is thinking outside the box, using comment examples.
33
+ [Testy][] is interesting - it looks like its output is YAML! [Tryouts][] is thinking outside the box, using comment examples.
34
34
 
35
35
  [Zebra][] addresses the apparent duplication of the test name and the test body, but does it by introducing an [RSpec][]-esque method on every object. Wild. Also, it's an extension of [Test::Unit][], so that's strike two for me, personally.
36
36
 
@@ -66,7 +66,7 @@ These will all be very familiar to most people who are already users of [shoulda
66
66
  end
67
67
  end
68
68
 
69
- Simple, right? Note that we don't need an outer subclass of `Test::Unit::TestCase`; it's nice to lose that noise, but otherwise so far so same-old-same-old. That's kind-of the point. Anyway, here's what you get when you run this:
69
+ Simple, right? Note that we don't need an outer subclass of `Minitest::Test` or `Test::Unit::TestCase`; it's nice to lose that noise, but otherwise so far so same-old-same-old. That's kind-of the point. Anyway, here's what you get when you run this:
70
70
 
71
71
  A thing
72
72
  should act like a thing: F
@@ -11,13 +11,17 @@ module Kintama
11
11
  autoload :Assertions, 'kintama/assertions'
12
12
 
13
13
  class << self
14
+ def no_conflict?
15
+ ENV["KINTAMA_NO_CONFLICT"]
16
+ end
17
+
14
18
  def reset
15
19
  @default_context = Class.new(Runnable)
16
20
  @default_context.send(:include, Kintama::Context)
17
21
  end
18
22
 
19
23
  def default_context
20
- reset unless @default_context
24
+ reset unless instance_variable_defined?(:@default_context) && @default_context
21
25
  @default_context
22
26
  end
23
27
 
@@ -43,6 +47,16 @@ module Kintama
43
47
  default_context.teardown(&block)
44
48
  end
45
49
 
50
+ def on_start(&block)
51
+ default_context.on_start(&block)
52
+ end
53
+ alias_method :before_all, :on_start
54
+
55
+ def on_finish(&block)
56
+ default_context.on_finish(&block)
57
+ end
58
+ alias_method :after_all, :on_finish
59
+
46
60
  # Makes behaviour available within tests:
47
61
  #
48
62
  # module SomeModule
@@ -63,12 +77,12 @@ module Kintama
63
77
  end
64
78
 
65
79
  def options
66
- unless @options
67
- @options = OpenStruct.new(
80
+ @options ||= begin
81
+ options = OpenStruct.new(
68
82
  :reporter => Kintama::Reporter.default,
69
83
  :runner => Kintama::Runner.default
70
84
  )
71
- opts = OptionParser.new do |opts|
85
+ cmd_options = OptionParser.new do |opts|
72
86
  opts.banner = "Usage: ruby <test_file> [options]"
73
87
 
74
88
  opts.separator ""
@@ -76,9 +90,7 @@ module Kintama
76
90
 
77
91
  opts.on("-r", "--reporter NAME",
78
92
  "Use the given reporter (inline or verbose)") do |reporter|
79
- puts "reporter!"
80
93
  options.reporter = Kintama::Reporter.called(reporter)
81
- p options.reporter
82
94
  end
83
95
  opts.on("-l", "--line LINE",
84
96
  "Run the test or context on the given line") do |line|
@@ -89,16 +101,16 @@ module Kintama
89
101
  exit
90
102
  end
91
103
  end
92
- opts.parse!(ARGV)
104
+ cmd_options.parse!(ARGV)
105
+ options
93
106
  end
94
- @options
95
107
  end
96
108
 
97
109
  # Adds the hook to automatically run all known tests using #run when
98
110
  # ruby exits; this is most useful when running a test file from the command
99
111
  # line or from within an editor
100
112
  def add_exit_hook
101
- return if @__added_exit_hook
113
+ return if instance_variable_defined?(:@__added_exit_hook)
102
114
  at_exit { exit(options.runner.with(Kintama.default_context).run(options.reporter) ? 0 : 1) }
103
115
  @__added_exit_hook = true
104
116
  end
@@ -127,9 +139,11 @@ module Kintama
127
139
  end
128
140
  end
129
141
 
130
- [:context, :given, :describe, :testcase].each do |method|
131
- unless self.respond_to?(method)
132
- eval %|def #{method}(*args, &block); Kintama.#{method}(*args, &block); end|
142
+ unless Kintama.no_conflict?
143
+ [:context, :given, :describe, :testcase].each do |method|
144
+ unless self.respond_to?(method)
145
+ eval %|def #{method}(*args, &block); Kintama.#{method}(*args, &block); end|
146
+ end
133
147
  end
134
148
  end
135
149
 
@@ -1,4 +1,5 @@
1
1
  require "set"
2
+ require "stringio"
2
3
 
3
4
  module Kintama
4
5
  module Assertions
@@ -30,10 +31,18 @@ module Kintama
30
31
  assert (string =~ regexp), message
31
32
  end
32
33
 
34
+ def assert_no_match(regexp, string, message="expected #{string.inspect} not to match #{regexp.inspect}")
35
+ assert !(string =~ regexp), message
36
+ end
37
+
33
38
  def assert_kind_of(klass, thing, message="should be a kind of #{klass}")
34
39
  assert thing.is_a?(klass), message
35
40
  end
36
41
 
42
+ def assert_same(expected, actual, message="Expected #{expected.inspect} (oid=#{expected.object_id}) to be the same as #{actual.inspect} (oid=#{actual.object_id})")
43
+ assert actual.equal?(expected), message
44
+ end
45
+
37
46
  def assert_same_elements(expected, object, message = "#{object.inspect} does not contain the same elements as #{expected.inspect}")
38
47
  assert Set.new(expected) == Set.new(object), message
39
48
  end
@@ -62,5 +71,35 @@ module Kintama
62
71
  ensure
63
72
  raise Kintama::TestFailure, message unless raised
64
73
  end
74
+
75
+ def assert_output(expected, message="Expected output to match #{expected.inspect}", &block)
76
+ output = capture_output(&block).read.strip
77
+ if expected.is_a?(Regexp)
78
+ assert_match expected, output, message
79
+ else
80
+ assert_equal expected, output, message
81
+ end
82
+ end
83
+
84
+ def assert_not_output(not_expected, message="Expected output not to match #{not_expected.inspect}", &block)
85
+ output = capture_output(&block).read.strip
86
+ if not_expected.is_a?(Regexp)
87
+ assert_no_match not_expected, output, message
88
+ else
89
+ assert_not_equal not_expected, output, message
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def capture_output(&block)
96
+ out = StringIO.new
97
+ $stdout = out
98
+ yield
99
+ out.rewind
100
+ return out
101
+ ensure
102
+ $stdout = STDOUT
103
+ end
65
104
  end
66
- end
105
+ end
@@ -1,9 +1,46 @@
1
1
  module Kintama
2
2
  module Context
3
- def setup # noop
3
+ def setup
4
+ (setup_blocks + after_setup_blocks).each do |block|
5
+ instance_eval(&block)
6
+ end
7
+ end
8
+
9
+ def teardown
10
+ blocks = teardown_blocks
11
+ blocks.each do |block|
12
+ instance_eval(&block)
13
+ end
14
+ end
15
+
16
+ def setup_blocks
17
+ context = self.class
18
+ blocks = context.setup_blocks
19
+ while(context.superclass.respond_to?(:setup_blocks))
20
+ context = context.superclass
21
+ blocks.unshift(*context.setup_blocks)
22
+ end
23
+ blocks
24
+ end
25
+
26
+ def after_setup_blocks
27
+ context = self.class
28
+ blocks = context.after_setup_blocks
29
+ while(context.superclass.respond_to?(:after_setup_blocks))
30
+ context = context.superclass
31
+ blocks.unshift(*context.after_setup_blocks)
32
+ end
33
+ blocks
4
34
  end
5
35
 
6
- def teardown # noop
36
+ def teardown_blocks
37
+ context = self.class
38
+ blocks = context.teardown_blocks
39
+ while(context.superclass.respond_to?(:teardown_blocks))
40
+ context = context.superclass
41
+ blocks.push(*context.teardown_blocks)
42
+ end
43
+ blocks
7
44
  end
8
45
 
9
46
  def self.included(base)
@@ -13,7 +50,7 @@ module Kintama
13
50
  module ClassMethods
14
51
 
15
52
  def find_definition_1_8
16
- line = caller.find { |line| line =~ /^[^:]+:(\d+)$/ }
53
+ line = caller.find { |l| l =~ /^[^:]+:(\d+)$/ }
17
54
  if line
18
55
  parts = line.split(":")
19
56
  parts[1] = parts[1].to_i
@@ -21,22 +58,22 @@ module Kintama
21
58
  end
22
59
  end
23
60
 
24
- def find_definition_1_9(&block)
61
+ def find_definition_yarv(&block)
25
62
  block.source_location if block
26
63
  end
27
64
 
28
65
  def find_definition_rbx(&block)
29
66
  if block
30
- m = block.block.code
31
- [m.file, m.first_line]
67
+ block_environment = block.block
68
+ [block_environment.file, block_environment.line]
32
69
  end
33
70
  end
34
71
 
35
72
  def find_definition(&block)
36
73
  if defined? RUBY_ENGINE
37
74
  case RUBY_ENGINE
38
- when "ruby"
39
- RUBY_VERSION =~ /^1.9/ ? find_definition_1_9(&block) : find_definition_1_8
75
+ when "ruby", "jruby"
76
+ Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9') ? find_definition_yarv(&block) : find_definition_1_8
40
77
  when "rbx"
41
78
  find_definition_rbx(&block)
42
79
  end
@@ -67,6 +104,10 @@ module Kintama
67
104
  @setup_blocks ||= []
68
105
  end
69
106
 
107
+ def after_setup_blocks
108
+ @after_setup_blocks ||= []
109
+ end
110
+
70
111
  def teardown_blocks
71
112
  @teardown_blocks ||= []
72
113
  end
@@ -75,26 +116,17 @@ module Kintama
75
116
  # It will also be run for any subcontexts, before their own setup blocks
76
117
  def setup(&block)
77
118
  self.setup_blocks << block
119
+ end
78
120
 
79
- # redefine setup for the current set of blocks
80
- blocks = self.setup_blocks
81
- define_method(:setup) do
82
- super()
83
- blocks.each { |b| instance_eval(&b) }
84
- end
121
+ def after_setup(&block)
122
+ self.after_setup_blocks << block
85
123
  end
124
+ alias_method :action, :after_setup
86
125
 
87
126
  # Define the teardown for this context.
88
127
  # It will also be run for any subcontexts, after their own teardown blocks
89
128
  def teardown(&block)
90
129
  self.teardown_blocks << block
91
-
92
- # redefine teardown for the current set of blocks
93
- blocks = self.teardown_blocks
94
- define_method(:teardown) do
95
- blocks.each { |b| instance_eval(&b) }
96
- super()
97
- end
98
130
  end
99
131
 
100
132
  def on_start_blocks
@@ -115,11 +147,20 @@ module Kintama
115
147
  end
116
148
  alias_method :after_all, :on_finish
117
149
 
150
+ def let(name, &block)
151
+ define_method(name) do
152
+ memo = "@__#{name}"
153
+ if instance_variable_defined?(memo)
154
+ instance_variable_get(memo)
155
+ else
156
+ instance_variable_set(memo, instance_eval(&block))
157
+ end
158
+ end
159
+ end
160
+
118
161
  # Defines the subject of any matcher-based tests.
119
162
  def subject(&block)
120
- define_method(:subject) do
121
- @__subject ||= yield
122
- end
163
+ let("subject", &block)
123
164
  end
124
165
 
125
166
  # Define a test to run in this context.
@@ -199,28 +240,6 @@ module Kintama
199
240
  subcontexts.find { |s| s.name == name } || tests.find { |t| t.name == name }
200
241
  end
201
242
 
202
- def method_missing(name, *args, &block)
203
- if self[de_methodize(name)]
204
- self[de_methodize(name)]
205
- else
206
- begin
207
- super
208
- rescue NameError, NoMethodError => e
209
- if parent
210
- parent.send(name, *args, &block)
211
- else
212
- raise e
213
- end
214
- end
215
- end
216
- end
217
-
218
- def respond_to?(name)
219
- self[name] ||
220
- super ||
221
- (parent ? parent.respond_to?(name) : false)
222
- end
223
-
224
243
  # Runs all tests in this context and any subcontexts.
225
244
  # Returns true if all tests passed; otherwise false
226
245
  def run(reporter=nil)
@@ -257,10 +276,6 @@ module Kintama
257
276
 
258
277
  private
259
278
 
260
- def de_methodize(name)
261
- name.to_s.gsub("_", " ")
262
- end
263
-
264
279
  def ran_tests
265
280
  @ran_tests || []
266
281
  end
@@ -1,12 +1,34 @@
1
- require 'mocha/standalone'
2
-
3
- Kintama.include Mocha::API
4
- Kintama.teardown do
5
- begin
6
- mocha_verify
7
- rescue Mocha::ExpectationError => e
8
- raise e
9
- ensure
10
- mocha_teardown
1
+ require 'kintama'
2
+ require 'mocha/api'
3
+
4
+ module Kintama::Mocha
5
+ module Expect
6
+ def expect(name, &block)
7
+ context do
8
+ setup(&block)
9
+ test("expect " + name) {}
10
+ end
11
+ end
12
+ end
13
+
14
+ def self.setup
15
+ Kintama.include Mocha::API
16
+ Kintama.include Mocha::Hooks
17
+ Kintama.extend(Kintama::Mocha::Expect)
18
+
19
+ Kintama.setup do
20
+ mocha_setup
21
+ end
22
+ Kintama.teardown do
23
+ begin
24
+ mocha_verify
25
+ rescue Mocha::ExpectationError => e
26
+ raise e
27
+ ensure
28
+ mocha_teardown
29
+ end
30
+ end
11
31
  end
12
32
  end
33
+
34
+ Kintama::Mocha.setup
@@ -0,0 +1,2 @@
1
+ ENV["KINTAMA_NO_CONFLICT"] = "true"
2
+ require "kintama"
@@ -2,7 +2,8 @@ module Kintama
2
2
  class Reporter
3
3
 
4
4
  def self.default
5
- Verbose.new(colour=$stdin.tty?)
5
+ colour = $stdin.tty?
6
+ Verbose.new(colour)
6
7
  end
7
8
 
8
9
  def self.called(name)
@@ -17,9 +18,9 @@ module Kintama
17
18
  end
18
19
 
19
20
  class Base
20
- attr_reader :runner
21
+ attr_reader :runner, :test_count
21
22
 
22
- def initialize
23
+ def initialize(*args)
23
24
  @test_count = 0
24
25
  end
25
26
 
@@ -65,7 +66,7 @@ module Kintama
65
66
  end
66
67
 
67
68
  def character_status_of(test)
68
- character = if test.pending?
69
+ if test.pending?
69
70
  'P'
70
71
  elsif test.passed?
71
72
  '.'
@@ -139,22 +140,22 @@ module Kintama
139
140
  end
140
141
  end
141
142
 
142
- def color(text, color_code)
143
- "#{color_code}#{text}\e[0m"
143
+ def colour(text, colour_code)
144
+ "#{colour_code}#{text}\e[0m"
144
145
  end
145
146
 
146
147
  def green(text)
147
- color(text, "\e[32m")
148
+ colour(text, "\e[32m")
148
149
  end
149
150
 
150
151
  def red(text)
151
- color(text, "\e[31m")
152
+ colour(text, "\e[31m")
152
153
  end
153
154
 
154
155
  def yellow(text)
155
- color(text, "\e[33m")
156
+ colour(text, "\e[33m")
156
157
  end
157
158
  end
158
159
 
159
160
  end
160
- end
161
+ end