ReModuleTest.py :  » Ajax » pyjamas » src » examples » libtest » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Ajax » pyjamas 
pyjamas » src » examples » libtest » ReModuleTest.py
# Testing time module

import sys
import UnitTest
import re

#from __pyjamas__ import debugger

class ReModuleTest(UnitTest.UnitTest):

    def matchTest(self, msg, pat, flags, string, groups, span):
        r = re.compile(pat, flags)
        m = r.match(string)
        if groups is None:
            self.assertTrue(m is None, "%s: None expected" % msg)
            return
        self.assertTrue(m is not None, "%s: Unexpected None" % msg)
        self.assertTrue(len(m.groups()) == len(groups)-1, "%s: len(m.groups()) = %s != %s" % (msg, len(m.groups()), len(groups)-1))
        for i in range(len(groups)):
            self.assertEqual(m.group(i), groups[i], "%s: m.group(%d) = '%s' != groups[%d] = '%s'" % (msg, i, m.group(i), i, groups[i]))
        self.assertEqual(m.start(), span[0], "%s: start = %d != %d" % (msg, m.start(), span[0]))
        self.assertEqual(m.end(), span[1], "%s: end = %d != %d" % (msg, m.end(), span[1]))
        self.assertTrue(m.span() == span, "%s: span = %r != %r" % (msg, m.span(), span[1]))

    def searchTest(self, msg, pat, flags, string, groups, span):
        r = re.compile(pat, flags)
        m = r.search(string)
        if groups is None:
            self.assertTrue(m is None, "%s: None expected" % msg)
            return
        self.assertTrue(m is not None, "%s: Unexpected None" % msg)
        self.assertTrue(len(m.groups()) == len(groups)-1, "%s: len(m.groups()) = %s != %s" % (msg, len(m.groups()), len(groups)-1))
        for i in range(len(groups)):
            self.assertEqual(m.group(i), groups[i], "%s: m.group(%d) = '%s' != groups[%d] = '%s'" % (msg, i, m.group(i), i, groups[i]))
        self.assertEqual(m.start(), span[0], "%s: start = %d != %d" % (msg, m.start(), span[0]))
        self.assertEqual(m.end(), span[1], "%s: end = %d != %d" % (msg, m.end(), span[1]))
        self.assertTrue(m.span() == span, "%s: span = %r != %r" % (msg, m.span(), span[1]))


    def testMatchBasics(self):
        self.matchTest('test 1', 'Ab.cd', 0, 'AbXcd', ['AbXcd'], (0,5))
        self.matchTest('test 2', 'Ab.cd', 0, 'abXcd', None, (0,5))
        self.matchTest('test 3a', 'Ab.cd', re.I, 'abXcd', ['abXcd'], (0,5))
        self.matchTest('test 3b', '(?i)Ab.cd', 0, 'abXcd', ['abXcd'], (0,5))
        self.matchTest('test 4', 'Ab.cd', 0, 'ab\ncd', None, (0,5))
        self.matchTest('test 5a', 'Ab.cd', re.S, 'Ab\ncd', ['Ab\ncd'], (0,5))
        self.matchTest('test 5b', '(?s)Ab.cd', 0, 'Ab\ncd', ['Ab\ncd'], (0,5))
        # bug #288: even re.compile on these two tests puts webkit/chrome into
        # an infinite CPU loop.
        self.matchTest('test 6a', 'A(b).(c)d', re.I | re.S, 'ab\ncd', ['ab\ncd', 'b', 'c'], (0,5))
        self.matchTest('test 6b', '(?is)A(b).(c)d', 0, 'ab\ncd', ['ab\ncd', 'b', 'c'], (0,5))

        m = re.match("1..4", "1234")
        self.assertFalse(m is None, """re.match("1..4", "1234")""")
 
    def testSearchBasics(self):
        self.searchTest('test 1', 'Ab.cd', 0, 'AbXcd', ['AbXcd'], (0,5))
        self.searchTest('test 2', 'Ab.cd', 0, 'abXcd', None, (0,5))
        self.searchTest('test 3a', 'Ab.cd', re.I, 'abXcd', ['abXcd'], (0,5))
        self.searchTest('test 3b', '(?i)Ab.cd', 0, 'abXcd', ['abXcd'], (0,5))
        self.searchTest('test 4', 'Ab.cd', 0, 'ab\ncd', None, (0,5))
        self.searchTest('test 5a', 'Ab.cd', re.S, 'Ab\ncd', ['Ab\ncd'], (0,5))
        self.searchTest('test 5b', 'Ab.cd(?s)', 0, 'Ab\ncd', ['Ab\ncd'], (0,5))
        self.searchTest('test 6a', 'A(b).(c)d', re.I | re.S, 'ab\ncd', ['ab\ncd', 'b', 'c'], (0,5))
        self.searchTest('test 6b', 'A(b)(?is).(c)d', 0, 'ab\ncd', ['ab\ncd', 'b', 'c'], (0,5))
        self.searchTest('test 7', 'Ab.cd', 0, 'AAAbXcd', ['AbXcd'], (2,7))
        self.searchTest('test 8', ' ', 0, 'Spaces in a sentence', [' '], (6,7))

        m = re.search("ab", "dab abba a b")
        self.assertFalse(m is None, """re.search("ab", "dab abba a b")""")

    # bug #258 - this is throwing a javascript syntax error in FF2
    def testFindallBasics(self):
        e = re.compile("e").findall("Where are all these eees")
        self.assertEqual(len(e), 8)

    def testSubBasics(self):
        matches = []
        def fn(m):
            matches.append(m)
            return "%s" % len(matches)
        r = re.compile('e')
        s = "Where are all these eees"
        self.assertEqual(r.sub("Q", s), "WhQrQ arQ all thQsQ QQQs")
        self.assertEqual(r.sub(fn, s), "Wh1r2 ar3 all th4s5 678s")
        self.assertEqual(r.sub("Q", s, 4), "WhQrQ arQ all thQse eees")
        matches = []
        self.assertEqual(r.sub(fn, s, 5), "Wh1r2 ar3 all th4s5 eees")

        self.assertEqual(r.subn("Q", s), ("WhQrQ arQ all thQsQ QQQs", 8))


    def testSplitBasics(self):
        r = re.compile('e')
        s = "Where are all these eees"

        self.assertEqual(r.split(s), ['Wh', 'r', ' ar', ' all th', 's', ' ', '', '', 's'])


    def testMatchExtended(self):
        r = re.compile("ed")
        m = r.match("ed ed", 0)
        self.assertEqual(m.group(0), "ed")

        m = r.match("ed ed", 1)
        self.assertTrue(m is None, """match("ed ed", 1)""")

        m = r.match("ed ed", 3)
        self.assertEqual(m.group(0), "ed")

        r = re.compile("^a.*$", re.M)
        m = r.match("a  ")
        self.assertEqual(m.group(0), "a  ")

        m = r.match("a1\na2")
        self.assertEqual(m.group(0), "a1")

        m = r.match("a1\na2", 2)
        self.assertTrue(m is None, """match("a1\na2", 2)""")

        m = r.match("a1\na2", 3)
        self.assertEqual(m.group(0), "a2")

        m = r.match("a1\na2", 3, 4)
        self.assertEqual(m.group(0), "a")

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.