spectrum 0.0.1 → 0.2.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.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/HISTORY.md +56 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +255 -0
  5. data/lib/spectrum/describe.rb +55 -0
  6. data/lib/spectrum/feature/advice.rb +31 -0
  7. data/lib/spectrum/feature/feature.rb +192 -0
  8. data/lib/spectrum/feature/featurette.rb +68 -0
  9. data/lib/spectrum/feature/scenario.rb +203 -0
  10. data/lib/spectrum/feature/step.rb +50 -0
  11. data/lib/spectrum/feature.rb +45 -0
  12. data/lib/spectrum/spec/context.rb +409 -0
  13. data/lib/spectrum/spec/example.rb +231 -0
  14. data/lib/spectrum/spec/hooks.rb +83 -0
  15. data/lib/spectrum/testcase/test_case.rb +328 -0
  16. data/lib/spectrum/testcase/test_proc.rb +143 -0
  17. data/lib/spectrum/testcase.rb +28 -0
  18. data/lib/spectrum/version.rb +3 -0
  19. data/lib/spectrum/world.rb +15 -0
  20. data/lib/spectrum.rb +14 -138
  21. metadata +71 -72
  22. data/History.txt +0 -93
  23. data/Install.txt +0 -18
  24. data/Licence.txt +0 -27
  25. data/README.txt +0 -35
  26. data/lib/spectrum/cmyk.rb +0 -281
  27. data/lib/spectrum/css.rb +0 -28
  28. data/lib/spectrum/grayscale.rb +0 -212
  29. data/lib/spectrum/hsl.rb +0 -221
  30. data/lib/spectrum/palette/adobecolor.rb +0 -272
  31. data/lib/spectrum/palette/gimp.rb +0 -116
  32. data/lib/spectrum/palette/monocontrast.rb +0 -180
  33. data/lib/spectrum/palette.rb +0 -16
  34. data/lib/spectrum/rgb/metallic.rb +0 -43
  35. data/lib/spectrum/rgb-colors.rb +0 -355
  36. data/lib/spectrum/rgb.rb +0 -453
  37. data/lib/spectrum/yiq.rb +0 -84
  38. data/test/test_adobecolor.rb +0 -419
  39. data/test/test_all.rb +0 -23
  40. data/test/test_cmyk.rb +0 -128
  41. data/test/test_color.rb +0 -141
  42. data/test/test_css.rb +0 -29
  43. data/test/test_gimp.rb +0 -101
  44. data/test/test_grayscale.rb +0 -121
  45. data/test/test_hsl.rb +0 -153
  46. data/test/test_monocontrast.rb +0 -142
  47. data/test/test_rgb.rb +0 -344
  48. data/test/test_yiq.rb +0 -73
