File: TestContext.py

package info (click to toggle)
parsedatetime 2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 568 kB
  • sloc: python: 5,161; makefile: 44
file content (57 lines) | stat: -rw-r--r-- 1,939 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
"""
Test pdtContext
"""

import sys
import time
import parsedatetime as pdt
from parsedatetime.context import pdtContext

if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest


class test(unittest.TestCase):

    def setUp(self):
        self.cal = pdt.Calendar(version=pdt.VERSION_CONTEXT_STYLE)
        (self.yr, self.mth, self.dy, self.hr, self.mn,
         self.sec, self.wd, self.yd, self.isdst) = time.localtime()

    def testContext(self):
        self.assertEqual(self.cal.parse('5 min from now')[1],
                         pdtContext(pdtContext.ACU_MIN | pdtContext.ACU_NOW))
        self.assertEqual(self.cal.parse('5 min from now',
                                        version=pdt.VERSION_FLAG_STYLE)[1], 2)
        self.assertEqual(self.cal.parse('7/11/2015')[1],
                         pdtContext(pdtContext.ACU_YEAR |
                                    pdtContext.ACU_MONTH | pdtContext.ACU_DAY))
        self.assertEqual(self.cal.parse('7/11/2015',
                                        version=pdt.VERSION_FLAG_STYLE)[1], 1)
        self.assertEqual(self.cal.parse('14/32/2015')[1],
                         pdtContext(0))
        self.assertEqual(self.cal.parse('25:23')[1],
                         pdtContext())

    def testSources(self):
        self.assertEqual(self.cal.parse('afternoon 5pm')[1],
                         pdtContext(pdtContext.ACU_HALFDAY |
                                    pdtContext.ACU_HOUR))

        self.assertEqual(self.cal.parse('morning')[1],
                         pdtContext(pdtContext.ACU_HALFDAY))

        self.assertEqual(self.cal.parse('night', version=1)[1], 2)

    def testThreadRun(self):
        from threading import Thread
        t = Thread(target=lambda: self.cal.evalRanges('4p-6p'))
        # should not throw out AttributeError
        t.start()


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