• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
32
33"""Tests for mock module."""
34
35
36import Queue
37import threading
38import unittest
39
40import set_sys_path  # Update sys.path to locate mod_pywebsocket module.
41
42from test import mock
43
44
45class MockConnTest(unittest.TestCase):
46    """A unittest for MockConn class."""
47
48    def setUp(self):
49        self._conn = mock.MockConn('ABC\r\nDEFG\r\n\r\nHIJK')
50
51    def test_readline(self):
52        self.assertEqual('ABC\r\n', self._conn.readline())
53        self.assertEqual('DEFG\r\n', self._conn.readline())
54        self.assertEqual('\r\n', self._conn.readline())
55        self.assertEqual('HIJK', self._conn.readline())
56        self.assertEqual('', self._conn.readline())
57
58    def test_read(self):
59        self.assertEqual('ABC\r\nD', self._conn.read(6))
60        self.assertEqual('EFG\r\n\r\nHI', self._conn.read(9))
61        self.assertEqual('JK', self._conn.read(10))
62        self.assertEqual('', self._conn.read(10))
63
64    def test_read_and_readline(self):
65        self.assertEqual('ABC\r\nD', self._conn.read(6))
66        self.assertEqual('EFG\r\n', self._conn.readline())
67        self.assertEqual('\r\nHIJK', self._conn.read(9))
68        self.assertEqual('', self._conn.readline())
69
70    def test_write(self):
71        self._conn.write('Hello\r\n')
72        self._conn.write('World\r\n')
73        self.assertEqual('Hello\r\nWorld\r\n', self._conn.written_data())
74
75
76class MockBlockingConnTest(unittest.TestCase):
77    """A unittest for MockBlockingConn class."""
78
79    def test_read(self):
80        """Tests that data put to MockBlockingConn by put_bytes method can be
81        read from it.
82        """
83
84        class LineReader(threading.Thread):
85            """A test class that launches a thread, calls readline on the
86            specified conn repeatedly and puts the read data to the specified
87            queue.
88            """
89
90            def __init__(self, conn, queue):
91                threading.Thread.__init__(self)
92                self._queue = queue
93                self._conn = conn
94                self.setDaemon(True)
95                self.start()
96
97            def run(self):
98                while True:
99                    data = self._conn.readline()
100                    self._queue.put(data)
101
102        conn = mock.MockBlockingConn()
103        queue = Queue.Queue()
104        reader = LineReader(conn, queue)
105        self.failUnless(queue.empty())
106        conn.put_bytes('Foo bar\r\n')
107        read = queue.get()
108        self.assertEqual('Foo bar\r\n', read)
109
110
111class MockTableTest(unittest.TestCase):
112    """A unittest for MockTable class."""
113
114    def test_create_from_dict(self):
115        table = mock.MockTable({'Key': 'Value'})
116        self.assertEqual('Value', table.get('KEY'))
117        self.assertEqual('Value', table['key'])
118
119    def test_create_from_list(self):
120        table = mock.MockTable([('Key', 'Value')])
121        self.assertEqual('Value', table.get('KEY'))
122        self.assertEqual('Value', table['key'])
123
124    def test_create_from_tuple(self):
125        table = mock.MockTable((('Key', 'Value'),))
126        self.assertEqual('Value', table.get('KEY'))
127        self.assertEqual('Value', table['key'])
128
129    def test_set_and_get(self):
130        table = mock.MockTable()
131        self.assertEqual(None, table.get('Key'))
132        table['Key'] = 'Value'
133        self.assertEqual('Value', table.get('Key'))
134        self.assertEqual('Value', table.get('key'))
135        self.assertEqual('Value', table.get('KEY'))
136        self.assertEqual('Value', table['Key'])
137        self.assertEqual('Value', table['key'])
138        self.assertEqual('Value', table['KEY'])
139
140
141if __name__ == '__main__':
142    unittest.main()
143
144
145# vi:sts=4 sw=4 et
146