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

.misspelled(string) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/test_values/string_values.rb', line 39

def self.misspelled(string)
  self.verify_kind_of('string', String, string)
  unless string.match(/\w/)
    message = "Can only misspell string matching /\\w/, not '#{string}'"
    raise ArgumentError.new(message)
  end
  misspelling = string.clone
  index = nil
  misspelling.scan(/./).each_with_index do |char, i|
    next unless char.match(/\w/)
    index = i
    break
  end
  char = misspelling[index]
  misspelling[index] = case
                          when ('A'..'Z').include?(char)
                            char == 'Z' ? 'A' : (1 + char.ord).chr
                          when ('a'..'z').include?(char)
                            char == 'z' ? 'a' : (1 + char.ord).chr
                          when ('0'..'9').include?(char)
                            char == '9' ? '0' : (1 + char.ord).chr
                          else
                            'A'
                        end
  misspelling
end

.numerics_in_range(range) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/test_values/string_values.rb', line 81

def self.numerics_in_range(range)
  values = NumericValues.numerics_in_range(range)
  values.each_pair do |key, value|
    values.store(key, value.to_s)
  end
  values
end

.numerics_not_in_range(range) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/test_values/string_values.rb', line 89

def self.numerics_not_in_range(range)
  values = NumericValues.numerics_not_in_range(range)
  values.each_pair do |key, value|
    values.store(key, value.to_s)
  end
  values
end

.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_kind_of('length', Integer, length)
  self.verify_kind_of('base_string', String, base_string)
  self.verify_integer_in_range('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_kind_of('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:



73
74
75
76
77
78
79
# File 'lib/test_values/string_values.rb', line 73

def self.strings_not_in_length_range(range, base_string=BASE_STRING)
  self.verify_kind_of('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