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,231 @@
1
+ module Spectrum
2
+ module Spec
3
+
4
+ # Example behavior.
5
+ #
6
+ # This is the `it` in your specs.
7
+ #
8
+ class Example
9
+
10
+ #
11
+ # Defines a specification procedure.
12
+ #
13
+ def initialize(options={}, &procedure)
14
+ @parent = options[:parent]
15
+ @label = options[:label]
16
+ @skip = options[:skip]
17
+ @tags = options[:tags] || []
18
+ @topic = options[:topic]
19
+ @file = options[:file]
20
+ @line = options[:line]
21
+
22
+ # An example without a block is one still to be written.
23
+ @procedure = procedure || proc{ raise NotImplementedError, "example not written" }
24
+ end
25
+
26
+ #
27
+ # The context to which this example belongs.
28
+ #
29
+ attr :parent
30
+
31
+ #
32
+ # Description of example.
33
+ #
34
+ attr :label
35
+
36
+ #
37
+ # List of identifying tags attached to example. These should
38
+ # be a list of Symbols, with an optional Symbol=>Object tail element.
39
+ #
40
+ attr :tags
41
+
42
+ #
43
+ # A map of Symbol => Object, which is taken from the end of `tags`.
44
+ # Unlike tags, keys allow key-value relationships, rather than just
45
+ # symbolic names.
46
+ #
47
+ def keys
48
+ Hash === tags.last ? tags.last : {}
49
+ end
50
+
51
+ #
52
+ # In RSpec data keys are called metadata.
53
+ #
54
+ alias_method :metadata, :keys
55
+
56
+ #
57
+ # Test procedure, in which test assertions should be made.
58
+ #
59
+ attr :procedure
60
+
61
+ #
62
+ # Rubytest uses `type` to describe the nature of a test object.
63
+ #
64
+ def type
65
+ 'it'
66
+ end
67
+
68
+ #
69
+ # Skip this spec?
70
+ #
71
+ def skip?
72
+ @skip
73
+ end
74
+
75
+ #
76
+ # Set +true+ to skip, or String to skip with reason.
77
+ #
78
+ def skip=(reason)
79
+ @skip = reason
80
+ end
81
+
82
+ #
83
+ # Return label string.
84
+ #
85
+ # @return [String]
86
+ #
87
+ def to_s
88
+ label.to_s
89
+ end
90
+
91
+ #
92
+ # Rubytest looks for `topic` as the desciption of the subject.
93
+ #
94
+ def topic
95
+ @topic.to_s
96
+ end
97
+
98
+ #
99
+ # Location of the example's definition.
100
+ #
101
+ def source_location
102
+ [@file, @line]
103
+ end
104
+
105
+ #
106
+ # If +match+ is a Regexp or String, match against label.
107
+ # If +match+ is a Hash, match against keys.
108
+ # If +match+ is a Symbol, match against tags.
109
+ #
110
+ def match?(match)
111
+ case match
112
+ when Regexp, String
113
+ match === label.to_s
114
+ when Hash
115
+ match.any?{ |k,m| m === keys[k] }
116
+ else
117
+ tags.include?(match.to_sym)
118
+ end
119
+ end
120
+
121
+ #
122
+ # Execute example: the before hooks, the example, and the after
123
+ # hooks, which run whether or not the example passes. Hooks of the
124
+ # contexts around the example's own apply too: before hooks from
125
+ # the outermost in, after hooks from the innermost out.
126
+ #
127
+ # Each example runs in a scope of its own, so nothing one example
128
+ # sets is seen by the next.
129
+ #
130
+ def call
131
+ scope = Scope.new(parent)
132
+ contexts = parent.lineage
133
+
134
+ contexts.each do |context|
135
+ context.hooks.run(self, :before, :each, scope)
136
+ end
137
+ begin
138
+ scope.instance_exec(&procedure)
139
+ ensure
140
+ contexts.reverse_each do |context|
141
+ context.hooks.run(self, :after, :each, scope)
142
+ end
143
+ end
144
+ end
145
+
146
+ #
147
+ # Example can be converted to a Proc object.
148
+ #
149
+ # @return [Proc]
150
+ #
151
+ def to_proc
152
+ lambda{ call }
153
+ end
154
+
155
+ # The object an example runs in. It has the helper methods, `let`
156
+ # values and `subject` of the example's context, and of the
157
+ # contexts around that one.
158
+ #
159
+ class Scope
160
+ include World
161
+
162
+ #
163
+ # @param [Context] group
164
+ #
165
+ def initialize(group)
166
+ @_group = group
167
+ @_let = {}
168
+ extend group.scope
169
+ end
170
+
171
+ #
172
+ # Mark the example as still to be done.
173
+ #
174
+ # @raise [NotImplementedError] Example is a "todo".
175
+ #
176
+ def pending(message=nil)
177
+ raise NotImplementedError.new(message || "pending")
178
+ end
179
+
180
+ #
181
+ # The subject of the example: what a `subject` block in the
182
+ # context returns, or else a new instance of the class being
183
+ # described.
184
+ #
185
+ def subject
186
+ return @_subject if defined?(@_subject)
187
+ @_subject = defined?(super) ? super : described_class.new
188
+ end
189
+
190
+ #
191
+ # The class being described, if the context names one.
192
+ #
193
+ def described_class
194
+ group = @_group
195
+ while group
196
+ return group.subject if Module === group.subject
197
+ group = group.parent
198
+ end
199
+ nil
200
+ end
201
+
202
+ private
203
+
204
+ #
205
+ # Handle implicit subject: a method the example doesn't have is
206
+ # sent to the subject, if there is one that has it.
207
+ #
208
+ def method_missing(s, *a, &b)
209
+ if __subject__? && subject.respond_to?(s)
210
+ subject.public_send(s, *a, &b)
211
+ else
212
+ super
213
+ end
214
+ end
215
+
216
+ #
217
+ def respond_to_missing?(s, include_all=false)
218
+ (__subject__? && subject.respond_to?(s)) || super
219
+ end
220
+
221
+ # Is there a subject to speak of?
222
+ def __subject__?
223
+ defined?(@_subject) || singleton_class.instance_method(:subject).owner != Scope || Class === described_class
224
+ end
225
+
226
+ end
227
+
228
+ end
229
+
230
+ end
231
+ end
@@ -0,0 +1,83 @@
1
+ module Spectrum
2
+ module Spec
3
+
4
+ # The before and after procedures of a context.
5
+ #
6
+ class Hooks
7
+
8
+ #
9
+ def initialize
10
+ @hooks_all = {:before=>[], :after=>[]}
11
+ @hooks_each = {:before=>[], :after=>[]}
12
+ end
13
+
14
+ #
15
+ def add(tense, range, *tags, &proc)
16
+ case range
17
+ when :all
18
+ raise ArgumentError, "tagged #{tense}-all advice is useless" unless tags.empty?
19
+ @hooks_all[tense] << Hook.new(tense, range, *tags, &proc)
20
+ when :each
21
+ @hooks_each[tense] << Hook.new(tense, range, *tags, &proc)
22
+ else
23
+ raise ArgumentError, "range must be :all or :each"
24
+ end
25
+ end
26
+
27
+ #
28
+ # Run the hooks. After-hooks run in the reverse of the order they
29
+ # were defined.
30
+ #
31
+ def run(target, tense, range, scope)
32
+ case range
33
+ when :all
34
+ list = hooks_all[tense]
35
+ when :each
36
+ list = hooks_each[tense].select{ |hook| hook.match?(target) }
37
+ else
38
+ raise ArgumentError, "range must be :all or :each"
39
+ end
40
+ list = list.reverse if tense == :after
41
+ list.each do |hook|
42
+ scope.instance_eval(&hook)
43
+ end
44
+ end
45
+
46
+ protected
47
+
48
+ #
49
+ attr :hooks_all
50
+
51
+ #
52
+ attr :hooks_each
53
+
54
+ # Encapsulates a hook procedure along with it's match tags.
55
+ #
56
+ class Hook
57
+ def initialize(tense, scope, *tags, &proc)
58
+ @tense = tense
59
+ @scope = scope
60
+ @tags = tags
61
+ @proc = proc
62
+ end
63
+
64
+ #
65
+ # Check for matching labels or tags for each advice.
66
+ #
67
+ def match?(it)
68
+ return true if @tags.empty?
69
+ @tags.any? do |t|
70
+ it.match?(t)
71
+ end
72
+ end
73
+
74
+ #
75
+ def to_proc
76
+ @proc
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,328 @@
1
+ module Spectrum
2
+
3
+ # A test case is a collection of tests, which can be organized into
4
+ # groups of contexts. Each test case, and each context, is a subclass
5
+ # of TestCase, and its tests run in an instance of it.
6
+ #
7
+ class TestCase
8
+ include World
9
+
10
+ class << self
11
+
12
+ # Brief description of the test case.
13
+ attr :label
14
+
15
+ # Symbol list of tags. Trailing element may be Hash
16
+ # of `symbol => object`.
17
+ attr :tags
18
+
19
+ # Code unit that is subject of test case.
20
+ attr :unit
21
+
22
+ # List of tests and sub-cases.
23
+ def tests
24
+ @tests ||= []
25
+ end
26
+
27
+ # Set up a new test case.
28
+ #
29
+ def __set__(settings={}, &block)
30
+ @label = settings[:label]
31
+ @tags = settings[:tags]
32
+ @skip = settings[:skip]
33
+
34
+ @unit = calc_unit(@label)
35
+
36
+ @tests = []
37
+
38
+ class_eval(&block)
39
+ end
40
+
41
+ #
42
+ # The unit under test, taken from the label: a class or module, or
43
+ # a method of the parent case's unit if the label names one, e.g.
44
+ # `#join`.
45
+ #
46
+ def calc_unit(label)
47
+ case label
48
+ when Module, Class
49
+ label
50
+ when /^(\.|\#|\:\:)\w+/
51
+ if Module === superclass.unit
52
+ [superclass.unit, label].join('')
53
+ else
54
+ label
55
+ end
56
+ end
57
+ end
58
+
59
+ #
60
+ # Add new test or sub-case.
61
+ #
62
+ # @param [Class<TestCase>,TestProc] test_object
63
+ # Test sub-case or procedure to add to this case.
64
+ #
65
+ def <<(test_object)
66
+ tests << test_object
67
+ end
68
+
69
+ #
70
+ # Is test case to be skipped?
71
+ #
72
+ # @return [Boolean,String]
73
+ # If +false+ or +nil+ if not skipped, otherwise
74
+ # +true+ or a string explain why to skip.
75
+ #
76
+ def skip?
77
+ @skip
78
+ end
79
+
80
+ #
81
+ # Set test case to be skipped.
82
+ #
83
+ # @param [Boolean,String] reason
84
+ # Set to +false+ or +nil+ if not skipped, otherwise
85
+ # +true+ or a string explain why to skip.
86
+ #
87
+ def skip=(reason)
88
+ @skip = reason
89
+ end
90
+
91
+ #
92
+ # Create a sub-case.
93
+ #
94
+ # @param [String] label
95
+ # The breif description of the test case.
96
+ #
97
+ # @param [Array<Symbol,Hash>] tags
98
+ # List of symbols with optional trailing `symbol=>object` hash.
99
+ # These can be used as a means of filtering tests.
100
+ #
101
+ def Context(label, *tags, &block)
102
+ context = Class.new(self)
103
+ context.__set__(
104
+ :skip => @_skip,
105
+ :label => label,
106
+ :tags => tags,
107
+ &block
108
+ )
109
+
110
+ self << context unless equal?(Spectrum::TestCase)
111
+
112
+ context
113
+ end
114
+
115
+ alias :context :Context
116
+
117
+ #
118
+ # Create a test, or a parameterized test.
119
+ #
120
+ # A test whose block takes arguments is parameterized: each `ok`
121
+ # that follows makes a test of it with the arguments given.
122
+ #
123
+ # A test without a block is one still to be written.
124
+ #
125
+ # @param [String] label
126
+ # The breif description of the test case.
127
+ #
128
+ # @param [Array<Symbol,Hash>] tags
129
+ # List of symbols with optional trailing `symbol=>object` hash.
130
+ # These can be used as a means of filtering tests.
131
+ #
132
+ def Test(label=nil, *tags, &procedure)
133
+ location = caller_locations(1,1).first
134
+
135
+ settings = {
136
+ :context => self,
137
+ :skip => @_skip,
138
+ :label => label,
139
+ :tags => tags,
140
+ :file => location.path,
141
+ :line => location.lineno
142
+ }
143
+
144
+ procedure ||= proc{ raise NotImplementedError, "test not written" }
145
+
146
+ if procedure.arity == 0
147
+ test = TestProc.new(settings, &procedure)
148
+
149
+ self << test
150
+
151
+ @_test = nil
152
+
153
+ return test
154
+ else
155
+ @_test = [settings, procedure]
156
+ end
157
+ end
158
+
159
+ alias :test :Test
160
+
161
+ #
162
+ # Actualize a parameterized test.
163
+ #
164
+ def Ok(*args)
165
+ raise ArgumentError, "no parameterized test to apply `ok` to" unless @_test
166
+
167
+ settings, procedure = *@_test
168
+
169
+ location = caller_locations(1,1).first
170
+ settings = settings.merge(
171
+ :label => "#{settings[:label]} (#{args.map{ |a| a.inspect }.join(', ')})",
172
+ :file => location.path,
173
+ :line => location.lineno
174
+ )
175
+
176
+ test = TestProc.new(settings) do
177
+ instance_exec(*args, &procedure)
178
+ end
179
+
180
+ self << test
181
+
182
+ return test
183
+ end
184
+
185
+ alias :ok :Ok
186
+
187
+ #
188
+ # Setup is used to set things up for each unit test.
189
+ # The setup procedure is run before each unit.
190
+ #
191
+ # @param [String] label
192
+ # A brief description of what the setup procedure sets-up.
193
+ #
194
+ def Setup(label=nil, &proc)
195
+ define_method(:setup, &proc)
196
+ # if the setup is reset, then so should the teardown
197
+ define_method(:teardown){}
198
+ end
199
+
200
+ alias :setup :Setup
201
+
202
+ #
203
+ # Teardown procedure is used to clean-up after each unit test.
204
+ #
205
+ def Teardown(&proc)
206
+ define_method(:teardown, &proc)
207
+ end
208
+
209
+ alias :teardown :Teardown
210
+
211
+ #
212
+ # Mark tests or sub-cases to be skipped. If block is given, then
213
+ # tests defined within the block are skipped. Without a block
214
+ # all subsquent tests defined in a context will be skipped.
215
+ #
216
+ # @param [Boolean,String] reason
217
+ # Set to +false+ or +nil+ if not skipped, otherwise
218
+ # +true+ or a string explain why to skip.
219
+ #
220
+ # @example
221
+ # skip("awaiting new feature") do
222
+ # test "some test" do
223
+ # ...
224
+ # end
225
+ # end
226
+ #
227
+ # @example
228
+ # skip("not on jruby") if jruby?
229
+ # test "some test" do
230
+ # ...
231
+ # end
232
+ #
233
+ def Skip(reason=true, &block)
234
+ if block
235
+ previous, @_skip = @_skip, reason
236
+ begin
237
+ block.call
238
+ ensure
239
+ @_skip = previous
240
+ end
241
+ else
242
+ @_skip = reason
243
+ end
244
+ end
245
+
246
+ alias :skip :Skip
247
+
248
+ #
249
+ # Test case label.
250
+ #
251
+ # @return [String]
252
+ #
253
+ def to_s
254
+ label ? label.to_s : super
255
+ end
256
+
257
+ alias :inspect :to_s
258
+
259
+ end
260
+
261
+ #
262
+ # Iterate over each test and sub-case.
263
+ #
264
+ def each
265
+ self.class.tests.each do |test_object|
266
+ case test_object
267
+ when Class #TestCase
268
+ yield(test_object.new)
269
+ when TestProc
270
+ yield(test_object.for(self.class.new))
271
+ end
272
+ end
273
+ end
274
+
275
+ #
276
+ # Number of tests and sub-cases.
277
+ #
278
+ # @return [Integer] size
279
+ #
280
+ def size
281
+ self.class.tests.size
282
+ end
283
+
284
+ #
285
+ # Is the test case to be skipped?
286
+ #
287
+ def skip?
288
+ self.class.skip?
289
+ end
290
+
291
+ #
292
+ # Rubytest uses `type` to describe the nature of a test object.
293
+ #
294
+ def type
295
+ 'TestCase'
296
+ end
297
+
298
+ #
299
+ # The unit under test, if the test case names one.
300
+ #
301
+ def unit
302
+ self.class.unit
303
+ end
304
+
305
+ #
306
+ # Test case label.
307
+ #
308
+ # @return [String]
309
+ #
310
+ def to_s
311
+ self.class.label.to_s
312
+ end
313
+
314
+ #
315
+ # Dummy method for setup.
316
+ #
317
+ def setup
318
+ end
319
+
320
+ #
321
+ # Dummy method for teardown.
322
+ #
323
+ def teardown
324
+ end
325
+
326
+ end
327
+
328
+ end