From e8e8abaac6d0c29433e324cb018b6cd995f45ce1 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Thu, 23 Jul 2020 16:39:00 +0100 Subject: [PATCH] Rename 'random.rb' => 'random_content_generator.rb' 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. --- .../{random.rb => random_content_generator.rb} | 2 +- lib/govuk_schemas/random_example.rb | 4 ++-- ...m_generator.rb => random_schema_generator.rb} | 16 ++++++++-------- ..._spec.rb => random_content_generator_spec.rb} | 4 ++-- ...r_spec.rb => random_schema_generator_spec.rb} | 10 +++++----- 5 files changed, 18 insertions(+), 18 deletions(-) rename lib/govuk_schemas/{random.rb => random_content_generator.rb} (99%) rename lib/govuk_schemas/{random_item_generator.rb => random_schema_generator.rb} (90%) rename spec/lib/{random_spec.rb => random_content_generator_spec.rb} (50%) rename spec/lib/{random_item_generator_spec.rb => random_schema_generator_spec.rb} (85%) diff --git a/lib/govuk_schemas/random.rb b/lib/govuk_schemas/random_content_generator.rb similarity index 99% rename from lib/govuk_schemas/random.rb rename to lib/govuk_schemas/random_content_generator.rb index e78480c..7996fbe 100644 --- a/lib/govuk_schemas/random.rb +++ b/lib/govuk_schemas/random_content_generator.rb @@ -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 diff --git a/lib/govuk_schemas/random_example.rb b/lib/govuk_schemas/random_example.rb index c17e87c..7529a5c 100644 --- a/lib/govuk_schemas/random_example.rb +++ b/lib/govuk_schemas/random_example.rb @@ -1,4 +1,4 @@ -require "govuk_schemas/random_item_generator" +require "govuk_schemas/random_schema_generator" require "json-schema" require "json" @@ -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. diff --git a/lib/govuk_schemas/random_item_generator.rb b/lib/govuk_schemas/random_schema_generator.rb similarity index 90% rename from lib/govuk_schemas/random_item_generator.rb rename to lib/govuk_schemas/random_schema_generator.rb index 7b8b9db..c7be70b 100644 --- a/lib/govuk_schemas/random_item_generator.rb +++ b/lib/govuk_schemas/random_schema_generator.rb @@ -1,7 +1,7 @@ -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 @@ -9,7 +9,7 @@ module GovukSchemas # 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? @@ -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 @@ -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) \ @@ -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 diff --git a/spec/lib/random_spec.rb b/spec/lib/random_content_generator_spec.rb similarity index 50% rename from spec/lib/random_spec.rb rename to spec/lib/random_content_generator_spec.rb index 7d01412..7f7b480 100644 --- a/spec/lib/random_spec.rb +++ b/spec/lib/random_content_generator_spec.rb @@ -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 diff --git a/spec/lib/random_item_generator_spec.rb b/spec/lib/random_schema_generator_spec.rb similarity index 85% rename from spec/lib/random_item_generator_spec.rb rename to spec/lib/random_schema_generator_spec.rb index ce2fde0..823df46 100644 --- a/spec/lib/random_item_generator_spec.rb +++ b/spec/lib/random_schema_generator_spec.rb @@ -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 = { @@ -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 @@ -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 @@ -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 @@ -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")