kintama 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,6 @@
1
+ require "ostruct"
2
+ require "optparse"
3
+
1
4
  module Kintama
2
5
  autoload :Runnable, 'kintama/runnable'
3
6
  autoload :Context, 'kintama/context'
@@ -59,12 +62,44 @@ module Kintama
59
62
  default_context.extend(mod)
60
63
  end
61
64
 
65
+ def options
66
+ unless @options
67
+ @options = OpenStruct.new(
68
+ :reporter => Kintama::Reporter.default,
69
+ :runner => Kintama::Runner.default
70
+ )
71
+ opts = OptionParser.new do |opts|
72
+ opts.banner = "Usage: ruby <test_file> [options]"
73
+
74
+ opts.separator ""
75
+ opts.separator "Specific options:"
76
+
77
+ opts.on("-r", "--reporter NAME",
78
+ "Use the given reporter (inline or verbose)") do |reporter|
79
+ puts "reporter!"
80
+ options.reporter = Kintama::Reporter.called(reporter)
81
+ p options.reporter
82
+ end
83
+ opts.on("-l", "--line LINE",
84
+ "Run the test or context on the given line") do |line|
85
+ options.runner = Kintama::Runner::Line.new(line)
86
+ end
87
+ opts.on_tail("-h", "--help", "Show this message") do
88
+ puts opts
89
+ exit
90
+ end
91
+ end
92
+ opts.parse!(ARGV)
93
+ end
94
+ @options
95
+ end
96
+
62
97
  # Adds the hook to automatically run all known tests using #run when
63
98
  # ruby exits; this is most useful when running a test file from the command
64
99
  # line or from within an editor
65
100
  def add_exit_hook
66
101
  return if @__added_exit_hook
67
- at_exit { exit(Runner.from_args(ARGV, Kintama.default_context.subcontexts).run ? 0 : 1) }
102
+ at_exit { exit(options.runner.with(Kintama.default_context).run(options.reporter) ? 0 : 1) }
68
103
  @__added_exit_hook = true
69
104
  end
70
105
 
@@ -14,10 +14,10 @@ module Kintama
14
14
 
15
15
  # Create a new context. If this is called within a context, a new subcontext
16
16
  # will be created. Aliases are 'testcase' and 'describe'
17
- def context(name, parent=self, &block)
17
+ def context(name=nil, parent=self, &block)
18
18
  c = Class.new(parent)
19
19
  c.send(:include, Kintama::Context)
20
- c.name = name.to_s
20
+ c.name = name.to_s if name
21
21
  c.definition = caller.find { |line| line =~ /^#{block.__file__}:(\d+)$/ }
22
22
  c.class_eval(&block)
23
23
  c
@@ -84,7 +84,9 @@ module Kintama
84
84
 
85
85
  # Defines the subject of any matcher-based tests.
86
86
  def subject(&block)
87
- define_method(:subject, &block)
87
+ define_method(:subject) do
88
+ @__subject ||= yield
89
+ end
88
90
  end
89
91
 
90
92
  # Define a test to run in this context.
@@ -5,6 +5,17 @@ module Kintama
5
5
  Verbose.new(colour=$stdin.tty?)
6
6
  end
7
7
 
8
+ def self.called(name)
9
+ case name.to_s
10
+ when /verbose/i
11
+ default
12
+ when /inline/i
13
+ Inline.new
14
+ else
15
+ default
16
+ end
17
+ end
18
+
8
19
  class Base
9
20
  attr_reader :runner
10
21
 
@@ -41,7 +52,6 @@ module Kintama
41
52
  end
42
53
 
43
54
  def show_results
44
- puts
45
55
  puts test_summary
46
56
  puts "\n" + failure_messages.join("\n\n") if runner.failures.any?
47
57
  end
@@ -90,13 +100,17 @@ module Kintama
90
100
  end
91
101
 
92
102
  def context_started(context)
93
- print indent + context.name + "\n" if context.name
94
- @current_indent_level += 1
103
+ unless context == Kintama.default_context || context.name == nil
104
+ print indent + context.name + "\n" if context.name
105
+ @current_indent_level += 1
106
+ end
95
107
  end
96
108
 
97
109
  def context_finished(context)
98
- @current_indent_level -= 1
99
- puts if @current_indent_level == 0 && context != runner.runnables.last
110
+ unless context == Kintama.default_context || context.name == nil
111
+ @current_indent_level -= 1
112
+ puts if @current_indent_level == 0 && context != runner.runnables.last
113
+ end
100
114
  end
101
115
 
102
116
  def test_started(test)
@@ -1,21 +1,23 @@
1
1
  module Kintama
2
2
  class Runner
3
- def self.from_args(args, runnables)
4
- if args[0] == "--line"
5
- Kintama::Runner::Line.new(args[1], runnables)
6
- else
7
- Kintama::Runner::Default.new(runnables)
8
- end
3
+
4
+ def self.default
5
+ Default.new
9
6
  end
10
7
 
11
8
  class Base
12
9
  attr_reader :runnables
13
10
 
14
- def initialize(runnables)
11
+ def initialize
12
+ @runnables = []
13
+ end
14
+
15
+ def with(*runnables)
15
16
  @runnables = runnables
17
+ self
16
18
  end
17
19
 
18
- def run(reporter=Kintama::Reporter.default, args=ARGV)
20
+ def run(reporter=Kintama::Reporter.default)
19
21
  reporter.started(self)
20
22
  @ran_runnables = run_tests(reporter)
21
23
  reporter.finished
@@ -48,8 +50,7 @@ module Kintama
48
50
 
49
51
  # Runs only the test or context which contains the provided line
50
52
  class Line < Base
51
- def initialize(line, runnables)
52
- super(runnables)
53
+ def initialize(line)
53
54
  @line = line.to_i
54
55
  end
55
56
 
@@ -65,11 +66,15 @@ module Kintama
65
66
  end
66
67
  heirarchy.each { |context| reporter.context_started(context) }
67
68
  runnable.parent.run_tests([runnable], false, reporter)
69
+ heirarchy.reverse.each { |context| reporter.context_finished(context) }
68
70
  [runnable.parent]
69
71
  else
70
72
  runnable.run(reporter)
71
73
  [runnable]
72
74
  end
75
+ else
76
+ puts "Nothing runnable found on line #{@line}"
77
+ exit -1
73
78
  end
74
79
  end
75
80
  end
@@ -45,7 +45,7 @@ class LineBasedRunningTest < Test::Unit::TestCase
45
45
  end
46
46
  end
47
47
  end}
