File: test_faker_hipster.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 (108 lines) | stat: -rw-r--r-- 3,301 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
# frozen_string_literal: true

require_relative '../../test_helper'

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

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

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

  # Words should not return any word with spaces
  def test_words_without_spaces
    @words = @tester.words(number: 1000)

    @words.each { |w| refute_match(/\s/, 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::Hipster.word generates random word from standard wordlist
  def test_word
    @tester = Faker::Hipster
    @standard_wordlist = I18n.translate('faker.hipster.words')

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

  # Word should not return any word with spaces
  def test_word_without_spaces
    @tester = Faker::Hipster

    deterministically_verify -> { @tester.word }, depth: 5 do |word|
      refute_match(/\s/, word)
    end
  end

  def test_exact_count_param
    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
    ws = @tester.words(number: 2..5)
    ss = @tester.sentences(number: 2..5)
    ps = @tester.paragraphs(number: 2..5)

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

  def test_array_count_param
    ws = @tester.words(number: [1, 4])
    ss = @tester.sentences(number: [1, 4])
    ps = @tester.paragraphs(number: [1, 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_sentence_with_open_compounds_allowed
    deterministically_verify -> { @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: true) }, depth: 5 do |sentence|
      assert_operator(sentence.split.length, :>=, 5)
    end
  end

  # Sentence should not contain any open compounds
  def test_sentence_without_open_compounds_allowed
    deterministically_verify -> { @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: false) }, depth: 5 do |sentence|
      assert_equal(5, sentence.split.length)
    end
  end

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

    assert_equal(256, paragraph.length)
  end
end