class StringValues

Constants

BASE_STRING

Public Class Methods

misspelled(string) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Return a string of the given length, built by trimming or extending the given base_string. @param length [Integer] the length of the string to be returned. @param base_string [String] the base string to be trimmed or extended. @return [String] a string of the given length. @raise [TypeError] if length is not an Integer. @raise [TypeError] if base_string is not a String. @raise [RangeError] if length is negative.

# 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) click to toggle source

Return a hash of strings of minimum and maximum length for the given range. @param range [Range] specifies the minimum and maximum string lengths. @param base_string [String] specifies the base_string @see string_of_length string_of_length for exceptions raised. @return [Hash] Symbol/String pairs

with keys +:min_length+ and +:max_length+.
# 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) click to toggle source

Return a hash of strings not within minimum and maximum length for the given range. @param range [Range] specifies the minimum and maximum string lengths. @param base_string [String] specifies the base_string @see string_of_length string_of_length for exceptions raised. @return [Hash] Symbol/String pairs

with keys +:too_short+ and +:too_long+.
# 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