File: test_faker_lorem.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 (185 lines) | stat: -rw-r--r-- 5,781 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerLorem < Test::Unit::TestCase
  def setup
    @tester = Faker::Lorem
    @standard_wordlist = I18n.translate('faker.lorem.words')
    @complete_wordlist =
      @standard_wordlist + I18n.translate('faker.lorem.supplemental')
  end

  def test_character
    assert_equal(1, @tester.character.length)
  end

  def test_character_type
    assert_instance_of String, @tester.character
  end

  def test_characters
    assert_equal(255, @tester.characters.length)
  end

  def test_characters_negatives
    assert_equal '', @tester.characters(number: -1)
    assert_equal '', @tester.characters(number: (-2..-1))
    assert_equal '', @tester.characters(number: [-1, -2])
  end

  def test_characters_with_args
    deterministically_verify -> { @tester.characters(number: 500).length }, depth: 5 do |character_length|
      assert_equal(500, character_length)
    end
  end

  # Words delivered by a standard request should be on the standard wordlist.
  def test_standard_words
    @words = @tester.words(number: 1000)

    assert_equal 1000, @words.length
    @words.each { |w| assert_includes @standard_wordlist, w }
  end

  # Words requested from the supplemental list should all be in that list.
  def test_supplemental_words
    @words = @tester.words(number: 10_000, supplemental: true)

    @words.each { |w| assert_includes @complete_wordlist, w }
  end

  # Faker::Lorem.word generates random word from standard wordlist
  def test_word
    @standard_wordlist = I18n.translate('faker.lorem.words')

    deterministically_verify -> { @tester.word }, depth: 5 do |word|
      assert_includes @standard_wordlist, word
    end
  end

  def test_excluded_words_on_word
    excluded_words_array = @tester.words(number: 2)
    w = @tester.word(exclude_words: excluded_words_array)

    assert_not_equal w, excluded_words_array[0]
    assert_not_equal w, excluded_words_array[1]
  end

  def test_excluded_words_as_string_on_word
    excluded_words_array = @tester.words(number: 2)
    excluded_word_string = excluded_words_array.join(', ')
    w = @tester.word(exclude_words: excluded_word_string)

    assert_not_equal w, excluded_words_array[0]
    assert_not_equal w, excluded_words_array[1]
  end

  def test_exact_sentence_word_count
    assert_equal 2, @tester.sentence(word_count: 2, supplemental: false, random_words_to_add: 0).split.length
  end

  def test_exact_count_param
    assert_equal(2, @tester.characters(number: 2).length)
    assert_equal(2, @tester.words(number: 2).length)
    assert_equal(2, @tester.sentences(number: 2).length)
    assert_equal(2, @tester.paragraphs(number: 2).length)
  end

  def test_range_count_param
    cs = @tester.characters(number: 2..5)
    ws = @tester.words(number: 2..5)
    ss = @tester.sentences(number: 2..5)
    ps = @tester.paragraphs(number: 2..5)

    assert(cs.length.between?(2, 5))
    assert(ws.length.between?(2, 5))
    assert(ss.length.between?(2, 5))
    assert(ps.length.between?(2, 5))
  end

  def test_exclusive_range_count_param
    cs = @tester.characters(number: 2...3)
    ws = @tester.words(number: 2...3)
    ss = @tester.sentences(number: 2...3)
    ps = @tester.paragraphs(number: 2...3)

    assert_equal(2, cs.length)
    assert_equal(2, ws.length)
    assert_equal(2, ss.length)
    assert_equal(2, ps.length)
  end

  def test_array_count_param
    cs = @tester.characters(number: [1, 4])
    ws = @tester.words(number: [1, 4])
    ss = @tester.sentences(number: [1, 4])
    ps = @tester.paragraphs(number: [1, 4])

    assert(cs.length == 1 || cs.length == 4)
    assert(ws.length == 1 || ws.length == 4)
    assert(ss.length == 1 || ss.length == 4)
    assert(ps.length == 1 || ps.length == 4)
  end

  def test_words_with_large_count_params
    exact = @tester.words(number: 500)
    range = @tester.words(number: 250..500)
    array = @tester.words(number: [250, 500])

    assert_equal(500, exact.length)
    assert(range.length.between?(250, 500))
    assert(array.length == 250 || array.length == 500)
  end

  def test_multibyte
    assert_kind_of String, @tester.multibyte
    assert_includes %w[😀 ❤ 😡], @tester.multibyte
  end

  def test_paragraph_char_count
    paragraph = @tester.paragraph_by_chars(number: 256)

    assert_equal(256, paragraph.length)
  end

  def test_unique_with_already_set_values
    values = ('a'..'z').to_a + ('0'..'9').to_a
    @tester.unique.exclude(:character, [], values)
    assert_raise(Faker::UniqueGenerator::RetryLimitExceeded) { @tester.unique.character }
  end

  def test_unique_with_already_set_values_and_parameter
    values = ('a'..'z').to_a + ('0'..'9').to_a
    @tester.unique.exclude(:characters, [number: 1], values)
    assert_raise(Faker::UniqueGenerator::RetryLimitExceeded) { @tester.unique.characters(number: 1) }
  end

  def test_excluded_words_as_string
    excluded_word_string = @tester.word
    @words = @tester.words(number: 10_000, exclude_words: excluded_word_string)

    @words.each { |w| assert_not_equal w, excluded_word_string }
  end

  def test_excluded_words_as_comma_delimited_string
    excluded_words_array = @tester.words(number: 2)
    excluded_words_string = excluded_words_array.join(', ')
    @words = @tester.words(number: 10_000, exclude_words: excluded_words_string)

    @words.each do |w|
      assert_not_equal w, excluded_words_array[0]
      assert_not_equal w, excluded_words_array[1]
    end
  end

  def test_excluded_words_as_array
    excluded_words_array = @tester.words(number: 2)
    @words = @tester.words(number: 10_000, exclude_words: excluded_words_array)

    @words.each do |w|
      assert_not_equal w, excluded_words_array[0]
      assert_not_equal w, excluded_words_array[1]
    end
  end
end