test_common_chartypes.py :  » Parser » SimpleParse » SimpleParse-2.1.1a2 » tests » 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 » Parser » SimpleParse 
SimpleParse » SimpleParse 2.1.1a2 » tests » test_common_chartypes.py
import unittest, string
from simpleparse.parser import Parser
from simpleparse.common import chartypes,timezone_names
from simpleparse import dispatchprocessor

fulltrans = string.maketrans("","")

class CommonTests(unittest.TestCase):
  def doBasicTest(self, definition, parserName, testValue, expected, ):
    result = Parser( definition).parse( testValue, parserName )
    assert result == expected, '''\nexpected:%s\n     got:%s\n'''%( expected, result )
  def _testSet( self, set, singleName, multiName ):
    """Test multi-line definitions"""
    decl = """single := %s multiple := %s"""%( singleName, multiName )
    p = Parser(decl)
    notset = string.translate( fulltrans, fulltrans, set )
    for char in set:
      success, children, next = p.parse( char, singleName)
      assert success and (next == 1), """Parser for %s couldn't parse %s"""%( singleName, char )
    for char in notset:
      success, children, next = p.parse( char, singleName)
      assert (not success) and (next == 0), """Parser for %s parsed %s"""%( singleName, char )
      success, children, next = p.parse( char, multiName)
      assert (not success) and (next == 0), """Parser for %s parsed %s"""%( multiName, char )
    success, children, next = p.parse( set, multiName)
    assert success and (next == len(set)), """Parser for %s couldn't parse full set of chars, failed at %s"""%( multiName, set[next:] )
  def testBasic( self ):
    for set, single, multiple in (
      ("digits", "digit", "digits"),
      ("uppercase", "uppercasechar", "uppercase"),
      ("lowercase", "lowercasechar", "lowercase"),
      ("letters", "letter", "letters"),
      ("whitespace", "whitespacechar", "whitespace"),
      ("octdigits", "octdigit", "octdigits"),
      ("hexdigits", "hexdigit", "hexdigits"),
      ("printable", "printablechar", "printable"),
      ("punctuation", "punctuationchar", "punctuation"),

      ("ascii_lowercase", "ascii_lowercasechar", "ascii_lowercase"),
      ("ascii_uppercase", "ascii_uppercasechar", "ascii_uppercase"),
    ):
      try:
        set = getattr( string, set)
        self._testSet(
          set,
          single,
          multiple,
        )
      except AttributeError:
        pass
  def testEOF( self ):
    p = Parser( """this := 'a',EOF""", 'this')
    success, children, next = p.parse( 'a' )
    assert success, """EOF didn't match at end of string"""
  def testEOFFail( self ):
    p = Parser( """this := 'a',EOF""", 'this')
    success, children, next = p.parse( 'a ' )
    assert not success, """EOF matched before end of string"""
  
  def testTZ( self ):
    names = timezone_names.timezone_mapping.keys()
    names.sort() # tests that the items don't match shorter versions...
    decl = Parser("""this := (timezone_name, ' '?)+""", 'this')
    proc = dispatchprocessor.DispatchProcessor()
    proc.timezone_name = timezone_names.TimeZoneNameInterpreter()
    text = string.join( names, ' ')
    success, result, next = decl.parse( text, processor = proc )
    assert success, """Unable to complete parsing the timezone names, stopped parsing at char %s %s"""%(next, text[next:])
    assert result == map( timezone_names.timezone_mapping.get, names), """Got different results for interpretation than expected (expected first, recieved second)\n%s\n%s"""%(map( timezone_names.timezone_mapping.get, names), result)
    
    
      
    
def getSuite():
  return unittest.makeSuite(CommonTests, 'test')

if __name__ == "__main__":
  unittest.main(defaultTest="getSuite")
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.