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
-
#get_i18n_string(character_type) ⇒ String
Gets a string consisting of all values in this library for the specified character type.
-
#get_rng(seed = nil) ⇒ Object
Gets a random number generator with optional seed.
-
#random_alpha_numeric(length, seed = nil) ⇒ String
produces a random English alpha-numeric string.
-
#random_characters(utf8_ranges, length, seed = nil) ⇒ String
Generate random strings from various utf8 character ranges.
-
#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.
-
#random_english(length, seed = nil) ⇒ String
produces a random English language string.
-
#random_english_sentence(length, seed = nil) ⇒ String
produces a random English language sentence.
-
#random_german(length, seed = nil) ⇒ String
produces a random German string.
-
#random_multi_lang(length, seed = nil) ⇒ String
produces a random multiple language string including Chinese, German and English characters.
-
#random_multi_lang_sentence(length, seed = nil) ⇒ String
produces a random multiple language sentence including Chinese, German and English characters.
-
#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.
Instance Method Details
#get_i18n_string(character_type) ⇒ String
Gets a string consisting of all values in this library for the specified 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
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
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.
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
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
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
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
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
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
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}")
}
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 |