huh 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -13,7 +13,7 @@ or from source
13
13
  git clone git://github.com/justinbaker/huh.git
14
14
  cd huh
15
15
  rake
16
- gem install huh-1.0.6.gem
16
+ gem install huh-1.0.8.gem
17
17
 
18
18
  Usage
19
19
  =========
@@ -31,6 +31,14 @@ Usage
31
31
 
32
32
  Don't forget the finish!; it prints out number of tests, assertions, failures, as well as percentage of passing tests.
33
33
 
34
+ Running tests from `irb` or `rails console`
35
+ =============================================
36
+
37
+ Just require huh, and call Huh#run with all test files as an argument.
38
+
39
+ require "huh"
40
+ Huh.run(Dir.glob("examples/*.rb"))
41
+
34
42
  Assertions
35
43
  ============
36
44
 
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + "/../lib/huh"
2
2
 
3
3
  class Test < Huh
4
-
4
+
5
5
  test "the truth" do
6
6
  assert true
7
7
  end
data/lib/huh.rb CHANGED
@@ -1,18 +1,16 @@
1
1
  class Huh
2
- V = "1.0.6"
2
+ V = "1.0.8" unless defined?(V)
3
3
  class Failure < StandardError; end
4
4
  def self.test(name, &block)
5
5
  @t = oz(@t) + 1
6
- begin
7
- yield
8
- rescue Failure => e
9
- print "Testing #{name} [\033[1;49;31mFAILED\033[0m]\n"
10
- else
11
- print "Testing #{name} [\033[1;49;32mPASSED\033[0m]\n"
12
- end
6
+ 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
13
7
  end
8
+ def self.setup(&block); @setup = block; end
9
+ def self.teardown(&block); @teardown = block; end
14
10
  def self.assert(truth)
11
+ @setup.call if @setup
15
12
  !!truth ? (@a = oz(@a) + 1) : (@f = oz(@f) + 1; raise Failure)
13
+ @teardown.call if @teardown
16
14
  end
17
15
  def self.oz(v); v or 0; end
18
16
  def self.flunk; assert(false); end
@@ -34,6 +32,7 @@ class Huh
34
32
  def self.assert_in_delta(e,a,d); assert((e.to_f - a.to_f).abs <= d.to_f); end
35
33
  def self.assert_nothing_thrown(&block); assert(begin; yield; true; rescue; false;end); end
36
34
  def self.assert_nothing_raised(&block); assert(begin; yield;true; rescue; false;end) end
37
- 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 # spit out info
35
+ 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
36
+ def self.run(f);f.each{|t|puts "\nrunning tests from #{t}\n"; require t}; end
38
37
  end
39
38
 
data/lib/huh/task.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Huh
2
+ class Task
3
+ def initialize(name = :test, files = [])
4
+ task name do
5
+ Huh.run(files)
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/huh_expanded.rb CHANGED
@@ -1,9 +1,40 @@
1
+ # Huh is a small unit testing library
2
+ # It supports many assertions commonly used, and has no external dependencies.
3
+ #
4
+ # Author:: Justin Baker (mailto:bakermoto@gmail.com)
5
+ # Copyright:: Copyright (c) 2010 Justin Baker
6
+ # License:: MIT license
7
+
8
+ # This class creates a new test case. Your test classes should inherit from this class.
9
+ #
10
+ # Example
11
+ # class MyTest < Huh
12
+ # test "the truth" do
13
+ # assert true
14
+ # end
15
+ # end
16
+ #
17
+ # You can do almost anything you do with Test::Unit included in the standard library, or MiniTest.
18
+ # For all of the assertions, see README.md or examples/all_assertions for usage.
19
+ #
20
+ #
1
21
  class Huh
2
22
 
3
- VERSION = "1.0.5"
23
+ VERSION = "1.0.8" unless defined?(VERSION) #:nodoc:
4
24
 
25
+ # The error thrown from a failing test
26
+ #
5
27
  class Failure < StandardError; end
6
28
 
29
+ # The method used for creating a new test
30
+ #
31
+ # Example
32
+ # class PostTest < Huh
33
+ # test "create_new_post_and_save" do
34
+ # assert Post.new.save
35
+ # end
36
+ # end
37
+ #
7
38
  def self.test(name, &block)
8
39
  @tests = oz(@tests) + 1
9
40
  begin