48
- assert_match /#{passing("should run this test")}\n#{passing("should run this test too")}\n\n\n2 tests/, run_test(test_file, "--line 6")
48
+ assert_match /#{passing("should run this test")}\n#{passing("should run this test too")}\n\n2 tests/, run_test(test_file, "--line 6")
49
49
  end
50
50
 
51
51
  def test_should_be_able_to_run_a_test_defined_in_a_second_top_level_context
@@ -59,7 +59,7 @@ class LineBasedRunningTest < Test::Unit::TestCase
59
59
  should "run this test" do
60
60
  end
61
61
  end}
62
- assert_match /#{passing("should run this test")}\n\n\n1 tests/, run_test(test_file, "--line 8")
62
+ assert_match /#{passing("should run this test")}\n\n1 tests/, run_test(test_file, "--line 8")
63
63
  end
64
64
 
65
65
  def test_should_print_out_the_full_nested_test_name
@@ -83,6 +83,28 @@ class LineBasedRunningTest < Test::Unit::TestCase
83
83
  assert_no_match /1 pending/, run_test(test_file, "--line 3")
84
84
  end
85
85
 
86
+ def test_should_be_able_to_target_a_top_level_context
87
+ def test_should_not_show_pending_tests_in_the_same_context_as_pending_when_not_targeted
88
+ test_file = %{
89
+ context "given a context with a pending test" do
90
+ should "run this" do
91
+ end
92
+ should "run this too" do
93
+ end
94
+ end}
95
+ assert_match /2 tests/, run_test(test_file, "--line 2")
96
+ end
97
+ end
98
+
99
+ def test_should_report_if_nothing_runnable_can_be_found_for_that_line
100
+ test_file = %{
101
+ context "given a short context" do
102
+ should "not run this" do
103
+ end
104
+ end}
105
+ assert_match /Nothing runnable found on line 1/, run_test(test_file, "--line 1")
106
+ end
107
+
86
108
  private
87
109
 
88
110
  def write_test(string, path)
@@ -35,6 +35,21 @@ class MatcherTest < Test::Unit::TestCase
35
35
  assert_match /^Expected 456, but got 123/, c.failures.first.failure_message
36
36
  end
37
37
 
38
+ def test_should_use_a_single_instance_of_the_subject_within_a_test
39
+ c = context "x" do
40
+ subject { Array.new }
41
+ should "allow me to poke around with subject like it was a variable" do
42
+ subject << 1
43
+ assert_equal [1], subject
44
+ end
45
+ should "now be empty again" do
46
+ assert subject.empty?
47
+ end
48
+ end
49
+ c.run
50
+ assert c.passed?
51
+ end
52
+
38
53
  def test_should_allow_negation_of_matchers
39
54
  c = context "x" do
40
55
  subject { 123 }
@@ -151,7 +151,7 @@ class BaseReporterTest < Test::Unit::TestCase
151
151
  private
152
152
 
153
153
  def runner(*args)
154
- Kintama::Runner::Default.new(args)
154
+ Kintama::Runner::Default.new.with(*args)
155
155
  end
156
156
 
157
157
  end
@@ -63,6 +63,6 @@ class InlineReporterTest < Test::Unit::TestCase
63
63
  private
64
64
 
65
65
  def runner(*args)
66
- Kintama::Runner::Default.new(args)
66
+ Kintama::Runner::Default.new.with(*args)
67
67
  end
68
68
  end
@@ -98,6 +98,19 @@ class VerboseReporterTest < Test::Unit::TestCase
98
98
  end
99
99
  end
100
100
 
101
+ def test_should_treat_a_context_as_transparent_if_it_has_no_name
102
+ c1 = context "given something" do
103
+ context do
104
+ should "pass" do
105
+ assert true
106
+ end
107
+ end
108
+ end
109
+ assert_output(/^given something\n should pass: \./) do
110
+ runner(c1).run(@reporter)
111
+ end
112
+ end
113
+
101
114
  def test_should_print_out_test_names_in_colour_if_colour_is_set
102
115
  c = context "given something" do
103
116
  should "be red" do
@@ -129,6 +142,6 @@ class VerboseReporterTest < Test::Unit::TestCase
129
142
  private
130
143
 
131
144
  def runner(*args)
132
- Kintama::Runner::Default.new(args)
145
+ Kintama::Runner::Default.new.with(*args)
133
146
  end
134
147
  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: 29
5
- prerelease:
4
+ hash: 17
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - James Adam
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  requirements: []
86
86
 
87
87
  rubyforge_project:
88
- rubygems_version: 1.4.1
88
+ rubygems_version: 1.3.7
89
89
  signing_key:
90
90
  specification_version: 3
91
91
  summary: It's for writing tests.