stendhal 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stendhal (0.1.2)
4
+ stendhal (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/Readme.md CHANGED
@@ -8,8 +8,9 @@ development.
8
8
 
9
9
  ##Current features
10
10
 
11
+ * Pretty decent reporter with colors
11
12
  * Test doubles and stubs (no partial stubbing yet)
12
- * Nested example groups
13
+ * Nested example groups (declare them with either describe or context)
13
14
  * Pending examples
14
15
  * Lame reporter (but will get better eventually)
15
16
  * Matchers (use with object.must or object.must_not)
@@ -62,6 +63,12 @@ development.
62
63
  my_object.future_method
63
64
  end
64
65
 
66
+ context "under some unknown circumstances" do
67
+ it "may act differently" do
68
+ MyClass.must be_a(Class)
69
+ end
70
+ end
71
+
65
72
  end
66
73
 
67
74
  describe "Test double" do
@@ -85,27 +92,29 @@ development.
85
92
 
86
93
  stendhal foo_spec.rb
87
94
 
88
- ###And the output...
95
+ ###And the nice colored output...
89
96
 
90
97
  Foo
91
- * does something
92
- * fails when 7 is not 9 [FAILED]
93
- expected 7 to equal 9
94
- * asks for a kind of object
95
- * asks things to objects [FAILED]
96
- expected "string" to be frozen
97
- * has common sense
98
+ * does something
99
+ * fails when 7 is not 9 [FAILED]
100
+ expected 7 to equal 9
101
+ * asks for a kind of object
102
+ * asks things to objects [FAILED]
103
+ expected "string" to be frozen
104
+ * has common sense
98
105
 
99
106
  Pending examples
100
- * should do something but I don't know what yet
101
- * will do something else
107
+ * should do something but I don't know what yet
108
+ * will do something else
109
+ under some unknown circumstances
110
+ * may act differently
102
111
 
103
112
  Test double
104
- * is declared with fake
105
- * is declared with double as well
106
- * can be given stubs
113
+ * is declared with fake
114
+ * is declared with double as well
115
+ * can be given stubs
107
116
 
108
- 10 examples, 2 failures, 2 pending
117
+ 11 examples, 2 failures, 2 pending
109
118
 
110
119
  ##Note on Patches/Pull Requests
111
120
 
@@ -80,6 +80,11 @@ Feature: Examples and example groups
80
80
  it "does this" do
81
81
  # put your code here
82
82
  end
83
+ context "under certain circumstances" do
84
+ it "does that" do
85
+ # put your code here
86
+ end
87
+ end
83
88
  end
84
89
  describe "pending" do
85
90
  pending "todo" do
@@ -96,8 +101,10 @@ Feature: Examples and example groups
96
101
  And the output should contain "something"
97
102
  And the output should contain "inside another thing"
98
103
  And the output should contain "* does this"
104
+ And the output should contain "under certain circumstances"
105
+ And the output should contain "* does that"
99
106
  And the output should contain "pending"
100
107
  And the output should contain "* todo"
101
108
  And the output should contain "* fails [FAILED]"
102
109
  And the output should contain "indeed"
103
- And the output should contain "3 examples, 1 failure, 1 pending"
110
+ And the output should contain "4 examples, 1 failure, 1 pending"
@@ -0,0 +1,31 @@
1
+ Feature: Reporter
2
+
3
+ Stendhal reports examples in a decent visual manner.
4
+
5
+ Scenario: simple colored report
6
+ Given a directory named "stendhal_project"
7
+ When I cd to "stendhal_project"
8
+ Given a file named "sample_spec.rb" with:
9
+ """
10
+ describe "something" do
11
+ it "does something" do
12
+ false.must eq(false)
13
+ end
14
+ end
15
+
16
+ describe "other thing" do
17
+ describe "under other circumstances" do
18
+ it "fails" do
19
+ true.must_not eq(true)
20
+ end
21
+ end
22
+ end
23
+ """
24
+ When I run "stendhal sample_spec.rb"
25
+ Then the exit status should be 0
26
+ And the output should contain "37msomething"
27
+ And the output should contain "32m* does something"
28
+ And the output should contain "37mother thing"
29
+ And the output should contain "37munder other circumstances"
30
+ And the output should contain "31m* fails"
31
+ And the output should contain "31m2 examples, 1 failure"
@@ -5,7 +5,10 @@ ARGV.each do |file|
5
5
  end
6
6
 
7
7
  examples, failures, pending = Stendhal::ExampleGroup.run_all
8
- report = "\n#{examples} #{examples != 1 ? 'examples' : 'example'}, #{failures} #{failures != 1 ? 'failures' : 'failure'}"
8
+ report = "#{examples} #{examples != 1 ? 'examples' : 'example'}, #{failures} #{failures != 1 ? 'failures' : 'failure'}"
9
9
  report += ", #{pending} pending" if pending > 0
10
- puts report
10
+
11
+ Stendhal::Reporter.whitespace
12
+ color = failures == 0 ? :green : :red
13
+ Stendhal::Reporter.line report, :color => color
11
14
 
@@ -1,7 +1,13 @@
1
1
  module Kernel
2
2
  def describe(*args,&blk)
3
- Stendhal::ExampleGroup.new(*args,&blk)
3
+ if self.is_a? Stendhal::ExampleGroup
4
+ self.add_example_group(*args,&blk)
5
+ else
6
+ Stendhal::ExampleGroup.new(*args,&blk)
7
+ end
4
8
  end
9
+ alias_method :context, :describe
10
+
5
11
  def must(matcher)
6
12
  matcher.match self
7
13
  end
@@ -3,36 +3,66 @@ module Stendhal
3
3
  @@example_groups = []
4
4
 
5
5
  attr_reader :description
6
+ attr_reader :example_groups
6
7
  attr_reader :block
7
8
  attr_reader :examples
8
9
 
9
10
  def initialize(docstring, options = {}, &block)
10
11
  @description = docstring
11
12
  @block = block
13
+ @parent = options[:parent]
14
+ @example_groups = []
12
15
  @examples = []
13
16
  instance_exec(&@block) if block_given?
14
17
  @@example_groups << self
15
18
  end
16
19
 
20
+ def add_example_group(*args, &block)
21
+ if args.last.is_a?(Hash)
22
+ args.last.update(:parent => true)
23
+ else
24
+ args << {:parent => true}
25
+ end
26
+ @example_groups << ExampleGroup.new(*args, &block)
27
+ end
28
+
29
+ def has_parent?
30
+ !@parent.nil?
31
+ end
32
+
17
33
  def run
34
+ Reporter.line @description, :indent => Reporter.current_indentation, :color => :white
35
+ original_indentation = Reporter.current_indentation
36
+
18
37
  failures = pending = 0
19
38
  examples.reject do |example|
20
39
  if example.pending?
21
40
  pending += 1
22
- $stdout.print "* #{example.description} [PENDING]\n"
41
+ Reporter.line "* #{example.description} [PENDING]", :indent => Reporter.current_indentation + 1, :color => :yellow
23
42
  true
24
43
  else
25
44
  false
26
45
  end
27
46
  end.each do |example|
28
47
  failures += example.run
48
+ color = :red
49
+ color = :green unless example.failed? || example.aborted?
29
50
  status = " [FAILED]" if example.failed?
30
51
  status = " [ABORTED]" if example.aborted?
31
- $stdout.print "* #{example.description}#{status || ''}\n"
32
- $stdout.print "\t#{example.failed_message}\n" if example.failed?
33
- $stdout.print "\t#{example.aborted_message}\n" if example.aborted?
52
+ Reporter.line "* #{example.description}#{status || ''}", :indent => Reporter.current_indentation + 1, :color => color
53
+ Reporter.line "#{example.failed_message}", :indent => Reporter.current_indentation + 2, :color => :red if example.failed?
54
+ Reporter.line "#{example.aborted_message}", :indent => Reporter.current_indentation + 2, :color => :red if example.aborted?
34
55
  end
35
- [examples.count, failures, pending]
56
+ group_result = [examples.count, failures, pending]
57
+
58
+ @example_groups.each do |group|
59
+ Reporter.current_indentation += 1
60
+ sub_result = group.run
61
+ group_result = group_result.zip(sub_result).map{ |pair| pair[0] + pair[1] } if sub_result
62
+ end
63
+ Reporter.current_indentation = original_indentation
64
+
65
+ group_result
36
66
  end
37
67
 
38
68
  def add_example(example)
@@ -47,16 +77,20 @@ module Stendhal
47
77
  end
48
78
 
49
79
  def run_all
80
+ Reporter.whitespace
50
81
  result = [0,0,0]
51
- @@example_groups.each do |g|
52
- puts "\n"
53
- puts g.description
82
+ primary_example_groups.each do |g|
54
83
  group_result = g.run
84
+ Reporter.whitespace
55
85
  result = result.zip(group_result).map{ |pair| pair[0] + pair[1] }
56
86
  end
57
87
  result
58
88
  end
59
89
 
90
+ def primary_example_groups
91
+ @@example_groups.reject { |g| g.has_parent? }
92
+ end
93
+
60
94
  def count
61
95
  @@example_groups.count
62
96
  end
@@ -10,10 +10,6 @@ module Stendhal
10
10
  end
11
11
  alias_method :eql, :eq
12
12
 
13
- def caca(other)
14
- Equality.new(other)
15
- end
16
-
17
13
  def be_a(kind)
18
14
  Kind.new(kind)
19
15
  end
@@ -0,0 +1,75 @@
1
+ require 'singleton'
2
+
3
+ module Stendhal
4
+ class Reporter
5
+ include ::Singleton
6
+
7
+ def initialize
8
+ @current_indentation = 0
9
+ @output = $stdout
10
+ end
11
+
12
+ attr_writer :output
13
+ attr_accessor :current_indentation
14
+
15
+ COLORS =
16
+ {"black" => "30",
17
+ "red" => "31",
18
+ "green" => "32",
19
+ "yellow" => "33",
20
+ "blue" => "34",
21
+ "magenta" => "35",
22
+ "cyan" => "36",
23
+ "white" => "37",
24
+
25
+ "RED" => "41",
26
+ "GREEN" => "42",
27
+ "YELLOW" => "43",
28
+ "BLUE" => "44",
29
+ "MAGENTA" => "45",
30
+ "CYAN" => "46",
31
+ "WHITE" => "47"}
32
+
33
+ def reset
34
+ @output = $stdout
35
+ end
36
+
37
+ def line(text, options = {})
38
+ options[:indent].times { tab } if options[:indent]
39
+ with_color(options[:color]) do
40
+ @output.print text
41
+ end
42
+ @output.print "\n"
43
+ end
44
+
45
+ def whitespace
46
+ @output.print "\n"
47
+ end
48
+
49
+ def tab
50
+ @output.print " "
51
+ end
52
+
53
+ # Delegate all methods to instance
54
+ class << self
55
+ def method_missing(*args, &block)
56
+ instance.send(*args, &block)
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def with_color(color,&block)
63
+ return unless block_given?
64
+ if color
65
+ @output.print "\e[0;#{COLORS[color.to_s]}m"
66
+ yield
67
+ @output.print "\e[0m"
68
+ else
69
+ yield
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+
@@ -1,3 +1,3 @@
1
1
  module Stendhal
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/stendhal.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'stendhal/reporter'
1
2
  require 'stendhal/exceptions'
2
3
  require 'stendhal/matchers'
3
4
  require 'stendhal/mocks'
@@ -49,6 +49,7 @@ module Stendhal
49
49
  @group.add_example @failing_example
50
50
  @pending_example = Example.new("pending")
51
51
  @group.add_example @pending_example
52
+ @group.add_example_group("another group")
52
53
  end
53
54
 
54
55
  it "runs all non-pending examples" do
@@ -59,11 +60,26 @@ module Stendhal
59
60
  @group.run
60
61
  end
61
62
 
63
+ it "runs all its children" do
64
+ @group.example_groups.each do |g|
65
+ g.should_receive(:run)
66
+ end
67
+ @group.run
68
+ end
69
+
62
70
  it "returns an array with total examples, failures and pendings" do
63
71
  @group.run.should == [6,1,1]
64
72
  end
65
73
  end
66
74
 
75
+ describe "#add_example_group" do
76
+ it "adds a nested example group inside self" do
77
+ example_group = ExampleGroup.new("group")
78
+ example_group.add_example_group(ExampleGroup.new("other group"))
79
+ example_group.example_groups.first.should have_parent
80
+ end
81
+ end
82
+
67
83
  describe "class methods" do
68
84
  describe "#count" do
69
85
  it "returns the number of example groups in memory" do
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ module Stendhal
4
+ describe Reporter do
5
+
6
+ let(:out) { StringIO.new }
7
+
8
+ after(:each) do
9
+ Reporter.reset
10
+ out.flush
11
+ end
12
+
13
+ it 'is a singleton' do
14
+ Reporter.instance.should be_a(Reporter)
15
+ end
16
+
17
+ describe "instance methods" do
18
+
19
+ before(:each) do
20
+ Reporter.output = out
21
+ end
22
+
23
+ describe "#line" do
24
+
25
+ it 'outputs a line' do
26
+ Reporter.line "This is a line"
27
+ out.string.should include("This is a line\n")
28
+ end
29
+
30
+ context "with indentation" do
31
+ it 'outputs an indented line' do
32
+ Reporter.line "This is a line", :indent => 2
33
+ out.string.should include(" This is a line\n")
34
+ end
35
+ end
36
+
37
+ context "with color" do
38
+ it 'outputs a coloured line' do
39
+ Reporter.line "This is a line", :color => :red
40
+ out.string.should include("\e[0;31mThis is a line\e[0m\n")
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ describe "#whitespace" do
47
+ it 'outputs whitespace' do
48
+ Reporter.whitespace
49
+ out.string.should match(/\n/)
50
+ end
51
+ end
52
+
53
+ describe "#tab" do
54
+ it 'outputs a tab' do
55
+ Reporter.tab
56
+ out.string.should include(" ")
57
+ end
58
+ end
59
+
60
+ describe "#current_indentation" do
61
+ it 'returns 0 by default' do
62
+ Reporter.current_indentation.should == 0
63
+ end
64
+ it 'can be set' do
65
+ Reporter.current_indentation = 2
66
+ Reporter.current_indentation.should == 2
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Josep M. Bach
@@ -91,6 +91,7 @@ files:
91
91
  - bin/stendhal
92
92
  - features/examples.feature
93
93
  - features/expectations.feature
94
+ - features/reporter.feature
94
95
  - features/support/env.rb
95
96
  - features/test_doubles_and_stubs.feature
96
97
  - lib/stendhal.rb
@@ -107,6 +108,7 @@ files:
107
108
  - lib/stendhal/matchers/predicate.rb
108
109
  - lib/stendhal/mocks.rb
109
110
  - lib/stendhal/mocks/test_double.rb
111
+ - lib/stendhal/reporter.rb
110
112
  - lib/stendhal/version.rb
111
113
  - script/console
112
114
  - spec/spec_helper.rb
@@ -118,6 +120,7 @@ files:
118
120
  - spec/stendhal/matchers/kind_spec.rb
119
121
  - spec/stendhal/matchers/predicate_spec.rb
120
122
  - spec/stendhal/matchers_spec.rb
123
+ - spec/stendhal/reporter_spec.rb
121
124
  - spec/stendhal/test_double_spec.rb
122
125
  - stendhal.gemspec
123
126
  has_rdoc: true
@@ -155,6 +158,7 @@ summary: Stendhal is a really simple test framework.
155
158
  test_files:
156
159
  - features/examples.feature
157
160
  - features/expectations.feature
161
+ - features/reporter.feature
158
162
  - features/support/env.rb
159
163
  - features/test_doubles_and_stubs.feature
160
164
  - spec/spec_helper.rb
@@ -166,4 +170,5 @@ test_files:
166
170
  - spec/stendhal/matchers/kind_spec.rb
167
171
  - spec/stendhal/matchers/predicate_spec.rb
168
172
  - spec/stendhal/matchers_spec.rb
173
+ - spec/stendhal/reporter_spec.rb
169
174
  - spec/stendhal/test_double_spec.rb