File: test_array_sample_method_compat.rb

package info (click to toggle)
ruby-faker 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,596 kB
  • sloc: ruby: 20,656; sh: 6; makefile: 6
file content (64 lines) | stat: -rw-r--r-- 1,617 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
58
59
60
61
62
63
64
# frozen_string_literal: true

require_relative '../../test_helper'

# when these tests are run under Ruby 1.8.7, they will use the
# self-defined Array#sample method in lib/extensions/array and will test whether it
# behaves as the built-in Array#sample method from Ruby 1.9 or greater.
# Under Ruby 1.9, they simply use the built-in Array#sample method
class TestArraySampleMethodCompatibility < Test::Unit::TestCase
  def test_returns_nil_or_empty_array_with_empty_source
    source = []

    result = source.sample

    assert_nil result

    result = source.sample(1)

    assert_empty(result)
  end

  def test_returns_one_array_elem_without_param
    source = %w[foo bar]
    result = source.sample

    assert_includes source, result
  end

  def test_returns_empty_array_with_param_zero
    source = %w[foo bar]
    result = source.sample(0)

    assert_empty(result)
  end

  def test_returns_an_array_with_integer_param
    source = %w[foo bar baz]
    result = source.sample(2)

    assert_kind_of Array, result
    assert_equal(2, result.length)
    assert_empty(result - source)
  end

  def test_returns_source_array_with_integer_param_equal_or_bigger_than_source_length
    source = %w[foo bar]
    result = source.sample(2)

    assert_kind_of Array, result
    assert_predicate(source.sort <=> result.sort, :zero?)

    result = source.sample(3)

    assert_kind_of Array, result
    assert_predicate(source.sort <=> result.sort, :zero?)
  end

  def test_raises_argument_error_with_negative_param
    source = %w[foo bar]
    assert_raise ArgumentError do
      source.sample(-1)
    end
  end
end