From 2f11a2121f34f4d88d142351f815665e05043062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=C5=9Fk=C4=B1n=20Gedik?= Date: Sun, 17 Jan 2016 22:31:08 +0200 Subject: [PATCH] add team & company.logo --- README.md | 29 +++++++- spec/company_spec.cr | 8 ++ spec/faker_spec.cr | 6 +- spec/internet_spec.cr | 169 ++++++++++++++++++++++++++++++++++++++++++ spec/team_spec.cr | 19 +++++ src/faker/company.cr | 5 ++ src/faker/internet.cr | 39 +++++++++- src/faker/lorem.cr | 8 +- src/faker/team.cr | 15 ++++ 9 files changed, 287 insertions(+), 11 deletions(-) create mode 100644 spec/company_spec.cr create mode 100644 spec/internet_spec.cr create mode 100644 spec/team_spec.cr create mode 100644 src/faker/team.cr diff --git a/README.md b/README.md index c259256..c137fb9 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,6 @@ Faker::Address.longitude #=> "-156.65548382095133" ``` ### Faker::Business ------------------- ```crystal @@ -93,6 +92,8 @@ Faker::Company.name #=> "Hirthe-Ritchie" Faker::Company.suffix #=> "Group" +# Get a random company logo url in PNG format. +Faker::Company.logo #=> "https://pigment.github.com/fake-logos/logos/medium/color/5.png" ``` @@ -136,8 +137,14 @@ Faker::Internet.ip_v4_address #=> "24.29.18.175" Faker::Internet.ip_v6_address #=> "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df" +# Optional argument prefix='' +Faker::Internet.mac_address #=> "e6:0d:00:11:ed:4f" +Faker::Internet.mac_address('55:44:33') #=> "55:44:33:02:1d:9b" + # Optional arguments: host=domain_name, path="/#{user_name}" Faker::Internet.url #=> "http://thiel.com/chauncey_simonis" +Faker::Internet.url('example.com') #=> "http://example.com/clotilde.swift" +Faker::Internet.url('example.com', '/foobar.html') #=> "http://example.com/foobar.html" # Optional arguments: words=nil, glue=nil Faker::Internet.slug #=> "pariatur_laudantium" @@ -165,6 +172,9 @@ Faker::Lorem.characters(10) #=> "ang9cbhoa8" Faker::Lorem.sentence #=> "Dolore illum animi et neque accusantium." Faker::Lorem.sentence(3) #=> "Commodi qui minus deserunt sed vero quia." Faker::Lorem.sentence(3, true) #=> "Inflammatio denego necessitatibus caelestis autus illum." +Faker::Lorem.sentence(3, false, 4) #=> "Aut voluptatem illum fugit ut sit." +Faker::Lorem.sentence(3, true, 4) #=> "Accusantium tantillus dolorem timor." + # Optional arguments: sentence_count=3, supplemental=false Faker::Lorem.sentences #=> ["Vero earum commodi soluta.", "Quaerat fuga cumque et vero eveniet omnis ut.", "Cumque sit dolor ut est consequuntur."] Faker::Lorem.sentences(1) #=> ["Ut perspiciatis explicabo possimus doloribus enim quia."] @@ -174,6 +184,8 @@ Faker::Lorem.sentences(1, true) #=> ["Quis capillus curo ager veritatis voro et Faker::Lorem.paragraph #=> "Neque dicta enim quasi. Qui corrupti est quisquam. Facere animi quod aut. Qui nulla consequuntur consectetur sapiente." Faker::Lorem.paragraph(2) #=> "Illo qui voluptas. Id sit quaerat enim aut cupiditate voluptates dolorum. Porro necessitatibus numquam dolor quia earum." Faker::Lorem.paragraph(2, true) #=> "" +Faker::Lorem.paragraph(2, false, 4) #=> "Neque aut et nemo aut incidunt voluptates. Dolore cum est sint est. Vitae assumenda porro odio dolores fugiat. Est voluptatum quia rerum." +Faker::Lorem.paragraph(2, true, 4) #=> "Vomito unde uxor annus. Et patior utilis sursum." # Optional arguments: paragraph_count=3, supplemental=false Faker::Lorem.paragraphs #=> "" @@ -225,6 +237,21 @@ Faker::PhoneNumber.phone_number #=> "397.693.1309" ``` +### Faker::Team + +```crystal + +# Random Team Creature +Faker::Team.creature #=> "gooses" + +# Random Team Name created from random US State (Faker::Address.state) prepended to a random Team Creature +Faker::Team.name #=> "Oregon vixens" + +# Random Team State +Faker::Team.state #=> "Oregon" + +``` + ## Contributing 1. Fork it ( https://github.com/askn/faker/fork ) diff --git a/spec/company_spec.cr b/spec/company_spec.cr new file mode 100644 index 0000000..677299f --- /dev/null +++ b/spec/company_spec.cr @@ -0,0 +1,8 @@ +require "./spec_helper" +require "http/client" + +describe Faker::Company do + it "logo" do + Faker::Company.logo.match(%r{https://pigment.github.io/fake-logos/logos/medium/color/\d+\.png}).should_not eq nil + end +end diff --git a/spec/faker_spec.cr b/spec/faker_spec.cr index a39dea8..167bf23 100644 --- a/spec/faker_spec.cr +++ b/spec/faker_spec.cr @@ -3,12 +3,12 @@ require "./spec_helper" describe Faker do it "numerify" do 100.times do - assert { Faker.numerify("###").match(/[1-9]\d{2}/).should_not eq nil } + Faker.numerify("###").match(/[1-9]\d{2}/).should_not eq nil end end it "letterify" do - assert { Faker.letterify("???").match(/[A-Z]{3}/).should_not eq nil } + Faker.letterify("???").match(/[A-Z]{3}/).should_not eq nil end it "regexify" do @@ -17,7 +17,7 @@ describe Faker do "us phone" => /^(1-?)[2-8][0-1][0-9]-\d{3}-\d{4}$/, }.each do |label, re| 10.times do - assert { Faker.regexify(re).match(re).should_not eq nil } + Faker.regexify(re).match(re).should_not eq nil end end end diff --git a/spec/internet_spec.cr b/spec/internet_spec.cr new file mode 100644 index 0000000..c838a28 --- /dev/null +++ b/spec/internet_spec.cr @@ -0,0 +1,169 @@ +require "./spec_helper" + +describe Faker::Internet do + it "email" do + Faker::Internet.email.match(/.+@.+\.\w+/).should_not eq nil + end + + it "free_email" do + Faker::Internet.free_email.match(/.+@(gmail|hotmail|yahoo)\.com/).should_not eq nil + end + + it "safe_email" do + Faker::Internet.safe_email.match(/.+@example.(com|net|org)/).should_not eq nil + end + + it "user_name" do + Faker::Internet.user_name.match(/[a-z]+((_|\.)[a-z]+)?/).should_not eq nil + end + + it "user_name_with_string_arg" do + Faker::Internet.user_name("bo peep").match(/(bo(_|\.)peep|peep(_|\.)bo)/).should_not eq nil + end + + it "user_name_with_integer_arg" do + (1..32).to_a.each do |min_length| + assert { Faker::Internet.user_name(min_length).size >= min_length } + end + end + + it "user_name_with_closed_range_arg" do + (1..32).to_a.each do |min_length| + (min_length..32).each do |max_length| + l = Faker::Internet.user_name((min_length..max_length)).size + assert { l >= min_length } + assert { l <= max_length } + end + end + end + + it "user_name_with_open_range_arg" do + (1..32).to_a.each do |min_length| + (min_length + 1..33).each do |max_length| + l = Faker::Internet.user_name((min_length...max_length)).size + assert { l >= min_length } + assert { l <= max_length - 1 } + end + end + end + + it "user_name_with_range_and_separators" do + (1..32).to_a.each do |min_length| + (min_length + 1..33).each do |max_length| + u = Faker::Internet.user_name((min_length...max_length), %w(=)) + assert { u.size.between? min_length, max_length - 1 } + assert { u.match(/\A[a-z]+((=)?[a-z]*)*\z/) } + end + end + end + + it "password" do + Faker::Internet.password.match(/\w{3}/).should_not eq nil + end + + it "password_with_integer_arg" do + # (1..32).to_a.each do |min_length| + # assert { Faker::Internet.password(min_length).size >= min_length } + # end + end + + it "password_max_with_integer_arg" do + # (1..32).to_a.each do |min_length| + # max_length = min_length + 4 + # assert { Faker::Internet.password(min_length, max_length).size <= max_length } + # end + end + + it "password_with_mixed_case" do + assert { Faker::Internet.password.match(/[A-Z]+/) } + end + + it "password_without_mixed_case" do + # Faker::Internet.password(8, 12, false).match(/[^A-Z]+/).should_not eq nil + end + + it "password_with_special_chars" do + # Faker::Internet.password(8, 12, true, true).match(/[!@#\$%\^&\*]+/).should_not eq nil + end + + it "password_without_special_chars" do + # Faker::Internet.password(8, 12, true).match(/[^!@#\$%\^&\*]+/).should_not eq nil + end + + it "domain_name" do + Faker::Internet.domain_name.match(/\w+\.\w+/).should_not eq nil + end + + it "domain_word" do + Faker::Internet.domain_word.match(/^\w+$/).should_not eq nil + end + + it "domain_suffix" do + Faker::Internet.domain_suffix.match(/^\w+(\.\w+)?/).should_not eq nil + end + + it "ip_v4_address" do + Faker::Internet.ip_v4_address.count('.').should eq 3 + + 100.times do + assert { Faker::Internet.ip_v4_address.split(".").map { |octet| octet.to_i }.max <= 255 } + end + end + + it "public_ip_v4_address" do + ten_dot = /^10\./ + one_two_seven = /^127\./ + one_six_nine = /^169\.254/ + one_nine_two = /^192\.168\./ + one_seven_two = /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./ + + 1000.times do + # address = Faker::Internet.public_ip_v4_address + # assert_not_match ten_dot, address + # assert_not_match one_two_seven, address + # assert_not_match one_six_nine, address + # assert_not_match one_nine_two, address + # assert_not_match one_seven_two, address + end + end + + it "mac_address" do + Faker::Internet.mac_address.count(":").should eq 5 + Faker::Internet.mac_address("").count(":").should eq 5 + + 100.times do + assert { Faker::Internet.mac_address.split(":").map { |d| d.to_i(16) }.max <= 255 } + end + + assert { Faker::Internet.mac_address("fa:fa:fa").starts_with?("fa:fa:fa") } + assert { Faker::Internet.mac_address("01:02").starts_with?("01:02") } + end + + it "ip_v6_address" do + Faker::Internet.ip_v6_address.count(":").should eq 7 + + 100.times do + # assert { Faker::Internet.ip_v6_address.split(".").map { |h| "0x#{h}".to_i }.max <= 65535 } + end + end + + it "slug" do + Faker::Internet.slug.match(/^[a-z]+(_|\.|\-)[a-z]+$/).should_not eq nil + end + + it "slug_with_content_arg" do + Faker::Internet.slug("Foo bAr baZ").match(/^foo(_|\.|\-)bar(_|\.|\-)baz$/).should_not eq nil + end + + it "slug_with_glue_arg" do + Faker::Internet.slug(nil, "+").match(/^[a-z]+\+[a-z]+$/).should_not eq nil + end + + it "url" do + Faker::Internet.url("domain.com", "/username").match(/^http:\/\/domain\.com\/username$/).should_not eq nil + end + + it "device_token" do + # Faker::Internet.device_token.size.should eq 64 + end +end diff --git a/spec/team_spec.cr b/spec/team_spec.cr new file mode 100644 index 0000000..14abf5e --- /dev/null +++ b/spec/team_spec.cr @@ -0,0 +1,19 @@ +require "./spec_helper" + +describe Faker::Team do + it "name" do + Faker::Team.name.match(/(\w+\.? ?){2}/).should_not eq nil + end + + it "creature" do + Faker::Team.creature.match(/(\w+){1}/).should_not eq nil + end + + it "state" do + Faker::Team.state.match(/(\w+){1}/).should_not eq nil + end + + it "sport" do + # Faker::Team.sport.match(/(\w+){1}/).should_not eq nil + end +end diff --git a/src/faker/company.cr b/src/faker/company.cr index b09a255..dfd3222 100644 --- a/src/faker/company.cr +++ b/src/faker/company.cr @@ -17,5 +17,10 @@ module Faker data = Data["company"]["bs"] as Array(Array(String)) Faker.fetch(data.flatten) end + + def self.logo + rand_num = Random.rand(13) + 1 + "https://pigment.github.io/fake-logos/logos/medium/color/#{rand_num}.png" + end end end diff --git a/src/faker/internet.cr b/src/faker/internet.cr index e128afd..8637cd4 100644 --- a/src/faker/internet.cr +++ b/src/faker/internet.cr @@ -12,7 +12,34 @@ module Faker [user_name(name), "example." + %w(org com net).shuffle.first].join("@") end - def self.user_name(name = nil, separators = %w(. _)) + def self.user_name(specifier = nil, separators = %w(. _)) + if specifier.is_a? String + return specifier.scan(/\w+/).map { |s| s[0] }.shuffle.join(separators.sample).downcase + elsif specifier.is_a? Int + tries = 0 # Don't try forever in case we get something like 1_000_000. + result = "" + while (tries < 7) + result = user_name(nil, separators) + tries += 1 + unless result.size < specifier + break + end + end + until result.size >= specifier + result = result * 2 + end + return result + elsif specifier.is_a? Range + tries = 0 + result = "" + while (tries < 7) + result = user_name(specifier.min, separators) + tries += 1 + break if specifier.includes?(result.size) + end + return result[0...specifier.max] + end + return name.scan(/\w+/).shuffle.map(&.[0]).join(separators.sample).downcase if name [ ->{ Name.first_name.gsub(/\W/, "").downcase }, @@ -51,8 +78,14 @@ module Faker container.map { |n| n.to_s(16) }.join(':') end - def self.url - "http://#{domain_name}/#{user_name}" + def self.mac_address(prefix = "") + prefix_digits = prefix.split(":").map { |d| d.to_i?(16) ? d.to_i?(16) : 0 } + address_digits = (1..(6 - prefix_digits.size)).map { rand(256) } + (prefix_digits + address_digits).map { |d| "%02x" % d }.join(":") + end + + def self.url(host = domain_name, path = "/#{user_name}") + "http://#{host}#{path}" end def self.slug(words = nil, glue = nil) diff --git a/src/faker/lorem.cr b/src/faker/lorem.cr index 16ae0d4..9e8b468 100644 --- a/src/faker/lorem.cr +++ b/src/faker/lorem.cr @@ -22,8 +22,8 @@ module Faker words.shuffle[0, num] end - def self.sentence(word_count = 4, supplemental = false) - words(word_count + rand(6), supplemental).join(" ").capitalize + "." + def self.sentence(word_count = 4, supplemental = false, random_words_to_add = 6) + words(word_count + rand(random_words_to_add.to_i).to_i, supplemental).join(" ").capitalize + "." end def self.sentences(sentence_count = 3, supplemental = false) @@ -34,8 +34,8 @@ module Faker end end - def self.paragraph(sentence_count = 3, supplemental = false) - sentences(sentence_count + rand(3), supplemental).join(" ") + def self.paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3) + sentences(sentence_count + rand(random_sentences_to_add.to_i).to_i, supplemental).join(" ") end def self.paragraphs(paragraph_count = 3, supplemental = false) diff --git a/src/faker/team.cr b/src/faker/team.cr new file mode 100644 index 0000000..fb4779f --- /dev/null +++ b/src/faker/team.cr @@ -0,0 +1,15 @@ +module Faker + class Team + def self.name + Faker.fetch(Data["team"]["name"]) + end + + def self.creature + Faker.fetch(Data["team"]["creature"]) + end + + def self.state + Faker.fetch(Data["address"]["state"]) + end + end +end