class Jfuzz::StringGenerator

Constants

CHARSET
DATE
DATE_REGEXP
DATE_TIME
DATE_TIME_REGEXP
FORMATS
TIME_REGEXP

Public Class Methods

type() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 33
def self.type
  "string"
end

Public Instance Methods

generate() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 23
def generate
  if format?
    return generate_from_format
  elsif pattern?
    return generate_from_pattern
  end

  generate_string
end

Private Instance Methods

format() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 83
def format
  property.fetch("format", nil)
end
format?() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 71
def format?
  !format.nil?
end
generate_from_format() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/jfuzz/generators/string_generator.rb, line 45
def generate_from_format
  str = FORMATS[format].random_example
  return str[0..max_length - 1] if max_length?
  str
end
generate_from_pattern() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/jfuzz/generators/string_generator.rb, line 52
def generate_from_pattern
  str = Regexp.new(pattern).random_example

  return str[0..max_length - 1] if max_length?
  str
end
generate_string() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 39
def generate_string
  length = rand(min_length..max_length)
  Array.new(length) { CHARSET.sample }.join
end
max_length() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 59
def max_length
  property.fetch("maxLength", rand(min_length..100)).to_i
end
max_length?() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 75
def max_length?
  !property.fetch("maxLength", nil).to_s.empty?
end
min_length() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 63
def min_length
  property.fetch("minLength", 1).to_i
end
pattern() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 79
def pattern
  property.fetch("pattern", nil)
end
pattern?() click to toggle source
# File lib/jfuzz/generators/string_generator.rb, line 67
def pattern?
  !pattern.nil?
end