Skip to content

Commit 4b0cff7

Browse files
committed
Add unit tests for browser history functionality
1 parent 0ad2a2a commit 4b0cff7

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import unittest
2+
import sys
3+
import os
4+
5+
# Add parent directory to path to import backend
6+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
7+
from backend import BrowserHistory
8+
9+
class TestBrowserHistory(unittest.TestCase):
10+
def setUp(self):
11+
"""Set up test cases"""
12+
self.browser = BrowserHistory("homepage.com")
13+
14+
def test_initialization(self):
15+
"""Test proper initialization of BrowserHistory"""
16+
self.assertEqual(self.browser._curr.val, "homepage.com")
17+
self.assertEqual(self.browser._back_count, 0)
18+
self.assertEqual(self.browser._forward_count, 0)
19+
self.assertIsNone(self.browser._curr.nxt)
20+
self.assertIsNone(self.browser._curr.prev)
21+
22+
def test_visit(self):
23+
"""Test visit functionality and forward history cleanup"""
24+
self.browser.visit("page1.com")
25+
self.assertEqual(self.browser._curr.val, "page1.com")
26+
self.assertEqual(self.browser._back_count, 1)
27+
self.assertEqual(self.browser._forward_count, 0)
28+
29+
# Test forward history cleanup
30+
self.browser.visit("page2.com")
31+
self.browser.back(1)
32+
self.browser.visit("page3.com") # Should clear forward history
33+
self.assertIsNone(self.browser._curr.nxt)
34+
self.assertEqual(self.browser._forward_count, 0)
35+
36+
def test_back_navigation(self):
37+
"""Test back navigation with counter validation"""
38+
# Setup history
39+
self.browser.visit("page1.com")
40+
self.browser.visit("page2.com")
41+
42+
# Test normal back navigation
43+
result = self.browser.back(1)
44+
self.assertEqual(result, "page1.com")
45+
self.assertEqual(self.browser._back_count, 1)
46+
self.assertEqual(self.browser._forward_count, 1)
47+
48+
# Test back with more steps than available
49+
result = self.browser.back(5) # Should only go back 1 step
50+
self.assertEqual(result, "homepage.com")
51+
self.assertEqual(self.browser._back_count, 0)
52+
self.assertEqual(self.browser._forward_count, 2)
53+
54+
def test_forward_navigation(self):
55+
"""Test forward navigation with counter validation"""
56+
# Setup history and position
57+
self.browser.visit("page1.com")
58+
self.browser.visit("page2.com")
59+
self.browser.back(2) # Go back to homepage
60+
61+
# Test normal forward navigation
62+
result = self.browser.forward(1)
63+
self.assertEqual(result, "page1.com")
64+
self.assertEqual(self.browser._forward_count, 1)
65+
self.assertEqual(self.browser._back_count, 1)
66+
67+
# Test forward with more steps than available
68+
result = self.browser.forward(5) # Should only go forward remaining 1 step
69+
self.assertEqual(result, "page2.com")
70+
self.assertEqual(self.browser._forward_count, 0)
71+
self.assertEqual(self.browser._back_count, 2)
72+
73+
def test_complex_navigation(self):
74+
"""Test complex navigation patterns"""
75+
self.browser.visit("page1.com")
76+
self.browser.visit("page2.com")
77+
self.browser.visit("page3.com")
78+
79+
# Back navigation
80+
self.assertEqual(self.browser.back(2), "page1.com")
81+
82+
# New visit should clear forward history
83+
self.browser.visit("page4.com")
84+
self.assertEqual(self.browser._forward_count, 0)
85+
self.assertIsNone(self.browser._curr.nxt)
86+
87+
# Verify we can't go forward to cleared history
88+
self.assertEqual(self.browser.forward(1), "page4.com")
89+
90+
if __name__ == '__main__':
91+
unittest.main()

0 commit comments

Comments
 (0)