File: test_esm.py

package info (click to toggle)
python-esmre 1.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208 kB
  • sloc: ansic: 467; python: 391; makefile: 3
file content (112 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# encoding: utf-8

# esm_tests.py - tests for esm extension module
# Copyright (C) 2007 Tideway Systems Limited.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
# USA

import unittest
import esm


class IndexTests(unittest.TestCase):
    def testHasConstructor(self):
        self.assertTrue(esm.Index())

    def testIndexHasEnterMethod(self):
        esm.Index().enter("keyword")

    def testIndexMustHaveStringForFirstArgument(self):
        self.assertRaises(TypeError, esm.Index().enter, 0)
        self.assertRaises(TypeError, esm.Index().enter, [])
        self.assertRaises(TypeError, esm.Index().enter)

    def testIndexFix(self):
        index = esm.Index()
        index.fix()

    def testQuery(self):
        index = esm.Index()
        index.enter("he")
        index.enter("she")
        index.enter("his")
        index.enter("hers")
        index.fix()

        self.assertEqual(
            [((1, 4), "his"), ((5, 7), "he"), ((13, 16), "his")],
            index.query("this here is history"),
        )

    #                                     0123456789.123456789

    def testEnterObject(self):
        index = esm.Index()

        mint_object = dict()
        index.enter("mint", mint_object)

        pepper_object = dict()
        index.enter("pepper", pepper_object)

        index.fix()
        results = index.query("mint sauce")

        self.assertEqual(1, len(results))
        self.assertTrue(isinstance(results[0], tuple))

        slice_indices, associated_object = results[0]

        self.assertEqual((0, 4), slice_indices)
        self.assertTrue(associated_object is mint_object)

    def testCantFixIndexWhenAlreadyFixed(self):
        index = esm.Index()
        index.fix()

        self.assertRaises(TypeError, index.fix)

    def testCantEnterWhenAlreadyFixed(self):
        index = esm.Index()
        index.fix()

        self.assertRaises(TypeError, index.enter, "foo")

    def testQueryUntilFixed(self):
        index = esm.Index()
        self.assertRaises(TypeError, index.query, "foo")

    def testObjectsForCommonEndingsAreDecrefedCorrectly(self):
        o = "Owt"
        import sys

        initial_ref_count = sys.getrefcount(o)

        index = esm.Index()
        index.enter("food", o)
        index.enter("ood", o)
        index.fix()
        index.query("blah")

        index = None
        del index

        self.assertEqual(initial_ref_count, sys.getrefcount(o))


if __name__ == "__main__":
    unittest.main()