Module: Whitestone::Assertion::Guards
Overview
Various methods to guard against invalid assertions. All of these raise AssertionSpecificationError if there is a problem.
Instance Method Summary collapse
-
#args_or_block_one_only(args, block) ⇒ Object
Return a lambda that can be run.
- #block_required(block) ⇒ Object
- #no_block_allowed ⇒ Object
- #one_argument(array) ⇒ Object
- #two_arguments(array) ⇒ Object
- #two_or_three_arguments(array) ⇒ Object
- #type_check(args, types) ⇒ Object
Instance Method Details
#args_or_block_one_only(args, block) ⇒ Object
Return a lambda that can be run. If the user specified a block, it’s that. If not, it’s the first argument. If both or neither, it’s an error. If there’s more arguments than necessary, it’s an error.
37 38 39 40 41 42 43 44 45 |
# File 'lib/whitestone/assertion_classes.rb', line 37 def args_or_block_one_only(args, block) if block and args.empty? block elsif !block and args.size == 1 lambda { args.first } else raise AssertionSpecificationError, "Improper arguments to T" end end |
#block_required(block) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/whitestone/assertion_classes.rb', line 74 def block_required(block) unless block raise AssertionSpecificationError, "The method requires a block" end block end |
#no_block_allowed ⇒ Object
68 69 70 71 72 |
# File 'lib/whitestone/assertion_classes.rb', line 68 def no_block_allowed if @block raise AssertionSpecificationError, "This method doesn't take a block" end end |
#one_argument(array) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/whitestone/assertion_classes.rb', line 47 def one_argument(array) unless array.size == 1 raise AssertionSpecificationError, "Exactly one argument required" end array.first end |
#two_arguments(array) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/whitestone/assertion_classes.rb', line 54 def two_arguments(array) unless array.size == 2 raise AssertionSpecificationError, "Exactly two arguments required" end array end |
#two_or_three_arguments(array) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/whitestone/assertion_classes.rb', line 61 def two_or_three_arguments(array) unless array.size == 2 or array.size == 3 raise AssertionSpecificationError, "Exactly two or three arguments required" end array end |
#type_check(args, types) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/whitestone/assertion_classes.rb', line 81 def type_check(args, types) if Class === types types = args.map { types } end if types.size != args.size raise AssertionSpecificationError, "Incorrect number of types provided" end args.zip(types).each do |arg, type| unless arg.is_a? type msg = "Argument error: expected #{type}; "\ "got #{arg.inspect} (#{arg.class})" raise AssertionSpecificationError, msg end end end |