Class: Hegel::Generators::IpAddressesGenerator

Inherits:
Hegel::Generator show all
Defined in:
lib/hegel/generators.rb,
sig/hegel.rbs

Overview

Hegel::Syntax::Methods#ip_addresses. An IPAddr, v4 or v6 depending on +v4+/+v6+. Both true (the default) draws a boolean to pick the family for each value; both false has no family left to draw from, so it raises here rather than reaching hegel_generate_ipv4 or hegel_generate_ipv6 at all.

Spanned with HEGEL_LABEL_IP_ADDRESS around the whole draw, the same way OptionalGenerator spans its own boolean-then-delegate draw (see the HEGEL_LABEL_* table): when both families are enabled this generator makes two native calls (the family choice, then the address) to produce one value, and the span is what lets the shrinker retry that pair together instead of the two calls separately.

Instance Method Summary collapse

Methods inherited from Hegel::Generator

#filter, #map

Constructor Details

#initialize(v4:, v6:) ⇒ IpAddressesGenerator

Returns a new instance of IpAddressesGenerator.

Parameters:

  • v4: (Boolean)
  • v6: (Boolean)


540
541
542
543
544
# File 'lib/hegel/generators.rb', line 540

def initialize(v4:, v6:)
  super()
  @v4 = v4
  @v6 = v6
end

Instance Method Details

#do_draw(tc) ⇒ Object

Parameters:

  • tc (Object)

Returns:

  • (Object)

Raises:



546
547
548
549
550
551
552
553
554
555
# File 'lib/hegel/generators.rb', line 546

def do_draw(tc)
  raise Hegel::Error, "ip_addresses: v4 and v6 must not both be false" if !@v4 && !@v6

  tc.start_span(LibHegel::HEGEL_LABEL_IP_ADDRESS)
  begin
    draw_address(tc)
  ensure
    tc.stop_span(discard: false)
  end
end

#draw_address(tc) ⇒ Object

hegel_generate_ipv4/hegel_generate_ipv6 return the address's raw network-order bytes (see TestCase#generate_ipv4/#generate_ipv6). IPAddr.new_ntoh builds an IPAddr straight from that byte string, picking v4 or v6 by its length (4 or 16), so no manual byte-to- integer conversion belongs here.

Parameters:

  • tc (Object)

Returns:

  • (Object)


564
565
566
567
# File 'lib/hegel/generators.rb', line 564

def draw_address(tc)
  use_v4 = (@v4 && @v6) ? tc.generate_boolean : @v4
  IPAddr.new_ntoh(use_v4 ? tc.generate_ipv4 : tc.generate_ipv6)
end