Class: Brutal::Scaffold
- Inherits:
-
Object
- Object
- Brutal::Scaffold
- Defined in:
- lib/brutal/scaffold.rb
Overview
Brutal::Scaffold
Instance Attribute Summary collapse
-
#actuals ⇒ Object
readonly
Specifies templates to challenge evaluated subjects & get results.
-
#contexts ⇒ Object
readonly
Specifies a list of variables to populate the subject’s template.
-
#header ⇒ Object
readonly
Specifies the code to execute before generating the test suite.
-
#subject ⇒ Object
readonly
Specifies the template of the code to be declined across contexts.
Instance Method Summary collapse
- #blank_line ⇒ Object
- #combinations_values ⇒ Object
- #context_names ⇒ Object
- #contexts_values ⇒ Object
-
#initialize(header, subject, *actuals, **contexts) ⇒ Scaffold
constructor
Initialize a new scaffold generator.
-
#inspect(object) ⇒ Object
Return a Ruby string that can be evaluated.
-
#to_s ⇒ String
Return a string representation.
Constructor Details
#initialize(header, subject, *actuals, **contexts) ⇒ Scaffold
Initialize a new scaffold generator.
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
#actuals ⇒ Object (readonly)
Specifies templates to challenge evaluated subjects & get results.
9 10 11 |
# File 'lib/brutal/scaffold.rb', line 9 def actuals @actuals end |
#contexts ⇒ Object (readonly)
Specifies a list of variables to populate the subject’s template.
12 13 14 |
# File 'lib/brutal/scaffold.rb', line 12 def contexts @contexts end |
#header ⇒ Object (readonly)
Specifies the code to execute before generating the test suite.
15 16 17 |
# File 'lib/brutal/scaffold.rb', line 15 def header @header end |
#subject ⇒ Object (readonly)
Specifies the template of the code to be declined across contexts.
18 19 20 |
# File 'lib/brutal/scaffold.rb', line 18 def subject @subject end |
Instance Method Details
#blank_line ⇒ Object
70 71 72 73 74 |
# File 'lib/brutal/scaffold.rb', line 70 def blank_line "\n" \ "# #{"-" * 78}\n" \ "\n" end |
#combinations_values ⇒ Object
84 85 86 |
# File 'lib/brutal/scaffold.rb', line 84 def combinations_values Array(contexts_values[0]).product(*Array(contexts_values[1..])) end |
#context_names ⇒ Object
76 77 78 |
# File 'lib/brutal/scaffold.rb', line 76 def context_names contexts.keys.sort end |
#contexts_values ⇒ Object
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.
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_s ⇒ String
Return a string representation.
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 |