rspec-extra-formatters 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -69,14 +69,18 @@ class JUnitFormatter < RSpec::Core::Formatters::BaseFormatter
69
69
  output.puts("<testsuite errors=\"0\" failures=\"#{failure_count+pending_count}\" tests=\"#{example_count}\" time=\"#{duration}\" timestamp=\"#{Time.now.iso8601}\">")
70
70
  output.puts(" <properties />")
71
71
  @test_results[:successes].each do |t|
72
- md = t.metadata
73
- runtime = md[:execution_result][:run_time]
74
- output.puts(" <testcase classname=\"#{md[:file_path]}\" name=\"#{md[:full_description]}\" time=\"#{runtime}\" />")
72
+ md = t.metadata
73
+ runtime = md[:execution_result][:run_time]
74
+ description = _xml_escape(md[:full_description])
75
+ file_path = _xml_escape(md[:file_path])
76
+ output.puts(" <testcase classname=\"#{file_path}\" name=\"#{description}\" time=\"#{runtime}\" />")
75
77
  end
76
78
  @test_results[:failures].each do |t|
77
- md = t.metadata
78
- runtime = md[:execution_result][:run_time]
79
- output.puts(" <testcase classname=\"#{md[:file_path]}\" name=\"#{md[:full_description]}\" time=\"#{runtime}\">")
79
+ md = t.metadata
80
+ description = _xml_escape(md[:full_description])
81
+ file_path = _xml_escape(md[:file_path])
82
+ runtime = md[:execution_result][:run_time]
83
+ output.puts(" <testcase classname=\"#{file_path}\" name=\"#{description}\" time=\"#{runtime}\">")
80
84
  output.puts(" <failure message=\"failure\" type=\"failure\">")
81
85
  output.puts("<![CDATA[ #{read_failure(t)} ]]>")
82
86
  output.puts(" </failure>")
@@ -85,4 +89,10 @@ class JUnitFormatter < RSpec::Core::Formatters::BaseFormatter
85
89
  output.puts("</testsuite>")
86
90
  end
87
91
 
92
+ def _xml_escape(x)
93
+ x.gsub("&", "&amp;").
94
+ gsub("\"", "&quot;").
95
+ gsub(">", "&gt;").
96
+ gsub("<", "&lt;")
97
+ end
88
98
  end
@@ -26,22 +26,24 @@
26
26
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
27
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
 
29
+ require "stringio"
29
30
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper.rb")
30
31
 
31
32
  describe JUnitFormatter do
32
33
 
33
34
  before(:each) do
34
- @output = mock("output")
35
+ @now = Time.now
36
+ Time.stub(:now).and_return(@now)
35
37
  end
36
38
 
37
39
  it "should initialize the tests with failures and success" do
38
- JUnitFormatter.new(@output).test_results.should eql({:failures=>[], :successes=>[]})
40
+ JUnitFormatter.new(StringIO.new).test_results.should eql({:failures=>[], :successes=>[]})
39
41
  end
40
42
 
41
43
  describe "example_passed" do
42
44
 
43
45
  it "should push the example obj into success list" do
44
- f = JUnitFormatter.new(@output)
46
+ f = JUnitFormatter.new(StringIO.new)
45
47
  f.example_passed("foobar")
46
48
  f.test_results[:successes].should eql(["foobar"])
47
49
  end
@@ -51,7 +53,7 @@ describe JUnitFormatter do
51
53
  describe "example_failed" do
52
54
 
53
55
  it "should push the example obj into failures list" do
54
- f = JUnitFormatter.new(@output)
56
+ f = JUnitFormatter.new(StringIO.new)
55
57
  f.example_failed("foobar")
56
58
  f.test_results[:failures].should eql(["foobar"])
57
59
  end
@@ -61,7 +63,7 @@ describe JUnitFormatter do
61
63
  describe "example_pending" do
62
64
 
63
65
  it "should do the same as example_failed" do
64
- f = JUnitFormatter.new(@output)
66
+ f = JUnitFormatter.new(StringIO.new)
65
67
  f.example_pending("foobar")
66
68
  f.test_results[:failures].should eql(["foobar"])
67
69
  end
@@ -75,7 +77,7 @@ describe JUnitFormatter do
75
77
  example.should_receive(:metadata).exactly(2).times.and_return({:execution_result => { :exception_encountered => nil \
76
78
  , :exception => nil \
77
79
  }})
