nutrasuite 0.2.1 → 0.2.2

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.
@@ -1,5 +1,25 @@
1
+ # Public: Namespace module for all Nutrasuite functionality.
1
2
  module Nutrasuite
3
+ # Public: ContextHelpers contains all of the Nutrasuite Context methods. This
4
+ # will be included in Test::Unit::TestCase by default.
2
5
  module ContextHelpers
6
+
7
+ # Public: These methods will each define a context and push it onto the
8
+ # context stack for the duration of the passed in block.
9
+ #
10
+ # Signature
11
+ # <article> "name of the context"
12
+ #
13
+ # Examples
14
+ #
15
+ # a "User with a bad hair day" do
16
+ # ...tests...
17
+ # end
18
+ #
19
+ # the "SingletonObject" do
20
+ # ...tests...
21
+ # end
22
+ #
3
23
  ["a", "an", "and", "that", "the"].each do |article|
4
24
  eval <<-HERE
5
25
  def #{article}(name, &block)
@@ -9,7 +29,21 @@ module Nutrasuite
9
29
  HERE
10
30
  end
11
31
 
12
- # Code to be run before the context
32
+ # Public: use before to declare steps that need to be run before every test
33
+ # in a context.
34
+ def before(&block)
35
+ if Context.current_context?
36
+ Context.current_context.setups << block
37
+ else
38
+ warn "Not in a context"
39
+ end
40
+ end
41
+
42
+ # Deprecated: As much as I'd like to use setup and teardown, they screw up
43
+ # at times with ActiveSupport test cases. Better to use before and after.
44
+ #
45
+ # This method will be removed by 0.3.0. Please adjust your tests
46
+ # accordingly.
13
47
  def setup(&block)
14
48
  if Context.current_context?
15
49
  Context.current_context.setups << block
@@ -18,7 +52,21 @@ module Nutrasuite
18
52
  end
19
53
  end
20
54
 
21
- # Code to be run when the context is finished
55
+ # Public: use after to declare steps that need to be run after every test in
56
+ # a context.
57
+ def after(&block)
58
+ if Context.current_context?
59
+ Context.current_context.teardowns << block
60
+ else
61
+ warn "Not in a context"
62
+ end
63
+ end
64
+
65
+ # Deprecated: As mich as I'd like to use setup and teardown, they screw up
66
+ # at times with ActiveSupport test cases. Better to use before and after.
67
+ #
68
+ # This method will be removed by 0.3.0. Please adjust your tests
69
+ # accordingly.
22
70
  def teardown(&block)
23
71
  if Context.current_context?
24
72
  Context.current_context.teardowns << block
@@ -27,16 +75,50 @@ module Nutrasuite
27
75
  end
28
76
  end
29
77
 
30
- # Defines an actual test based on the given context
78
+ # Public: defines a test to be executed. Will run any setup blocks on the
79
+ # context stack before execution, then execute the specified test, then will
80
+ # run any teardown blocks on the context stack after the test has executed.
81
+ #
82
+ # name - The name of the test, used for human-readable test identification.
83
+ # &block - the body of the test, can use any and all assertions that would
84
+ # otherwise be available to a MiniTest test.
85
+ #
86
+ # Examples
87
+ #
88
+ # it "tests that true is truthy" do
89
+ # assert true
90
+ # end
91
+ #
31
92
  def it(name, &block)
32
93
  build_test(name, &block)
33
94
  end
34
95
 
35
- # Defines a test based on the given context that will be skipped for now
96
+ # Public: defines a test method that should be skipped. Context
97
+ # setup/teardown will still be executed, but the actual test method will
98
+ # show up as a MiniTest skip with a description of "not yet implemented."
99
+ #
100
+ # This method exists to make it easy to switch a test between a pending and
101
+ # an active state: just switch the method name from "it" to "it eventually."
102
+ #
103
+ # name - The name of the test, used for human-readable test identification.
104
+ # &block - the body of the test. Doesn't have to be functional ruby as the
105
+ # block will be skipped in this method.
106
+ #
107
+ # Examples
108
+ #
109
+ # it_eventually "has some really smart logic" do
110
+ # assert self.has_smart_logic?
111
+ # end
112
+ #
36
113
  def it_eventually(name, &block)
37
114
  build_test("eventually #{name}", :skip => true, &block)
38
115
  end
39
116
 
117
+ # Internal: This method actually builds out the test method that MiniTest
118
+ # knows how to execute. It's responsible for running the current list of
119
+ # setups and teardowns and relies on 'define_method' to set up a test method
120
+ # that MiniTest can parse and execute.
121
+ #
40
122
  def build_test(name, options = {}, &block)
41
123
  test_name = Context.build_test_name(name)
42
124
 
@@ -61,16 +143,35 @@ module Nutrasuite
61
143
  end
62
144
  end
63
145
 
146
+ # Internal: warn simply outputs a warning message for the user if they
147
+ # appear to be using Nutrasuite incorrectly.
148
+ #
64
149
  def warn(message)
65
150
  puts " * Warning: #{message}"
66
151
  end
67
152
 
68
153
  end
69
154
 
155
+ # Internal: The Context class represents each context that can go on the
156
+ # context stack. Each context is responsible for tracking its specific
157
+ # information (mostly its name and its setup and teardown methods). Contexts
158
+ # should only be created using one of the article methods in ContextHelpers.
70
159
  class Context
71
160
 