@@ -0,0 +1,203 @@
1
+ module Spectrum
2
+ class Feature
3
+
4
+ # A scenario is a list of steps, run in order in a scope of their
5
+ # own, so one scenario's state is not seen by the next.
6
+ #
7
+ class Scenario
8
+
9
+ #
10
+ def initialize(feature, settings={}, &block)
11
+ @feature = feature
12
+ @label = settings[:label]
13
+ @skip = settings[:skip]
14
+ @steps = []
15
+
16
+ Builder.new(self, &block)
17
+ end
18
+
19
+ # Parent feature.
20
+ attr :feature
21
+
22
+ # Description of scenario.
23
+ attr :label
24
+
25
+ # List scenario steps.
26
+ attr :steps
27
+
28
+ # The object the scenario's step definitions run in. It has the
29
+ # helper methods of the feature and of Spectrum::World.
30
+ def scope
31
+ @scope ||= (
32
+ scope = Object.new
33
+ scope.extend(World)
34
+ scope.extend(feature.scope)
35
+ scope
36
+ )
37
+ end
38
+
39
+ # Skip this scenario from runs.
40
+ #
41
+ # @return [Boolean,String] reason for skipping
42
+ def skip?
43
+ @skip
44
+ end
45
+
46
+ # Scenario steps must be run in order.
47
+ def ordered?
48
+ true
49
+ end
50
+
51
+ # Iterate over steps.
52
+ def each(&block)
53
+ @steps.each(&block)
54
+ end
55
+
56
+ # Number of steps.
57
+ def size
58
+ @steps.size
59
+ end
60
+
61
+ # The type is "Scenario". This is used by Rubytest in test output.
62
+ def type
63
+ "Scenario"
64
+ end
65
+
66
+ # Provided the scenario label.
67
+ def to_s
68
+ @label.to_s
69
+ end
70
+
71
+ #
72
+ # Start from a clean scope, then yield to the steps.
73
+ #
74
+ def call
75
+ @scope = nil
76
+ @blocked = false
77
+ yield
78
+ end
79
+
80
+ #
81
+ # Run a step in the context of this scenario, by the first step
82
+ # definition of its type whose description matches.
83
+ #
84
+ # A step without a definition is one still to be written. Once a
85
+ # step has failed, or wasn't defined, those after it are not run,
86
+ # since they depend on it; they are reported as still to be done.
87
+ #
88
+ def run(step)
89
+ if @blocked
90
+ raise NotImplementedError, "not run, because an earlier step did not pass"
91
+ end
92
+
93
+ procedure, match = definition(step)
94
+
95
+ unless procedure
96
+ @blocked = true
97
+ raise NotImplementedError, "no definition for step: #{step}"
98
+ end
99
+
100
+ begin
101
+ scope.instance_exec(*match[1..-1], &procedure)
102
+ rescue Exception
103
+ @blocked = true
104
+ raise
105
+ end
106
+ end
107
+
108
+ #
109
+ # Find the definition of a step.
110
+ #
111
+ # @return [Array] the procedure and the MatchData, or nil if there
112
+ # is none
113
+ #
114
+ def definition(step)
115
+ feature.advice[step.type].each do |mask, procedure|
116
+ if match = match_regexp(mask).match(step.label)
117
+ return procedure, match
118
+ end
119
+ end
120
+ nil
121
+ end
122
+
123
+ #
124
+ # Convert a description into a regular expression. Text within
125
+ # parentheses, e.g. `(\d+)`, is kept as regular expression, and
126
+ # what it matches is passed to the step's procedure. The rest is
127
+ # matched literally, save that case and the amount of whitespace
128
+ # don't matter. The whole of the step's description has to match.
129
+ #
130
+ # To use a regular expression, but leave the resulting match out of
131
+ # the arguments use `?:`, e.g. `(?:\d+)`.
132
+ #
133
+ def match_regexp(str)
134
+ return str if Regexp === str
135
+ str = str.split(/(\(.*?\))(?!\))/).map{ |x|
136
+ x =~ /\A\((.*)\)\Z/ ? "(#{$1})" : Regexp.escape(x)
137
+ }.join
138
+ str = str.gsub(/(\\\s)+/, '\s+')
139
+ Regexp.new('\A' + str + '\z', Regexp::IGNORECASE)
140
+ end
141
+
142
+ # Evaluates a scenario's block, collecting its steps.
143
+ #
144
+ class Builder
145
+
146
+ #
147
+ def initialize(scenario, &block)
148
+ @scenario = scenario
149
+ instance_eval(&block) if block
150
+ end
151
+
152
+ # Given ...
153
+ #
154
+ # @param [String] label
155
+ # A matching description of the _given_ procedure.
156
+ #
157
+ def Given(label)
158
+ step(:given, label, caller_locations(1,1).first)
159
+ end
160
+
161
+ alias :given :Given
162
+
163
+ # When ...
164
+ #
165
+ # @param [String] label
166
+ # A matching description of the _when_ procedure.
167
+ #
168
+ def When(label)
169
+ step(:when, label, caller_locations(1,1).first)
170
+ end
171
+
172
+ alias :wence :When
173
+
174
+ # Then ...
175
+ #
176
+ # @param [String] label
177
+ # A matching description of the _then_ procedure.
178
+ #
179
+ def Then(label)
180
+ step(:then, label, caller_locations(1,1).first)
181
+ end
182
+
183
+ alias :hence :Then
184
+
185
+ # And ..., a further step of the same type as the one before.
186
+ #
187
+ def And(label)
188
+ type = @scenario.steps.empty? ? :given : @scenario.steps.last.type
189
+ step(type, label, caller_locations(1,1).first)
190
+ end
191
+
192
+ private
193
+
194
+ def step(type, label, location)
195
+ @scenario.steps << Step.new(@scenario, label, :type=>type, :file=>location.path, :line=>location.lineno)
196
+ end
197
+
198
+ end
199
+
200
+ end
201
+
202
+ end
203
+ end
@@ -0,0 +1,50 @@
1
+ module Spectrum
2
+ class Feature
3
+
4
+ # A step of a scenario: one `Given`, `When` or `Then` line. To
5
+ # Rubytest each step is a test.
6
+ #
7
+ class Step
8
+
9
+ #
10
+ def initialize(scenario, label, settings={})
11
+ @scenario = scenario
12
+ @label = label
13
+ @type = settings[:type]
14
+ @file = settings[:file]
15
+ @line = settings[:line]
16
+ end
17
+
18
+ # The type of step (:given, :when or :then).
19
+ attr :type
20
+
21
+ # The scenario to which this step belongs.
22
+ attr :scenario
23
+
24
+ # Label of test.
25
+ attr :label
26
+
27
+ #
28
+ def to_s
29
+ "#{type.to_s.capitalize} #{label}"
30
+ end
31
+
32
+ # Location of the step in its scenario.
33
+ def source_location
34
+ [@file, @line]
35
+ end
36
+
37
+ #
38
+ def to_proc
39
+ lambda{ call }
40
+ end
41
+
42
+ #
43
+ def call
44
+ scenario.run(self)
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,45 @@
1
+ require_relative '../spectrum'
2
+ require_relative 'feature/advice'
3
+ require_relative 'feature/step'
4
+ require_relative 'feature/scenario'
5
+ require_relative 'feature/featurette'
6
+ require_relative 'feature/feature'
7
+
8
+ module Spectrum
9
+
10
+ class Feature
11
+
12
+ # The words the feature structure adds to the toplevel.
13
+ #
14
+ module DSL
15
+ #
16
+ # Define a feature.
17
+ #
18
+ def Feature(label, &block)
19
+ location = caller_locations(1,1).first
20
+ require_featurettes(File.dirname(location.absolute_path || location.path))
21
+ feature = Spectrum::Feature.new(:label=>label, &block)
22
+ $TEST_SUITE << feature
23
+ feature
24
+ end
25
+
26
+ alias :feature :Feature
27
+
28
+ #
29
+ # Require any featurettes located in `{feature_dir}/featurettes` directory.
30
+ #
31
+ def require_featurettes(feature_dir)
32
+ featurettes_dir = File.join(feature_dir, 'featurettes')
33
+ if File.directory?(featurettes_dir)
34
+ Dir[File.join(featurettes_dir, '*.rb')].sort.each do |file|
35
+ require file
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ extend Spectrum::Feature::DSL
@@ -0,0 +1,409 @@
1
+ module Spectrum
2
+ module Spec
3
+
4
+ # This is the BDD form of a test case. It encapsulates a collection
5
+ # of examples.
6
+ #
7
+ # This is the `describe` in your specs.
8
+ #
9
+ class Context
10
+
11
+ #
12
+ # The parent context in which this describe resides.
13
+ #
14
+ attr :parent
15
+
16
+ #
17
+ # A description of the describe clause.
18
+ #
19
+ attr :label
20
+
21
+ #
22
+ # A target class, if any.
23
+ #
24
+ attr :subject
25
+
26
+ #
27
+ # Array and/or metadata Hash of tags.
28
+ #
29
+ attr :tags
30
+
31
+ #
32
+ # List of examples and sub-specifications.
33
+ #
34
+ attr :specs
35
+
36
+ #
37
+ # The before and after hooks.
38
+ #
39
+ attr :hooks
40
+
41
+ #
42
+ # Skip critera.
43
+ #
44
+ # @return [Array<Array>] pairs of reason and Proc
45
+ #
46
+ attr :skips
47
+
48
+ #
49
+ # Omit criteria.
50
+ #
51
+ # @return [Array<Array>] pairs of reason and Proc
52
+ #
53
+ attr :omits
54
+
55
+ #
56
+ # DSL module for evaluating `describe` blocks.
57
+ #
58
+ attr :scope
59
+
60
+ #
61
+ # A context's subject, if it has one, is a class or module.
62
+ #
63
+ def initialize(settings={}, &block)
64
+ @parent = settings[:parent]
65
+ @subject = settings[:subject]
66
+ @label = settings[:label]
67
+ @tags = settings[:tags] || []
68
+ @skip = settings[:skip]
69
+
70
+ @hooks = Hooks.new
71
+ @skips = []
72
+ @omits = []
73
+
74
+ @specs = []
75
+
76
+ @scope = Scope.new(self)
77
+
78
+ evaluate(&block)
79
+ end
80
+
81
+ #
82
+ # Evalute a block of code in the context of the Context's scope.
83
+ # When finished it applies `omits` and `skips` to the examples,
84
+ # removing them and marking them to be skipped respectively.
85
+ #
86
+ def evaluate(&block)
87
+ @scope.module_eval(&block) if block
88
+ apply(omits, skips)
89
+ end
90
+
91
+ #
92
+ # Apply omit and skip criteria to the examples of this context and
93
+ # of the contexts within it.
94
+ #
95
+ def apply(omits, skips)
96
+ specs.delete_if do |spec|
97
+ Example === spec && omits.any?{ |reason, test| test.call(spec) }
98
+ end
99
+
100
+ specs.each do |spec|
101
+ if Example === spec
102
+ skips.each do |reason, test|
103
+ spec.skip = reason if test.call(spec)
104
+ end
105
+ else
106
+ spec.apply(omits, skips)
107
+ end
108
+ end
109
+ end
110
+
111
+ #
112
+ # This context and those around it, outermost first.
113
+ #
114
+ def lineage
115
+ parent ? parent.lineage + [self] : [self]
116
+ end
117
+
118
+ #
119
+ # The scope that before-all and after-all hooks run in.
120
+ #
121
+ def it_scope
122
+ @it_scope ||= Example::Scope.new(self)
123
+ end
124
+
125
+ #
126
+ # Add `it` or sub-`describe` to group.
127
+ #
128
+ def <<(spec)
129
+ @specs << spec
130
+ end
131
+
132
+ #
133
+ # Iterate over each test and subcase.
134
+ #
135
+ def each(&block)
136
+ specs.each(&block)
137
+ end
138
+
139
+ #
140
+ # Run before-all and after-all advice around yielding to the
141
+ # examples.
142
+ #
143
+ def call
144
+ hooks.run(self, :before, :all, it_scope)
145
+ begin
146
+ yield
147
+ ensure
148
+ hooks.run(self, :after, :all, it_scope)
149
+ end
150
+ end
151
+
152
+ #
153
+ # Number of specs plus subcases.
154
+ #
155
+ def size
156
+ specs.size
157
+ end
158
+
159
+ #
160
+ # Rubytest uses `type` to describe the nature of a test object.
161
+ #
162
+ def type
163
+ 'describe'
164
+ end
165
+
166
+ #
167
+ # Returns the subject/label as string.
168
+ #
169
+ def to_s
170
+ label.to_s
171
+ end
172
+
173
+ alias :inspect :to_s
174
+
175
+ #
176
+ # Skip this group?
177
+ #
178
+ def skip?
179
+ @skip
180
+ end
181
+
182
+ #
183
+ #
184
+ #
185
+ def skip=(reason)
186
+ @skip = reason
187
+ end
188
+
189
+ # The module a `describe` block is evaluated in. Its instance
190
+ # methods, such as helpers and `let` values, are for the examples;
191
+ # the methods below are the words of the specification.
192
+ #
193
+ class Scope < Module
194
+
195
+ #
196
+ # Setup new evaluation scope.
197
+ #
198
+ def initialize(group)
199
+ super()
200
+
201
+ @_group = group
202
+ @_hooks = group.hooks
203
+ @_skips = group.skips
204
+ @_omits = group.omits
205
+
206
+ if group.parent
207
+ include(group.parent.scope)
208
+ end
209
+ end
210
+
211
+ #
212
+ # Create a new sub-specification.
213
+ #
214
+ def describe(topic, *tags, &block)
215
+ settings = {}
216
+ settings[:parent] = @_group
217
+ settings[:subject] = @_group.subject
218
+ settings[:tags] = tags
219
+
220
+ if Module === topic
221
+ settings[:subject] = topic
222
+ settings[:label] = topic.name
223
+ else
224
+ settings[:label] = topic
225
+ end
226
+
227
+ group = Context.new(settings, &block)
228
+
229
+ @_group.specs << group
230
+
231
+ return group
232
+ end
233
+
234
+ alias_method :context, :describe
235
+
236
+ #
237
+ # Create an example behavior. Without a block, the example is
238
+ # one still to be written.
239
+ #
240
+ # If tags or keys are given, so must a label.
241
+ #
242
+ def it(label=nil, *tags, &procedure)
243
+ location = caller_locations(1,1).first
244
+
245
+ settings = {
246
+ :parent => @_group,
247
+ :topic => @_topic,
248
+ :label => label,
249
+ :tags => tags,
250
+ :file => location.path,
251
+ :line => location.lineno
252
+ }
253
+
254
+ spec = Example.new(settings, &procedure)
255
+
256
+ @_group.specs << spec
257
+
258
+ spec
259
+ end
260
+
261
+ alias_method :example, :it
262
+
263
+ #
264
+ # Define before procedure.
265
+ #
266
+ # @example
267
+ # describe '#puts' do
268
+ # it "gives standard output" do
269
+ # puts "Hello"
270
+ # end
271
+ #
272
+ # before do
273
+ # $stdout = StringIO.new
274
+ # end
275
+ #
276
+ # after do
277
+ # $stdout = STDOUT
278
+ # end
279
+ # end
280
+ #
281
+ # @param [Symbol] which
282
+ # Must be either `:all` or `:each`, the later being the default.
283
+ #
284
+ # @param [Array<Symbol>] tags
285
+ # List of match critera that must be matched
286
+ # against tags to trigger the before procedure.
287
+ #
288
+ def before(which=:each, *tags, &procedure)
289
+ @_hooks.add(:before, which, *tags, &procedure)
290
+ end
291
+
292
+ #
293
+ # Define an after procedure. See #before.
294
+ #
295
+ # @param [Symbol] which
296
+ # Must be either `:all` or `:each`, the later being the default.
297
+ #
298
+ # @param [Array<Symbol>] tags
299
+ # List of match critera that must be matched
300
+ # to trigger the after procedure.
301
+ #
302
+ def after(which=:each, *tags, &procedure)
303
+ @_hooks.add(:after, which, *tags, &procedure)
304
+ end
305
+
306
+ #
307
+ # Mark examples to be skipped: those matching the criteria, or
308
+ # those the block returns true for, or all of them if neither is
309
+ # given. It applies to the whole context and the contexts within
310
+ # it, wherever in the context it is written.
311
+ #
312
+ # @example
313
+ # it "does something" do
314
+ # ...
315
+ # end
316
+ #
317
+ # skip("not on jruby", /something/) if jruby?
318
+ #
319
+ def skip(reason=true, *criteria, &block)
320
+ @_skips << [reason, criterion(criteria, &block)]
321
+ end
322
+
323
+ #
324
+ # Mark examples to be omitted. Omitting an example is different
325
+ # from skipping, in that a skipped example is still sent up to
326
+ # the test harness, where as an omitted one is never added at all.
327
+ #
328
+ # @example
329
+ # it "does something" do
330
+ # ...
331
+ # end
332
+ #
333
+ # omit(/something/) if jruby?
334
+ #
335
+ def omit(*criteria, &block)
336
+ @_omits << [true, criterion(criteria, &block)]
337
+ end
338
+
339
+ #
340
+ # Define a value for the examples, computed the first time an
341
+ # example asks for it.
342
+ #
343
+ def let(name, &block)
344
+ define_method(name) do
345
+ @_let.fetch(name){ |k| @_let[k] = instance_eval(&block) }
346
+ end
347
+ end
348
+
349
+ #
350
+ # Like #let, but computed before each example whether or not it
351
+ # asks for it.
352
+ #
353
+ def let!(name, &block)
354
+ let(name, &block)
355
+ before { __send__(name) }
356
+ end
357
+
358
+ #
359
+ # Define the subject of the examples.
360
+ #
361
+ def subject(topic=nil, &block)
362
+ @_topic = topic
363
+ define_method(:subject) do
364
+ return @_subject if defined?(@_subject)
365
+ @_subject = instance_eval(&block)
366
+ end
367
+ end
368
+
369
+ #
370
+ # Subject-oriented example: the block runs in the result of
371
+ # calling the method, or chain of methods, on the subject.
372
+ #
373
+ def its(invocation, &block)
374
+ it(invocation.to_s) do
375
+ calls = invocation.to_s.split('.')
376
+ calls.inject(subject){ |s,m| s.__send__(m) }.instance_eval(&block)
377
+ end
378
+ end
379
+
380
+ #
381
+ # Bring in a set of shared examples.
382
+ #
383
+ def it_behaves_like(label)
384
+ proc = Spectrum::Spec.shared_examples[label]
385
+ raise ArgumentError, "no shared examples for #{label.inspect}" unless proc
386
+ module_eval(&proc)
387
+ end
388
+
389
+ alias_method :include_examples, :it_behaves_like
390
+
391
+ private
392
+
393
+ # A procedure that tells whether an example meets the criteria.
394
+ def criterion(criteria, &block)
395
+ if block
396
+ block
397
+ elsif criteria.empty?
398
+ lambda{ |spec| true }
399
+ else
400
+ lambda{ |spec| criteria.any?{ |c| spec.match?(c) } }
401
+ end
402
+ end
403
+
404
+ end
405
+
406
+ end
407
+
408
+ end
409
+ end