Class: Hegel::Generators::ArrayGenerator
- Inherits:
-
Hegel::Generator
- Object
- Hegel::Generator
- Hegel::Generators::ArrayGenerator
- Defined in:
- lib/hegel/generators.rb,
sig/hegel.rbs
Overview
Hegel::Syntax::Methods#arrays. An Array of values from elements,
with [min_size, max_size] entries, unbounded above by default.
Instance Method Summary collapse
- #do_draw(tc) ⇒ Array[untyped]
- #draw_element(tc) ⇒ Object
- #draw_elements(tc, max_size) ⇒ Array[untyped]
-
#initialize(elements, min_size:, max_size:) ⇒ ArrayGenerator
constructor
A new instance of ArrayGenerator.
Methods inherited from Hegel::Generator
Constructor Details
#initialize(elements, min_size:, max_size:) ⇒ ArrayGenerator
Returns a new instance of ArrayGenerator.
142 143 144 145 146 147 |
# File 'lib/hegel/generators.rb', line 142 def initialize(elements, min_size:, max_size:) super() @elements = elements @min_size = min_size @max_size = max_size end |
Instance Method Details
#do_draw(tc) ⇒ Array[untyped]
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/hegel/generators.rb', line 149 def do_draw(tc) raise Hegel::Error, "arrays: min_size must not be negative" if @min_size.negative? max_size = @max_size || LibHegel::HEGEL_COLLECTION_MAX_SIZE_UNBOUNDED raise Hegel::Error, "arrays: max_size < min_size" if max_size < @min_size # HEGEL_LABEL_LIST around the whole array, HEGEL_LABEL_LIST_ELEMENT # around each element (#draw_element below): the reference binding # (hegel-typescript's drawList) wraps both, and the shrinker needs # both to shrink a compound draw correctly -- a missing or # misplaced span here shows up as a larger-than-minimal # counterexample, not a test failure (see docs/adr/0006). tc.start_span(LibHegel::HEGEL_LABEL_LIST) begin draw_elements(tc, max_size) ensure tc.stop_span(discard: false) end end |
#draw_element(tc) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/hegel/generators.rb', line 182 def draw_element(tc) tc.start_span(LibHegel::HEGEL_LABEL_LIST_ELEMENT) @elements.do_draw(tc) ensure tc.stop_span(discard: false) end |
#draw_elements(tc, max_size) ⇒ Array[untyped]
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/hegel/generators.rb', line 171 def draw_elements(tc, max_size) collection = tc.new_collection(@min_size, max_size) begin result = [] result << draw_element(tc) while tc.collection_more(collection) result ensure tc.collection_free(collection) end end |