Class: Brutal::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/brutal/scaffold.rb

Overview

Brutal::Scaffold

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, subject, *actuals, **contexts) ⇒ Scaffold

Initialize a new scaffold generator.

Since:

  • 1.0.0



21
22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#actualsObject (readonly)

Specifies templates to challenge evaluated subjects & get results.

Since:

  • 1.0.0



9
10
11
# File 'lib/brutal/scaffold.rb', line 9

def actuals
  @actuals
end

#contextsObject (readonly)

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

Since:

  • 1.0.0



12
13
14
# File 'lib/brutal/scaffold.rb', line 12

def contexts
  @contexts
end

#headerObject (readonly)

Specifies the code to execute before generating the test suite.

Since:

  • 1.0.0



15
16
17
# File 'lib/brutal/scaffold.rb', line 15

def header
  @header
end

#subjectObject (readonly)

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

Since:

  • 1.0.0



18
19
20
# File 'lib/brutal/scaffold.rb', line 18

def subject
  @subject
end

Instance Method Details

#blank_lineObject

Since:

  • 1.0.0



70
71
72
73
74
# File 'lib/brutal/scaffold.rb', line 70

def blank_line
  "\n"              \
    "# #{"-" * 78}\n" \
    "\n"
end

#combinations_valuesObject

Since:

  • 1.0.0



84
85
86
# File 'lib/brutal/scaffold.rb', line 84

def combinations_values
  Array(contexts_values[0]).product(*Array(contexts_values[1..]))
end

#context_namesObject

Since:

  • 1.0.0



76
77
78
# File 'lib/brutal/scaffold.rb', line 76

def context_names
  contexts.keys.sort
end

#contexts_valuesObject

Since:

  • 1.0.0



80
81
82
# File 'lib/brutal/scaffold.rb', line 80

def contexts_values
  context_names.map { |context_name| contexts.fetch(context_name) }
end

#inspect(object) ⇒ Object

Return a Ruby string that can be evaluated.

Since:

  • 1.0.0



35
36
37
38
39
# File 'lib/brutal/scaffold.rb', line 35

def inspect(object)
  return object.to_s unless object.is_a?(::String)

  object.strip
end

#to_sString

Return a string representation.

Returns:

  • (String)

Since:

  • 1.0.0



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/brutal/scaffold.rb', line 44

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