protest 0.4.2 → 0.5.0

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.
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  doc
2
2
  dist
3
+ tmp
4
+ *.rbc
@@ -0,0 +1,12 @@
1
+ ### v0.5.0
2
+
3
+ * Added ability of defining the report to be used in the `PROTEST_REPORT`
4
+ environment variable.
5
+ * `Documentation` is the new default report.
6
+ * Deprecated `global_setup` and `global_teardown` methods.
7
+ * Deprecated `before` and `after` aliases for `setup` and `teardown`.
8
+ * Deprecated `story` and `scenario` aliases.
9
+ * Removed `Stories` report.
10
+ * Removed `Protest::Stories` module.
11
+ * Refactored reports.
12
+ * Modified Summaries module to include the full filtered backtrace in all reports.
data/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
1
  (The MIT License)
2
2
 
3
3
  Copyright (c) 2009 Nicolas Sanguinetti
4
- Copyright (c) 2010 Matías Flores
4
+ Copyright (c) 2010-2013 Matías Flores
5
5
 
6
6
  Permission is hereby granted, free of charge, to any person obtaining
7
7
  a copy of this software and associated documentation files (the
@@ -0,0 +1,256 @@
1
+ # Protest [<img src="https://secure.travis-ci.org/matflores/protest.png?branch=master" alt="Build Status" />](http://travis-ci.org/matflores/protest)
2
+
3
+ Protest is a tiny, simple, and easy-to-extend testing framework for ruby.
4
+
5
+ ## Get it
6
+
7
+ ```
8
+ gem install protest
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ require "protest"
15
+
16
+ Protest.describe "A user" do
17
+ setup do
18
+ @user = User.new(name: "John Doe", email: "john@example.org")
19
+ end
20
+
21
+ it "has a name" do
22
+ assert_equal "John Doe", @user.name
23
+ end
24
+
25
+ it "has an email" do
26
+ assert_equal "john@example.org", @user.email
27
+ end
28
+ end
29
+ ```
30
+
31
+ Use `Protest.context` or `Protest.describe` at the top level to define
32
+ a new test case. Inside the block, you can use `test`, `it` or `should`
33
+ for defining each individual test.
34
+
35
+ ## Setup and teardown
36
+
37
+ If you need to run code before or after each test, declare a `setup` or
38
+ `teardown` block:
39
+
40
+ ```ruby
41
+ Protest.context "A user" do
42
+ setup do # this runs before each test
43
+ @user = User.create(name: "John")
44
+ end
45
+
46
+ teardown do # this runs after each test
47
+ @user.destroy
48
+ end
49
+ end
50
+ ```
51
+
52
+ `setup` and `teardown` blocks are evaluated in the same context as your test,
53
+ which means any instance variables defined in any of them are available in the
54
+ rest.
55
+
56
+ ## Nested contexts
57
+
58
+ Break down your test into logical chunks with nested contexts:
59
+
60
+ ```ruby
61
+ Protest.describe "A user" do
62
+ setup do
63
+ @user = User.make
64
+ end
65
+
66
+ context "when validating" do
67
+ it "validates name" do
68
+ @user.name = nil
69
+ assert !@user.valid?
70
+ end
71
+
72
+ # etc, etc
73
+ end
74
+
75
+ context "doing something else" do
76
+ # you get the idea
77
+ end
78
+ end
79
+ ```
80
+
81
+ Any `setup` or `teardown` blocks you defined in a context will run in that
82
+ context and in _any_ other context nested in it.
83
+
84
+ ## Pending tests
85
+
86
+ There are two ways of marking a test as pending. You can declare a test with no
87
+ body:
88
+
89
+ ```ruby
90
+ Protest.context "Some tests" do
91
+ test "this test will be marked as pending"
92
+
93
+ test "this tests is also pending"
94
+
95
+ test "this test isn't pending" do
96
+ assert true
97
+ end
98
+ end
99
+ ```
100
+
101
+ Or you can call the `pending` method from inside your test:
102
+
103
+ ```ruby
104
+ Protest.context "Some tests" do
105
+ test "this test is pending" do
106
+ pending "oops, this doesn't work"
107
+ assert false
108
+ end
109
+ end
110
+ ```
111
+
112
+ ## Assertions
113
+
114
+ Protest includes just three basic assertion methods:
115
+
116
+ * assert(condition, message)
117
+
118
+ Ensure that a condition is met, otherwise it raises an `AssertionFailed`
119
+ exception. The second argument is optional and it is used to override
120
+ the default failure message.
121
+
122
+ * assert_equal(expected, actual, message)
123
+
124
+ Syntax sugar for `assert(expected == actual, message)`.
125
+
126
+ * assert_raise(exception, message) do ... end
127
+
128
+ Passes if the code block raises the specified exception. If no exception
129
+ is specified, this assertion passes if _any_ exception is raised inside
130
+ the block. The second argument is optional and it is used to override
131
+ the default failure message.
132
+
133
+ ## Custom assertions
134
+
135
+ If you want to add more assertion methods, just define new methods that
136
+ rely on `assert`.
137
+
138
+ For example:
139
+
140
+ ```ruby
141
+ module AwesomenessAssertions
142
+ def assert_awesomeness(object)
143
+ assert object.awesome?, "#{object.inspect} is not awesome enough"
144
+ end
145
+ end
146
+
147
+ class Protest::TestCase
148
+ include AwesomenessAssertions
149
+ end
150
+ ```
151
+
152
+ You could also define rspec-like matchers if you like that style. See
153
+ `matchers.rb` in the examples directory for an example.
154
+
155
+ ## Reports
156
+
157
+ Protest can report the output of a test suite in many ways. The library ships
158
+ with a few reports defined by default.
159
+
160
+ You can select which report to use using the `report_with` method:
161
+
162
+ ```ruby
163
+ Protest.report_with(:documentation)
164
+ Protest.report_with(:progress)
165
+ Protest.report_with(:my_awesome_custom_report)
166
+ ```
167
+
168
+ By default, Protest will use the report defined in the `PROTEST_REPORT`
169
+ environment variable. If this variable is not defined, the Documentation
170
+ report will be used.
171
+
172
+ ### Progress report
173
+
174
+ Use this report by calling `Protest.report_with(:progress)`.
175
+
176
+ The progress report will output the "classic" Test::Unit output of periods for
177
+ passing tests, "F" for failing assertions, "E" for unrescued exceptions, and
178
+ "P" for pending tests, in full color.
179
+
180
+ ### Documentation report
181
+
182
+ Use this report by calling `Protest.report_with(:progress)`.
183
+
184
+ For each testcase in your suite, this will output the description of the test
185
+ case (whatever you passed to `TestCase.context`), followed by the name of each
186
+ test in that context, one per line. For example:
187
+
188
+ ```ruby
189
+ Protest.context "A user" do
190
+ test "has a name"
191
+ test "has an email"
192
+
193
+ context "validations" do
194
+ test "ensure the email can't be blank"
195
+ end
196
+ end
197
+ ```
198
+
199
+ Will output, when run with the `:documentation` report:
200
+
201
+ ```
202
+ A user
203
+ - has a name (Not Yet Implemented)
204
+ - has an email (Not Yet Implemented)
205
+
206
+ A user validations
207
+ - ensure the email can't be blank (Not Yet Implemented)
208
+ ```
209
+
210
+ (The 'Not Yet Implemented' messages are because the tests have no body. See
211
+ "Pending tests", above.)
212
+
213
+ This is similar to the specdoc runner in [rspec](http://rspec.info).
214
+
215
+ ### Summary report
216
+
217
+ Use this report by calling `Protest.report_with(:summary)`.
218
+
219
+ This report will output a brief summary with the total number of tests,
220
+ assertions, passed tests, pending tests, failed tests and errors.
221
+
222
+ ### Turn report
223
+
224
+ Use this report by calling `Protest.report_with(:turn)`.
225
+
226
+ This report displays each test on a separate line with failures being displayed
227
+ immediately instead of at the end of the tests.
228
+
229
+ You might find this useful when running a large test suite, as it can be very
230
+ frustrating to see a failure (....F...) and then have to wait until all the
231
+ tests finish before you can see what the exact failure was.
232
+
233
+ This report is based on the output displayed by [TURN](http://github.com/TwP/turn),
234
+ Test::Unit Reporter (New) by Tim Pease.
235
+
236
+ ### Defining your own reports
237
+
238
+ This is really, really easy. All you need to do is subclass `Report`, and
239
+ register your subclass by calling `Protest.add_report`. See the
240
+ documentation for details, or take a look at the source code for
241
+ `Protest::Reports::Progress` and `Protest::Reports::Documentation`.
242
+
243
+ ## Using Rails?
244
+
245
+ If you are using Rails you may want to take a look at [protest-rails](http://github.com/matflores/protest-rails).
246
+
247
+ ## Credits
248
+
249
+ Protest was created by [Nicolás Sanguinetti](http://nicolassanguinetti.info)
250
+ and is currently maintained by [Matías Flores](http://matflores.com).
251
+
252
+ ## License
253
+
254
+ Distributed under the terms of the MIT license.
255
+ See bundled [LICENSE](https://github.com/matflores/protest/blob/master/LICENSE)
256
+ file for more info.
data/Rakefile CHANGED
@@ -1,24 +1,5 @@
1
- begin
2
- require "hanna/rdoctask"
3
- rescue LoadError
4
- require "rake/rdoctask"
5
- end
6
-
7
1
  require "rake/testtask"
8
2
 
9
- Rake::RDocTask.new do |rd|
10
- rd.main = "README.rdoc"
11
- rd.title = "API Documentation for Protest"
12
- rd.rdoc_files.include("README.rdoc", "LICENSE", "lib/**/*.rb")
13
- rd.rdoc_dir = "doc"
14
- end
15
-
16
- begin
17
- require "mg"
18
- MG.new("protest.gemspec")
19
- rescue LoadError
20
- end
21
-
22
3
  Rake::TestTask.new do |t|
23
4
  t.libs << "test"
24
5
  end
@@ -1,6 +1,4 @@
1
1
  module Protest
2
- VERSION = "0.4.2"
3
-
4
2
  # Exception raised when an assertion fails. See TestCase#assert
5
3
  class AssertionFailed < StandardError; end
6
4
 
@@ -19,14 +17,7 @@ module Protest
19
17
  #
20
18
  # See Protest.report_with to see how to select which report will be used.
21
19
  def self.add_report(name, report)
22
- available_reports[name] = report
23
- end
24
-
25
- # Register a test case to be run with Protest. This is done automatically
26
- # whenever you subclass Protest::TestCase, so you probably shouldn't pay
27
- # much attention to this method.
28
- def self.add_test_case(test_case)
29
- available_test_cases << test_case
20
+ reports[name] = report
30
21
  end
31
22
 
32
23
  # Set to +false+ to avoid running tests +at_exit+. Default is +true+.
@@ -45,7 +36,7 @@ module Protest
45
36
  #
46
37
  # See Protest.add_test_case and Protest.report_with
47
38
  def self.run_all_tests!(*report_args)
48
- Runner.new(@report).run(*available_test_cases)
39
+ Runner.new(@report).run(*test_cases)
49
40
  end
50
41
 
51
42
  # Select the name of the Report to use when running tests. See
@@ -53,7 +44,7 @@ module Protest
53
44
  #
54
45
  # Any extra arguments will be forwarded to the report's #initialize method.
55
46
  #
56
- # The default report is Protest::Reports::Progress
47
+ # The default report is Protest::Reports::Documentation
57
48
  def self.report_with(name, *report_args)
58
49
  @report = report(name, *report_args)
59
50
  end
@@ -62,7 +53,7 @@ module Protest
62
53
  # If the given +name+ doesn't match a report registered via
63
54
  # Protest.add_report then the method will raise IndexError.
64
55
  def self.report(name, *report_args)
65
- available_reports.fetch(name).new(*report_args)
56
+ reports.fetch(name).new(*report_args)
66
57
  end
67
58
 
68
59
  # Set what object will filter the backtrace. It must respond to
@@ -76,17 +67,17 @@ module Protest
76
67
  @backtrace_filter
77
68
  end
78
69
 
79
- def self.available_test_cases
70
+ def self.test_cases
80
71
  @test_cases ||= []
81
72
  end
82
- private_class_method :available_test_cases
83
73
 
84
- def self.available_reports
85
- @available_reports ||= {}
74
+ def self.reports
75
+ @reports ||= {}
86
76
  end
87
- private_class_method :available_reports
77
+ private_class_method :reports
88
78
  end
89
79
 
80
+ require "protest/version"
90
81
  require "protest/utils"
91
82
  require "protest/utils/backtrace_filter"
92
83
  require "protest/utils/summaries"
@@ -100,10 +91,9 @@ require "protest/reports/progress"
100
91
  require "protest/reports/documentation"
101
92
  require "protest/reports/turn"
102
93
  require "protest/reports/summary"
103
- require "protest/reports/stories"
104
94
 
105
95
  Protest.autorun = true
106
- Protest.report_with(:progress)
96
+ Protest.report_with((ENV["PROTEST_REPORT"] || "documentation").to_sym)
107
97
  Protest.backtrace_filter = Protest::Utils::BacktraceFilter.new
108
98
 
109
99
  at_exit do
@@ -1,5 +1,15 @@
1
1
  module Protest
2
2
  class Report
3
+ include Utils::Summaries
4
+ include Utils::ColorfulOutput
5
+
6
+ attr_reader :stream #:nodoc:
7
+
8
+ # Set the stream where the report will be written to. STDOUT by default.
9
+ def initialize(stream=STDOUT)
10
+ @stream = stream
11
+ end
12
+
3
13
  # Define an event handler for your report. The different events fired in a
4
14
  # report's life cycle are:
5
15
  #
@@ -30,16 +30,6 @@ module Protest
30
30
  #
31
31
  # This is based on the specdoc runner in rspec[http://rspec.info].
32
32
  class Reports::Documentation < Report
33
- include Utils::Summaries
34
- include Utils::ColorfulOutput
35
-
36
- attr_reader :stream #:nodoc:
37
-
38
- # Set the stream where the report will be written to. STDOUT by default.
39
- def initialize(stream=STDOUT)
40
- @stream = stream
41
- end
42
-
43
33
  on :enter do |report, context|
44
34
  report.puts context.description unless context.tests.empty?
45
35
  end
@@ -7,16 +7,6 @@ module Protest
7
7
  # files and line numbers, and after that a list of all failures and errors,
8
8
  # which also contains the first 3 lines of the backtrace for each.
9
9
  class Reports::Progress < Report
10
- include Utils::Summaries
11
- include Utils::ColorfulOutput
12
-
13
- attr_reader :stream #:nodoc:
14
-
15
- # Set the stream where the report will be written to. STDOUT by default.
16
- def initialize(stream=STDOUT)
17
- @stream = stream
18
- end
19
-
20
10
  on :end do |report|
21
11
  report.puts
22
12
  report.puts
@@ -3,16 +3,6 @@ module Protest
3
3
  # of tests, assertions, passed tests, pending tests, failed tests and
4
4
  # errors.
5
5
  class Reports::Summary < Report
6
- include Utils::Summaries
7
- include Utils::ColorfulOutput
8
-
9
- attr_reader :stream #:nodoc:
10
-
11
- # Set the stream where the report will be written to. STDOUT by default.
12
- def initialize(stream=STDOUT)
13
- @stream = stream
14
- end
15
-
16
6
  on :end do |report|
17
7
  report.summarize_test_totals
18
8
  end