Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/api_pi/assertion.rb
Overview
Patches created to write test assertions
Instance Method Summary collapse
-
#check_if(test, msg = nil) ⇒ Object
‘check_if` is used to build assertions.
-
#has_key(kkey) ⇒ Object
The ‘has_key` assertion can be used to query if a certain field is present in a JSON block:.
-
#has_keys(*kkeys) ⇒ Object
The ‘has_keys` assertion works the same as `has_key`, except it takes multiple keys and tests them all at once:.
-
#includes(item) ⇒ Object
‘includes` can be used for array to query if specific values are present:.
-
#is(this) ⇒ Object
‘is` assertions are used to compare exact values.
-
#is_a(klass) ⇒ Object
(also: #is_an)
The ‘is_a` and `is_an` assertions are used to assert Type on a field:.
-
#lacks_key(kkey) ⇒ Object
‘lacks_key` is the opposite of `has_key`, in that it tests that a field is NOT present in a JSON block:.
-
#matches(regex) ⇒ Object
‘matches` expectes a Regex formed value.
-
#not_nil? ⇒ Boolean
‘not_nil?` checks to see if the value is nil:.
-
#stripped? ⇒ Boolean
‘stripped?` will test is there is whitespace before or after a string:.
Instance Method Details
#check_if(test, msg = nil) ⇒ Object
‘check_if` is used to build assertions. Create your own with it!
11 12 13 14 15 16 17 |
# File 'lib/api_pi/assertion.rb', line 11 def check_if test, msg=nil msg ||= "I don't know about that" unless test then raise ApiPi::AssertionError, msg end true end |
#has_key(kkey) ⇒ Object
The ‘has_key` assertion can be used to query if a certain field is present in a JSON block:
{ "here": "present!" }.has_key "here" => true
{ "here": "present!" }.has_key "nope!" => false
52 53 54 55 |
# File 'lib/api_pi/assertion.rb', line 52 def has_key kkey msg = "#{kkey} was not found in #{self.keys.join(', ')}}" check_if self.respond_to?(kkey), msg end |
#has_keys(*kkeys) ⇒ Object
The ‘has_keys` assertion works the same as `has_key`, except it takes multiple keys and tests them all at once:
{ "here": "present!", "hello": "world!" }.has_keys "here","hello" => true
{ "here": "present!", "hello": "world!" }.has_keys "here","hope" => false
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/api_pi/assertion.rb', line 63 def has_keys *kkeys not_found = [] kkeys.each do |k| unless self.respond_to?(k) not_found << k end end if not_found.empty? true else nf = not_found.join(", ") msg = "#{nf} were not found in #{self.keys.join(', ')}" raise ApiPi::AssertionError, msg end end |
#includes(item) ⇒ Object
‘includes` can be used for array to query if specific values are present:
[1,2,3,4].includes 2 => true
103 104 105 106 |
# File 'lib/api_pi/assertion.rb', line 103 def includes item msg = "#{self} did not include #{item}" check_if self.include?(item), msg end |
#is(this) ⇒ Object
‘is` assertions are used to compare exact values.
"this".is "this" => true
200.is "200" => true
"this".is "that" => false
25 26 27 28 29 30 31 32 |
# File 'lib/api_pi/assertion.rb', line 25 def is this msg = "#{self} is not #{this}" if this.is_a? Integer check_if self == this.to_s, msg else check_if self == this, msg end end |
#is_a(klass) ⇒ Object Also known as: is_an
The ‘is_a` and `is_an` assertions are used to assert Type on a field:
"Hello, World!".is_a String => true
3.14159265359.is_a Float => true
"Not an array!".is_an Array => false
40 41 42 43 |
# File 'lib/api_pi/assertion.rb', line 40 def is_a klass msg = "#{self} is not a #{klass}" check_if self.is_a?(klass), msg end |
#lacks_key(kkey) ⇒ Object
‘lacks_key` is the opposite of `has_key`, in that it tests that a field is NOT present in a JSON block:
{ "here": "present!" }.lacks_key "nope!" => true
{ "here": "present!" }.lacks_key "here" => false
85 86 87 88 |
# File 'lib/api_pi/assertion.rb', line 85 def lacks_key kkey msg = "#{kkey} was found in #{self.keys.join(', ')}}" check_if !self.respond_to?(kkey), msg end |
#matches(regex) ⇒ Object
‘matches` expectes a Regex formed value.
"hello".matches /ll/ => true
94 95 96 97 |
# File 'lib/api_pi/assertion.rb', line 94 def matches regex msg = "#{self} did not match regex #{regex}" check_if self.match(regex), msg end |
#not_nil? ⇒ Boolean
‘not_nil?` checks to see if the value is nil:
nil.not_nil? => false
"something".not_nil? => true
113 114 115 116 |
# File 'lib/api_pi/assertion.rb', line 113 def not_nil? msg = "#{self} was nil/null" check_if !self.nil?, msg end |
#stripped? ⇒ Boolean
‘stripped?` will test is there is whitespace before or after a string:
"works".stripped? => true
" spaces ".stripped? => false
123 124 125 126 |
# File 'lib/api_pi/assertion.rb', line 123 def stripped? msg = "#{self} contains whitespace" check_if(self.strip == self, msg) end |