minitest-filecontent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2c6d3ba73fb0b3d7f25ea36866fe71a2b552aeb
4
+ data.tar.gz: eb92bfdeac003881f7cfd6627c5d5e22799e589b
5
+ SHA512:
6
+ metadata.gz: 028988b9d09eaff1b71c590d685511b02f6fb3720a21766354b1a96b5febf0d3626c2f9e054015ec97cc658071812420c109fdc7dd42f02b257d036bf8c733d3
7
+ data.tar.gz: 293534e549eccf6a8f7bb9658c1bef4b38365b3de5136d03b4aa4e7ef05b0ea99866aba1be2f11150d2a42f6a97b39f385f6d43ad4120a394c01f20ffa50447d
@@ -0,0 +1,37 @@
1
+ #encoding: utf-8
2
+ =begin rdoc
3
+ Example for usage of gem minitest-filecontent
4
+ =end
5
+ $:.unshift('../lib')
6
+ require 'minitest/filecontent'
7
+
8
+ #Some content to be tested.
9
+ DUMMYTEXT = <<dummytext
10
+ Some text to be tested.
11
+ More text.
12
+ dummytext
13
+
14
+ puts "===================="
15
+ puts "=Remark: This example works correct, if 2 of the 3 assertions fail"
16
+ puts "=After the execution, there should be a directory failure_<isodate> with real results"
17
+ puts "===================="
18
+
19
+ class TestExample < MiniTest::Test
20
+ def test_missing_reference_file
21
+ assert_equal_filecontent('not_existing_filename.txt', DUMMYTEXT, 'My test with a missing reference file works correct, if this error is reported')
22
+ end
23
+ def test_file
24
+ filename = '%s.txt' % __method__
25
+ File.open(filename, 'w'){|f| f << DUMMYTEXT }
26
+ assert_equal_filecontent(filename, DUMMYTEXT)
27
+ end
28
+ def test_file_difference
29
+ filename = '%s.txt' % __method__
30
+ File.open(filename, 'w'){|f|
31
+ f << DUMMYTEXT
32
+ f << "and some other text"
33
+ }
34
+ assert_equal_filecontent(filename, DUMMYTEXT, 'My test with a modified content works correct, if this error is reported')
35
+ end
36
+ end
37
+
@@ -0,0 +1,173 @@
1
+ #encoding: utf-8
2
+ =begin rdoc
3
+ =Gem MiniTest-Filecontent
4
+
5
+ Imagine you have a method and the result is a long text.
6
+
7
+ You can compare the result via assert_equal, but in case of
8
+ an error you must find the difference in the long text.
9
+
10
+ Wouldn't it be nice, you can store your expected result in a file and
11
+ in case of differences you get the real result in another file?
12
+ Now you can take your favorite file difference tool and can compare
13
+ the expected and the real result.
14
+
15
+ Here it is: MiniTest::Test#assert_equal_filecontent
16
+
17
+ =end
18
+
19
+ gem 'minitest', '>= 4.0.0'
20
+ require 'minitest/autorun'
21
+
22
+ require 'fileutils'
23
+ require 'date' #needed for Ruby 1.9 (class Date no standard?)
24
+
25
+
26
+ #
27
+ module MiniTest
28
+ module Filecontent
29
+ VERSION = '0.1.0'
30
+ end
31
+
32
+ =begin rdoc
33
+ Extend the class TestCase with additional methods/assertions:
34
+
35
+ * MiniTest::Test#assert_equal_filecontent
36
+ =end
37
+ class Test
38
+ #Default path in case of an error.
39
+ FOLDER_FOR_FAILURE = "failure_#{Date.today}"
40
+ #Default path in case of an error. Overwrites FOLDER_FOR_FAILURE.
41
+ #With 'false' no file is written
42
+ attr_writer :folder_for_failure
43
+ =begin rdoc
44
+ Takes the content of the file 'filename' and compares it with 'actual' like in assert_equal.
45
+ If 'filename' doesn't exist, the failure
46
+ Reference file <#{filename}> missing
47
+ is returned.
48
+
49
+ 'folder_for_failure' will contain all results with differences.
50
+
51
+ Example of the usage:
52
+ assert_equal_filecontent( "expected/test_section.html",
53
+ text.to_doc(:html),
54
+ 'short description of test'
55
+ )
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 (See FOLDER_FOR_FAILURE )
65
+ 5. The file 'test_section.html' with the result is created in FOLDER_FOR_FAILURE
66
+ 6. You can use a compare tool to compare the expected result and the real result.
67
+
68
+ If you don't want a failure file (step 4 and 5), you may suppress it with:
69
+ def text_no_failure file
70
+ self.folder_for_failure = false #inactivate error/reference file creation.
71
+ #...
72
+ end
73
+
74
+ ==Recommendation to build up your test.
75
+ 1. Define your tests with your assertions.
76
+ 2. Run the test
77
+ 3. You will get errors 'Reference file <xx/not_available.txt> missing'
78
+ and a directory 'failure_<date>
79
+ 4. Rename the folder 'failure' to 'expected'
80
+ 5. Check, if the content of the folder is the wanted result.
81
+ 6. Rerun again, you have no failure (hopefully ;-) )
82
+
83
+ If you already have the result, you may start like this:
84
+ 1. Create a folder: 'expected'
85
+ 2. Define your assertion with non-existing filename in the 'expected'-folder.
86
+ 3. Copy the expected results to the 'expected'-folder.
87
+ 4. Run the test
88
+ 5. In case of errors:
89
+ Compare the content of the 'expected'-folder with the failure-folder.
90
+ Adapt your code or your expected result.
91
+ 6. Rerun again, until you get the expected results
92
+
93
+ ==filename
94
+ The filename may contains some parameters in <x>.
95
+ Details see MiniTest::Test#build_filename
96
+
97
+ =end
98
+ def assert_equal_filecontent( filename, actual, message = nil )
99
+
100
+ filename = build_filename(filename)
101
+ #Set encoding for the file
102
+ encoding = actual.encoding if actual.respond_to?(:encoding)
103
+ encoding ||= 'Binary'
104
+
105
+ full_message = []
106
+ full_message << message if message
107
+
108
+ expected = nil
109
+ if File.exist?(filename)
110
+ File.open(filename, 'r', :external_encoding => encoding ){|file| expected = file.read }
111
+ #This message is only used in case of a difference
112
+ full_message << "Result differs to file content #{filename}"
113
+ else
114
+ full_message << "Reference file <#{filename}> is missing"
115
+ end
116
+
117
+ #Write the real result to a file if a failure folder is given.
118
+ if @folder_for_failure != false and expected != actual
119
+ folder_for_failure = @folder_for_failure || FOLDER_FOR_FAILURE
120
+ FileUtils.makedirs(folder_for_failure) unless File.directory?(folder_for_failure)
121
+ File.open( "#{folder_for_failure}/#{File.basename(filename)}", 'w', :external_encoding => encoding){|f|
122
+ f << actual
123
+ }
124
+ full_message << "\t-> Build <#{folder_for_failure}/#{File.basename(filename)}>"
125
+ end
126
+
127
+
128
+ assert( expected == actual, full_message.join("\n") )
129
+
130
+ end #assert_equal_filecontent( filename, actual, message = nil )
131
+
132
+ =begin rdoc
133
+ Make filename conversion.
134
+
135
+ The filename may contains some parameters in <x>. _x_ may be:
136
+ * c - test class name
137
+ * m - methodname from caller[level]
138
+ * cm - test class name - methodname
139
+
140
+ Parameter 'level' is needed to get the method.
141
+
142
+ Example:
143
+
144
+ class MyTest < Test::Unit::TestCase
145
+ def test_xx
146
+ assert_equal_filecontent( "<m>.txt", mycode )
147
+ end
148
+ end
149
+
150
+ The caller stack will be
151
+ ___lib/more_unit_test/assert_equal_filecontent.rb:112:in `assert_equal_filecontent'
152
+ quicktest.rb:13:in `test_xx'
153
+ ___test-unit-2.4.0/lib/test/unit/testcase.rb:531:in `run_test'
154
+
155
+ The result with default level 1 is 'test_xx' - the method from your test.
156
+
157
+ =end
158
+ def build_filename(filename, level = 1)
159
+ method = caller[level].match(/^(.+?):(\d+)(?::in `(.*)')?/)[3] #methodname
160
+ filename.gsub(/<.*?>/){ |hit|
161
+ case hit
162
+ when '<c>'; self.class.name
163
+ when '<m>'; method
164
+ when '<cm>'; "#{self.class}-#{method}" #methodname
165
+ else; raise ArgumentError, "Undefined option #{hit} in filename #{filename}"
166
+ end
167
+ }.sub(/block (\(\d+ levels\) )?in /,'')
168
+ end #
169
+ end #class Test
170
+ end #Minitest
171
+
172
+ __END__
173
+
@@ -0,0 +1,24 @@
1
+ =MiniTest-Filecontent
2
+
3
+ Compare expected result with content of a file.
4
+
5
+ Usefull, if you compare long strings or generated texts in your tests.
6
+
7
+ See MiniTest::Test for details.
8
+
9
+
10
+ =Related Gems
11
+
12
+ ==Minitest
13
+ MiniTest-Filecontent is a extension of Minitest
14
+
15
+ * https://rubygems.org/gems/minitest
16
+
17
+
18
+
19
+ ==Minitest::Filesystem
20
+ Check content of a filesystem.
21
+ This gem checks the existence of files and directory structures.
22
+ There is no check and comparison of the content of files.
23
+
24
+ * https://rubygems.org/gems/minitest-filesystem
@@ -0,0 +1,182 @@
1
+ #encoding: utf-8
2
+ =begin rdoc
3
+ Tests for minitest-filecontent
4
+ =end
5
+ $:.unshift('../lib')
6
+ require 'minitest/filecontent'
7
+
8
+ #Some content to be tested.
9
+ DUMMYTEXT = <<dummytext
10
+ Some text to be tested.
11
+ More text.
12
+ dummytext
13
+
14
+ DUMMYTEXT_utf = <<dummytext
15
+ Some text to be tested.
16
+ More text.
17
+
18
+ Umlauts: öäü
19
+ dummytext
20
+
21
+ #
22
+ #This two directories will be created.
23
+ #
24
+ $expected = File.dirname(__FILE__) + '/tmp_expected'
25
+ $folder_for_failure = File.dirname(__FILE__) + '/tmp_failure'
26
+
27
+
28
+
29
+ #
30
+ class Test_equal_filecontent_filename < MiniTest::Test
31
+ def test_normal
32
+ assert_equal('x', build_filename('x'))
33
+ end
34
+ def test_c
35
+ assert_equal(self.class.to_s, build_filename('<c>'))
36
+ end
37
+ def test_m
38
+ assert_equal(__method__.to_s, build_filename('<m>',0))
39
+ assert_equal('test_m', build_filename('<m>',0))
40
+ end
41
+ def test_cm
42
+ assert_equal("#{self.class}-#{__method__}", build_filename('<cm>',0))
43
+ end
44
+
45
+ #test if the filename is taken from deeper.
46
+ def my_cascaded_test(methodname, level=0)
47
+ assert_equal(methodname.to_s, build_filename('<m>',level), 'The filename is not generated by the calling process')
48
+ end
49
+ def my_cascaded_test2(methodname, level)
50
+ my_cascaded_test(methodname, level)
51
+ end
52
+ def test_m_cascaded
53
+ my_cascaded_test('my_cascaded_test')
54
+ my_cascaded_test(__method__,1)
55
+ my_cascaded_test2(__method__,2)
56
+ end
57
+ end #Test_equal_filecontent_filename
58
+
59
+ =begin
60
+ This is a test in the test (we test with MiniTest the behavour of minitest.
61
+
62
+ Example:
63
+
64
+ msg = assert_raises( Minitest::Assertion ){
65
+ assert_equal_filecontent( "#{$expected}/test_build.txt", DUMMYTEXT )
66
+ }
67
+
68
+ assert_raises checks, if the check assert_equal_filecontent throws an test-exception.
69
+ This exception (msg) can be analysed.
70
+ =end
71
+ class Test_equal_filecontent < MiniTest::Test
72
+
73
+ #~ Minitest::Assertion = Test::Unit::Minitest::Assertion
74
+
75
+ #Build reference data
76
+ def self.before_tests()
77
+ FileUtils.makedirs($expected)
78
+ File.open("#{$expected}/test.txt", 'w'){|f| f << DUMMYTEXT }
79
+ File.open("#{$expected}/test_utf.txt", 'w:utf-8'){|f| f << DUMMYTEXT_utf }
80
+ File.open("#{$expected}/test_build.txt", 'w'){|f| f << DUMMYTEXT*2 }
81
+ end
82
+ before_tests() #init
83
+ def self.after_tests
84
+ ###FIXME does not work
85
+
86
+ FileUtils.rmtree($expected)
87
+ FileUtils.rmtree($folder_for_failure)
88
+ FileUtils.rmtree(FOLDER_FOR_FAILURE)
89
+ end
90
+
91
+ def test_ok()
92
+ #~ #Make test
93
+ assert_equal_filecontent( "#{$expected}/test.txt", DUMMYTEXT )
94
+ end
95
+
96
+ def test_utf()
97
+ assert_equal_filecontent( "#{$expected}/test_utf.txt", DUMMYTEXT_utf )
98
+ end
99
+
100
+ #Check error
101
+ def test_no_reference_file()
102
+ self.folder_for_failure = false #inactivate error/reference file creation.
103
+ #Check exception and get the message
104
+ msg = assert_raises( Minitest::Assertion ){
105
+ assert_equal_filecontent( "#{$expected}/not_available.txt", DUMMYTEXT )
106
+ }
107
+ assert_instance_of(Minitest::Assertion, msg)
108
+ assert_equal( "Reference file <#{$expected}/not_available.txt> is missing", msg.to_s)
109
+ end
110
+
111
+ #Check, if "Failure" is created in default path
112
+ def test_build_reference_file_default_path()
113
+
114
+ #First check. There is a difference, in failure we get the correct result
115
+ msg = assert_raises( Minitest::Assertion ){
116
+ assert_equal_filecontent( "#{$expected}/test_build.txt", DUMMYTEXT )
117
+ }
118
+ assert_match( %r{Result differs to file content #{$expected}/test_build.txt\n\t-> Build <#{FOLDER_FOR_FAILURE}/test_build.txt}m,
119
+ msg.to_s, 'Difference message is wrong' )
120
+
121
+ #Check for file with correkt result
122
+ assert(File.exist?("#{FOLDER_FOR_FAILURE}/test_build.txt"), 'Reference file is not created')
123
+ #Check with new file (normally you would copy the file to $expected
124
+ assert_equal_filecontent( "#{FOLDER_FOR_FAILURE}/test_build.txt", DUMMYTEXT, nil )
125
+ end
126
+
127
+ #Check, if "Failure" is created in other path
128
+ def test_build_reference_file_default_path()
129
+ self.folder_for_failure = $folder_for_failure #set other failure folder for assertion in this test
130
+
131
+ #First check. There is a difference, in failure we get the correct result
132
+ msg = assert_raises( Minitest::Assertion ){
133
+ assert_equal_filecontent( "#{$expected}/test_build.txt", DUMMYTEXT )
134
+ }
135
+ assert_match( %r{Result differs to file content #{$expected}/test_build.txt\n\t-> Build <#{$folder_for_failure}/test_build.txt}m,
136
+ msg.to_s, 'Difference message is wrong' )
137
+
138
+ #Check for file with correkt result
139
+ assert(File.exist?("#{$folder_for_failure}/test_build.txt"), 'Reference file is not created')
140
+ #Check with new file (normally you would copy the file to $expected
141
+ assert_equal_filecontent( "#{$folder_for_failure}/test_build.txt", DUMMYTEXT, nil )
142
+ end
143
+
144
+ def test_build_reference_file_parameter_error()
145
+ #First check. There is a difference, in failure we get the correct result
146
+ msg = assert_raises( ArgumentError ){
147
+ assert_equal_filecontent( "<??>.txt", DUMMYTEXT )
148
+ }
149
+ end
150
+
151
+ def test_build_reference_file_parameter_c()
152
+ filename = self.class.name
153
+ #First check. There is a difference, in failure we get the correct result
154
+ msg = assert_raises( Minitest::Assertion ){
155
+ assert_equal_filecontent( "#{$expected}/<c>.txt", DUMMYTEXT )
156
+ }
157
+ assert_match( %r{Reference file <#{$expected}/#{filename}.txt> is missing.*-> Build <#{FOLDER_FOR_FAILURE}/#{filename}.txt}m,
158
+ msg.to_s )
159
+
160
+ #Check for file with correkt result
161
+ assert(File.exist?("#{FOLDER_FOR_FAILURE}/#{filename}.txt" ))
162
+ #Check with new file (normally you would copy the file to $expected
163
+ assert_equal_filecontent( "#{FOLDER_FOR_FAILURE}/#{filename}.txt", DUMMYTEXT, nil )
164
+ end
165
+
166
+ def test_build_reference_file_parameter_m()
167
+ filename = __method__
168
+ #First check. There is a difference, in failure we get the correct result
169
+ msg = assert_raises( Minitest::Assertion ){
170
+ assert_equal_filecontent( "#{$expected}/<m>.txt", DUMMYTEXT )
171
+ }
172
+ assert_match( %r{Reference file <#{$expected}/#{filename}.txt> is missing.*-> Build <#{FOLDER_FOR_FAILURE}/#{filename}.txt}m,
173
+ msg.to_s )
174
+
175
+
176
+ #Check for file with correkt result
177
+ assert(File.exist?("#{FOLDER_FOR_FAILURE}/#{filename}.txt" ))
178
+ #Check with new file (normally you would copy the file to $expected
179
+ assert_equal_filecontent( "#{FOLDER_FOR_FAILURE}/#{filename}.txt", DUMMYTEXT, nil )
180
+ end
181
+
182
+ end #Test_equal_filecontent
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-filecontent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Knut Lickert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: |
28
+ Support unit tests with expectations in files
29
+ email: knut@lickert.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - readme.rdoc
34
+ files:
35
+ - examples/example_minitest_filecontent.rb
36
+ - lib/minitest/filecontent.rb
37
+ - readme.rdoc
38
+ - unittest/test_minitest_filecontent.rb
39
+ homepage:
40
+ licenses: []
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options:
44
+ - "--main"
45
+ - readme.rdoc
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.4.5
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Support unit tests with expectations in files
64
+ test_files:
65
+ - unittest/test_minitest_filecontent.rb