Debian Codegraph @

testing/web-platform/tests/tools/third_party/pywebsocket3/test/test_memorizingfile.py

raw
1 #!/usr/bin/env python
2 #
3 # Copyright 2011, Google Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
15 # distribution.
16 # * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Tests for memorizingfile module."""
32
33 from __future__ import absolute_import
34
35 import unittest
36
37 import six
38
39 import set_sys_path # Update sys.path to locate pywebsocket3 module.
40 from pywebsocket3 import memorizingfile
41
42
43 class UtilTest(unittest.TestCase):
44 """A unittest for memorizingfile module."""
45 def check(self, memorizing_file, num_read, expected_list):
46 for unused in range(num_read):
47 memorizing_file.readline()
48 actual_list = memorizing_file.get_memorized_lines()
49 self.assertEqual(len(expected_list), len(actual_list))
50 for expected, actual in zip(expected_list, actual_list):
51 self.assertEqual(expected, actual)
52
53 def check_with_size(self, memorizing_file, read_size, expected_list):
54 read_list = []
55 read_line = ''
56 while True:
57 line = memorizing_file.readline(read_size)
58 line_length = len(line)
59 self.assertTrue(line_length <= read_size)
60 if line_length == 0:
61 if read_line != '':
62 read_list.append(read_line)
63 break
64 read_line += line
65 if line[line_length - 1] == '\n':
66 read_list.append(read_line)
67 read_line = ''
68 actual_list = memorizing_file.get_memorized_lines()
69 self.assertEqual(len(expected_list), len(actual_list))
70 self.assertEqual(len(expected_list), len(read_list))
71 for expected, actual, read in zip(expected_list, actual_list,
72 read_list):
73 self.assertEqual(expected, actual)
74 self.assertEqual(expected, read)
75
76 def test_get_memorized_lines(self):
77 memorizing_file = memorizingfile.MemorizingFile(
78 six.StringIO('Hello\nWorld\nWelcome'))
79 self.check(memorizing_file, 3, ['Hello\n', 'World\n', 'Welcome'])
80
81 def test_get_memorized_lines_limit_memorized_lines(self):
82 memorizing_file = memorizingfile.MemorizingFile(
83 six.StringIO('Hello\nWorld\nWelcome'), 2)
84 self.check(memorizing_file, 3, ['Hello\n', 'World\n'])
85
86 def test_get_memorized_lines_empty_file(self):
87 memorizing_file = memorizingfile.MemorizingFile(six.StringIO(''))
88 self.check(memorizing_file, 10, [])
89
90 def test_get_memorized_lines_with_size(self):
91 for size in range(1, 10):
92 memorizing_file = memorizingfile.MemorizingFile(
93 six.StringIO('Hello\nWorld\nWelcome'))
94 self.check_with_size(memorizing_file, size,
95 ['Hello\n', 'World\n', 'Welcome'])
96
97
98 if __name__ == '__main__':
99 unittest.main()
100
101 # vi:sts=4 sw=4 et