lib/rast/parameter_generator.rb in rast-0.15.1 vs lib/rast/parameter_generator.rb in rast-0.18.0

- old
+ new

@@ -4,12 +4,10 @@ require 'rast/rast_spec' require 'rast/rules/rule' require 'rast/rules/rule_evaluator' require 'rast/rules/rule_validator' -require 'rast/converters/default_converter' - # Generates the test parameters. class ParameterGenerator # Allow access so yaml-less can build the config via dsl. attr_accessor :specs_config @@ -22,11 +20,15 @@ return nil if @specs_config.nil? spec_config = @specs_config[spec_id] spec_config[:description] = spec_id + + # Keep, for backwards compatibility spec_config['rules'] ||= spec_config['outcomes'] + spec_config['default'] ||= spec_config['else'] + spec = instantiate_spec(spec_config) list = [] variables = spec.variables @@ -41,26 +43,41 @@ end private def valid_case?(scenario, spec) - return true if spec.exclude_clause.nil? + return true if spec.exclude_clause.nil? && spec.include_clause.nil? - exclude_clause = Rule.sanitize(clause: spec.exclude_clause) rule_evaluator = RuleEvaluator.new(converters: spec.converters) - rule_evaluator.parse(expression: exclude_clause) - rule_evaluator.evaluate(scenario: scenario, rule_token_convert: spec.token_converter) == "false" + include_result = true + unless spec.exclude_clause.nil? + exclude_clause = Rule.sanitize(clause: spec.exclude_clause) + rule_evaluator.parse(expression: exclude_clause) + evaluate_result = rule_evaluator.evaluate(scenario: scenario, rule_token_convert: spec.token_converter) + include_result = evaluate_result == 'false' + end + + return include_result if spec.include_clause.nil? || !include_result + + include_clause = Rule.sanitize(clause: spec.include_clause) + rule_evaluator.parse(expression: include_clause) + include_result = rule_evaluator.evaluate(scenario: scenario, rule_token_convert: spec.token_converter) == "true" + + include_result end # add all fixtures to the list. def add_fixtures(scenarios: [], spec: nil, list: []) validator = RuleValidator.new scenarios.each do |scenario| - next unless valid_case?(scenario, spec) + good = valid_case?(scenario, spec) + # p "#{good} #{scenario}" + next unless good + list << build_param(validator, scenario, spec) end end def build_param(validator, scenario, spec) @@ -107,20 +124,25 @@ end spec = RastSpec.new( description: spec_config[:description], variables: spec_config['variables'], - rule: Rule.new(rules: spec_config['rules']) + rule: Rule.new(rules: spec_config['rules']), + default_outcome: spec_config['default'] || spec_config['else'] ) pair_config = calculate_pair(spec_config) spec.init_pair(pair_config: pair_config) unless pair_config.nil? unless spec_config['exclude'].nil? spec.init_exclusion(spec_config['exclude']) end + unless spec_config['include'].nil? + spec.init_inclusion(spec_config['include']) + end + converters_config = spec_config['converters'] converters = if converters_config.nil? # when no converters defined, we detect if type is consistent, otherwise assume it's string. default_converter = DefaultConverter.new spec_config['variables'].map do |_key, array| @@ -128,18 +150,10 @@ RuleEvaluator::DEFAULT_CONVERT_HASH[array.first.class] else default_converter end end - elsif converters_config.first.class == String - # when converters defined, determined by the converter name as String. - spec_config['converters'].map do |converter| - Object.const_get(converter).new - end - else - # converters defined, probably programmatically when yaml-less, just return it. - converters_config end spec.init_converters(converters: converters) end @@ -155,10 +169,10 @@ if %w[true false].include?(outcomes.first) return { outcomes.first => outcomes.first == 'true' ? 'false' : 'true' } end - return { outcomes.first => spec_config['else'] } if spec_config['else'] + return { outcomes.first => spec_config['default'] } if spec_config['default'] end {} end