lib/randomized.rb in flores-0.0.1 vs lib/randomized.rb in flores-0.0.2

- old
+ new

@@ -1,39 +1,69 @@ +# This file is part of ruby-flores. +# Copyright (C) 2015 Jordan Sissel +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# encoding: utf-8 + # A collection of methods intended for use in randomized testing. module Randomized + # A selection of UTF-8 characters + # + # I'd love to generate this, but I don't yet know enough about how unicode + # blocks are allocated to do that. For now, hardcode a set of possible + # characters. + CHARACTERS = [ + # Basic Latin + *(32..126).map(&:chr), + + # hand-selected CJK Unified Ideographs Extension A + "㐤", "㐨", "㐻", "㑐", + + # hand-selected Hebrew + "א", "ב", "ג", "ד", "ה", + + # hand-selected Cyrillic + "Є", "Б", "Р", "н", "я" + ] + # Generates text with random characters of a given length (or within a length range) # # * The length can be a number or a range `x..y`. If a range, it must be ascending (x < y) # * Negative lengths are not permitted and will raise an ArgumentError # # @param length [Fixnum or Range] the length of text to generate # @return [String] the def self.text(length) - if length.is_a?(Range) - raise ArgumentError, "Requires ascending range, you gave #{length}." if length.end < length.begin - raise ArgumentError, "A negative length is not permitted, I received range #{length}" if length.begin < 0 + return text_range(length) if length.is_a?(Range) - length = integer(length) - else - raise ArgumentError, "A negative length is not permitted, I received #{length}" if length < 0 - end - + raise ArgumentError, "A negative length is not permitted, I received #{length}" if length < 0 length.times.collect { character }.join end # def text + def self.text_range(range) + raise ArgumentError, "Requires ascending range, you gave #{range}." if range.end < range.begin + raise ArgumentError, "A negative range values are not permitted, I received range #{range}" if range.begin < 0 + text(integer(range)) + end + # Generates a random character (A string of length 1) # # @return [String] def self.character - # TODO(sissel): Add support to generate valid UTF-8. I started reading - # Unicode 7 (http://www.unicode.org/versions/Unicode7.0.0/) and after much - # reading, I realized I wasn't in my house anymore but had somehow lost - # track of time and was alone in a field. Civilization had fallen centuries - # ago. :P - - # Until UTF-8 is supported, just return a random lower ASCII character - integer(32..127).chr + return CHARACTERS[integer(0...CHARACTERS.length)] end # def character # Return a random integer value within a given range. # # @param range [Range] @@ -62,6 +92,6 @@ nil else integer(range).times end end # def iterations -end +end # module Randomized