78
- f = JUnitFormatter.new(@output)
80
+ f = JUnitFormatter.new(StringIO.new)
79
81
  f.read_failure(example).should eql("")
80
82
  end
81
83
 
@@ -89,7 +91,7 @@ describe JUnitFormatter do
89
91
  , :exception => strace \
90
92
  }})
91
93
 
92
- f = JUnitFormatter.new(@output)
94
+ f = JUnitFormatter.new(StringIO.new)
93
95
  f.read_failure(example).should eql("foobar\nfoo\nbar")
94
96
  end
95
97
 
@@ -101,7 +103,7 @@ describe JUnitFormatter do
101
103
  example = mock("example")
102
104
  example.should_receive(:metadata).exactly(2).times.and_return({:execution_result => {:exception_encountered => strace}})
103
105
 
104
- f = JUnitFormatter.new(@output)
106
+ f = JUnitFormatter.new(StringIO.new)
105
107
  f.read_failure(example).should eql("foobar\nfoo\nbar")
106
108
  end
107
109
 
@@ -115,12 +117,12 @@ describe JUnitFormatter do
115
117
  strace.should_receive(:backtrace).and_return(["foo","bar"])
116
118
 
117
119
  example0 = mock("example-0")
118
- example1 = mock("example-1")
119
120
  example0.should_receive(:metadata).and_return({ :full_description => "foobar-success" \
120
121
  , :file_path => "lib/foobar-s.rb" \
121
122
  , :execution_result => { :run_time => 0.1 } \
122
123
  })
123
124
 
125
+ example1 = mock("example-1")
124
126
  example1.should_receive(:metadata).exactly(3).times.and_return({ :full_description => "foobar-failure" \
125
127
  , :file_path => "lib/foobar-f.rb" \
126
128
  , :execution_result => { :exception_encountered => strace \
@@ -128,21 +130,45 @@ describe JUnitFormatter do
128
130
  }
129
131
  })
130
132
 
131
- @output.should_receive(:puts).with("<?xml version=\"1.0\" encoding=\"utf-8\" ?>")
132
- @output.should_receive(:puts).with("<testsuite errors=\"0\" failures=\"1\" tests=\"2\" time=\"0.1\" timestamp=\"#{Time.now.iso8601}\">")
133
- @output.should_receive(:puts).with(" <properties />")
134
- @output.should_receive(:puts).with(" <testcase classname=\"lib/foobar-s.rb\" name=\"foobar-success\" time=\"0.1\" />")
135
- @output.should_receive(:puts).with(" <testcase classname=\"lib/foobar-f.rb\" name=\"foobar-failure\" time=\"0.1\">")
136
- @output.should_receive(:puts).with(" <failure message=\"failure\" type=\"failure\">")
137
- @output.should_receive(:puts).with("<![CDATA[ foobar\nfoo\nbar ]]>")
138
- @output.should_receive(:puts).with(" </failure>")
139
- @output.should_receive(:puts).with(" </testcase>")
140
- @output.should_receive(:puts).with("</testsuite>")
141
-
142
- f = JUnitFormatter.new(@output)
133
+ output = StringIO.new
134
+ f = JUnitFormatter.new(output)
143
135
  f.example_passed(example0)
144
136
  f.example_failed(example1)
145
137
  f.dump_summary("0.1", 2, 1, 0)
138
+
139
+ output.string.should == <<-EOF
140
+ <?xml version="1.0" encoding="utf-8" ?>
141
+ <testsuite errors="0" failures="1" tests="2" time="0.1" timestamp="#{@now.iso8601}">
142
+ <properties />
143
+ <testcase classname="lib/foobar-s.rb" name="foobar-success" time="0.1" />
144
+ <testcase classname="lib/foobar-f.rb" name="foobar-failure" time="0.1">
145
+ <failure message="failure" type="failure">
146
+ <![CDATA[ foobar\nfoo\nbar ]]>
147
+ </failure>
148
+ </testcase>
149
+ </testsuite>
150
+ EOF
151
+ end
152
+
153
+ it "should escape characteres <,>,&,\" before building xml" do
154
+ example0 = mock("example-0")
155
+ example0.should_receive(:metadata).and_return({ :full_description => "foobar-success >>> &\"& <<<" \
156
+ , :file_path => "lib/>foobar-s.rb" \
157
+ , :execution_result => { :run_time => 0.1 } \
158
+ })
159
+
160
+ output = StringIO.new
161
+ f = JUnitFormatter.new(output)
162
+ f.example_passed(example0)
163
+ f.dump_summary("0.1", 2, 1, 0)
164
+
165
+ output.string.should == <<-EOF
166
+ <?xml version="1.0" encoding="utf-8" ?>
167
+ <testsuite errors="0" failures="1" tests="2" time="0.1" timestamp="#{@now.iso8601}">
168
+ <properties />
169
+ <testcase classname="lib/&gt;foobar-s.rb" name="foobar-success &gt;&gt;&gt; &amp;&quot;&amp; &lt;&lt;&lt;" time="0.1" />
170
+ </testsuite>
171
+ EOF
146
172
  end