@@ -15,95 +46,191 @@ class Huh
15
46
  end
16
47
  end
17
48
 
49
+ # runs a block before every test
50
+ #
51
+ # Example
52
+ # class PostTest < Huh
53
+ # setup do
54
+ # @post = Post.new
55
+ # end
56
+ # test "create_new_post_and_save" do
57
+ # assert @post.save
58
+ # end
59
+ # end
60
+ #
61
+ def self.setup(&block)
62
+ @setup = block
63
+ end
64
+
65
+ # runs a block after every test
66
+ #
67
+ # Example
68
+ # class PostTest < Huh
69
+ # teardown do
70
+ # @post = nil
71
+ # end
72
+ # test "create_new_post_and_save" do
73
+ # @post = Post.new
74
+ # assert @post.save
75
+ # end
76
+ # end
77
+ #
78
+ def self.teardown(&block)
79
+ @teardown = block
80
+ end
81
+
82
+ # validates that a condition is true
83
+ #
84
+ # Example
85
+ # class PostTest < Huh
86
+ # test "create_new_post_and_save" do
87
+ # @post = Post.new
88
+ # assert @post.save
89
+ # end
90
+ # end
91
+ #
18
92
  def self.assert(truth)
93
+ @setup.call if @setup
19
94
  !!truth ? (@assertions = oz(@assertions) + 1) : (@failures = oz(@failures) + 1; raise Failure)
95
+ @teardown.call if @teardown
20
96
  end
21
-
97
+
98
+ # return value if it isn't zero
99
+ #
22
100
  def self.oz(value)
23
101
  value or 0
24
102
  end
25
103
 
104
+ # assertion that ALWAYS fails
105
+ #
26
106
  def self.flunk
27
107
  assert false
28
108
  end
29
109
 
110
+ # validate that actual == expected
111
+ #
30
112
  def self.assert_equal(expected, actual)
31
113
  assert expected == actual
32
114
  end
33
115
 
116
+ # validate that actual != expected
117
+ #
34
118
  def self.assert_not_equal(expected, actual)
35
119
  assert expected != actual
36
120
  end
37
121
 
122
+ # validate that expected.equal?(actual)
123
+ #
38
124
  def self.assert_same(expected, actual)
39
125
  assert expected.equal?(actual)
40
126
  end
41
127
 
128
+ # validate that !expected.equal?(actual)
129
+ #
42
130
  def self.assert_not_same(expected, actual)
43
131
  assert !expected.equal?(actual)
44
132
  end
45
133
 
134
+ # validate that value.nil?
135
+ #
46
136
  def self.assert_nil(value)
47
137
  assert value.nil?
48
138
  end
49
139
 
140
+ # validate that !value.nil?
141
+ #
50
142
  def self.assert_not_nil(value)
51
143
  assert !value.nil?
52
144
  end
53
145
 
146
+ # validate that object.instance_of?(type)
147
+ #
54
148
  def self.assert_instance_of(type, object)
55
149
  assert object.instance_of?(type)
56
150
  end
57
151
 
152
+ # validate that object.kind_of?(type)
153
+ #
58
154
  def self.assert_kind_of(kind, object)
59
155
  assert object.kind_of?(kind)
60
156
  end
61
157
 
158
+ # validate that pattern.match(string)
159
+ #
62
160
  def self.assert_match(pattern, string)
63
161
  assert pattern.match(string)
64
162
  end
65
163
 
164
+ # validate that !pattern.match(string)
165
+ #
66
166
  def self.assert_no_match(pattern, string)
67
167
  assert !pattern.match(string)
68
168
  end
69
169
 
170
+ # validate that object.respond_to?(meth)
171
+ #
70
172
  def self.assert_respond_to(meth, object)
71
173
  assert object.respond_to?(meth)
72
174
  end
73
-
175
+
176
+ # validate that the block raises an error
177
+ #
74
178
  def self.assert_raises(&block)
75
179
  assert(begin; yield; rescue; true; end)
76
180
  end
77
-
181
+
182
+ # validate that the block returns true
183
+ #
78
184
  def self.assert_block(&block)
79
185
  assert(begin;yield; rescue; false; end)
80
186
  end
81
-
82
- def self.assert_operator(a,b,operator)
83
- assert a.send(operator, b)
187
+
188
+ # validate that the block returns true when operator is called on it with value
189
+ #
190
+ def self.assert_operator(object, value,operator)
191
+ assert object.send(operator, value)
84
192
  end
