File: test_faker_html.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 (85 lines) | stat: -rw-r--r-- 2,159 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
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerHTML < Test::Unit::TestCase
  def setup
    @tester = Faker::HTML
  end

  def test_heading
    header = @tester.heading
    level = header.match(/<h(\d)>/i)[1].to_i
    open_tag = "<h#{level}>"
    close_tag = "</h#{level}>"

    assert(header.start_with?(open_tag))
    assert(header.end_with?(close_tag))
  end

  def test_paragraph
    assert_match(/<p>.+<\/p>/, @tester.paragraph)
  end

  def test_emphasis
    assert_match(/<em>.*<\/em>/, @tester.emphasis)
  end

  def test_ordered_list
    assert_match(/<ol>.*<\/ol>/m, @tester.ordered_list)
  end

  def test_unordered_list
    assert_match(/<ul>.*<\/ul>/m, @tester.unordered_list)
  end

  def test_code
    assert_match(/<code>.+<\/code>/, @tester.code)
  end

  def test_table
    table_html = @tester.table

    assert(table_html.start_with?('<table>'))
    assert(table_html.end_with?('</table>'))

    assert_equal(1, count_occurrences(table_html, '<thead>'))
    assert_equal(1, count_occurrences(table_html, '<tbody>'))
    assert_equal(1, count_occurrences(table_html, '<tfoot>'))
    assert_equal(3, count_occurrences(table_html, '<th>'))
    assert_equal(5, count_occurrences(table_html, '<tr>'))
    assert_equal(12, count_occurrences(table_html, '<td>'))
  end

  def test_script
    assert_match(/<script src=".+"><\/script>/, @tester.script)
  end

  def test_link
    assert_match(/<link rel=".+" href=".+">/, @tester.link)
    assert_match(/<link rel="alternate" href=".+">/, @tester.link(rel: 'alternate'))
  end

  def test_element
    assert_match(/<div .+>.+<\/div>/, @tester.element)
    assert_match(/<span .+>.+<\/span>/, @tester.element(tag: 'span'))
  end

  def test_random
    assert_match(/<[^>]+>.*<\/[^>]+>|<link[^>]+>/, @tester.random)
  end

  def test_sandwich
    sandwich = @tester.sandwich(sentences: 2, repeat: 3)

    assert_match(/<h\d>[\w\s]+<\/h\d>/i, sandwich)
    assert_match(/<p>[\w\s.,!?]+<\/p>/i, sandwich)
    assert_match(/<[^>]+>[\w\s.,!?]*<\/[^>]+>/i, sandwich)
  end

  private

  def count_occurrences(text, substring)
    text.scan(substring).length
  end
end