147
173
 
148
174
  end
@@ -26,16 +26,13 @@
26
26
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
27
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
 
29
+ require "stringio"
29
30
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper.rb")
30
31
 
31
32
  describe TapFormatter do
32
33
 
33
- before(:each) do
34
- @output = mock("output")
35
- end
36
-
37
34
  it "should initialize the counter to 0" do
38
- TapFormatter.new(@output).total.should eql(0)
35
+ TapFormatter.new(StringIO.new).total.should eql(0)
39
36
  end
40
37
 
41
38
  describe "example_passed" do
@@ -43,12 +40,13 @@ describe TapFormatter do
43
40
  it "should increment the counter and use the full_description attribute" do
44
41
  example = mock("example")
45
42
  example.should_receive(:metadata).and_return({:full_description => "foobar"})
46
- @output.should_receive(:puts).with("ok 1 - foobar")
47
43
 
48
- f = TapFormatter.new(@output)
44
+ output = StringIO.new
45
+ f = TapFormatter.new(output)
49
46
  f.example_passed(example)
50
47
 
51
48
  f.total.should eql(1)
49
+ output.string.should == "ok 1 - foobar\n"
52
50
  end
53
51
 
54
52
  end
@@ -58,12 +56,13 @@ describe TapFormatter do
58
56
  it "should increment the counter and use the full_description attribute" do
59
57
  example = mock("example")
60
58
  example.should_receive(:metadata).and_return({:full_description => "foobar"})
61
- @output.should_receive(:puts).with("not ok 1 - foobar")
62
-
63
- f = TapFormatter.new(@output)
59
+
60
+ output = StringIO.new
61
+ f = TapFormatter.new(output)
64
62
  f.example_failed(example)
65
63
 
66
64
  f.total.should eql(1)
65
+ output.string.should == "not ok 1 - foobar\n"
67
66
  end
68
67
  end
69
68
 
@@ -72,12 +71,13 @@ describe TapFormatter do
72
71
  it "should do the same as example_failed" do
73
72
  example = mock("example")
74
73
  example.should_receive(:metadata).and_return({:full_description => "foobar"})
75
- @output.should_receive(:puts).with("not ok 1 - foobar")
76
74
 
77
- f = TapFormatter.new(@output)
75
+ output = StringIO.new
76
+ f = TapFormatter.new(output)
78
77
  f.example_pending(example)
79
78
 
80
79
  f.total.should eql(1)
80
+ output.string.should == "not ok 1 - foobar\n"
81
81
  end
82
82
 
83
83
  end
@@ -89,16 +89,20 @@ describe TapFormatter do
89
89
  example.should_receive(:metadata).and_return({:full_description => "foobar"})
90
90
  example.should_receive(:metadata).and_return({:full_description => "foobar"})
91
91
  example.should_receive(:metadata).and_return({:full_description => "foobar"})
92
- @output.should_receive(:puts).with("ok 1 - foobar")
93
- @output.should_receive(:puts).with("not ok 2 - foobar")
94
- @output.should_receive(:puts).with("not ok 3 - foobar")
95
- @output.should_receive(:puts).with("1..3")
96
92
 
97
- f = TapFormatter.new(@output)
93
+ output = StringIO.new
94
+ f = TapFormatter.new(output)
98
95
  f.example_passed(example)
99
96
  f.example_failed(example)
100
97
  f.example_pending(example)
101
98
  f.dump_summary(0.1, 3, 1, 1)
99
+
100
+ output.string.should == <<-EOF
101
+ ok 1 - foobar
102
+ not ok 2 - foobar
103
+ not ok 3 - foobar
104
+ 1..3
105
+ EOF
102
106
  end
103
107
 
104
108
  it "should print nothing if there were not tests" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rspec-extra-formatters
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.1"
5
+ version: "0.2"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Diego Souza