Module: Beaker::DSL::Helpers::BeakerQaI18n::I18nStringGenerator

Defined in:
lib/beaker-qa-i18n/i18n_string_generator.rb

Constant Summary collapse

CHINESE_CHARACTERS =
[*"\u4E00".."\u4E20"]
GERMAN_CHARACTERS =
["\u00C4", "\u00E4", "\u00D6", "\u00F6", "\u00DC", "\u00FC"]
ENGLISH_CHARACTERS =
[*"\u0041".."\u007A"]
NUMERIC_CHARACTERS =
[*"\u0030".."\u0039"]
WHITE_SPACE_CHARACTERS =
[" ", *"\u2002".."\u200B"]
MAX_LENGTH_CHARACTERS =
["\u00DF"]
SYNTAX_CHARACTERS =
['&', '+', '/', '\\', '"', "'", '(', ')', '?', '.', '#', '@', '_', '-', '~']

Instance Method Summary collapse

Instance Method Details

#get_i18n_string(character_type) ⇒ String

Gets a string consisting of all values in this library for the specified character type

Parameters:

  • character_type (Symbol)
    • :chinese, :german, :english, :numeric, :max_length, :white_space, :syntax

Returns:

  • (String)
    • a string containing all of the characters from the character type



28
29
30
31
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 28

def get_i18n_string(character_type)
  array = instance_eval("#{character_type.to_s.upcase}_CHARACTERS")
  get_strings_of_length_from_char_array(array, nil)[0]
end

#get_rng(seed = nil) ⇒ Object

Gets a random number generator with optional seed

Parameters:

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (Object)
    • an instance of the Random class



19
20
21
22
23
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 19

def get_rng(seed = nil)
  seed ||= Random.new_seed
  logger.debug "random seed used: #{seed}"
  Random.new(seed)
end

#random_alpha_numeric(length, seed = nil) ⇒ String

produces a random English alpha-numeric string

Parameters:

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



92
93
94
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 92

def random_alpha_numeric(length, seed = nil)
  random_characters([ENGLISH_CHARACTERS, NUMERIC_CHARACTERS], length, seed)
end

#random_characters(utf8_ranges, length, seed = nil) ⇒ String

Generate random strings from various utf8 character ranges. Can repeat characters.

Parameters:

  • utf8_ranges (Array<Array<Char>>)
    • an array of an array of character ranges

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



109
110
111
112
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 109

def random_characters(utf8_ranges, length, seed = nil)
  (length.times.map { utf8_ranges.flatten }).flatten # flatten the arrays to a single array, then duplicate the array length
      .sample(length, random: get_rng(seed)).join("") # Sample the resultant array with a seeded random number generator
end

#random_chinese_characters(length, seed = nil) ⇒ String

produces a random Chinese language string The Chinese, Japanese and Korean (CJK) scripts share a common background, collectively known as CJK characters

Parameters:

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



61
62
63
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 61

def random_chinese_characters(length, seed = nil)
  random_characters(CHINESE_CHARACTERS, length, seed)
end

#random_english(length, seed = nil) ⇒ String

produces a random English language string

Parameters:

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



69
70
71
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 69

def random_english(length, seed = nil)
  random_characters(ENGLISH_CHARACTERS, length, seed)
end

#random_english_sentence(length, seed = nil) ⇒ String

produces a random English language sentence

Parameters:

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



77
78
79
80
81
82
83
84
85
86
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 77

def random_english_sentence(length, seed = nil)
  raise('length of sentence must be at least 2') unless length > 1
  chars = random_characters(ENGLISH_CHARACTERS, length, seed)
  index = 0
  while index + 13 < length
    index = index + (1..12).to_a.sample(1)
    chars.insert(index, ' ')
  end
  chars[0..(length-1)] + '.'
end

#random_german(length, seed = nil) ⇒ String

produces a random German string

Parameters:

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



100
101
102
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 100

def random_german(length, seed = nil)
  random_characters(GERMAN_CHARACTERS, length, seed)
end

#random_multi_lang(length, seed = nil) ⇒ String

produces a random multiple language string including Chinese, German and English characters

Parameters:

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



37
38
39
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 37

def random_multi_lang(length, seed=nil)
  random_characters([CHINESE_CHARACTERS, ENGLISH_CHARACTERS, GERMAN_CHARACTERS], length, seed)
end

#random_multi_lang_sentence(length, seed = nil) ⇒ String

produces a random multiple language sentence including Chinese, German and English characters

Parameters:

  • length (Int)
    • Length of string desired

  • seed (Int) (defaults to: nil)
    • Random seed for re-playing random value.

Returns:

  • (String)
    • The random string



45
46
47
48
49
50
51
52
53
54
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 45

def random_multi_lang_sentence(length, seed = nil)
  raise('length of sentence must be at least 2') unless length > 1
  chars = random_characters([CHINESE_CHARACTERS, ENGLISH_CHARACTERS, GERMAN_CHARACTERS], length, seed)
  index = 0
  while index + 13 < length
    index = index + (1..12).to_a.sample(1)[0]
    chars.insert(index, ' ')
  end
  chars[0..(length-2)] + '.'
end

#test_i18n_strings(string_length, exclude = [], &block) ⇒ Object

Creates an array of strings of a certain length, then iterates through them passing each string to a block for testing. You can optionally exclude syntax and white space chararcters if the input being tested does not allow them. Example: iterates through array of strings of length 10, testing all special characters according to the block test_i18n_strings(10) { |test_string|

# Enter data
create_user("User#{test_string})
# Validate data
verify_user_name_exists("User#{test_string}")

}

Parameters:

  • string_length (Int)
    • The length of the string you want to test with

  • exclude (Array<Symbol>) (defaults to: [])
    • String types to exclude from testing :syntax or :white_space



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/beaker-qa-i18n/i18n_string_generator.rb', line 127

def test_i18n_strings(string_length, exclude=[], &block)
  raise("test_i18n_strings requires a block with arity of 1") unless block_given? && block.arity == 1

  logger.debug('Testing Chinese characters')
  get_strings_of_length_from_char_array(CHINESE_CHARACTERS, string_length).each { |string|
    yield string
  }
  logger.debug 'Testing German characters'
  get_strings_of_length_from_char_array(GERMAN_CHARACTERS, string_length).each { |string|
    yield string
  }
  logger.debug 'Testing max length characters'
  get_strings_of_length_from_char_array(MAX_LENGTH_CHARACTERS, string_length).each { |string|
    yield string
  }
  logger.debug 'Testing syntax characters' unless exclude.include?(:syntax)
  get_strings_of_length_from_char_array(SYNTAX_CHARACTERS, string_length).each { |string|
    yield string
  } unless exclude.include?(:syntax)
  logger.debug 'Testing white space characters' unless exclude.include?(:white_space)
  get_strings_of_length_from_char_array(WHITE_SPACE_CHARACTERS, string_length).each { |string|
    yield string
  } unless exclude.include?(:white_space)
end