161
+ # Internal: Get the name, setup methods, and teardown methods for this
162
+ # Context.
72
163
  attr_reader :name, :setups, :teardowns
73
164
 
165
+ # Internal: Create a new Context object.
166
+ #
167
+ # name - The name of the context, which will be prepended to any nested
168
+ # contexts/tests in the final test name
169
+ # &block - The block that defines the contents of the context; should
170
+ # consist of:
171
+ # - test definitions
172
+ # - sub-context definitions
173
+ # - setup and teardown declarations
174
+ #
74
175
  def initialize(name, &block)
75
176
  @name = name
76
177
  @block = block
@@ -79,10 +180,25 @@ module Nutrasuite
79
180
  @teardowns = []
80
181
  end
81
182
 
183
+ # Internal: build runs the block that defines the context's contents, thus
184
+ # setting up any nested contexts and tests.
185
+ #
82
186
  def build
83
187
  @block.call
84
188
  end
85
189
 
190
+ # Internal: builds a test name based on the current state of the context
191
+ # stack.
192
+ #
193
+ # Examples
194
+ #
195
+ # # Assuming the context stack looks like "a user","that is an admin":
196
+ # Context.build_test_name("has admin privileges")
197
+ # #=> "test a user that is an admin has admin privileges"
198
+ #
199
+ # Returns a string name for a method that will automatically be executed by
200
+ # MiniTest.
201
+ #
86
202
  def self.build_test_name(name="")
87
203
  full_name = "test "
88
204
  context_stack.each do |context|
@@ -91,6 +207,13 @@ module Nutrasuite
91
207
  full_name << name
92
208
  end
93
209
 
210
+ # Internal: pushes a new context onto the stack, builds that context out,
211
+ # and then removes it from the stack.
212
+ #
213
+ # This method should be the only means by which contexts get built, as it
214
+ # ensures that the context is removed from the context stack when it's done
215
+ # building itself.
216
+ #
94
217
  def self.push(name, &block)
95
218
  context = Context.new(name, &block)
96
219
  context_stack.push(context)
@@ -100,21 +223,36 @@ module Nutrasuite
100
223
  context_stack.pop
101
224
  end
102
225
 
226
+ # Internal: get the context stack, or initialize it to an empty list if
227
+ # necessary.
228
+ #
229
+ # Returns: an Array representing the context stack.
103
230
  def self.context_stack
104
231
  @context_stack ||= []
105
232
  end
106
233
 
234
+ # Internal: get the current context.
235
+ #
236
+ # Returns: the context currently at the top of the stack, or nil if there
237
+ # are no contexts in the stack at the moment.
107
238
  def self.current_context
108
239
  context_stack.last
109
240
  end
110
241
 
242
+ # Internal: determine whether there is currently a context active.
243
+ #
244
+ # Returns: true if there is at least one context on the stack, false
245
+ # otherwise.
111
246
  def self.current_context?
112
247
  !context_stack.empty?
113
248
  end
114
249
  end
115
250
  end
116
251
 
117
- # backwards compatibility with some slightly older versions of minitest
252
+ # In slightly older versions of MiniTest the TestCase#current method is not
253
+ # defined, but Nutrasuite definitely needs it in order to be useful. This bit of
254
+ # monkey-patching hackery ensures that this method is present even if you're
255
+ # using an older version of the library.
118
256
  unless defined? MiniTest::Unit::TestCase.current
119
257
  class MiniTest::Unit::TestCase
120
258
  def initialize name
@@ -1,3 +1,3 @@
1
1
  module Nutrasuite
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -15,11 +15,11 @@ class ContextsTest < Test::Unit::TestCase
15
15
  end
16
16
 
17
17
  a "Context" do
18
- setup do
18
+ before do
19
19
  @a = 5
20
20
  end
21
21
 
22
- it "runs setups before tests" do
22
+ it "runs befores before tests" do
23
23
  assert_equal @a, 5
24
24
  end
25
25
 
@@ -28,33 +28,44 @@ class ContextsTest < Test::Unit::TestCase
28
28
  end
29
29
 
30
30
  that "has a nested Context" do
31
- setup do
31
+ before do
32
32
  @b = 10
33
33
  end
34
34
 
35
- it "runs both setups" do
35
+ it "runs both befores" do
36
36
  assert_equal @a, 5
37
37
  assert_equal @b, 10
38
38
  end
39
39
  end
40
40
 
41
- that "has a nested Context with multiple setups" do
42
- setup do
41
+ that "has a nested Context with multiple befores" do
42
+ before do
43
43
  @b = 6
44
44
  end
45
45
 
46
- setup do
46
+ before do
47
47
  @c = 7
48
48
  @b = @b + 1
49
49
  end
50
50
 
51
- it "runs all setups in order" do
51
+ it "runs all befores in order" do
52
52
  assert_equal @b, 7
53
53
  assert_equal @b, @c
54
54
  end
55
55
  end
56
56
  end
57
57
 
58
+ a "Second context" do
59
+ before do
60
+ @b = true
61
+ end
62
+
63
+ it "isn't affected by befores from other contexts" do
64
+ assert @b
65
+ assert @a.nil?
66
+ end
67
+ end
68
+
58
69
  the "'the' context" do
59
70
  it "works" do
60
71
  assert true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nutrasuite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-26 00:00:00.000000000 Z
13
+ date: 2012-03-04 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Nutrasuite is a low-calorie syntax sweetener for minitest
16
16
  email: