Module: Ronin::Script::Testable
- Includes:
- UI::Output::Helpers
- Included in:
- Buildable, Deployable
- Defined in:
- lib/ronin/script/testable.rb
Overview
Adds testing methods to an Ronin::Script.
Instance Method Summary collapse
-
#flunk(message) ⇒ Object
protected
Flunks the testing process.
-
#initialize(attributes = {}) ⇒ Object
Initializes the testable script.
-
#test { ... } ⇒ Script
protected
Registers a given block to be called when the script is tested.
-
#test! ⇒ true
Tests that the script is properly configured.
-
#test?(message) { ... } ⇒ true
protected
Tests whether an expression is true.
-
#test_equal(name, expected_value, message = nil) ⇒ true
protected
Tests whether a method has the expected value.
-
#test_in(name, expected_values, message = nil) ⇒ true
protected
Tests a method has a value in the expected values.
-
#test_match(name, pattern, message = nil) ⇒ true
protected
Tests whether a method matches the pattern.
-
#test_no_match(name, pattern, message = nil) ⇒ true
protected
Tests whether a method does not matches the pattern.
-
#test_not_equal(name, unexpected_value, message = nil) ⇒ true
protected
Tests whether a method does not have the unexpected value.
-
#test_not_in(name, unexpected_values, message = nil) ⇒ true
protected
Tests a method does not have a value in the unexpected values.
-
#test_set(name, message = nil) ⇒ true
protected
Tests whether a method returns a non-
nil
value.
Instance Method Details
#flunk(message) ⇒ Object (protected)
Flunks the testing process.
85 86 87 |
# File 'lib/ronin/script/testable.rb', line 85 def flunk() raise(TestFailed,) end |
#initialize(attributes = {}) ⇒ Object
Initializes the testable script.
43 44 45 46 47 |
# File 'lib/ronin/script/testable.rb', line 43 def initialize(attributes={}) super(attributes) @test_blocks = [] end |
#test { ... } ⇒ Script (protected)
Registers a given block to be called when the script is tested.
102 103 104 105 |
# File 'lib/ronin/script/testable.rb', line 102 def test(&block) @test_blocks << block return self end |
#test! ⇒ true
Tests that the script is properly configured.
61 62 63 64 65 66 67 68 |
# File 'lib/ronin/script/testable.rb', line 61 def test! print_info "Testing #{script_type} ..." @test_blocks.each { |block| block.call() } print_info "#{script_type} tested!" return true end |
#test?(message) { ... } ⇒ true (protected)
Tests whether an expression is true.
126 127 128 |
# File 'lib/ronin/script/testable.rb', line 126 def test?(,&block) test { flunk() unless block.call() } end |
#test_equal(name, expected_value, message = nil) ⇒ true (protected)
Tests whether a method has the expected value.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ronin/script/testable.rb', line 152 def test_equal(name,expected_value,=nil) name = name.to_sym test do actual_value = self.send(name) ||= "#{name} (#{actual_value.inspect}) must equal #{expected_value.inspect}" flunk() unless actual_value == expected_value end end |
#test_in(name, expected_values, message = nil) ⇒ true (protected)
Tests a method has a value in the expected values.
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/ronin/script/testable.rb', line 320 def test_in(name,expected_values,=nil) name = name.to_sym test do actual_value = self.send(name) ||= "#{name} (#{actual_value.inspect}) must be one of #{expected_values.inspect}" flunk() unless expected_values.include?(actual_value) end end |
#test_match(name, pattern, message = nil) ⇒ true (protected)
Tests whether a method matches the pattern.
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/ronin/script/testable.rb', line 254 def test_match(name,pattern,=nil) name = name.to_sym test do actual_value = self.send(name) ||= "#{name} (#{actual_value.inspect}) must match #{pattern.inspect}" flunk() unless actual_value.match(pattern) end end |
#test_no_match(name, pattern, message = nil) ⇒ true (protected)
Tests whether a method does not matches the pattern.
287 288 289 290 291 292 293 294 295 296 |
# File 'lib/ronin/script/testable.rb', line 287 def test_no_match(name,pattern,=nil) name = name.to_sym test do actual_value = self.send(name) ||= "#{name} (#{actual_value.inspect}) cannot match #{pattern.inspect}" flunk() unless !actual_value.match(pattern) end end |
#test_not_equal(name, unexpected_value, message = nil) ⇒ true (protected)
Tests whether a method does not have the unexpected value.
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ronin/script/testable.rb', line 185 def test_not_equal(name,unexpected_value,=nil) name = name.to_sym test do actual_value = self.send(name) ||= "#{name} (#{actual_value.inspect}) cannot equal #{unexpected_value.inspect}" flunk() unless actual_value != unexpected_value end end |
#test_not_in(name, unexpected_values, message = nil) ⇒ true (protected)
Tests a method does not have a value in the unexpected values.
353 354 355 356 357 358 359 360 361 362 |
# File 'lib/ronin/script/testable.rb', line 353 def test_not_in(name,unexpected_values,=nil) name = name.to_sym test do actual_value = self.send(name) ||= "#{name} (#{actual_value.inspect}) cannot be one of #{unexpected_values.inspect}" flunk() unless !unexpected_values.include?(actual_value) end end |
#test_set(name, message = nil) ⇒ true (protected)
Tests whether a method returns a non-nil
value.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/ronin/script/testable.rb', line 215 def test_set(name,=nil) name = name.to_sym test do actual_value = self.send(name) ||= "#{name} is not set" blank = if actual_value.respond_to?(:empty?) actual_value.empty? else actual_value.nil? end flunk() if blank end end |