lib/riot/context_helpers.rb in riot-0.12.2 vs lib/riot/context_helpers.rb in riot-0.12.3
- old
+ new
@@ -78,28 +78,36 @@
#
# Furthermore, the pattern of testing an attribute on the topic is codified as
#
# asserts(:size).equals(2)
#
+ # Or with arguments:
+ #
+ # asserts(:foo,1,2).equals(3)
+ #
# Passing a Symbol to +asserts+ enables this behaviour. For more information on
# assertion macros, see {Riot::AssertionMacro}.
#
# @param [String, Symbol] what description of test or property to inspect on the topic
# @return [Riot::Assertion]
- def asserts(what, &definition)
- new_assertion("asserts", what, &definition)
+ def asserts(*what, &definition)
+ new_assertion("asserts", *what, &definition)
end
# Same as #asserts, except it uses the phrase "should" in the report output. Sometimes you feel like a
# nut, sometimes you don't.
#
# should("ensure expected") { "bar" }.equals("bar")
#
+ # #should also has the same shortcuts available to #asserts:
+ #
+ # should(:bar,1,2).equals(3)
+ #
# @param [String, Symbol] what description of test or property to inspect on the topic
# @return [Riot::Assertion]
- def should(what, &definition)
- new_assertion("should", what, &definition)
+ def should(*what, &definition)
+ new_assertion("should", *what, &definition)
end
# Like an assertion, but expects negative results.
#
# In the most basic form, a denial requires a descriptive name and a block.
@@ -113,39 +121,71 @@
#
# Furthermore, the pattern of testing an attribute on the topic is codified as
#
# denies(:size).equals(2)
#
+ # the shorcut can also pass additional arguments to the method like:
+ #
+ # denies(:foo,1,3).equals(2)
+ #
# Passing a Symbol to +denies+ enables this behaviour. For more information on
# assertion macros, see {Riot::AssertionMacro}.
#
# @param [String, Symbol] what description of test or property to inspect on the topic
# @return [Riot::Assertion]
- def denies(what, &definition)
- new_assertion("denies", what, true, &definition)
+ def denies(*what, &definition)
+ what << {:negative => true}
+ new_assertion "denies", *what, &definition
end
+ # This is the negative form of #should. This is exactly like denies. Just here for syntactic sugar.
+ #
+ # A basic eample is:
+ #
+ # should_not("have size equal 2") { topic.size == 2 }
+ #
+ # In addition, the #denies shortcut as available as well:
+ #
+ # should_not(:size).equals 3
+ #
+ # Or passing in arguments
+ #
+ # should_not(:foo,1,2).equals(2)
+ #
+ # @param [String,Symbol] what description or property to inspect on the topic
+ # @return [Riot::Assertion]
+ def should_not(*what, &definition)
+ what << {:negative => true}
+ new_assertion "should not", *what, &definition
+ end
+
# Makes an assertion on the topic itself, e.g.
#
# asserts_topic.matches(/^ab+/)
#
# @param [String] what description of test
# @return [Riot::Assertion]
def asserts_topic(what="that it")
asserts(what) { topic }
end
- private
- def new_assertion(scope, what, negative=false, &definition)
- if what.kind_of?(Symbol)
- definition ||= proc { topic.send(what) }
- description = "#{scope} ##{what}"
- elsif what.kind_of?(Array)
- definition ||= proc { topic.send(*what) }
- description = "#{scope} ##{what.first} with argument(s): #{what[1..-1]}"
- else
- description = "#{scope} #{what}"
- end
- (@assertions << assertion_class.new(description, negative, &definition)).last
+ # Makes a negative assertion on the topic itself, e.g.
+ #
+ # denies_topic.matches(/^ab+/)
+ #
+ # @param [String] what description of test
+ # @return [Riot::Assertion]
+ def denies_topic(what="that it")
+ denies(what) { topic }
end
+ private
+
+ def new_assertion(scope, *args, &definition)
+ options = args.extract_options!
+ definition ||= proc { topic.send(*args) }
+ description = "#{scope} #{args.first}"
+ description << " with arguments(s): #{args.slice(1, args.length)}" if args.size > 1
+ (@assertions << assertion_class.new(description, options[:negative], &definition)).last
+ end
+
end # AssertionHelpers
end # Riot