85
193
 
86
- def self.assert_send(a)
87
- assert(a[0].send(a[1], *a[2..-1]))
194
+ # validate that send_array[0] returns true when send_array[1] is called on it with the rest of send_array as args
195
+ #
196
+ def self.assert_send(send_array)
197
+ assert send_array[0].send(send_array[1], *send_array[2..-1])
88
198
  end
89
199
 
90
- def self.assert_in_delta(e,a,d)
91
- assert((e.to_f - a.to_f).abs <= d.to_f)
200
+ # validate that actual is less than delta away from expected
201
+ #
202
+ def self.assert_in_delta(expected, actual, delta)
203
+ assert (expected.to_f - actual.to_f).abs <= delta.to_f
92
204
  end
93
-
205
+
206
+ # validate that nothing is thrown by the block
207
+ #
94
208
  def self.assert_nothing_thrown(&block)
95
209
  assert(begin; yield; true; rescue; false;end)
96
210
  end
97
211
 
212
+ # validate that no errors are raised by the block
213
+ #
98
214
  def self.assert_nothing_raised(&block)
99
215
  assert(begin; yield;true; rescue; false;end)
100
216
  end
101
217
 
218
+ # print out statistics of test
219
+ #
102
220
  def self.finish!
103
- percentage = (((oz(@tests)-oz(@failures)).to_f/@tests.to_f)*100).to_i
221
+ percentage = ((( oz(@tests) - oz(@failures) ).to_f / @tests.to_f) * 100 ).to_i
104
222
  print "\n#{oz(@tests)} tests, #{oz(@assertions)} assertions, #{oz(@failures)} failures."
105
223
  print "#{percentage}% passing tests}\n"
106
224
  end
107
225
 
226
+ # use in irb/rails console to run tests
227
+ #
228
+ def self.run(files)
229
+ puts "\nrunning tests from #{t}\n"
230
+ files.each do |file|
231
+ require file
232
+ end
233
+ end
234
+
108
235
  end
109
236
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huh
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
4
+ hash: 7
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 6
10
- version: 1.0.6
9
+ - 8
10
+ version: 1.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Justin Baker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-24 00:00:00 -05:00
18
+ date: 2011-03-08 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,11 +30,10 @@ extra_rdoc_files: []
30
30
 
31
31
  files:
32
32
  - lib/huh_expanded.rb
33
+ - lib/huh/task.rb
33
34
  - lib/huh.rb
34
- - examples/all_assertions.rb
35
- - examples/flunk.rb
36
- - examples/post_test.rb
37
35
  - examples/basic.rb
36
+ - examples/all_assertions.rb
38
37
  - LICENSE
39
38
  - README.md
40
39
  has_rdoc: true
@@ -69,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
68
  requirements: []
70
69
 
71
70
  rubyforge_project: huh
72
- rubygems_version: 1.3.7
71
+ rubygems_version: 1.6.0
73
72
  signing_key:
74
73
  specification_version: 3
75
74
  summary: 40 line ruby unit testing library
data/examples/flunk.rb DELETED
@@ -1,11 +0,0 @@
1
- require File.dirname(__FILE__) + "/../lib/huh"
2
-
3
- class Test < Huh
4
-
5
- test "flunk should always fail" do
6
- flunk
7
- end
8
-
9
- finish!
10
-
11
- end
@@ -1,32 +0,0 @@
1
- require File.dirname(__FILE__) + "/../lib/huh"
2
-
3
- class PostTest < Huh
4
-
5
- test "post_should_not_save" do
6
- @post = Post.new
7
- assert !@post.save
8
- end
9
-
10
- test "post_should_not_save_without_title" do
11
- @post = Post.new(:content = "Lorem blah blah..")
12
- assert !@post.save
13
- end
14
-
15
- test "post_should_save" do
16
- @post = Post.new(:content = "Lorem blah blah..", :title => "My First Post")
17
- assert @post.save
18
- end
19
-
20
- test "post_should_be_published" do
21
- @post = Post.first
22
- assert @post.published?
23
- end
24
-
25
- test "post_should_not_be_published" do
26
- @post = Post.last
27
- assert @post.published?
28
- end
29
-
30
- finish!
31
-
32
- end