File: test_number.rb

package info (click to toggle)
ruby-ffaker 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,776 kB
  • sloc: ruby: 12,788; makefile: 8; sh: 1
file content (57 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

require_relative 'helper'

class TestNumber < Test::Unit::TestCase
  include DeterministicHelper

  assert_methods_are_deterministic(FFaker::Number, :number, :decimal, :between)

  def setup
    @tester = FFaker::Number
  end

  def test_number
    assert @tester.number.is_a?(Integer)
    assert @tester.number.digits.count == 1
    assert @tester.number(digits: 3).digits.count == 3
    assert_match(/\d/, @tester.number.to_s)
    assert_match(/\d{3}/, @tester.number(digits: 3).to_s)
  end

  def test_number_when_invalid_argument
    assert_raise(ArgumentError.new('Digits cannot be less than 1')) { @tester.number(digits: 0) }
  end

  def test_decimal
    assert @tester.decimal.is_a?(Float)

    whole_digits = rand(2..5)
    fractional_digits = rand(2..5)
    decimal = @tester.decimal(whole_digits: whole_digits, fractional_digits: fractional_digits)
    assert decimal.to_s.split('.').first.size == whole_digits
    assert decimal.to_s.split('.').last.size == fractional_digits
  end

  def test_decimal_when_invalid_argument
    assert_raise(ArgumentError.new('Digits cannot be less than 1')) do
      @tester.decimal(whole_digits: 0)
    end

    assert_raise(ArgumentError.new('Digits cannot be less than 1')) do
      @tester.decimal(fractional_digits: 0)
    end
  end

  def test_between
    from = -50
    to = 50
    assert_random_between(from..to) { @tester.between(from: from, to: to) }
    assert_instance_of Integer, @tester.between(from: from, to: to)

    from = -50.0
    to = 50.0
    assert_random_between(from..to) { @tester.between(from: from, to: to) }
    assert_instance_of Float, @tester.between(from: from, to: to)
  end
end