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
- #actual_ruby_code(actual_str) ⇒ Object
- #actual_ruby_codes ⇒ Object
- #attributes(*values) ⇒ Object
- #combinations_values ⇒ Object
- #context_names ⇒ Object
- #contexts_values ⇒ Object
- #header_ruby_code ⇒ Object
-
#initialize(header, subject, *actuals, **contexts) ⇒ Scaffold
constructor
Initialize a new scaffold generator.
-
#inspect(object) ⇒ Object
Return a Ruby string that can be evaluated.
- #ruby_lines ⇒ Object
- #separator_ruby_code ⇒ Object
- #thematic_break_ruby_code ⇒ Object
-
#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
#actual_ruby_code(actual_str) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/brutal/scaffold.rb', line 85 def actual_ruby_code(actual_str) <<~RUBY_CODE actual = begin #{actual_str.gsub(/^/, ' ')} end RUBY_CODE end |
#actual_ruby_codes ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/brutal/scaffold.rb', line 70 def actual_ruby_codes combinations_values.map do |values| actual_str = format(inspect(subject), **attributes(*values)) string = actual_ruby_code(actual_str) 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 end |
#attributes(*values) ⇒ Object
48 49 50 51 52 |
# File 'lib/brutal/scaffold.rb', line 48 def attributes(*values) context_names.each_with_index.inject({}) do |h, (name, i)| h.merge(name.to_sym => inspect(values.fetch(i))) end end |
#combinations_values ⇒ Object
62 63 64 |
# File 'lib/brutal/scaffold.rb', line 62 def combinations_values Array(contexts_values[0]).product(*Array(contexts_values[1..])) end |
#context_names ⇒ Object
54 55 56 |
# File 'lib/brutal/scaffold.rb', line 54 def context_names contexts.keys.sort end |
#contexts_values ⇒ Object
58 59 60 |
# File 'lib/brutal/scaffold.rb', line 58 def contexts_values context_names.map { |context_name| contexts.fetch(context_name) } end |
#header_ruby_code ⇒ Object
94 95 96 97 98 |
# File 'lib/brutal/scaffold.rb', line 94 def header_ruby_code <<~RUBY_CODE #{header.chomp} RUBY_CODE 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 |
#ruby_lines ⇒ Object
66 67 68 |
# File 'lib/brutal/scaffold.rb', line 66 def ruby_lines [header_ruby_code] + actual_ruby_codes end |
#separator_ruby_code ⇒ Object
100 101 102 103 104 105 |
# File 'lib/brutal/scaffold.rb', line 100 def separator_ruby_code <<~RUBY_CODE #{thematic_break_ruby_code} RUBY_CODE end |
#thematic_break_ruby_code ⇒ Object
107 108 109 110 111 |
# File 'lib/brutal/scaffold.rb', line 107 def thematic_break_ruby_code <<~RUBY_CODE # #{'-' * 78} RUBY_CODE end |
#to_s ⇒ String
Return a string representation.
44 45 46 |
# File 'lib/brutal/scaffold.rb', line 44 def to_s ruby_lines.join(separator_ruby_code) end |