yamltest 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == yamltest 0.5 2009-01-21
2
+
3
+ * 1 major improvement
4
+ * First release after extraction from zena (http://zenadmin.org). [gaspard]
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/yamltest.rb
6
+ script/console
7
+ script/destroy
8
+ script/generate
9
+ test/gem_test
10
+ test/gem_test/foo.yml
11
+ test/gem_test/simple_test.rb
12
+ test/test_helper.rb
13
+ test/test_yamltest.rb
14
+ test/yamltest/simple.yml
15
+ test/zoo/complicated.yml
data/README.rdoc ADDED
@@ -0,0 +1,62 @@
1
+ = yamltest
2
+
3
+ * http://github.com/zena/yamltest/tree
4
+
5
+ == DESCRIPTION:
6
+
7
+ yamltest lets you configure unit test with yaml documents. Simply define a "yt_parse" method
8
+ that will transform your "src" (source) into a "res" (result).
9
+
10
+ == FEATURES:
11
+
12
+ * Ease of context definition.
13
+ * Multiple step testing with different keys.
14
+ * Tests are only created if a test with the same name does not exist.
15
+ * Named tests (easy to get information on failed tests).
16
+ * If someone asks, I can try to fix my TextMate Bundle that runs the tests (all or one at a time)
17
+ from the yaml file and put it online.
18
+
19
+ == SYNOPSIS:
20
+
21
+ class TestSomething < Test::Unit::TestCase
22
+ yamltest :directory => 'relative/path/to/yaml/files'
23
+
24
+ def yt_parse(key, source, context)
25
+ do_something_with(source, context)
26
+ end
27
+
28
+ yt_make
29
+ end
30
+
31
+ == REQUIREMENTS:
32
+
33
+ * yaml, test/unit
34
+
35
+ == INSTALL:
36
+
37
+ * sudo gem install yamltest
38
+
39
+ == LICENSE:
40
+
41
+ (The MIT License)
42
+
43
+ Copyright (c) 2008-2009 Gaspard Bucher
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ 'Software'), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
59
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
60
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
61
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
62
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/yamltest'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('yamltest', Yamltest::VERSION) do |p|
7
+ p.developer('Gaspard Bucher', 'gaspard@teti.ch')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.rubyforge_name = p.name
10
+
11
+ p.extra_dev_deps = [
12
+ ['newgem', ">= #{::Newgem::VERSION}"]
13
+ ]
14
+
15
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
16
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
17
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
18
+ p.rsync_args = '-av --delete --ignore-errors'
19
+ end
20
+
21
+ require 'newgem/tasks' # load /tasks/*.rake
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # task :default => [:spec, :features]
data/lib/yamltest.rb ADDED
@@ -0,0 +1,164 @@
1
+ require 'test/unit'
2
+ require 'yaml'
3
+
4
+ $:.unshift(File.dirname(__FILE__)) unless
5
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
+
7
+ module Yamltest
8
+ VERSION = '0.5.0'
9
+ module Helper
10
+ def self.included(obj)
11
+ obj.extend Yamltest::ClassMethods
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+
17
+ # build an array of file_name, file_path, options
18
+ def file_list(caller_path, opts)
19
+ directories = opts[:directories] || [opts[:directory] || :default]
20
+
21
+ if files = opts[:files]
22
+ files.map!{|f| f.to_s}
23
+ end
24
+
25
+ directories = directories.map do |dir|
26
+ if dir == :default
27
+ if caller_path.split('/').last =~ /^(.*)_test.rb/ || caller_path.split('/').last =~ /^test_(.*).rb/
28
+ directories = [File.join(File.dirname(caller_path), $1)]
29
+ else
30
+ puts "Bad file name for yaml_tests '#{caller_path}'. Should be 'xxx_test.rb' or 'test_xxx.rb'. Trying parent directory."
31
+ directories = [File.dirname(caller_path)]
32
+ end
33
+ else
34
+ # relative to caller
35
+ Dir[File.join(File.dirname(caller_path),dir)]
36
+ end
37
+ end.flatten
38
+
39
+ file_list = []
40
+
41
+ directories.each do |dir|
42
+ Dir.foreach(dir) do |f|
43
+ next unless f =~ /^([\w_-]+).yml/
44
+ next if files && !files.include?($1)
45
+ file_list << [$1, File.join(dir, "#{$1}.yml"), opts[$1] || opts[$1.to_sym] || {}]
46
+ end
47
+ end
48
+
49
+ file_list
50
+ end
51
+
52
+
53
+ # Setup yaml testing.
54
+ # usage:
55
+ # class SuperTest < Test::Unit::TestCase
56
+ # yamltest
57
+ #
58
+ # or to define custom search directories for tests definitions
59
+ # class SuperTest < Test::Unit::TestCase
60
+ # yamltest :directories => ["sub/**", "/absolute/location"]
61
+ #
62
+ # to use only some files:
63
+ # class SuperTest < Test::Unit::TestCase
64
+ # yamltest :files => ["one", "two"]
65
+ #
66
+ # to pass parameters during testing of a specific file:
67
+ # class SuperTest < Test::Unit::TestCase
68
+ # yamltest :options => {:latex => {:module => Latex}}
69
+ #
70
+ def yamltest(opts = {})
71
+ # We need to do a class_eval so that the class variables 'test_strings, test_methods, ...' are scoped
72
+ # in the final class and are not global to all tests using the YamlTest helper.
73
+
74
+ class_eval %Q{
75
+ @@test_strings = {}
76
+ @@test_methods = {}
77
+ @@test_options = {}
78
+ @@test_files = []
79
+ @@file_list = file_list(#{caller[0].inspect}, #{opts.inspect})
80
+
81
+ @@file_list.each do |file_name, file_path, opts|
82
+ strings = {}
83
+ test_methods = []
84
+ begin
85
+ YAML::load_documents( File.open( file_path ) ) do |doc|
86
+ doc.each do |elem|
87
+ test_methods << elem[0]
88
+ strings[elem[0]] = elem[1]
89
+ end
90
+ end
91
+ rescue ArgumentError => err
92
+ puts "Error while loading test file \#{file_path}"
93
+ raise err
94
+ end
95
+ class_eval "
96
+ def \#{file_name}
97
+ @@test_strings['\#{file_name}']
98
+ end
99
+ "
100
+ @@test_strings[file_name] = strings.freeze
101
+ @@test_methods[file_name] = test_methods
102
+ @@test_files << file_name
103
+ end
104
+
105
+ # Override this in your test class
106
+ def yt_parse(key, source, context)
107
+ source
108
+ end
109
+
110
+ def yt_do_test(file, test, context = yt_get('context',file,test))
111
+ @@test_strings[file][test].keys.each do |key|
112
+ next if ['src', 'context'].include?(key)
113
+ yt_assert yt_get(key,file,test), yt_parse(key, yt_get('src',file,test), context)
114
+ end
115
+ end
116
+
117
+ protected
118
+ def yt_assert(test_res, res)
119
+ if test_res.kind_of?(String) && test_res[0..1] == '!/'
120
+ assert_no_match %r{\#{test_res[2..-2]}}m, res
121
+ elsif test_res.kind_of?(String) && test_res[0..0] == '/'
122
+ assert_match %r{\#{test_res[1..-2]}}m, res
123
+ else
124
+ assert_equal test_res, res
125
+ end
126
+ end
127
+
128
+ def yt_get(key, file, test)
129
+ case key
130
+ when 'context', :context
131
+ context = @@test_strings[file][test]['context'] || {}
132
+ default_context = (@@test_strings[file]['default'] || {})['context'] || {}
133
+ context = default_context.merge(context)
134
+ when 'src'
135
+ @@test_strings[file][test]['src'] || test.gsub('_',' ')
136
+ else
137
+ @@test_strings[file][test][key.to_s]
138
+ end
139
+ end
140
+ }
141
+ end
142
+
143
+ def yt_make
144
+ class_eval %q{
145
+ return unless @@test_methods
146
+ tests = self.instance_methods.reject! {|m| !( m =~ /^test_/ )}
147
+ @@test_files.each do |tf|
148
+ @@test_methods[tf].each do |test|
149
+ unless tests.include?("test_#{tf}_#{test}")
150
+ tests << "test_#{tf}_#{test}"
151
+ class_eval <<-END
152
+ def test_#{tf}_#{test}
153
+ yt_do_test(#{tf.inspect}, #{test.inspect})
154
+ end
155
+ END
156
+ end
157
+ end
158
+ end
159
+ }
160
+ end
161
+ end
162
+ end
163
+
164
+ Test::Unit::TestCase.send :include, Yamltest::Helper
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/yamltest.rb'}"
9
+ puts "Loading yamltest gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,7 @@
1
+ default:
2
+ context:
3
+ src: "12345"
4
+ res: "54321"
5
+
6
+ new_york:
7
+ res: 'kroy wen'
@@ -0,0 +1,15 @@
1
+ # Simple file to test installed gem
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'yamltest'
6
+
7
+ class TestYamltestGem < Test::Unit::TestCase
8
+ yamltest :directory => '.'
9
+
10
+ def yt_parse(key, source, context)
11
+ source.split(//).reverse.join
12
+ end
13
+
14
+ yt_make
15
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/yamltest'
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ # patch Hash to sort keys on inspect for consistency
4
+ class Hash
5
+ def inspect
6
+ res = []
7
+ keys.map{|k| [k, k.to_s]}.sort{|a,b| a[1] <=> b[1]}.each do |k, ks|
8
+ res << "#{k.inspect} => #{self[k].inspect}"
9
+ end
10
+ "{#{res.join(', ')}}"
11
+ end
12
+ end
13
+
14
+ class TestDefaultFolder < Test::Unit::TestCase
15
+ yamltest
16
+
17
+ def yt_parse(key, source, context)
18
+ eval(source)
19
+ end
20
+
21
+ def test_to_make_sure_tests_are_valid
22
+ assert (respond_to?('test_simple_default') and
23
+ (method('test_simple_default').arity == 0 ||
24
+ method('test_simple_default').arity == -1))
25
+ end
26
+
27
+ yt_make
28
+ end
29
+
30
+ class TestCustomKeys < Test::Unit::TestCase
31
+ yamltest :directory => 'zoo'
32
+
33
+ def yt_parse(key, source, context)
34
+ res = source.gsub('o', 'a')
35
+ case key
36
+ when 'res'
37
+ res
38
+ when 'len'
39
+ res.length
40
+ when 'foo'
41
+ context['foo']
42
+ else
43
+ "bad test key #{key.inspect}"
44
+ end
45
+ end
46
+
47
+ # This test will not be redefined by 'make_tests'
48
+ def test_complicated_animal
49
+ context = yt_get('context', 'complicated', 'animal')
50
+ context['foo'] = 'bozo'
51
+ yt_do_test('complicated', 'animal', context)
52
+ end
53
+
54
+ yt_make
55
+ end
@@ -0,0 +1,16 @@
1
+ default:
2
+ context:
3
+ one: 1
4
+ two: 2
5
+ env:
6
+ login: 'Joe'
7
+ pass: 'secret'
8
+ src: "context.inspect"
9
+ res: '{"env" => {"login" => "Joe", "pass" => "secret"}, "one" => 1, "two" => 2}'
10
+
11
+ overwrite_key_replaces_hash:
12
+ context:
13
+ env:
14
+ login: 'Marc'
15
+ src: "context.inspect"
16
+ res: '{"env" => {"login" => "Marc"}, "one" => 1, "two" => 2}'
@@ -0,0 +1,16 @@
1
+ default:
2
+ src: "John is Good"
3
+ res: "Jahn is Gaad"
4
+
5
+ other_keys:
6
+ context:
7
+ env:
8
+ login: 'Marc'
9
+ src: "Moon"
10
+ len: 4
11
+ res: "Maan"
12
+
13
+ animal:
14
+ context:
15
+ foo: "bar"
16
+ foo: "bozo"
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamltest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Gaspard Bucher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-22 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ description: yamltest lets you configure unit test with yaml documents. Simply define a "yt_parse" method that will transform your "src" (source) into a "res" (result).
36
+ email:
37
+ - gaspard@teti.ch
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - README.rdoc
46
+ files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.rdoc
50
+ - Rakefile
51
+ - lib/yamltest.rb
52
+ - script/console
53
+ - script/destroy
54
+ - script/generate
55
+ - test/gem_test
56
+ - test/gem_test/foo.yml
57
+ - test/gem_test/simple_test.rb
58
+ - test/test_helper.rb
59
+ - test/test_yamltest.rb
60
+ - test/yamltest/simple.yml
61
+ - test/zoo/complicated.yml
62
+ has_rdoc: true
63
+ homepage: http://github.com/zena/yamltest/tree
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README.rdoc
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: yamltest
85
+ rubygems_version: 1.3.1
86
+ signing_key:
87
+ specification_version: 2
88
+ summary: yamltest lets you configure unit test with yaml documents
89
+ test_files:
90
+ - test/test_helper.rb
91
+ - test/test_yamltest.rb