Class: StringValues

Inherits:
ValuesBase show all
Defined in:
lib/test_values/string_values.rb

Constant Summary collapse

BASE_STRING =
'x'

Class Method Summary collapse

Class Method Details

.string_of_length(length, base_string = BASE_STRING) ⇒ String

Return a string of the given length, built by trimming or extending the given base_string.

Parameters:

  • length (Integer)

    the length of the string to be returned.

  • base_string (String) (defaults to: BASE_STRING)

    the base string to be trimmed or extended.

Returns:

  • (String)

    a string of the given length.

Raises:

  • (TypeError)

    if length is not an Integer.

  • (TypeError)

    if base_string is not a String.

  • (RangeError)

    if length is negative.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/test_values/string_values.rb', line 12

def self.string_of_length(length, base_string=BASE_STRING)
  self.verify_class('length', Integer, length)
  self.verify_class('base_string', String, base_string)
  self.verify_integer_size('length', (0..Float::INFINITY), length)
  return '' if length == 0
  s = base_string
  while s.length < length
    s = s * 2
  end
  return s[0..length-1]
end

.strings_in_length_range(range, base_string = BASE_STRING) ⇒ Hash

Return a hash of strings of minimum and maximum length for the given range.

Parameters:

  • range (Range)

    specifies the minimum and maximum string lengths.

  • base_string (String) (defaults to: BASE_STRING)

    specifies the base_string

Returns:

  • (Hash)

    Symbol/String pairs with keys :min_length and :max_length.

See Also:



31
32
33
34
35
36
37
# File 'lib/test_values/string_values.rb', line 31

def self.strings_in_length_range(range, base_string=BASE_STRING)
  self.verify_class('range', Range, range)
  {
      :min_length => self.string_of_length(range.first, base_string),
      :max_length => self.string_of_length(range.last, base_string),
  }
end

.strings_not_in_length_range(range, base_string = BASE_STRING) ⇒ Hash

Return a hash of strings not within minimum and maximum length for the given range.

Parameters:

  • range (Range)

    specifies the minimum and maximum string lengths.

  • base_string (String) (defaults to: BASE_STRING)

    specifies the base_string

Returns:

  • (Hash)

    Symbol/String pairs with keys :too_short and :too_long.

See Also:



46
47
48
49
50
51
52
# File 'lib/test_values/string_values.rb', line 46

def self.strings_not_in_length_range(range, base_string=BASE_STRING)
  self.verify_class('range', Range, range)
  {
      :too_short => self.string_of_length(range.first - 1, base_string),
      :too_long => self.string_of_length(range.last + 1, base_string),
  }
end