class Brutal::Scaffold

Brutal::Scaffold

@since 1.0.0

Attributes

actuals[R]

Specifies templates to challenge evaluated subjects & get results.

contexts[R]

Specifies a list of variables to populate the subject's template.

header[R]

Specifies the code to execute before generating the test suite.

subject[R]

Specifies the template of the code to be declined across contexts.

Public Class Methods

new(header, subject, *actuals, **contexts) click to toggle source

Initialize a new scaffold generator.

# File lib/brutal/scaffold.rb, line 21
def initialize(header, subject, *actuals, **contexts)
  warn("Empty subject!")        if subject.empty?
  warn("Empty actual values!")  if actuals.empty?
  warn("Empty contexts!")       if contexts.empty?

  eval(header) # rubocop:disable Security/Eval

  @header   = header
  @subject  = subject
  @actuals  = actuals
  @contexts = contexts
end

Public Instance Methods

blank_line() click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# File lib/brutal/scaffold.rb, line 73
def blank_line
  "\n"              \
  "# #{'-' * 78}\n" \
  "\n"
end
combinations_values() click to toggle source
# File lib/brutal/scaffold.rb, line 87
def combinations_values
  Array(contexts_values[0]).product(*Array(contexts_values[1..]))
end
context_names() click to toggle source
# File lib/brutal/scaffold.rb, line 79
def context_names
  contexts.keys.sort
end
contexts_values() click to toggle source
# File lib/brutal/scaffold.rb, line 83
def contexts_values
  context_names.map { |context_name| contexts.fetch(context_name) }
end
inspect(object) click to toggle source

Return a Ruby string that can be evaluated.

# File lib/brutal/scaffold.rb, line 35
def inspect(object)
  return object.to_s unless object.is_a?(::String)

  object.strip
end
to_s() click to toggle source

Return a string representation.

@return [String]

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

# File lib/brutal/scaffold.rb, line 46
    def to_s
      "#{header.chomp}\n#{blank_line}" + combinations_values.map do |values|
        attributes = context_names.each_with_index.inject({}) do |h, (name, i)|
          h.merge(name.to_sym => inspect(values.fetch(i)))
        end

        actual_str = format(inspect(subject), **attributes)

        string = <<~CODE
          actual = begin
          #{actual_str.gsub(/^/, '  ')}
          end

        CODE

        actual = eval(actual_str) # rubocop:disable Security/Eval, Lint/UselessAssignment

        actuals.each do |actual_value|
          result_str = format(actual_value, subject: "actual")
          string += "raise if #{result_str} != #{eval(result_str).inspect}\n" # rubocop:disable Security/Eval
        end

        string
      end.join(blank_line)
    end