param_test 0.0.1beta
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +2 -0
- data/README.md +4 -0
- data/Rakefile +13 -0
- data/lib/active_support/testing/parametrized.rb +76 -0
- data/lib/param_test.rb +8 -0
- data/param_test.gemspec +18 -0
- data/test/helper.rb +2 -0
- data/test/param_test_meta_test.rb +78 -0
- data/test/param_test_test.rb +25 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |test|
|
7
|
+
test.libs << 'lib' << 'test'
|
8
|
+
test.test_files = FileList['test/*_test.rb']
|
9
|
+
test.verbose = true
|
10
|
+
test.warning = true
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => [:test]
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module Testing
|
3
|
+
module Parametrized
|
4
|
+
|
5
|
+
# Generates parametrized tests, one for each set of parameters given.
|
6
|
+
# The description must be a format specification with the same number
|
7
|
+
# of substitutions as each parameter set has arguments (generally,
|
8
|
+
# just use one '%s' substitution per parameter). The body of the test
|
9
|
+
# is the given block executed with each parameter set.
|
10
|
+
#
|
11
|
+
# = Examples
|
12
|
+
#
|
13
|
+
# This code within an ActiveSupport::Testcase
|
14
|
+
#
|
15
|
+
# param_test "string %s is ASCII only",
|
16
|
+
# ["foo", "bar", "baz"] do |string|
|
17
|
+
# assert string.ascii_only?
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Generates 3 test methods, one for each parameter given, with names
|
21
|
+
# based on the description.
|
22
|
+
#
|
23
|
+
# Another example with multiple parameters, generating 2 test methods:
|
24
|
+
#
|
25
|
+
# param_test "%s is uppercase %s",
|
26
|
+
# [["FOO", "foo"], ["BAR", "bar"]] do |expected, param|
|
27
|
+
# assert_equal expected, param.upcase
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
def param_test(description_template, parameters, &block)
|
31
|
+
parameters.each do |param_set|
|
32
|
+
# Replacing nil values with 'nil' string because nil values will
|
33
|
+
# get converted to an empty string within a test name.
|
34
|
+
if param_set.kind_of? Array
|
35
|
+
sanitized_param_set = param_set.map do |param|
|
36
|
+
param.nil? ? 'nil' : param
|
37
|
+
end
|
38
|
+
else
|
39
|
+
sanitized_param_set = param_set.nil? ? 'nil' : param_set
|
40
|
+
end
|
41
|
+
|
42
|
+
begin
|
43
|
+
description = description_template % sanitized_param_set
|
44
|
+
rescue ArgumentError
|
45
|
+
raise ArgumentError, "Parameter set #{param_set} doesn't match number " +
|
46
|
+
"of arguments expected by description template '#{description_template}'"
|
47
|
+
end
|
48
|
+
|
49
|
+
# Make sure the description generates a unique test method name.
|
50
|
+
unique_description = description
|
51
|
+
if instance_methods.include? generate_test_name(unique_description)
|
52
|
+
count = 1
|
53
|
+
description += " %s"
|
54
|
+
unique_description = description % count
|
55
|
+
end
|
56
|
+
while instance_methods.include? generate_test_name(unique_description)
|
57
|
+
count += 1
|
58
|
+
unique_description = description % count
|
59
|
+
end
|
60
|
+
|
61
|
+
test unique_description do
|
62
|
+
instance_exec param_set, &block
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def generate_test_name(description)
|
70
|
+
# Declarative 'test' uses the same substitution of whitespace.
|
71
|
+
sanitized_description = description.gsub(/\s+/,'_')
|
72
|
+
"test_#{sanitized_description}".to_sym
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/lib/param_test.rb
ADDED
data/param_test.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'param_test'
|
3
|
+
s.version = '0.0.1beta'
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.authors = ['Nik Haldimann']
|
6
|
+
s.email = ['nhaldimann@gmail.com']
|
7
|
+
s.homepage = 'https://github.com/nikhaldi/param-test-ruby'
|
8
|
+
s.summary = 'Parametrized unit tests for Ruby/ActiveSupport'
|
9
|
+
s.description = 'Parametrized unit tests for Ruby/ActiveSupport'
|
10
|
+
|
11
|
+
s.add_runtime_dependency 'activesupport', '~> 3.2'
|
12
|
+
s.add_development_dependency 'rake'
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ParamTestMetaTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
def assert_test_method_count(clazz, expected_count)
|
6
|
+
instance_methods = clazz.instance_methods - clazz.superclass.instance_methods
|
7
|
+
assert_equal expected_count, instance_methods.grep(/^test/).size
|
8
|
+
end
|
9
|
+
|
10
|
+
def assert_has_test_method(clazz, expected_method_name)
|
11
|
+
assert_include clazz.instance_methods, expected_method_name
|
12
|
+
end
|
13
|
+
|
14
|
+
test "empty parameters adds no tests" do
|
15
|
+
test_case = Class.new ActiveSupport::TestCase do
|
16
|
+
param_test "adds no tests", [] do |param| end
|
17
|
+
end
|
18
|
+
assert_test_method_count test_case, 0
|
19
|
+
end
|
20
|
+
|
21
|
+
test "single parameter list adds one test" do
|
22
|
+
test_case = Class.new ActiveSupport::TestCase do
|
23
|
+
param_test "adds one test %s", [42] do |param| end
|
24
|
+
end
|
25
|
+
assert_test_method_count test_case, 1
|
26
|
+
assert_has_test_method test_case, :test_adds_one_test_42
|
27
|
+
end
|
28
|
+
|
29
|
+
test "multiple parameters generate correct method name" do
|
30
|
+
test_case = Class.new ActiveSupport::TestCase do
|
31
|
+
param_test "adds one test %s two params %s", [[41, 42]] do |first, second| end
|
32
|
+
end
|
33
|
+
assert_test_method_count test_case, 1
|
34
|
+
assert_has_test_method test_case, :test_adds_one_test_41_two_params_42
|
35
|
+
end
|
36
|
+
|
37
|
+
test "two parameter lists generate two methods" do
|
38
|
+
test_case = Class.new ActiveSupport::TestCase do
|
39
|
+
param_test "with two params %s %s",
|
40
|
+
[[41, 42], [43, 44]] do |first, second| end
|
41
|
+
end
|
42
|
+
assert_test_method_count test_case, 2
|
43
|
+
assert_has_test_method test_case, :test_with_two_params_41_42
|
44
|
+
assert_has_test_method test_case, :test_with_two_params_43_44
|
45
|
+
end
|
46
|
+
|
47
|
+
test "uneven parameter lists fails" do
|
48
|
+
assert_raise ArgumentError do
|
49
|
+
Class.new ActiveSupport::TestCase do
|
50
|
+
param_test "%s %s",
|
51
|
+
[[41, 42], [43], [44, 45]] do |first, second| end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
test "substitutes nil string for nil" do
|
57
|
+
test_case = Class.new ActiveSupport::TestCase do
|
58
|
+
param_test "params %s %s",
|
59
|
+
[[nil, nil]] do |param| end
|
60
|
+
end
|
61
|
+
assert_test_method_count test_case, 1
|
62
|
+
assert_has_test_method test_case, :test_params_nil_nil
|
63
|
+
end
|
64
|
+
|
65
|
+
test "generates unique test names" do
|
66
|
+
test_case = Class.new ActiveSupport::TestCase do
|
67
|
+
param_test "param %s",
|
68
|
+
[nil, "nil", "", " ", "\n \t"] do |param| end
|
69
|
+
end
|
70
|
+
assert_test_method_count test_case, 5
|
71
|
+
assert_has_test_method test_case, :test_param_nil
|
72
|
+
assert_has_test_method test_case, :test_param_nil_1
|
73
|
+
assert_has_test_method test_case, :test_param_
|
74
|
+
assert_has_test_method test_case, :test_param_1
|
75
|
+
assert_has_test_method test_case, :test_param_2
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ParamTestTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
param_test "single parameter: %s", [1, 2, 3] do |param|
|
6
|
+
assert param.is_a? Numeric
|
7
|
+
end
|
8
|
+
|
9
|
+
param_test "multiple parameters: %s %s",
|
10
|
+
[[1, 1], [2, 2], [3, 3]] do |first_param, second_param|
|
11
|
+
assert_equal first_param, second_param
|
12
|
+
end
|
13
|
+
|
14
|
+
# Example from the rdoc string
|
15
|
+
param_test "string %s is ASCII only",
|
16
|
+
["foo", "bar", "baz"] do |string|
|
17
|
+
assert string.ascii_only?
|
18
|
+
end
|
19
|
+
|
20
|
+
# 2nd example from the rdoc string
|
21
|
+
param_test "%s is uppercase %s",
|
22
|
+
[["FOO", "foo"], ["BAR", "bar"]] do |expected, param|
|
23
|
+
assert_equal expected, param.upcase
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: param_test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1beta
|
5
|
+
prerelease: 5
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nik Haldimann
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Parametrized unit tests for Ruby/ActiveSupport
|
47
|
+
email:
|
48
|
+
- nhaldimann@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/active_support/testing/parametrized.rb
|
58
|
+
- lib/param_test.rb
|
59
|
+
- param_test.gemspec
|
60
|
+
- test/helper.rb
|
61
|
+
- test/param_test_meta_test.rb
|
62
|
+
- test/param_test_test.rb
|
63
|
+
homepage: https://github.com/nikhaldi/param-test-ruby
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>'
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 1.3.1
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Parametrized unit tests for Ruby/ActiveSupport
|
87
|
+
test_files:
|
88
|
+
- test/helper.rb
|
89
|
+
- test/param_test_meta_test.rb
|
90
|
+
- test/param_test_test.rb
|
91
|
+
has_rdoc:
|