Class: Huh
- Inherits:
-
Object
- Object
- Huh
- Defined in:
- lib/huh_expanded.rb,
lib/huh.rb,
lib/huh/task.rb
Overview
This class creates a new test case. Your test classes should inherit from this class.
Example class MyTest < Huh test "the truth" do assert true end end
You can do almost anything you do with Test::Unit included in the standard library, or MiniTest. For all of the assertions, see README.md or examples/all_assertions for usage.
Defined Under Namespace
Constant Summary collapse
- V =
"1.0.8"- VERSION =
:nodoc:
"1.0.8"
Class Method Summary collapse
-
.assert(truth) ⇒ Object
validates that a condition is true.
-
.assert_block(&block) ⇒ Object
validate that the block returns true.
-
.assert_equal(expected, actual) ⇒ Object
validate that actual == expected.
-
.assert_in_delta(expected, actual, delta) ⇒ Object
validate that actual is less than delta away from expected.
-
.assert_instance_of(type, object) ⇒ Object
validate that object.instance_of?(type).
-
.assert_kind_of(kind, object) ⇒ Object
validate that object.kind_of?(type).
-
.assert_match(pattern, string) ⇒ Object
validate that pattern.match(string).
-
.assert_nil(value) ⇒ Object
validate that value.nil?.
-
.assert_no_match(pattern, string) ⇒ Object
validate that !pattern.match(string).
-
.assert_not_equal(expected, actual) ⇒ Object
validate that actual != expected.
-
.assert_not_nil(value) ⇒ Object
validate that !value.nil?.
-
.assert_not_same(expected, actual) ⇒ Object
validate that !expected.equal?(actual).
-
.assert_nothing_raised(&block) ⇒ Object
validate that no errors are raised by the block.
-
.assert_nothing_thrown(&block) ⇒ Object
validate that nothing is thrown by the block.
-
.assert_operator(object, value, operator) ⇒ Object
validate that the block returns true when operator is called on it with value.
-
.assert_raises(&block) ⇒ Object
validate that the block raises an error.
-
.assert_respond_to(meth, object) ⇒ Object
validate that object.respond_to?(meth).
-
.assert_same(expected, actual) ⇒ Object
validate that expected.equal?(actual).
- .assert_send(send_array) ⇒ Object
-
.finish! ⇒ Object
print out statistics of test.
-
.flunk ⇒ Object
assertion that ALWAYS fails.
-
.oz(value) ⇒ Object
return value if it isn't zero.
-
.run(files) ⇒ Object
use in irb/rails console to run tests.
-
.setup(&block) ⇒ Object
runs a block before every test Example class PostTest < Huh setup do @post = Post.new end test "create_new_post_and_save" do assert @post.save end end.
-
.teardown(&block) ⇒ Object
runs a block after every test Example class PostTest < Huh teardown do @post = nil end test "create_new_post_and_save" do @post = Post.new assert @post.save end end.
-
.test(name, &block) ⇒ Object
The method used for creating a new test Example class PostTest < Huh test "create_new_post_and_save" do assert Post.new.save end end.
Class Method Details
.assert(truth) ⇒ Object
validates that a condition is true
Example class PostTest < Huh test "create_new_post_and_save" do @post = Post.new assert @post.save end end
92 93 94 95 96 |
# File 'lib/huh_expanded.rb', line 92 def self.assert(truth) @setup.call if @setup !!truth ? (@a = oz(@a) + 1) : (@f = oz(@f) + 1; raise Failure) @teardown.call if @teardown end |
.assert_block(&block) ⇒ Object
validate that the block returns true
184 |
# File 'lib/huh_expanded.rb', line 184 def self.assert_block(&block); assert(begin;yield; rescue; false; end); end |
.assert_equal(expected, actual) ⇒ Object
validate that actual == expected
112 |
# File 'lib/huh_expanded.rb', line 112 def self.assert_equal(e,a); assert(e == a); end |
.assert_in_delta(expected, actual, delta) ⇒ Object
validate that actual is less than delta away from expected
202 |
# File 'lib/huh_expanded.rb', line 202 def self.assert_in_delta(e,a,d); assert((e.to_f - a.to_f).abs <= d.to_f); end |
.assert_instance_of(type, object) ⇒ Object
validate that object.instance_of?(type)
148 |
# File 'lib/huh_expanded.rb', line 148 def self.assert_instance_of(k,o); assert(o.instance_of?(k)); end |
.assert_kind_of(kind, object) ⇒ Object
validate that object.kind_of?(type)
154 |
# File 'lib/huh_expanded.rb', line 154 def self.assert_kind_of(k,o); assert(o.kind_of?(k)); end |
.assert_match(pattern, string) ⇒ Object
validate that pattern.match(string)
160 |
# File 'lib/huh_expanded.rb', line 160 def self.assert_match(p,s); assert(p.match(s)); end |
.assert_nil(value) ⇒ Object
validate that value.nil?
136 |
# File 'lib/huh_expanded.rb', line 136 def self.assert_nil(a); assert(a.nil?); end |
.assert_no_match(pattern, string) ⇒ Object
validate that !pattern.match(string)
166 |
# File 'lib/huh_expanded.rb', line 166 def self.assert_no_match(p,s); assert(!p.match(s)); end |
.assert_not_equal(expected, actual) ⇒ Object
validate that actual != expected
118 |
# File 'lib/huh_expanded.rb', line 118 def self.assert_not_equal(e,a); assert(e != a); end |
.assert_not_nil(value) ⇒ Object
validate that !value.nil?
142 |
# File 'lib/huh_expanded.rb', line 142 def self.assert_not_nil(a); assert(!a.nil?); end |
.assert_not_same(expected, actual) ⇒ Object
validate that !expected.equal?(actual)
130 |
# File 'lib/huh_expanded.rb', line 130 def self.assert_not_same(e,a); assert(!e.equal?(a)); end |
.assert_nothing_raised(&block) ⇒ Object
validate that no errors are raised by the block
214 |
# File 'lib/huh_expanded.rb', line 214 def self.assert_nothing_raised(&block); assert(begin; yield;true; rescue; false;end) end |
.assert_nothing_thrown(&block) ⇒ Object
validate that nothing is thrown by the block
208 |
# File 'lib/huh_expanded.rb', line 208 def self.assert_nothing_thrown(&block); assert(begin; yield; true; rescue; false;end); end |
.assert_operator(object, value, operator) ⇒ Object
validate that the block returns true when operator is called on it with value
190 |
# File 'lib/huh_expanded.rb', line 190 def self.assert_operator(a,b,o);assert(a.send(o, b)); end |
.assert_raises(&block) ⇒ Object
validate that the block raises an error
178 |
# File 'lib/huh_expanded.rb', line 178 def self.assert_raises(&block); assert(begin; yield; rescue; true; end); end |
.assert_respond_to(meth, object) ⇒ Object
validate that object.respond_to?(meth)
172 |
# File 'lib/huh_expanded.rb', line 172 def self.assert_respond_to(m,o); assert(o.respond_to?(m)); end |
.assert_same(expected, actual) ⇒ Object
validate that expected.equal?(actual)
124 |
# File 'lib/huh_expanded.rb', line 124 def self.assert_same(e,a); assert(e.equal?(a)); end |
.assert_send(send_array) ⇒ Object
196 |
# File 'lib/huh_expanded.rb', line 196 def self.assert_send(a); assert(a[0].send(a[1], *a[2..-1])); end |
.finish! ⇒ Object
print out statistics of test
220 |
# File 'lib/huh_expanded.rb', line 220 def self.finish!;puts "\n#{oz(@t)} tests, #{oz(@a)} assertions, #{oz(@f)} failures. #{(((oz(@t)-oz(@f)).to_f/@t.to_f)*100).to_i}% passing tests"; end |
.flunk ⇒ Object
assertion that ALWAYS fails
106 |
# File 'lib/huh_expanded.rb', line 106 def self.flunk; assert(false); end |
.oz(value) ⇒ Object
return value if it isn't zero
100 |
# File 'lib/huh_expanded.rb', line 100 def self.oz(v); v or 0; end |
.run(files) ⇒ Object
use in irb/rails console to run tests
228 |
# File 'lib/huh_expanded.rb', line 228 def self.run(f);f.each{|t|puts "\nrunning tests from #{t}\n"; require t}; end |
.setup(&block) ⇒ Object
runs a block before every test
Example class PostTest < Huh setup do @post = Post.new end test "create_new_post_and_save" do assert @post.save end end
61 |
# File 'lib/huh_expanded.rb', line 61 def self.setup(&block); @setup = block; end |
.teardown(&block) ⇒ Object
runs a block after every test
Example class PostTest < Huh teardown do @post = nil end test "create_new_post_and_save" do @post = Post.new assert @post.save end end
78 |
# File 'lib/huh_expanded.rb', line 78 def self.teardown(&block); @teardown = block; end |
.test(name, &block) ⇒ Object
The method used for creating a new test
Example class PostTest < Huh test "create_new_post_and_save" do assert Post.new.save end end
38 39 40 41 |
# File 'lib/huh_expanded.rb', line 38 def self.test(name, &block) @t = oz(@t) + 1 print begin; yield; "Testing #{name} [\033[1;49;32mPASSED\033[0m]\n"; rescue Failure => e; "Testing #{name} [\033[1;49;31mFAILED\033[0m]\n" ;end end |