Skip to content
Permalink
Browse files

data: add support for keep option

  • Loading branch information...
kou committed Nov 30, 2018
1 parent e084e89 commit 4c32174685568e135e815ea1d278b0635ccf26d8
Showing with 92 additions and 16 deletions.
  1. +5 −1 lib/test/unit/attribute.rb
  2. +33 −9 lib/test/unit/data-sets.rb
  3. +33 −5 lib/test/unit/data.rb
  4. +21 −1 test/test-data.rb
@@ -43,7 +43,11 @@ def method_added(name)
kept_attributes = StringifyKeyHash.new
@current_attributes.each do |attribute_name, attribute|
attributes[attribute_name] = attribute[:value]
kept_attributes[attribute_name] = attribute if attribute[:keep]
if attribute[:keep]
keep_hook = attribute[:keep_hook]
attribute = keep_hook.call(attribute) if keep_hook
kept_attributes[attribute_name] = attribute
end
end
set_attributes(name, attributes)
@current_attributes = kept_attributes
@@ -7,30 +7,53 @@ def initialize
@value_sets = []
end

def <<(data_set)
def add(data_set, options=nil)
options ||= {}
if data_set.respond_to?(:call)
@procs << data_set
@procs << [data_set, options]
elsif data_set.is_a?(Array)
@variables << data_set
@variables << [data_set, options]
else
@value_sets << data_set
@value_sets << [data_set, options]
end
end

def <<(data_set)
add(data_set)
end

def keep
new_data_sets = self.class.new
all_data_sets = Enumerator.new do |yielder|
block = lambda do |(data_set, options)|
yielder << [data_set, options]
end
@procs.each(&block)
@variables.each(&block)
@value_sets.each(&block)
end
all_data_sets.each do |data_set, options|
next if options.nil?
next unless options[:keep]
new_data_sets.add(data_set, options)
end
new_data_sets
end

def each
variables = @variables
value_sets = @value_sets
@procs.each do |proc|
@procs.each do |proc, options|
data_set = proc.call
case data_set
when Array
variables += [data_set]
variables += [[data_set, options]]
else
value_sets += [data_set]
value_sets += [[data_set, options]]
end
end

value_sets.each do |values|
value_sets.each do |values, _options|
values.each do |label, data|
yield(label, data)
end
@@ -73,7 +96,8 @@ def build_matrix(variables)
def build_raw_matrix(variables)
return [] if variables.empty?

(variable, patterns), *rest_variables = variables
current, *rest_variables = variables
(variable, patterns), _options = current
sub_matrix = build_raw_matrix(rest_variables)
return sub_matrix if patterns.empty?

@@ -110,28 +110,56 @@ module ClassMethods
# Generates test matrix from variable and patterns pairs.
#
def data(*arguments, &block)
options = nil
n_arguments = arguments.size
case n_arguments
when 0
raise ArgumentError, "no block is given" unless block_given?
data_set = block
when 1
data_set = arguments[0]
if block_given?
data_set = block
options = arguments[1]
else
data_set = arguments[0]
end
when 2
if arguments[0].is_a?(String)
case arguments[0]
when String
data_set = {arguments[0] => arguments[1]}
when Hash
data_set = arguments[0]
options = arguments[1]
else
variable = arguments[0]
patterns = arguments[1]
data_set = [variable, patterns]
end
when 3
case arguments[0]
when String
data_set = {arguments[0] => arguments[1]}
options = arguments[2]
else
variable = arguments[0]
patterns = arguments[1]
data_set = [variable, patterns]
options = arguments[2]
end
else
message = "wrong number arguments(#{n_arguments} for 1..2)"
message = "wrong number arguments(#{n_arguments} for 0..3)"
raise ArgumentError, message
end
options ||= {}
data_sets = current_attribute(:data)[:value] || DataSets.new
data_sets << data_set
attribute(:data, data_sets)
data_sets.add(data_set, options)
if options[:keep]
keep_hook = lambda do |attr|
attr.merge(value: attr[:value].keep)
end
options = options.merge(keep_hook: keep_hook)
end
attribute(:data, data_sets, options)
end

# This method provides Data-Driven-Test functionality.
@@ -114,6 +114,18 @@ class TestPatterns < TestCalc
def test_plus(data)
end
end

class TestPatternsKeep < TestCalc
data(:x, [-1, 1, 0], keep: true)
data(:y, [-100, 100])
data(:none, [])
data(:z, ["a", "b", "c"], keep: true)
def test_plus(data)
end

def test_plus_keep(data)
end
end
end

def setup
@@ -197,7 +209,7 @@ def test_data(data)
test_plus = data[:test_case].new("test_plus")
data_sets = Test::Unit::DataSets.new
data[:data_sets].each do |data_set|
data_sets << data_set
data_sets.add(data_set)
end
assert_equal(data_sets, test_plus[:data])
end
@@ -212,6 +224,14 @@ def test_data_patterns
assert_equal(data_sets, test_plus[:data])
end

def test_data_patterns_keep
test_plus = TestCalc::TestPatternsKeep.new("test_plus_keep")
data_sets = Test::Unit::DataSets.new
data_sets.add([:x, [-1, 1, 0]], {keep: true})
data_sets.add([:z, ["a", "b", "c"]], {keep: true})
assert_equal(data_sets, test_plus[:data])
end

data("data set" => TestCalc::TestDataSet,
"n-data" => TestCalc::TestNData,
"dynamic-data-set" => TestCalc::TestDynamicDataSet,

0 comments on commit 4c32174

Please sign in to comment.
You can’t perform that action at this time.