verify 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ *
2
+ * Lab419, The Ruby For 1.9 Labratory, or Labrador for 1.9
3
+ * Subproject: Verify ( Mockify )
4
+ *
5
+ * Distributed under the terms of the BSD License.
6
+ * Copyright (c) 2007, 2008, 2009 Robert Dober
7
+ * All rights reserved.
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted provided that the following conditions are met:
11
+ * * Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ * * Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in the
15
+ * documentation and/or other materials provided with the distribution.
16
+ * * Neither the name of the Labrador packahe nor the
17
+ * names of its contributors may be used to endorse or promote products
18
+ * derived from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY Robert Dober ``AS IS'' AND ANY
21
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL Robert Dober BE LIABLE FOR ANY
24
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README ADDED
@@ -0,0 +1,53 @@
1
+ This is the very simple Verify mini test framework
2
+
3
+ It emphasises on being able to check things out quickly without having to load a heavy test
4
+ framework.
5
+ It evolves around my needs but for the time being it is pretty lightweight.
6
+
7
+
8
+ require 'verify'
9
+
10
+ Verify "Some Important Facts" do
11
+ verify "42 is true" do
12
+ 42
13
+ end
14
+ refute "42 is pretty big" do
15
+ 42 > 100_000
16
+ end
17
+ verify_exceptions NoMethodError do
18
+ 42.unrelinguished!
19
+ end
20
+ end
21
+
22
+ These forms are very easy to remember, but sometimes one needs to see where the
23
+ errors, are, in order to achieve that one can use the parameter form
24
+
25
+ Verify "With parameters" do
26
+ verify msg: "Everything is well", target: 42, actual: 21 * 2
27
+ refute msg: "But not too much", target: 42, actual: 41
28
+ end
29
+
30
+ Mockify allows to capture stdout or simulate stdin, all you need to do is to
31
+ require 'mockify' *before* requiring 'verify' and than you can provide stdin
32
+ and get stdout
33
+
34
+ Verify "Providing stdin" do
35
+ with_input %w{The Quick Brown Fox} do
36
+ verify %{first line is "The"} do
37
+ "The" == gets.chomp
38
+ end
39
+ end
40
+ end
41
+
42
+ Verify "Capturing stdout" do
43
+ out = with_output do | o |
44
+ puts 42
45
+ verify target: "42\n", actual: o.string
46
+ end
47
+ end
48
+ verify msg: "with_output converted out to an array of lines",
49
+ actual: out.map( &:chomp ),
50
+ target: %w{ 42 }
51
+ end
52
+
53
+ Enjoy
data/RELEASE ADDED
@@ -0,0 +1,18 @@
1
+ 2009-06-18 Verify ( Mockify )
2
+ -----------------------------
3
+
4
+ Stripped away from Labrador/Lab419 as a standalone package, under the same license.
5
+ Ruby1.9 only!
6
+
7
+ * Mockify became a with_input method, thus stdin and stdout can be mocked now.
8
+
9
+ * Verify: verify and refute support the explicit argument form now:
10
+ While the original call syntax still is supported and ecouraged to be used, it makes it
11
+ difficult to debug failing tests. The explicit paramter form prints the - optional -
12
+ descriptive message *and* the target and the actual value.
13
+
14
+ * Verify shorter output for Verify do ... end, while the whole resume remains vervose and
15
+ still indicates failure "graphically" in the last line.
16
+
17
+ * All modules were kept in the Lab419 module which remains the "logical" super project for
18
+ this.
data/lib/mockify.rb ADDED
@@ -0,0 +1,142 @@
1
+ #!/usr/bin/ruby -w
2
+ # encoding: utf-8
3
+ #
4
+ #--
5
+ #*
6
+ #* Lab419, The Ruby For 1.9 Labratory, or Labrador for 1.9
7
+ #*
8
+ #* Distributed under the terms of the BSD License.
9
+ #* Copyright (c) 2007, 2008, 2009 Robert Dober
10
+ #* All rights reserved.
11
+ #*
12
+ #* Redistribution and use in source and binary forms, with or without
13
+ #* modification, are permitted provided that the following conditions are met:
14
+ #* * Redistributions of source code must retain the above copyright
15
+ #* notice, this list of conditions and the following disclaimer.
16
+ #* * Redistributions in binary form must reproduce the above copyright
17
+ #* notice, this list of conditions and the following disclaimer in the
18
+ #* documentation and/or other materials provided with the distribution.
19
+ #* * Neither the name of the Labrador packahe nor the
20
+ #* names of its contributors may be used to endorse or promote products
21
+ #* derived from this software without specific prior written permission.
22
+ #*
23
+ #* THIS SOFTWARE IS PROVIDED BY Robert Dober ``AS IS'' AND ANY
24
+ #* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25
+ #* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
+ #* DISCLAIMED. IN NO EVENT SHALL Robert Dober BE LIABLE FOR ANY
27
+ #* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28
+ #* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
+ #* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30
+ #* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
+ #* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
+ #* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ #
34
+ #++
35
+ #
36
+ # The Verify tool is a KISS verification and test tool, it is inspired by Ara T. Howard's testy.rb.
37
+ # Ara however has still implemented a BDD framework, the purpose of Verify is to provide a Ruby Programmer
38
+ # with a very simple yet effective tool to test her assumptions about code.
39
+ #
40
+ # Verify was motivated by the remark of Phlip who made an excellent point about assert. The Verify tool
41
+ # tries to implement exactly that idea.
42
+ #
43
+ # Verify has the following goals
44
+ #
45
+ # * Almost nothing to learn, one wrapper method and three test methods
46
+ # # At the end a resume of the verifications with an error message for each failed verification
47
+ # # will be printed to stderr.
48
+ # Verify optional_message do
49
+ # # All verifications to be reported go here and can have one of the following forms
50
+ # verify optional_message, &blk
51
+ # # verify that blk[] returns a true value
52
+ # verify_not optional_message, &blk
53
+ # # verify that vlk[] does not return a true value
54
+ # verify_exceptions *exceptions, &blk
55
+ # # verify that one exception of the list passed to the method is raised by blk[]
56
+ # end
57
+ #
58
+ #
59
+ # * No metaprogramming, no at_exit hook, execution of the verification inside a wrapper object, no global namespace pollution,
60
+ # except the Verify method.
61
+ #
62
+ # * Very simple output
63
+ require 'stringio'
64
+ module Lab419
65
+ #
66
+ # Mockify has been created for Verify but can easily be used as a general
67
+ # purpose mocking technique for stdin and stdout.
68
+ # If you are not using Mockify with Verify you simply have to include the
69
+ # module yourself before calling #with_input and #with_output:
70
+ # E.g.
71
+ #
72
+ # require 'mockify'
73
+ # include Lab419::Mockify
74
+ #
75
+ module Mockify
76
+ AUTHOR="Robert Dober"
77
+ VERSION="0.3"
78
+
79
+ #
80
+ # The provided block will be executed in an environement in which stdin
81
+ # is a StringIO object, created as a function of method parameter ary.
82
+ #
83
+ # If ary is an IO object it will be used directly.
84
+ # If the ary parameter is a String a StringIO object is constructed by
85
+ # passing ary to the constructor.
86
+ # If it is an Array, the array will be joined with newline before being
87
+ # fed to StringIO constructor.
88
+ # All other arguments will raise an ArgumentError!
89
+ #
90
+ def with_input ary, &blk
91
+ ary = case ary
92
+ when String
93
+ StringIO::new ary
94
+ when Enumerable
95
+ StringIO::new ary.to_a.join( "\n" )
96
+ when IO
97
+ ary
98
+ else
99
+ raise ArgumentError, "Cannot make an IO out of #{ary.inspect}"
100
+ end
101
+
102
+ backup = $stdin
103
+ $stdin = ary
104
+ blk.call
105
+ ensure
106
+ $stdin = backup
107
+ end
108
+
109
+ #
110
+ # The provided block will be executed in an environement in which stdout is captured by
111
+ # a StringIO object. This is either a newly created StringIO object or a StringIO object
112
+ # provided as a parameter to with_output. with_output will return the StringIO that has
113
+ # been used, but it will also pass it to the block. Therefore verifications can be made
114
+ # inside the block.
115
+ # The returned object is the StringIO object already converted to an array of lines, but
116
+ # the object passed into the block is the StringIO object itself that still will change
117
+ # by each "writing" to stdout in the block.
118
+ # self.
119
+ # e.g.
120
+ # out = with_output do | o |
121
+ # puts 42
122
+ # verify target: "42\n", actual: o.string
123
+ # puts
124
+ # end
125
+ # verify do [ "42", "" ] == out
126
+ #
127
+ def with_output ary=nil, &blk
128
+
129
+ ary ||= StringIO::new
130
+ raise ArgumentError, "output mocking objects must be stringio's" unless StringIO === ary
131
+
132
+ backup = $>
133
+ $> = ary
134
+ #instance_eval( &client )
135
+ blk[ ary ]
136
+ ary.rewind
137
+ ary.each_line.to_a
138
+ ensure
139
+ $> = backup
140
+ end
141
+ end
142
+ end
data/lib/verify.rb ADDED
@@ -0,0 +1,205 @@
1
+ #!/usr/bin/ruby -w
2
+ # encoding: utf-8
3
+ #
4
+ #--
5
+ #*
6
+ #* Lab419, The Ruby For 1.9 Labratory, or Labrador for 1.9
7
+ #*
8
+ #* Distributed under the terms of the BSD License.
9
+ #* Copyright (c) 2007, 2008, 2009 Robert Dober
10
+ #* All rights reserved.
11
+ #*
12
+ #* Redistribution and use in source and binary forms, with or without
13
+ #* modification, are permitted provided that the following conditions are met:
14
+ #* * Redistributions of source code must retain the above copyright
15
+ #* notice, this list of conditions and the following disclaimer.
16
+ #* * Redistributions in binary form must reproduce the above copyright
17
+ #* notice, this list of conditions and the following disclaimer in the
18
+ #* documentation and/or other materials provided with the distribution.
19
+ #* * Neither the name of the Labrador packahe nor the
20
+ #* names of its contributors may be used to endorse or promote products
21
+ #* derived from this software without specific prior written permission.
22
+ #*
23
+ #* THIS SOFTWARE IS PROVIDED BY Robert Dober ``AS IS'' AND ANY
24
+ #* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25
+ #* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
+ #* DISCLAIMED. IN NO EVENT SHALL Robert Dober BE LIABLE FOR ANY
27
+ #* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28
+ #* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
+ #* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30
+ #* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
+ #* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
+ #* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ #
34
+ #++
35
+ #
36
+ # The Verify tool is a KISS verification and test tool, it is inspired by Ara T. Howard's testy.rb.
37
+ # Ara however has still implemented a BDD framework, the purpose of Verify is to provide a Ruby Programmer
38
+ # with a very simple yet effective tool to test her assumptions about code.
39
+ #
40
+ # Verify was motivated by the remark of Phlip who made an excellent point about assert. The Verify tool
41
+ # tries to implement exactly that idea.
42
+ #
43
+ # Verify has the following goals
44
+ #
45
+ # * Almost nothing to learn, one wrapper method and three test methods
46
+ # # At the end a resume of the verifications with an error message for each failed verification
47
+ # # will be printed to stderr.
48
+ # Verify optional_message do
49
+ # # All verifications to be reported go here and can have one of the following forms
50
+ # verify optional_message, &blk
51
+ # # verify that blk[] returns a true value
52
+ # verify_not optional_message, &blk
53
+ # # verify that vlk[] does not return a true value
54
+ # verify_exceptions *exceptions, &blk
55
+ # # verify that one exception of the list passed to the method is raised by blk[]
56
+ # end
57
+ #
58
+ #
59
+ # * No metaprogramming, no at_exit hook, execution of the verification inside a wrapper object, no global namespace pollution,
60
+ # except the Verify method.
61
+ #
62
+ # * Very simple output
63
+
64
+ module Lab419
65
+ module Verify
66
+ VERSION = "0.3"
67
+ AUTHOR = "Robert Dober"
68
+ URLS = %w{http://labrador.rubyforge.org/verify}
69
+ end
70
+ end
71
+
72
+ total_failed = 0
73
+ total_verified = 0
74
+ report_stream = $stderr
75
+
76
+ optional_message = lambda do | msg |
77
+ msg ? "#{msg}: " : ""
78
+ end
79
+
80
+ percentage = lambda do | all, nok |
81
+ all.zero? ? "(***.**%)" :
82
+ "(%6.2f%%)" % ( 10_000 * ( all - nok ).to_f / all / 100 )
83
+ end
84
+
85
+ total_report = lambda do
86
+ report_stream << "\n"
87
+ resume =
88
+ "+++ S u m m a r y: Verifications: #{total_verified+total_failed}, failed: #{total_failed}, succeeded: #{total_verified} #{percentage[ total_verified + total_failed, total_failed ]} +++"
89
+ rchar = total_failed.zero? ? "=" : "*"
90
+ s = resume.size
91
+ report_stream << "+" * s
92
+ report_stream << "\n"
93
+ report_stream << "+++" << " " * ( s - 6 ) << "+++"
94
+ report_stream << "\n"
95
+ report_stream << ( resume << "\n" )
96
+ report_stream << "+++" << " " * ( s - 6 ) << "+++"
97
+ report_stream << "\n"
98
+ report_stream << ( rchar * resume.size.pred << "\n" )
99
+ end
100
+
101
+ include( Module::new do
102
+ define_method :Verify do |ver_msg=nil, ver_stream=nil, &ver_blk|
103
+ report_stream = ver_stream if ver_stream
104
+ messages = []
105
+ failed = 0
106
+ verified = 0
107
+
108
+ fail_verification = lambda do | level, msg |
109
+ p caller if $DEBUG
110
+ if level then
111
+ messages << "Error: #{msg} #{caller[level]}"
112
+ else
113
+ messages << "Error: #{msg}"
114
+ end
115
+ failed += 1
116
+ total_failed += 1
117
+ end
118
+
119
+ report = lambda do | msg = nil, stream = $stderr |
120
+ #stream << "\n"
121
+ messages.map{ |m| m << "\n" }.each do | line |
122
+ stream << line
123
+ end
124
+
125
+ rchar = failed.zero? ? "=" : "*"
126
+ resume =
127
+ "#{rchar}#{rchar} #{optional_message[ msg ]}Verifications: #{verified+failed}, failed: #{failed}, succeeded: #{verified} #{percentage[ verified + failed, failed ]}"
128
+ stream << ( resume << "\n" )
129
+ #stream << ( rchar * resume.size << "\n" )
130
+ end
131
+
132
+ succeed_verification = lambda do
133
+ verified += 1
134
+ total_verified += 1
135
+ end
136
+
137
+ Module::new do
138
+
139
+ begin
140
+ extend Lab419::Mockify
141
+ rescue NameError
142
+ # Mockify has not been required by the user
143
+ end
144
+
145
+ define_method :verify do | msg=nil, &blk |
146
+ begin
147
+ if Hash === msg then
148
+ t = msg[ :target ]
149
+ a = msg[ :actual ]
150
+ msg = msg[ :message ] || msg[ :msg ]
151
+ return fail_verification [ 2, "#{msg}\n.. target: #{t.inspect}\n.. actual: #{a.inspect}\n.. #{msg} in " ] unless t==a
152
+ return succeed_verification[]
153
+ end
154
+ return fail_verification[ 2, "#{msg} in " ] unless blk[]
155
+ succeed_verification[]
156
+ rescue Exception => e
157
+ fail_verification[ nil, "#{msg} #{e.message} in #{e.backtrace.first}" ]
158
+ end
159
+ end
160
+
161
+ define_method :refute do | msg=nil, &blk |
162
+ begin
163
+ if Hash === msg then
164
+ t = msg[ :target ]
165
+ a = msg[ :actual ]
166
+ msg = msg[ :message ] || msg[ :msg ]
167
+ return fail_verification [ 2, "#{msg}\n.. target: #{t.inspect}\n.. actual: #{a.inspect}\n.. #{msg} in " ] if t==a
168
+ return succeed_verification[]
169
+ end
170
+ return fail_verification[ 2, "#{msg} in " ] if blk[]
171
+ succeed_verification[]
172
+ rescue Exception => e
173
+ fail_verification[ nil, "#{msg} #{e.message} in #{e.backtrace.first}" ]
174
+ end
175
+ end
176
+
177
+ define_method :verify_exceptions do | *excpts, &blk |
178
+ begin
179
+ blk[]
180
+ fail_verification[ 2, "None of #{excpts} thrown in " ]
181
+ rescue *excpts
182
+ succeed_verification[]
183
+ rescue Exception => e
184
+ fail_verification[ 3, "#{e} #{e.class} was thrown, but one of \"#{excpts}\" was expected in " ]
185
+ end
186
+ end
187
+
188
+ extend self
189
+ module_eval( &ver_blk )
190
+ report[ ver_msg, ver_stream || report_stream ]
191
+
192
+ end
193
+
194
+ end
195
+ end
196
+ )
197
+
198
+ at_exit do
199
+ at_exit do
200
+ at_exit do
201
+ total_report[]
202
+ end
203
+ end
204
+ end
205
+ # vim: sw=2 sts=2 ft=ruby nu expandtab: