more_unit_test 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,47 +12,75 @@ the expected and the real result.
12
12
  Here it is: Test::Unit::TestCase#assert_equal_filecontent
13
13
 
14
14
  See http://rubyforge.org/tracker/?func=detail&aid=19509&group_id=5650&atid=21859
15
+
16
+ ==Ruby 1.9
17
+ With ruby 1.9 you get an additional encoding problem.
18
+ The "expected"-file is read with a default encoding,
19
+ your test result may differ.
20
+
21
+ You can set the default encoding for reading files with
22
+ Encoding.default_external
23
+
24
+ Or you define a read option for the test:
25
+ class MyTest < Test::Unit::TestCase
26
+ def test_utf()
27
+ self.readoption = 'r:utf-8'
28
+ assert_equal_filecontent( "expected/test_utf.txt", mycall() )
29
+ end
30
+ end
31
+
15
32
  =end
16
33
  require 'test/unit'
17
- require 'ftools'
18
- #
19
- #Extend the class TestCase with additional methods/assertions
20
- #
34
+ #~ puts RUBY_VERSION
35
+ #~ require 'ftools' #not in Ruby 1.9
36
+ require 'fileutils'
37
+ require 'date' #needed for Ruby 1.9 (class Date no standard?)
38
+
39
+ =begin rdoc
40
+ Extend the class TestCase with additional methods/assertions
41
+ =end
21
42
  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 ;-) )
43
+ #define option to read "expected"-file. may define conversions.
44
+ attr_writer :readoption
45
+
46
+ =begin rdoc
47
+ Takes the content of the file 'filename' and compares it with 'actual' like in assert_equal.
48
+ If 'filname' doesn't exist, the failure
49
+ Reference file <#{filename}> missing
50
+ is returned.
51
+
52
+ 'folder_for_failure' will contain all results with differences.
53
+
54
+ Example of the usage:
55
+ assert_equal_filecontent( "expected/test_section.html", text.to_doc(:html))
56
+
57
+ What will happen:
58
+ 1. text.to_doc(:html) is the test. It creates some HTML-Text
59
+ 2. "expected/test_section.html" is read and compared to text.to_doc(:html)
60
+ 1. If the file is missing -> error
61
+ 2. If it is the same -> fine
62
+ 3. If there are differences:
63
+ 3. A message is given (like in assert_equal)
64
+ 4. A folder "failure#{Date.today}" is created if not already exist
65
+ 5. The file 'test_section.html' with the result is created in "failure#{Date.today}/"
66
+ 6. I can use a compare tool to compare the expected result and the real result.
67
+
68
+ Recommendation to build up your test.
69
+ 1. Create two folders: 'expected' and 'failure'
70
+ 2. Define your assertion with a non-existing filename
71
+ 3. Run the test with folder_for_failure = 'failure'
72
+ 4. You will get a failure (filename is missing).
73
+ 5. Copy the file in 'failure' to 'expected'
74
+ 6. Rerun again, you have no failure (hopefully ;-) )
75
+ =end
51
76
  def assert_equal_filecontent( filename, actual, folder_for_failure = "failure#{Date.today}", message = nil )
52
77
  #
78
+ @readoption = 'r' unless @readoption
53
79
  #Make the tests
54
80
  if File.exist?(filename)
55
- expected = File.read(filename)
81
+ #~ expected = File.read(filename)
82
+ expected = nil
83
+ File.open(filename, @readoption){|file| expected = file.read }
56
84
  full_message = build_message(message, "<?> expected (#{filename}) but was\n<?>.\n", expected, actual).to_s
57
85
  else
58
86
  full_message = "Reference file <#{filename}> missing"
@@ -60,7 +88,7 @@ class Test::Unit::TestCase
60
88
 
61
89
  #Write the real result to a file if a failure folder is given.
62
90
  if folder_for_failure and expected != actual
63
- File.makedirs(folder_for_failure) if ! File.directory?(folder_for_failure)
91
+ FileUtils.makedirs(folder_for_failure) if ! File.directory?(folder_for_failure)
64
92
  File.open( "#{folder_for_failure}/#{File.basename(filename)}", 'w'){|f|
65
93
  f << actual
66
94
  }
@@ -68,9 +96,22 @@ class Test::Unit::TestCase
68
96
  end
69
97
 
70
98
  if File.exist?(filename)
71
- assert_block( full_message ){ expected == actual }
99
+ #~ assert_block( full_message ){ expected == actual }
100
+ case RUBY_VERSION
101
+ when /^1\.8/
102
+ assert_block( full_message ){ expected == actual }
103
+ when /^1.9/
104
+ #ruby 1.9: Add "\nExpected block to return true value." to message with assert_block
105
+ assert( expected == actual, full_message )
106
+ end
72
107
  else
73
- assert_block( full_message ){ false }
108
+ case RUBY_VERSION
109
+ when /^1\.8/
110
+ assert_block( full_message ){ false }
111
+ when /^1.9/
112
+ #ruby 1.9: Add "\nExpected block to return true value." to message with assert_block
113
+ assert( false, full_message )
114
+ end
74
115
  end
75
116
  end
76
117
  #
@@ -21,12 +21,12 @@ require 'test/unit'
21
21
  module Test::Unit
22
22
 
23
23
  #Class to catch the std output and stderr
24
- class Catch_IO < String
24
+ class Catch_IO < String
25
25
  #Receive content
26
- def write( cont )
27
- self << cont
28
- end
29
- end #MyIO
26
+ def write( cont )
27
+ self << cont
28
+ end
29
+ end #MyIO
30
30
 
31
31
  #
32
32
  #Extend the class TestCase with additional methods/assertions
@@ -76,10 +76,14 @@ Example:
76
76
  end
77
77
  =end
78
78
  def assert_stdout_block( expected, message = nil )
79
+ previous_stdout = $stdout
80
+ previous_STDOUT = STDOUT
79
81
  if block_given?
80
82
  $stdout = output = Catch_IO.new #temporary copy
83
+ #~ STDOUT.reopen($stdout)
81
84
  yield
82
- $stdout = STDOUT
85
+ $stdout = previous_stdout
86
+ #~ STDOUT.reopen(previous_STDOUT)
83
87
  full_message = build_message(message, "<?> expected in stdout, but was\n<?>.\n", expected, output)
84
88
  assert_block( full_message ){ expected == output }
85
89
  else
@@ -159,17 +163,17 @@ end #module Test::Unit
159
163
 
160
164
  __END__
161
165
 
162
- class MyIO < String
163
- def write( cont )
164
- self << cont
165
- end
166
+ class MyIO < String
167
+ def write( cont )
168
+ self << cont
169
+ end
166
170
  def self.catch_stdout( procedure )
167
171
  $stdout = output = MyIO.new #temporary copy
168
172
  procedure.call
169
173
  $stdout = STDOUT
170
174
  output
171
175
  end
172
- end #MyIO
176
+ end #MyIO
173
177
 
174
178
  #How to get stdout
175
179
  puts 'vorher'
@@ -1,7 +1,7 @@
1
1
  =begin rdoc
2
2
  ==Extend test/unit
3
3
  The more_unit_test-package extends test/unit with some additional tests
4
- to hande staou/stderr
4
+ to hande stdout/stderr
5
5
 
6
6
  ===more_unit_test/assert_equal_filecontent.rb
7
7
 
data/readme.rd ADDED
@@ -0,0 +1,32 @@
1
+ =Extend test/unit
2
+ The more_unit_test-package extends test/unit with some additional tests
3
+ to hande staou/stderr
4
+
5
+ ==more_unit_test/assert_equal_filecontent.rb
6
+
7
+ Compare expected result with file content
8
+ ==more_unit_test/assert_stdout.rb
9
+ Test for stdout/stderr
10
+
11
+
12
+
13
+ =Version history:
14
+ 0.0.1:
15
+ * Initial version.
16
+
17
+ 0.0.2:
18
+ * modified folder structure
19
+ * more documentation.
20
+ * published at gemcutter
21
+
22
+ 0.1.0 2010-02-07
23
+ * Restructured lib-directory
24
+ * Restructured the tests (using knut_tools/rake/testtask.rb)
25
+ * removed catch_output (is available in knut_tools.gem)
26
+
27
+ 0.1.1 2010-05-03
28
+ * first steps to Ruby 1.9
29
+
30
+ 0.1.2 2011-02-17
31
+ * Add readoption for read options of expected-file
32
+
@@ -1,4 +1,13 @@
1
+ #~ #encoding: utf-8
2
+ gem 'test-unit', '>= 2.1.1' #defines omit, pend...
1
3
  require 'test/unit'
4
+ if $0 == __FILE__
5
+ if RUBY_VERSION == '1.9.1'
6
+ $:.unshift("../lib")
7
+ else
8
+ $: << "../lib"
9
+ end
10
+ end
2
11
  require "more_unit_test/assert_equal_filecontent.rb"
3
12
 
4
13
  #Some content to be tested.
@@ -7,6 +16,13 @@ Some text to be tested.
7
16
  More text.
8
17
  dummytext
9
18
 
19
+ DUMMYTEXT_utf = <<dummytext
20
+ Some text to be tested.
21
+ More text.
22
+
23
+ Umlauts: öäü
24
+ dummytext
25
+
10
26
  #
11
27
  #This two directories will be created.
12
28
  #
@@ -21,32 +37,65 @@ $folder_for_failure = File.dirname(__FILE__) + '/tmp_failure'
21
37
 
22
38
  #
23
39
  class MyTest < Test::Unit::TestCase
24
- #~ class MyTest < Test::Unit::TC_Assertions #-> get check fails
40
+
41
+ #Machte Probleme ohne gem laden
42
+ #~ case RUBY_VERSION
43
+ #~ when /1.9/
44
+ #~ AssertionFailedError = MiniTest::Assertion
45
+ #~ when /1.8/
46
+ #~ AssertionFailedError = Test::Unit::AssertionFailedError
47
+ #~ else
48
+ #~ puts "!Check Ruby-Version! #{RUBY_VERSION }"
49
+ #~ AssertionFailedError = Test::Unit::AssertionFailedError
50
+ #~ end
51
+ AssertionFailedError = Test::Unit::AssertionFailedError
52
+
25
53
  def test_ok()
26
54
  #Build reference data
27
- File.makedirs($expected)
55
+ self.readoption = 'r:cp1252' unless RUBY_VERSION <= '1.8'
56
+ FileUtils.makedirs($expected)
28
57
  File.open("#{$expected}/test.txt", 'w'){|f| f << DUMMYTEXT }
29
58
  #Make test
30
59
  assert_equal_filecontent( "#{$expected}/test.txt", DUMMYTEXT )
31
60
  end
32
- #Check error
61
+
62
+ def test_utf()
63
+ omit_if(RUBY_VERSION <= '1.8', "No encoding check for ruby #{RUBY_VERSION}")
64
+ #Build reference data
65
+ FileUtils.makedirs($expected)
66
+ File.open("#{$expected}/test_utf.txt", 'w:utf-8'){|f| f << DUMMYTEXT_utf }
67
+ #Make test
68
+ self.readoption = 'r:utf-8'
69
+ assert_equal_filecontent( "#{$expected}/test_utf.txt", DUMMYTEXT_utf )
70
+ end
71
+
72
+ #Check error
33
73
  def test_no_reference_file()
34
74
 
35
75
  #Check exception and get the message
36
- msg = assert_raise( Test::Unit::AssertionFailedError ){
76
+ msg = assert_raise( AssertionFailedError ){
37
77
  assert_equal_filecontent( "#{$expected}/not_available.txt", DUMMYTEXT, nil )
38
78
  }
39
- assert_equal( "Reference file <#{$expected}/not_available.txt> missing", msg.to_s)
79
+ #fixme 1.9 assert_block ändert wert
80
+ case RUBY_VERSION
81
+ when /1.9/
82
+ pend("Check for Ruby-Version #{RUBY_VERSION }")
83
+ assert_equal( "Reference file <#{$expected}/not_available.txt> missing.\nExpected block to return true value.", msg.to_s)
84
+ when /1.8/
85
+ assert_equal( "Reference file <#{$expected}/not_available.txt> missing", msg.to_s)
86
+ else
87
+ notify("Ruby-Version #{RUBY_VERSION } undefined")
88
+ end
40
89
  end
41
90
  #Check, if "Failure" is created.
42
91
  def test_build_reference_file()
43
92
  #Initial expected file
44
- File.makedirs($expected)
93
+ FileUtils.makedirs($expected)
45
94
  File.open("#{$expected}/test_build.txt", 'w'){|f| f << DUMMYTEXT*2 }
46
95
  #
47
96
 
48
97
  #First check. There is a difference, in failure we get the correct result
49
- msg = assert_raise( Test::Unit::AssertionFailedError ){
98
+ msg = assert_raise( AssertionFailedError ){
50
99
  assert_equal_filecontent( "#{$expected}/test_build.txt", DUMMYTEXT, $folder_for_failure )
51
100
  }
52
101
  assert_match( %r{.*expected \(#{$expected}/test_build.txt\) but was.*-> Build <#{$folder_for_failure}/test_build.txt}m,
@@ -1,4 +1,7 @@
1
+ gem 'test-unit'
1
2
  require 'test/unit'
3
+
4
+ $:.unshift('../lib') if $0 == __FILE__
2
5
  require 'more_unit_test/assert_stdout.rb'
3
6
 
4
7
 
@@ -38,3 +41,37 @@ class MyTest < Test::Unit::TestCase
38
41
  assert_raise(RuntimeError){ assert_stderr_block("xx\n") }
39
42
  end
40
43
  end #class MyTest
44
+
45
+
46
+
47
+ require 'log4r'
48
+ require 'tempfile'
49
+ =begin rdoc
50
+ This gem does not work with log4r.
51
+
52
+ Check Log4rTest#test_alternative as another solution.
53
+ =end
54
+ class Log4rTest < Test::Unit::TestCase
55
+ def test_not_working()
56
+ log = Log4r::Logger.new('testlog')
57
+ log.outputters << Log4r::StdoutOutputter.new('log_stdout')
58
+ #Geht nicht. Wohin schreibt log4r?
59
+ #~ assert_stdout("[INFO ]testlog : a\n", proc{ log.info('a') })
60
+ #~ assert_stdout_block("[INFO ]testlog : a\n"){ log.info('a') }
61
+ end
62
+
63
+ def test_alternative()
64
+ log = Log4r::Logger.new('testlog')
65
+ begin
66
+ io = Tempfile.new(self.object_id)
67
+ rescue ArgumentError
68
+ omit("Tempfile error (ruby 1.9.2)")
69
+ end
70
+ log.outputters << Log4r::IOOutputter.new('log_stdout', io)
71
+ log.info('a')
72
+ assert_equal(" INFO testlog: a\n", io.open.read.to_s)
73
+ io.rewind #clear previous content
74
+ log.info('a')
75
+ assert_equal(" INFO testlog: a\n", io.open.read.to_s)
76
+ end
77
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: more_unit_test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
5
11
  platform: ruby
6
12
  authors:
7
13
  - Knut Lickert
@@ -9,19 +15,37 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-02-07 00:00:00 +01:00
18
+ date: 2011-02-17 00:00:00 +01:00
13
19
  default_executable:
14
20
  dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: test-unit
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
15
35
  - !ruby/object:Gem::Dependency
16
36
  name: knut_tools
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
20
40
  requirements:
21
41
  - - ">="
22
42
  - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
23
46
  version: "0"
24
- version:
47
+ type: :development
48
+ version_requirements: *id002
25
49
  description: |
26
50
  The more_unit_test-package extends test/unit with some additional tests:
27
51
  - assert_equal_filecontent.rb: Compare expected result with file content
@@ -32,40 +56,47 @@ executables: []
32
56
 
33
57
  extensions: []
34
58
 
35
- extra_rdoc_files: []
36
-
59
+ extra_rdoc_files:
60
+ - readme.rd
37
61
  files:
38
- - readme.txt
39
- - readme.html
62
+ - readme.rd
40
63
  - lib/more_unit_test.rb
41
64
  - lib/more_unit_test/assert_equal_filecontent.rb
42
65
  - lib/more_unit_test/assert_stdout.rb
66
+ - unittest/test_assert_stdout.rb
67
+ - unittest/test_assert_equal_filecontent.rb
43
68
  has_rdoc: true
44
- homepage: http://gems.rubypla.net/
69
+ homepage: http://gems.rubypla.net/more_unit_test/
45
70
  licenses: []
46
71
 
47
72
  post_install_message:
48
73
  rdoc_options:
49
74
  - --main
50
- - lib/more_unit_test.rb
75
+ - readme.rd
51
76
  require_paths:
52
77
  - lib
53
78
  required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
54
80
  requirements:
55
81
  - - ">="
56
82
  - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
57
86
  version: "0"
58
- version:
59
87
  required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
60
89
  requirements:
61
90
  - - ">="
62
91
  - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
63
95
  version: "0"
64
- version:
65
96
  requirements: []
66
97
 
67
98
  rubyforge_project: more_unit_test
68
- rubygems_version: 1.3.5
99
+ rubygems_version: 1.3.7
69
100
  signing_key:
70
101
  specification_version: 3
71
102
  summary: Additional assertions for big text results.
data/readme.html DELETED
@@ -1,48 +0,0 @@
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_more_unit_test.rb
6
- Target: readme.html
7
- 2010/02/06 20:03:43
8
-
9
- Generation-Info-End
10
- -->
11
- <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
12
- <html>
13
- <head ></head>
14
- <body ><h1 >Extend test/unit</h1>
15
- <p >
16
- The more_unit_test-package extends test/unit with some additional tests to hande staou/stderr
17
- </p>
18
- <h2 >more_unit_test/assert_equal_filecontent.rb</h2>
19
- <p >
20
- Compare expected result with file content
21
- </p>
22
- <h2 >more_unit_test/assert_stdout.rb</h2>
23
- <p >
24
- Test for stdout/stderr
25
- </p>
26
- <h1 >Version history:</h1>
27
- <p >
28
- 0.0.1: Initial version.
29
- </p>
30
- <p >
31
- 0.0.2:
32
- </p>
33
- <ul >
34
- <li > modified folder structure </li>
35
- <li > more documentation. </li>
36
- <li > published at gemcutter </li>
37
- </ul>
38
- <p >
39
- 0.1.0
40
- </p>
41
- <ul >
42
- <li > Restructured lib-directory </li>
43
- <li > Restructured the tests (using knut_tools/rake/testtask.rb) </li>
44
- <li > removed catch_output (is available in knut_tools.gem) </li>
45
- <li > added testtask.rb </li>
46
- </ul>
47
- </body>
48
- </html>
data/readme.txt DELETED
@@ -1,33 +0,0 @@
1
-
2
-
3
- Extend test/unit
4
- ------------------------------
5
-
6
- The more_unit_test-package extends test/unit with some additional tests to hande staou/stderr
7
-
8
- more_unit_test/assert_equal_filecontent.rb
9
- ------------------------------
10
-
11
- Compare expected result with file content
12
-
13
- more_unit_test/assert_stdout.rb
14
- ------------------------------
15
-
16
- Test for stdout/stderr
17
-
18
- Version history:
19
- ------------------------------
20
-
21
- 0.0.1: Initial version.
22
-
23
- 0.0.2:
24
- - modified folder structure
25
- - more documentation.
26
- - published at gemcutter
27
-
28
- 0.1.0
29
- - Restructured lib-directory
30
- - Restructured the tests (using knut_tools/rake/testtask.rb)
31
- - removed catch_output (is available in knut_tools.gem)
32
- - added testtask.rb
33
-