konacha 2.0.0.beta2 → 2.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ # The Metadata class mimics the public interface of RSpec::Core::Metadata.
2
+
3
+ module Konacha
4
+ class Reporter
5
+ class SpecException < Exception
6
+ def pending_fixed?
7
+ false
8
+ end
9
+ end
10
+
11
+ class Metadata
12
+ attr_reader :data
13
+
14
+ def initialize(data)
15
+ @data = data
16
+ end
17
+
18
+ def [](key)
19
+ respond_to?(key) ? send(key) : data[key]
20
+ end
21
+
22
+ def update(data)
23
+ @data.merge!(data)
24
+ end
25
+
26
+ def file_path
27
+ STDERR.puts "file_path not implemented" if Konacha.config.verbose
28
+ "" # RSpec's BaseFormatter expects the return value to be a string
29
+ end
30
+
31
+ alias_method :location, :file_path
32
+
33
+ def line_number
34
+ STDERR.puts "line_number not implemented" if Konacha.config.verbose
35
+ nil
36
+ end
37
+
38
+ def execution_result
39
+ @execution_result ||= {
40
+ :status => data['status'],
41
+ :started_at => nil,
42
+ :finished_at => nil,
43
+ :run_time => data['duration'],
44
+ :exception => exception
45
+ }
46
+ end
47
+
48
+ def exception
49
+ return unless data['status'] == "failed"
50
+
51
+ @exception ||= begin
52
+ e = Reporter::SpecException.new(data['error']['message'])
53
+ e.set_backtrace([])
54
+ e
55
+ end
56
+ end
57
+
58
+ def pending_message
59
+ STDERR.puts "pending_message not implemented" if Konacha.config.verbose
60
+ nil
61
+ end
62
+
63
+ def described_class
64
+ STDERR.puts "described_class not implemented" if Konacha.config.verbose
65
+ nil
66
+ end
67
+
68
+ def pending
69
+ data['status'] == "pending"
70
+ end
71
+
72
+ def description
73
+ data['title']
74
+ end
75
+
76
+ def full_description
77
+ data['fullTitle']
78
+ end
79
+ end
80
+ end
81
+ end
@@ -1,5 +1,4 @@
1
1
  require "capybara"
2
- require "colorize"
3
2
 
4
3
  module Konacha
5
4
  class Runner
@@ -7,105 +6,52 @@ module Konacha
7
6
  new.run
8
7
  end
9
8
 
10
- attr_reader :io, :examples
9
+ attr_reader :reporter
11
10
 
12
- def initialize(options = {})
13
- @io = options[:output] || STDOUT
11
+ def initialize
12
+ @reporter = Konacha::Reporter.new(*formatters)
14
13
  end
15
14
 
16
15
  def run
17
- before = Time.now
16
+ reporter.start
18
17
 
19
18
  begin
20
- session.visit("/")
19
+ session.visit('/')
21
20
 
22
- dots_printed = 0
21
+ events_consumed = 0
22
+ done = false
23
23
  begin
24
24
  sleep 0.1
25
- done, dots = session.evaluate_script('[Konacha.done, Konacha.dots]')
26
- if dots
27
- io.write colorize_dots(dots[dots_printed..-1])
28
- io.flush
29
- dots_printed = dots.length
25
+ events = JSON.parse(session.evaluate_script('Konacha.getEvents()'))
26
+ if events
27
+ events[events_consumed..-1].each do |event|
28
+ done = true if event['event'] == 'end'
29
+ reporter.process_mocha_event(event)
30
+ end
31
+
32
+ events_consumed = events.length
30
33
  end
31
34
  end until done
32
-
33
- @examples = JSON.parse(session.evaluate_script('Konacha.getResults()')).map do |row|
34
- Example.new(row)
35
- end
36
35
  rescue => e
37
36
  raise e, "Error communicating with browser process: #{e}", e.backtrace
38
37
  end
39
38
 
40
- io.puts ""
41
- io.puts ""
42
- failure_messages.each { |msg| io.write("#{msg}\n\n") }
43
-
44
- seconds = "%.2f" % (Time.now - before)
45
- io.puts "Finished in #{seconds} seconds"
46
- io.puts "#{examples.size} examples, #{failed_examples.size} failures, #{pending_examples.size} pending"
47
- passed?
48
- end
49
-
50
- def pending_examples
51
- examples.select { |example| example.pending? }
52
- end
53
-
54
- def failed_examples
55
- examples.select { |example| example.failed? }
56
- end
57
-
58
- def passed?
59
- examples.all? { |example| example.passed? || example.pending? }
60
- end
61
-
62
- def failure_messages
63
- examples.map { |example| example.failure_message }.compact
39
+ reporter.finish
40
+ reporter.passed?
64
41
  end
65
42
 
66
43
  def session
67
44
  @session ||= Capybara::Session.new(Konacha.driver, Konacha.application)
68
45
  end
69
46
 
