File: test_history.rb

package info (click to toggle)
libwww-mechanize-ruby 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 883
  • sloc: ruby: 6,621; makefile: 4
file content (142 lines) | stat: -rw-r--r-- 4,087 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
require "helper"

class TestHistory < Test::Unit::TestCase
  def setup
    @agent    = Mechanize.new
    @history  = Mechanize::History.new
  end

  def test_push
    assert_equal(0, @history.length)

    page = @agent.get("http://localhost/tc_bad_links.html")
    x = @history.push(page)
    assert_equal(x, @history)
    assert_equal(1, @history.length)
    assert(@history.visited?(page))
    assert(@history.visited?(page.uri))
    assert(@history.visited?(page.uri.to_s))
    assert_equal(page, @history.visited_page(page))
    assert_equal(page, @history.visited_page(page.uri))
    assert_equal(page, @history.visited_page(page.uri.to_s))

    @history.push(@agent.get("/tc_bad_links.html"))
    assert_equal(2, @history.length)
  end

  def test_shift
    assert_equal(0, @history.length)
    page = @agent.get("http://localhost/tc_bad_links.html")
    @history.push(page)
    assert_equal(1, @history.length)

    @history.push(@agent.get("/tc_bad_links.html"))
    assert_equal(2, @history.length)

    @history.push(@agent.get("/index.html"))
    assert_equal(3, @history.length)

    page2 = @history.shift
    assert_equal(page, page2)
    assert_equal(2, @history.length)

    @history.shift
    assert_equal(1, @history.length)
    assert_equal(false, @history.visited?(page))

    @history.shift
    assert_equal(0, @history.length)

    assert_nil(@history.shift)
    assert_equal(0, @history.length)
  end

  def test_pop
    assert_equal(0, @history.length)
    page = @agent.get("http://localhost/tc_bad_links.html")
    @history.push(page)
    assert_equal(1, @history.length)

    page2 = @agent.get("/index.html")
    @history.push(page2)
    assert_equal(2, @history.length)
    assert_equal(page2, @history.pop)
    assert_equal(1, @history.length)
    assert_equal(true, @history.visited?(page))
    assert_equal(false, @history.visited?(page2))
    assert_equal(page, @history.pop)
    assert_equal(0, @history.length)
    assert_equal(false, @history.visited?(page))
    assert_equal(false, @history.visited?(page2))
    assert_nil(@history.pop)
  end

  def test_max_size
    @history  = Mechanize::History.new(10)
    1.upto(20) do |i|
      page = @agent.get('http://localhost/index.html')
      @history.push page
      assert_equal(true, @history.visited?(page))
      if i < 10
        assert_equal(i, @history.length)
      else
        assert_equal(10, @history.length)
      end
    end

    @history.clear
    @history.max_size = 5
    1.upto(20) do |i|
      page = @agent.get('http://localhost/index.html')
      @history.push page
      assert_equal(true, @history.visited?(page))
      if i < 5
        assert_equal(i, @history.length)
      else
        assert_equal(5, @history.length)
      end
    end

    @history.max_size = 0
    1.upto(20) do |i|
      page = @agent.get('http://localhost/index.html')
      @history.push page
      assert_equal(false, @history.visited?(page))
      assert_equal(0, @history.length)
    end
  end

  def test_no_slash
    page = @agent.get('http://localhost')

    node = Struct.new(:href, :inner_text).new('http://localhost/', 'blah')
    link = Mechanize::Page::Link.new(node, nil, nil)
    assert(@agent.visited?(link))

    node = Struct.new(:href, :inner_text).new('http://localhost', 'blah')
    link = Mechanize::Page::Link.new(node, nil, nil)
    assert(@agent.visited?(link))
  end

  def test_with_slash
    page = @agent.get('http://localhost/')

    node = Struct.new(:href, :inner_text).new('http://localhost/', 'blah')
    link = Mechanize::Page::Link.new(node, nil, nil)
    assert(@agent.visited?(link))

    node = Struct.new(:href, :inner_text).new('http://localhost', 'blah')
    link = Mechanize::Page::Link.new(node, nil, nil)
    assert(@agent.visited?(link))
  end

  def test_clear
    page = nil
    20.times { @history.push(page = @agent.get('http://localhost/index.html')) }
    assert_equal(20, @history.length)
    assert_equal(true, @history.visited?(page))
    @history.clear
    assert_equal(0, @history.length)
    assert_equal(false, @history.visited?(page))
  end
end