Skip to content

Commit

Permalink
Rename 'random.rb' => 'random_content_generator.rb'
Browse files Browse the repository at this point in the history
This is to avoid clashing with the ruby standard lib
'Random' module, which we'll be using in the next
commit.

At the same time, I've renamed 'random_item_generator.rb' to
'random_schema_generator.rb', which is more reflective of its
purpose and helps to distinguish between the two classes.
  • Loading branch information
ChrisBAshton committed Jul 24, 2020
1 parent 9ddca1d commit e8e8aba
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GovukSchemas
# @private
module Random
module RandomContentGenerator
class << self
WORDS = %w[Lorem ipsum dolor sit amet consectetur adipiscing elit. Ut suscipit at mauris non bibendum. Ut ac massa est. Aenean tempor imperdiet leo vel interdum. Nam sagittis cursus sem ultricies scelerisque. Quisque porttitor risus vel risus finibus eu sollicitudin nisl aliquet. Sed sed lectus ac dolor molestie interdum. Nam molestie pellentesque purus ac vestibulum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse non tempor eros. Mauris eu orci hendrerit volutpat lorem in tristique libero. Duis a nibh nibh.].freeze

Expand Down
4 changes: 2 additions & 2 deletions lib/govuk_schemas/random_example.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "govuk_schemas/random_item_generator"
require "govuk_schemas/random_schema_generator"
require "json-schema"
require "json"

Expand Down Expand Up @@ -33,7 +33,7 @@ class RandomExample
# @return [GovukSchemas::RandomExample]
def initialize(schema:, seed: nil)
@schema = schema
@random_generator = RandomItemGenerator.new(schema: schema, seed: seed)
@random_generator = RandomSchemaGenerator.new(schema: schema, seed: seed)
end

# Returns a new `GovukSchemas::RandomExample` object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require "govuk_schemas/random"
require "govuk_schemas/random_content_generator"

module GovukSchemas
# The RandomItemGenerator takes a JSON schema and outputs a random hash that
# The RandomSchemaGenerator takes a JSON schema and outputs a random hash that
# is valid against said schema.
#
# The "randomness" here is quote relative, it's particularly tailored to the
# GOV.UK content schemas. For example, strings are limited to a couple of
# hundred characters to keep the resulting items small.
#
# @private
class RandomItemGenerator
class RandomSchemaGenerator
def initialize(schema:, seed: nil)
@schema = schema
srand(seed) unless seed.nil?
Expand Down Expand Up @@ -71,7 +71,7 @@ def generate_value(props)
elsif type == "array"
generate_random_array(props)
elsif type == "boolean"
Random.bool
RandomContentGenerator.bool
elsif type == "integer"
min = props["minimum"] || 0
max = props["maximum"] || 10
Expand All @@ -93,7 +93,7 @@ def generate_random_object(subschema)
# populate all of the keys in the hash. This isn't quite random, but I
# haven't found a nice way yet to ensure there's at least n elements in
# the hash.
should_generate_value = Random.bool \
should_generate_value = RandomContentGenerator.bool \
|| subschema["required"].to_a.include?(attribute_name) \
|| (one_of_sample["required"] || {}).to_a.include?(attribute_name) \
|| (one_of_sample["properties"] || {}).keys.include?(attribute_name) \
Expand Down Expand Up @@ -125,11 +125,11 @@ def generate_random_array(props)

def generate_random_string(props)
if props["format"]
Random.string_for_type(props["format"])
RandomContentGenerator.string_for_type(props["format"])
elsif props["pattern"]
Random.string_for_regex(props["pattern"])
RandomContentGenerator.string_for_regex(props["pattern"])
else
Random.string(props["minLength"], props["maxLength"])
RandomContentGenerator.string(props["minLength"], props["maxLength"])
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "spec_helper"

RSpec.describe GovukSchemas::Random do
RSpec.describe GovukSchemas::RandomContentGenerator do
describe ".random_identifier" do
it "generates a string" do
string = GovukSchemas::Random.random_identifier(separator: "_")
string = GovukSchemas::RandomContentGenerator.random_identifier(separator: "_")

expect(string).to be_a(String)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec_helper"

RSpec.describe GovukSchemas::RandomItemGenerator do
RSpec.describe GovukSchemas::RandomSchemaGenerator do
describe "#payload" do
it "generates an object with a required property" do
schema = {
Expand All @@ -13,7 +13,7 @@
},
}

generator = GovukSchemas::RandomItemGenerator.new(schema: schema)
generator = GovukSchemas::RandomSchemaGenerator.new(schema: schema)

expect(generator.payload.keys).to include("my_field")
end
Expand All @@ -34,7 +34,7 @@
},
}

generator = GovukSchemas::RandomItemGenerator.new(schema: schema)
generator = GovukSchemas::RandomSchemaGenerator.new(schema: schema)

expect(generator.payload.keys).to include("my_field")
end
Expand All @@ -59,7 +59,7 @@
},
}

generator = GovukSchemas::RandomItemGenerator.new(schema: schema)
generator = GovukSchemas::RandomSchemaGenerator.new(schema: schema)

expect(generator.payload.keys).to include("my_field")
end
Expand Down Expand Up @@ -87,7 +87,7 @@
],
}

generator = GovukSchemas::RandomItemGenerator.new(schema: schema)
generator = GovukSchemas::RandomSchemaGenerator.new(schema: schema)

expect(generator.payload["my_enum"]).to eq("a")
expect(generator.payload.keys).to include("my_field")
Expand Down

0 comments on commit e8e8aba

Please sign in to comment.