70
- def colorize_dots(dots)
71
- dots = dots.chars.map do |d|
72
- case d
73
- when 'E', 'F'; d.red
74
- when 'P'; d.yellow
75
- when '.'; d.green
76
- else; d
47
+ private
48
+ def formatters
49
+ if ENV['FORMAT']
50
+ ENV['FORMAT'].split(',').map do |string|
51
+ eval(string).new(STDOUT)
77
52
  end
78
- end
79
- dots.join ''
80
- end
81
- end
82
-
83
- class Example
84
- def initialize(row)
85
- @row = row
86
- end
87
-
88
- def passed?
89
- @row['passed']
90
- end
91
-
92
- def pending?
93
- @row['pending']
94
- end
95
-
96
- def failed?
97
- !(@row['passed'] || @row['pending'])
98
- end
99
-
100
- def failure_message
101
- if failed?
102
- msg = []
103
- msg << " Failed: #{@row['name']}"
104
- msg << " #{@row['message']}"
105
- msg << " in #{@row['trace']['fileName']}:#{@row['trace']['lineNumber']}" if @row['trace']
106
- msg.join("\n").red
107
- elsif pending?
108
- " Pending: #{@row['name']}".yellow
53
+ else
54
+ [Konacha::Formatter.new(STDOUT)]
109
55
  end
110
56
  end
111
57
  end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Konacha::Formatter do
