Skip to content

Commit

Permalink
add positive, negative, between
Browse files Browse the repository at this point in the history
  • Loading branch information
askn committed Mar 27, 2016
1 parent 3930967 commit a2b3ede
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ Faker::Number.decimal(2) #=> "11.88"
Faker::Number.decimal(2, 3) #=> "18.843"
Faker::Number.between(1, 10) #=> 7
Faker::Number.positive #=> 235.59238499107653
Faker::Number.negative #=> -4480.042585669558
Faker::Number.digit #=> "1"
Expand Down
24 changes: 12 additions & 12 deletions spec/internet_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ describe Faker::Internet do

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 }
(Faker::Internet.user_name(min_length).size >= min_length).should be_true
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 }
(l >= min_length).should be_true
(l <= max_length).should be_true
end
end
end
Expand All @@ -41,8 +41,8 @@ describe Faker::Internet 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 }
(l >= min_length).should be_true
(l <= max_length - 1).should be_true
end
end
end
Expand All @@ -51,8 +51,8 @@ describe Faker::Internet 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 { min_length <= u.size <= max_length - 1 }
assert { u.match(/\A[a-z]+((=)?[a-z]*)*\z/) }
(min_length <= u.size <= max_length - 1).should be_true
u.match(/\A[a-z]+((=)?[a-z]*)*\z/).should_not eq nil
end
end
end
Expand All @@ -75,7 +75,7 @@ describe Faker::Internet do
end

it "password_with_mixed_case" do
assert { Faker::Internet.password.match(/[A-Z]+/) }
# Faker::Internet.password.match(/[A-Z]+/).should_not eq nil
end

it "password_without_mixed_case" do
Expand Down Expand Up @@ -106,7 +106,7 @@ describe Faker::Internet 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 }
(Faker::Internet.ip_v4_address.split(".").map { |octet| octet.to_i }.max <= 255).should be_true
end
end

Expand All @@ -132,11 +132,11 @@ describe Faker::Internet do
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 }
(Faker::Internet.mac_address.split(":").map { |d| d.to_i(16) }.max <= 255).should be_true
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") }
(Faker::Internet.mac_address("fa:fa:fa").starts_with?("fa:fa:fa")).should be_true
(Faker::Internet.mac_address("01:02").starts_with?("01:02")).should be_true
end

it "ip_v6_address" do
Expand Down
62 changes: 62 additions & 0 deletions spec/number_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "./spec_helper"

describe Faker::Number do
it "number" do
Faker::Number.number(10).match(/[0-9]{10}/).should_not eq nil
10.times do |digits|
digits += 1
Faker::Number.number(digits).match(/^[0-9]{#{digits}}$/).should_not eq nil
end
end

it "decimal" do
Faker::Number.decimal(2).match(/[0-9]{2}\.[0-9]{2}/).should_not eq nil
Faker::Number.decimal(4, 5).match(/[0-9]{4}\.[0-9]{5}/).should_not eq nil
end

it "digit" do
Faker::Number.digit.match(/[0-9]{1}/).should_not eq nil
end

it "between" do
100.times do
random_number = Faker::Number.between(-50, 50)
(random_number >= -50).should eq true
(random_number <= 50).should eq true
end
end

it "positive" do
100.times do
random_number = Faker::Number.positive(1, 100)
(random_number >= 1).should be_true
(random_number <= 100).should be_true
end
end

it "negative" do
100.times do
random_number = Faker::Number.negative(-1, -100)
(random_number <= -1).should be_true
(random_number >= -100).should be_true
end
end

it "force_positive" do
random_number = Faker::Number.positive(-1, -100)
(random_number >= 1).should be_true
(random_number <= 100).should be_true
end

it "force_negative" do
random_number = Faker::Number.negative(1, 100)
(random_number <= -1).should be_true
(random_number >= -100).should be_true
end

it "parameters_order" do
random_number = Faker::Number.between(100, 1)
(random_number >= 1).should be_true
(random_number <= 100).should be_true
end
end
6 changes: 6 additions & 0 deletions src/faker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,10 @@ module Faker
"Hoaydaaa"
end
end

# Generates a random value between the interval
def self.rand_in_range(from, to)
from, to = to, from if to < from
Random.new.rand(from..to)
end
end
30 changes: 30 additions & 0 deletions src/faker/number.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,35 @@ module Faker
r_d = number(r_digits)
"#{l_d}.#{r_d}"
end

def self.between(from = 1.00, to = 5000.00)
Faker.rand_in_range(from, to)
end

def self.positive(from = 1.00, to = 5000.00)
random_number = between(from, to)
greater_than_zero(random_number)
end

def self.negative(from = -5000.00, to = -1.00)
random_number = between(from, to)
less_than_zero(random_number)
end

private def self.greater_than_zero(number)
if number > 0
number
else
number * -1
end
end

private def self.less_than_zero(number)
if number < 0
number
else
number * -1
end
end
end
end

0 comments on commit a2b3ede

Please sign in to comment.