more_unit_test 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ =begin rdoc
2
+ Imagine you have a method and the result is a long text.
3
+
4
+ You can compare the result via assert_equal, but in case of
5
+ an error you must find the difference in the long text.
6
+
7
+ Wouldn't it be nice, you can store your expected result in a file and
8
+ in case of differences you get the real result in another file?
9
+ Now you can take your favorite file difference tool and can compare
10
+ the expected and the real result.
11
+
12
+ Here it is: Test::Unit::TestCase#assert_equal_filecontent
13
+
14
+ See http://rubyforge.org/tracker/?func=detail&aid=19509&group_id=5650&atid=21859
15
+ =end
16
+ require 'test/unit'
17
+ require 'ftools'
18
+ #
19
+ #Extend the class TestCase with additional methods/assertions
20
+ #
21
+ class Test::Unit::TestCase
22
+ #
23
+ #Takes the content of the file 'filename' and compares it with 'actual' like in assert_equal.
24
+ #If 'filname' doesn't exist, the failure
25
+ # Reference file <#{filename}> missing
26
+ #is returned.
27
+ #
28
+ #'folder_for_failure' will contain all results with differences.
29
+ #
30
+ #Example of the usage:
31
+ # assert_equal_filecontent( "expected/test_section.html", text.to_doc(:html))
32
+ #
33
+ #What will happen:
34
+ #1. text.to_doc(:html) is the test. It creates some HTML-Text
35
+ #2. "expected/test_section.html" is read and compared to text.to_doc(:html)
36
+ # 1. If the file is missing -> error
37
+ # 2. If it is the same -> fine
38
+ # 3. If there are differences:
39
+ #3. A message is given (like in assert_equal)
40
+ #4. A folder "failure#{Date.today}" is created if not already exist
41
+ #5. The file 'test_section.html' with the result is created in "failure#{Date.today}/"
42
+ #6. I can use a compare tool to compare the expected result and the real result.
43
+ #
44
+ #Recommendation to build up your test.
45
+ #1. Create two folders: 'expected' and 'failure'
46
+ #2. Define your assertion with a non-existing filename
47
+ #3. Run the test with folder_for_failure = 'failure'
48
+ #4. You will get a failure (filename is missing).
49
+ #5. Copy the file in 'failure' to 'expected'
50
+ #6. Rerun again, you have no failure (hopefully ;-) )
51
+ def assert_equal_filecontent( filename, actual, folder_for_failure = "failure#{Date.today}", message = nil )
52
+ #
53
+ #Make the tests
54
+ if File.exist?(filename)
55
+ expected = File.read(filename)
56
+ full_message = build_message(message, "<?> expected (#{filename}) but was\n<?>.\n", expected, actual).to_s
57
+ else
58
+ full_message = "Reference file <#{filename}> missing"
59
+ end
60
+
61
+ #Write the real result to a file if a failure folder is given.
62
+ if folder_for_failure and expected != actual
63
+ File.makedirs(folder_for_failure) if ! File.directory?(folder_for_failure)
64
+ File.open( "#{folder_for_failure}/#{File.basename(filename)}", 'w'){|f|
65
+ f << actual
66
+ }
67
+ full_message << "\n\t-> Build <#{folder_for_failure}/#{File.basename(filename)}>"
68
+ end
69
+
70
+ if File.exist?(filename)
71
+ assert_block( full_message ){ expected == actual }
72
+ else
73
+ assert_block( full_message ){ false }
74
+ end
75
+ end
76
+ #
77
+ end
@@ -0,0 +1,186 @@
1
+ #
2
+ #Add some funcionality to check stdout and stderr-output with test/unit.
3
+ #
4
+ #Example:
5
+ # require 'assert_stdout'
6
+ # class MyTest < Test::Unit::TestCase
7
+ # #This is a method and I wnat to test the screen output.
8
+ # def any_output()
9
+ # puts "Just some output"
10
+ # end
11
+ # def test_stdout_ok()
12
+ # assert_stdout("Just some output\n", proc{ any_output()} )
13
+ # assert_stdout_block("Just some output\n"){ any_output() }
14
+ # end
15
+ # end
16
+ #
17
+ #Please be aware:
18
+ #Normally you should not test the output of methods, but the result of methods.
19
+ #
20
+ require 'test/unit'
21
+ module Test::Unit
22
+
23
+ #Class to catch the std output and stderr
24
+ class Catch_IO < String
25
+ #Receive content
26
+ def write( cont )
27
+ self << cont
28
+ end
29
+ end #MyIO
30
+
31
+ #
32
+ #Extend the class TestCase with additional methods/assertions
33
+ #
34
+ class TestCase
35
+ #Catch stdout for a given procedure (Proc-Element).
36
+ def catch_stdout( procedure )
37
+ $stdout = output = Catch_IO.new #temporary copy
38
+ procedure.call
39
+ $stdout = STDOUT
40
+ output
41
+ end
42
+ =begin rdoc
43
+ Compare the stdout-output with your exception.
44
+
45
+ Actual must be a procedure.
46
+ Another object (like a method call) would be executed first, before stdout could be redirected.
47
+
48
+ Example:
49
+ require 'assert_stdout'
50
+ class MyTest < Test::Unit::TestCase
51
+ def test_hello_world()
52
+ assert_stdout("Hello World\n", Proc.new{ puts 'Hello World'} )
53
+ end
54
+ end
55
+ =end
56
+ def assert_stdout( expected, actual, message = nil )
57
+ if actual.respond_to?(:call)
58
+ output = catch_stdout( actual )
59
+ full_message = build_message(message, "<?> expected in stdout, but was\n<?>.\n", expected, output)
60
+ assert_block( full_message ){ expected == output }
61
+ else
62
+ raise 'no Proc-object given'
63
+ full_message = build_message(message, "<?> expected in stdout, but no Proc-object is given to test.\n", expected )
64
+ assert_block( full_message ){ false } #force error
65
+ end
66
+ end
67
+ =begin rdoc
68
+ Compare the stdout-output of a given block with your exception.
69
+
70
+ Example:
71
+ require 'assert_stdout'
72
+ class MyTest < Test::Unit::TestCase
73
+ def test_hello_world()
74
+ assert_stdout_block("Hello World\n"){ puts 'Hello World'}
75
+ end
76
+ end
77
+ =end
78
+ def assert_stdout_block( expected, message = nil )
79
+ if block_given?
80
+ $stdout = output = Catch_IO.new #temporary copy
81
+ yield
82
+ $stdout = STDOUT
83
+ full_message = build_message(message, "<?> expected in stdout, but was\n<?>.\n", expected, output)
84
+ assert_block( full_message ){ expected == output }
85
+ else
86
+ raise 'no block given'
87
+ full_message = build_message(message, "<?> expected in stdout, but no block to test.\n", expected )
88
+ assert_block( full_message ){ false }
89
+ end
90
+ end
91
+ =begin rdoc
92
+ Catch stderr for a given procedure (Proc-Element).
93
+ =end
94
+ def catch_stderr( procedure )
95
+ $stderr = output = Catch_IO.new #temporary copy
96
+ procedure.call
97
+ $stderr = STDERR
98
+ output
99
+ end
100
+ =begin rdoc
101
+ Compare the stderr-output with your exception.
102
+
103
+ Actual must be a procedure.
104
+ Another object (like a method call) would be executed first, before stderr could be redirected.
105
+
106
+ Example:
107
+ require 'assert_stdout'
108
+ def puts_err( arg )
109
+ $stderr << arg
110
+ end
111
+
112
+ class MyTest < Test::Unit::TestCase
113
+ def test_hello_world()
114
+ assert_stderr("Hello World", Proc.new{ puts_err 'Hello World'} )
115
+ end
116
+ end
117
+ =end
118
+ def assert_stderr( expected, actual, message = nil )
119
+ if actual.respond_to?(:call)
120
+ output = catch_stderr( actual )
121
+ full_message = build_message(message, "<?> expected in stderr, but was\n<?>.\n", expected, output)
122
+ assert_block( full_message ){ expected == output }
123
+ else
124
+ raise 'no Proc-object given'
125
+ full_message = build_message(message, "<?> expected in stderr, but no Proc-object is given to test.\n", expected )
126
+ assert_block( full_message ){ false } #force error
127
+ end
128
+ end
129
+ =begin rdoc
130
+ Compare the stderr-output with your exception.
131
+
132
+ Example:
133
+ require 'assert_stdout'
134
+ def puts_err( arg )
135
+ $stderr << arg
136
+ end
137
+
138
+ class MyTest < Test::Unit::TestCase
139
+ def test_hello_world()
140
+ assert_stderr_block("Hello World"){ puts_err 'Hello World'}
141
+ end
142
+ end
143
+ =end
144
+ def assert_stderr_block( expected, message = nil )
145
+ if block_given?
146
+ $stderr = output = Catch_IO.new #temporary copy
147
+ yield
148
+ $stderr = STDERR
149
+ full_message = build_message(message, "<?> expected in stderr, but was\n<?>.\n", expected, output)
150
+ assert_block( full_message ){ expected == output }
151
+ else
152
+ raise 'no block given'
153
+ full_message = build_message(message, "<?> expected in stderr, but no block to test.\n", expected )
154
+ assert_block( full_message ){ false }
155
+ end
156
+ end
157
+ end #class TestCase
158
+ end #module Test::Unit
159
+
160
+ __END__
161
+
162
+ class MyIO < String
163
+ def write( cont )
164
+ self << cont
165
+ end
166
+ def self.catch_stdout( procedure )
167
+ $stdout = output = MyIO.new #temporary copy
168
+ procedure.call
169
+ $stdout = STDOUT
170
+ output
171
+ end
172
+ end #MyIO
173
+
174
+ #How to get stdout
175
+ puts 'vorher'
176
+ $stdout = output = MyIO.new #temporary copy
177
+ puts 'YY'
178
+ $stdout = STDOUT
179
+ puts 'nachher'
180
+ puts output
181
+
182
+
183
+ puts 'vorher'
184
+ output = MyIO.catch_stdout( proc{ puts 'xxYY' } )
185
+ puts 'nachher'
186
+ puts output
@@ -0,0 +1,105 @@
1
+ #
2
+ #Catch screen output.
3
+ #Can be used to avoid messages from programms called by system
4
+ #
5
+ #Example:
6
+ # require 'catch_output'
7
+ # include Catch_output
8
+ # stdout, stderr = catch_screen_output{
9
+ # %x{my_shell_application}
10
+ # }
11
+ #
12
+
13
+
14
+
15
+ #Class Tempfile for temporary redirection of stdout and stderr
16
+ require "tempfile"
17
+
18
+ #Catch screen output.
19
+ #Can be used to avoid messages from programms called by system
20
+ module Catch_output
21
+ STDOUT_ORIG = STDOUT.clone()
22
+ STDERR_ORIG = STDERR.clone()
23
+
24
+ #Catch stdout for the given block, but print stderr.
25
+ def catch_stdout( &block )
26
+ #~ raise 'no block' unless block_given?
27
+ raise 'no block' unless block.is_a?(Proc)
28
+ stdout, stderr = catch_screen_output( true, false, &block )
29
+ return stdout
30
+ end #catch_stdout()
31
+
32
+ #Catch stderr for the given block, but print stdout.
33
+ def catch_stderr( &block )
34
+ #~ raise 'no block' unless block_given?
35
+ raise 'no block' unless block.is_a?(Proc)
36
+ stdout, stderr = catch_screen_output( false, true, &block )
37
+ return stderr
38
+ end #catch_stderr()
39
+
40
+ #Catch the screen output (stdout and stderr) for the given block.
41
+ #You can set, which output you want to catch.
42
+ #
43
+ #Returnvalue is an array with the result of stdout and stderr.
44
+ #If any output wasn't catched, the return value in the array is nil.
45
+ def catch_screen_output(
46
+ catch_stdout = true,
47
+ catch_stderr = true,
48
+ stdout_orig = STDOUT.clone(),
49
+ stderr_orig = STDERR.clone()
50
+ )
51
+
52
+ raise 'no block' unless block_given?
53
+
54
+ if catch_stdout
55
+ #Create temporary file for stdout
56
+ tmpstdout = Tempfile.new( 'stdout')
57
+ #redirect stdout
58
+ STDOUT.reopen( tmpstdout )
59
+ end
60
+ if catch_stderr
61
+ #Create temporary file for stdout
62
+ tmpstderr = Tempfile.new( 'stderr')
63
+ #redirect stdout
64
+ STDERR.reopen( tmpstderr )
65
+ end
66
+
67
+
68
+ yield #Execute the block
69
+
70
+ if catch_stdout
71
+ #stdout is coming again to the screen.
72
+ tmpstdout.close()
73
+ STDOUT.reopen( stdout_orig)
74
+
75
+ # Get the result of stdout
76
+ tmpstdout.open()
77
+ stdout = tmpstdout.readlines().join
78
+ tmpstdout.close()
79
+ end
80
+
81
+
82
+ if catch_stderr
83
+ #stderr is coming again to the screen.
84
+ tmpstderr.close()
85
+ STDERR.reopen( stderr_orig)
86
+
87
+ # Get the result of stderr
88
+ tmpstderr.open()
89
+ stderr = tmpstderr.readlines().join
90
+ tmpstderr.close()
91
+ end
92
+ return [ stdout, stderr ]
93
+ end
94
+ end
95
+
96
+
97
+ #Frame to use module Catch_output
98
+ #
99
+ #Example:
100
+ # Frame_catch_output.catch_stdout{ puts 11 }
101
+ class Frame_catch_output
102
+ class << self
103
+ include Catch_output
104
+ end
105
+ end
data/readme.html ADDED
@@ -0,0 +1,40 @@
1
+ <!--
2
+
3
+ Build by C:/Program Files/ruby/lib/ruby/gems/1.8/gems/docgenerator-1.2.0/./lib/docgenerator/document.rb
4
+ Dir: C:/usr/Script/more_unit_test
5
+ Creator: rakefile.rb
6
+ Target: readme.html
7
+ 2010/01/02 01:00:41
8
+
9
+ Generation-Info-End
10
+ -->
11
+ <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
12
+ <html>
13
+ <head ></head>
14
+ <body ><p >
15
+ The more_unit_test-package extends test/unit with some additional tests:
16
+ </p>
17
+ <ul >
18
+ <li > assert_equal_filecontent.rb: Compare expected result with file content </li>
19
+ <li > assert_stdout.rb: test for stdout/stderr Test cases are added to the files directly. </li>
20
+ </ul>
21
+ <p >
22
+ In addition you get a function to catch outputs on stdout and stderr:
23
+ </p>
24
+ <ul >
25
+ <li > catch_output.rb </li>
26
+ </ul>
27
+ <h1 >Version history:</h1>
28
+ <p >
29
+ 0.0.1: Initial version.
30
+ </p>
31
+ <p >
32
+ 0.0.2:
33
+ </p>
34
+ <ul >
35
+ <li > modified folder structure </li>
36
+ <li > more documentation. </li>
37
+ <li > published at gemcutter </li>
38
+ </ul>
39
+ </body>
40
+ </html>
data/readme.txt ADDED
@@ -0,0 +1,19 @@
1
+
2
+
3
+ The more_unit_test-package extends test/unit with some additional tests:
4
+ - assert_equal_filecontent.rb: Compare expected result with file content
5
+ - assert_stdout.rb: test for stdout/stderr Test cases are added to the files directly.
6
+
7
+ In addition you get a function to catch outputs on stdout and stderr:
8
+ - catch_output.rb
9
+
10
+ Version history:
11
+ ------------------------------
12
+
13
+ 0.0.1: Initial version.
14
+
15
+ 0.0.2:
16
+ - modified folder structure
17
+ - more documentation.
18
+ - published at gemcutter
19
+
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require "../lib/assert_equal_filecontent.rb"
3
+
4
+ #Some content to be tested.
5
+ DUMMYTEXT = <<dummytext
6
+ Some text to be tested.
7
+ More text.
8
+ dummytext
9
+
10
+ #
11
+ #This is a test in the test -> we need test_assertions.rb, not only test/unit
12
+ #test_assertions.rb is not in path of tests -> full path, please adapt for your installation.
13
+ require "C:/Program Files/ruby/doc/ruby/ruby-1.8.6/test/testunit/test_assertions.rb"
14
+ #~ require 'test/testunit/test_assertions'
15
+
16
+ #
17
+ class MyTest < Test::Unit::TC_Assertions
18
+ def test_ok()
19
+ #Build reference data
20
+ File.makedirs('expected')
21
+ File.open('expected/test.txt', 'w'){|f| f << DUMMYTEXT }
22
+ #Make test
23
+ assert_equal_filecontent( 'expected/test.txt', DUMMYTEXT )
24
+ end
25
+ def test_no_reference_file()
26
+ check_fails(%r{Reference file <expected/not_available.txt> missing}){
27
+ assert_equal_filecontent( 'expected/not_available.txt', DUMMYTEXT, nil )
28
+ }
29
+ end
30
+ def test_build_reference_file()
31
+ #Initial expected file
32
+ File.makedirs('expected')
33
+ File.open('expected/test_build.txt', 'w'){|f| f << DUMMYTEXT*2 }
34
+ #
35
+ folder_for_failure = 'failure'
36
+ #~ assert_equal(false, File.exist?("#{folder_for_failure}/test.txt" ))
37
+
38
+ #First check. There is a difference, in failure we get the correct result
39
+ check_fails(%r{.*expected \(expected/test_build.txt\) but was.*-> Build <failure/test_build.txt}m){
40
+ assert_equal_filecontent( 'expected/test_build.txt', DUMMYTEXT, folder_for_failure )
41
+ }
42
+ #Check for file with correkt result
43
+ assert_equal(true, File.exist?("#{folder_for_failure}/test_build.txt" ))
44
+ #Check with new file (normally you would copy the file to 'expected'
45
+ assert_equal_filecontent( "#{folder_for_failure}/test_build.txt", DUMMYTEXT, nil )
46
+ end
47
+ end #class MyTest
@@ -0,0 +1,40 @@
1
+ require 'test/unit'
2
+ require '../lib/assert_stdout.rb'
3
+
4
+
5
+ class MyTest < Test::Unit::TestCase
6
+ def any_output()
7
+ puts "Just some output"
8
+ end
9
+ def test_stdout_ok()
10
+ assert_stdout("xx\n", proc{ puts 'xx' } )
11
+ assert_stdout("Just some output\n", proc{ any_output()} )
12
+ assert_stdout_block("xx\n"){ puts 'xx' }
13
+ assert_stdout_block("Just some output\n"){ any_output() }
14
+ assert_equal("xx\n", catch_stdout(proc{ puts 'xx' }) )
15
+ end
16
+ def test_stdout_err()
17
+ #Error: No Proc-object
18
+ assert_raise(RuntimeError){ assert_stdout("xx\n", 1+1) }
19
+ #Error: No Block
20
+ assert_raise(RuntimeError){ assert_stdout_block("xx\n") }
21
+ end
22
+ #Helper method to get content to stderr.
23
+ def any_output2stderr()
24
+ $stderr << "Just some output on stderr"
25
+ end
26
+ def test_stderr_ok()
27
+ #~ assert_stderr("xx\n", proc{ puts 'xx' } )
28
+ assert_stderr("Just some output on stderr", proc{ any_output2stderr()} )
29
+ #~ assert_stderr_block("xx\n"){ puts 'xx' }
30
+ assert_stderr_block("Just some output on stderr"){ any_output2stderr() }
31
+ #~ assert_equal("xx\n", catch_stderr(proc{ puts 'xx' }) )
32
+ assert_equal("Just some output on stderr", catch_stderr(proc{ any_output2stderr() } ) )
33
+ end
34
+ def test_stderr_err()
35
+ #Error: No Proc-object
36
+ assert_raise(RuntimeError){ assert_stderr("xx\n", 1+1) }
37
+ #Error: No Block
38
+ assert_raise(RuntimeError){ assert_stderr_block("xx\n") }
39
+ end
40
+ end #class MyTest
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: more_unit_test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Knut Lickert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-02 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: |
17
+ The more_unit_test-package extends test/unit with some additional tests:
18
+ - assert_equal_filecontent.rb: Compare expected result with file content
19
+ - assert_stdout.rb: test for stdout/stderr
20
+
21
+ email: knut@lickert.net
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - readme.txt
30
+ - readme.html
31
+ - lib/assert_equal_filecontent.rb
32
+ - lib/assert_stdout.rb
33
+ - lib/catch_output.rb
34
+ has_rdoc: true
35
+ homepage: http://gems.rubypla.net/
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Additional assertions for big text results.
62
+ test_files:
63
+ - unittest/test_assert_stdout.rb
64
+ - unittest/test_assert_equal_filecontent.rb