4
+ let(:io) { StringIO.new }
5
+ subject { described_class.new(io) }
6
+
7
+ shared_examples "test result" do |method, dot|
8
+ it "stores the example in the examples array" do
9
+ subject.send(method, nil)
10
+ subject.examples.should be_present
11
+ end
12
+
13
+ it "outputs the dot" do
14
+ subject.send(method, nil)
15
+ io.rewind
16
+ io.read.should include(dot)
17
+ end
18
+ end
19
+
20
+ describe "#example_passed" do
21
+ it_behaves_like "test result", :example_passed, "."
22
+ end
23
+
24
+ describe "#example_failed" do
25
+ it_behaves_like "test result", :example_failed, "F"
26
+ end
27
+
28
+ describe "#example_pending" do
29
+ it_behaves_like "test result", :example_pending, "P"
30
+ end
31
+
32
+ describe "#dump_pending" do
33
+ let(:example) { double('example', :pending? => true, :full_description => "Pending example") }
34
+ before { subject.stub(:examples => [example]) }
35
+
36
+ it "outputs the pending message" do
37
+ subject.dump_pending
38
+ io.rewind
39
+ io.read.should include(" Pending: Pending example")
40
+ end
41
+ end
42
+
43
+ describe "#dump_failures" do
44
+ let(:example) do
45
+ double('example',
46
+ :failed? => true,
47
+ :full_description => "Failed example",
48
+ :exception => double('exception',
49
+ :message => "exception",
50
+ :backtrace => nil))
51
+ end
52
+
53
+ before { subject.stub(:examples => [example]) }
54
+
55
+ it "outputs the failure message" do
56
+ subject.dump_failures
57
+ io.rewind
58
+ io.read.should include(" Failed: Failed example\n exception")
59
+ end
60
+ end
61
+
62
+ describe "#dump_summary" do
63
+ it "outputs the summary" do
64
+ subject.dump_summary(10, 10, 2, 3)
65
+ io.rewind
66
+ io.read.should == "\nFinished in 10.00 seconds\n10 examples, 2 failed, 3 pending\n"
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Konacha::Reporter::ExampleGroup do
4
+ subject { described_class.new({}, nil) }
5
+
6
+ describe "#initialize" do
7
+ it "loads up a metadata instance and the parent" do
8
+ data = double('data')
9
+ parent = double('parent')
10
+ example_group = described_class.new(data, parent)
11
+
12
+ example_group.parent.should == parent
13
+ example_group.metadata.should be_a(Konacha::Reporter::Metadata)
14
+ example_group.metadata.data.should == data
15
+ end
16
+ end
17
+
18
+ describe "delegated methods" do
19
+ let(:metadata) { double('metadata') }
20
+ before { Konacha::Reporter::Metadata.stub(:new) { metadata } }
21
+
22
+ [:full_description, :description, :file_path, :described_class].each do |method|
23
+ it "delegates #{method} to metadata" do
24
+ metadata.should_receive(method)
25
+ subject.send(method)
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "aliased_methods" do
31
+ it "aliases display_name to description" do
32
+ subject.display_name.should == subject.description
33
+ end
34
+ end
35
+
36
+ describe "#parent_groups" do
37
+ let(:parent) { double('parent') }
38
+ let(:grandparent) { double('grandparent') }
39
+
40
+ before do
41
+ grandparent.stub(:parent => nil)
42
+ parent.stub(:parent => grandparent)
43
+ subject.stub(:parent => parent)
44
+ end
45
+
46
+ it "finds all of this group's ancestors" do
47
+ subject.parent_groups.should == [parent, grandparent]
48
+ end
49
+
50
+ it "works via #ancestors" do
51
+ subject.ancestors.should == [parent, grandparent]
52
+ end
53
+ end
54
+
55
+ describe "#update_metadata" do
56
+ it "calls metadata.update" do
57
+ data = double('data')
58
+ metadata = double('metadata')
59
+ metadata.should_receive(:update).with(data)
60
+ Konacha::Reporter::Metadata.stub(:new) { metadata }
61
+ subject.update_metadata(data)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Konacha::Reporter::Example do
4
+ subject { described_class.new({}, nil) }
5
+
6
+ describe "#initialize" do
7
+ it "loads up a metadata instance and the parent" do
8
+ data = double('data')
9
+ parent = double('parent')
10
+ example = described_class.new(data, parent)
11
+
12
+ example.parent.should == parent
13
+ example.metadata.should be_a(Konacha::Reporter::Metadata)
14
+ example.metadata.data.should == data
15
+ end
16
+ end
17
+
18
+ describe "delegated methods" do
19
+ let(:metadata) { double('metadata') }
20
+ before { Konacha::Reporter::Metadata.stub(:new) { metadata } }
21
+
22
+ [:full_description, :description, :location, :file_path, :line_number, :pending, :pending_message, :exception, :execution_result].each do |method|
23
+ it "delegates #{method} to metadata" do
24
+ metadata.should_receive(method)
25
+ subject.send(method)
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "aliased_methods" do
31
+ it "aliases pending? to pending" do
32
+ subject.pending?.should == subject.pending
33
+ end
34
+
35
+ it "aliases options to metadata" do
36
+ subject.options.should == subject.metadata
37
+ end
38
+
39
+ it "aliases example_group to parent" do
40
+ subject.example_group.should == subject.parent
41
+ end
42
+ end
43
+
44
+ describe "#passed?" do
45
+ it "returns true iff execution_result[:status] is passed" do
46
+ subject.should_receive(:execution_result) { {:status => "passed"} }
47
+ subject.passed?.should be_true
48
+ end
49
+
50
+ it "returns false" do
51
+ subject.should_receive(:execution_result) { {} }
52
+ subject.passed?.should be_false
53
+ end
54
+ end
55
+
56
+ describe "#failed?" do
57
+ it "returns true iff execution_result[:status] is failed" do
58
+ subject.should_receive(:execution_result) { {:status => "failed"} }
59
+ subject.failed?.should be_true
60
+ end
61
+
62
+ it "returns false" do
63
+ subject.should_receive(:execution_result) { {} }
64
+ subject.failed?.should be_false
65
+ end
66
+ end
67
+
68
+ describe "#update_metadata" do
69
+ it "calls metadata.update" do
70
+ data = double('data')
71
+ metadata = double('metadata')
72
+ metadata.should_receive(:update).with(data)
73
+ Konacha::Reporter::Metadata.stub(:new) { metadata }
74
+ subject.update_metadata(data)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Konacha::Reporter::Metadata do
4
+ subject { described_class.new({}) }
5
+
6
+ describe "#[]" do
7
+ it "calls the method on self if available" do
8
+ subject.should_receive(:full_description)
9
+ subject[:full_description]
10
+ end
11
+
12
+ it "looks in the data hash" do
13
+ subject.should_receive(:data) { {} }
14
+ subject[:title]
15
+ end
16
+ end
17
+
18
+ describe "#update" do
19
+ it "merges the new data into the data hash" do
20
+ subject.update(:title => "test")
21
+ subject.data.should == {:title => "test"}
22
+ end
23
+ end
24
+
25
+ describe "#exception" do
26
+ it "returns unless the spec failed" do
27
+ subject.exception.should be_nil
28
+ end
29
+
30
+ it "builds a SpecException object" do
31
+ subject.update('status' => 'failed', 'error' => {'message' => 'expected this to work'})
32
+ subject.exception.should be_a(Konacha::Reporter::SpecException)
33
+ subject.exception.message.should == 'expected this to work'
34
+ subject.exception.backtrace.should == []
35
+ end
36
+ end
37
+
38
+ describe "#execution_result" do
39
+ it "returns a hash with execution details" do
40
+ subject.execution_result.keys.sort.should == [:exception, :finished_at, :run_time, :started_at, :status]
41
+ end
42
+ end
43
+
44
+ describe "#pending" do
45
+ it "returns true iff status is pending" do
46
+ subject.update('status' => 'pending')
47
+ subject.pending.should be_true
48
+ end
49
+
50
+ it "returns false" do
51
+ subject.pending.should be_false
52
+ end
53
+ end
54
+
55
+ describe "#description" do
56
+ it "looks for data['title']" do
57
+ subject.update('title' => 'super test')
58
+ subject.description.should == 'super test'
59
+ end
60
+ end
61
+
62
+ describe "#full_description" do
63
+ it "looks for data['fullTitle']" do
64
+ subject.update('fullTitle' => 'super test')
65
+ subject.full_description.should == 'super test'
66
+ end
67
+ end
68
+ end