test_common_iso_date.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_iso_date.py
import unittest, string
from simpleparse.parser import Parser
from simpleparse.common import iso_date,iso_date_loose
from mx import DateTime
import time

fulltrans = string.maketrans("","")
tzOffset = DateTime.DateTimeDelta( 0,0,0, time.timezone )

class CommonTests(unittest.TestCase):
  def testISODateLoose( self ):
    """Test the parsing of ISO date and time formats"""
    values = [
      ("2002-02-03", DateTime.DateTime( 2002, 2,3)),
      ("2002-02",DateTime.DateTime( 2002, 2)),
      ("2002",DateTime.DateTime( 2002)),
      ("2002-02-03 04:15", DateTime.DateTime( 2002, 2,3, 4,15)),
      ("2002-02-03 04:15:16", DateTime.DateTime( 2002, 2,3, 4,15, 16)),
      ("2002-02-03 04:15:16 +00:00", DateTime.DateTime( 2002, 2,3, 4,15, 16)-tzOffset),
      ("2002-02-03 4:5", DateTime.DateTime( 2002, 2,3, 4,5)),
      ("2002-02-03 4:5:16", DateTime.DateTime( 2002, 2,3, 4,5, 16)),
      ("2002-02-03 4:5:16 +00:00", DateTime.DateTime( 2002, 2,3, 4, 5,16)-tzOffset),
    ]
    p = Parser ("d:= ISO_date_time_loose", "d")
    proc = iso_date_loose.MxInterpreter()
    for string, date in values:
      success, children, next = p.parse( string, processor = proc)
      assert success, """Unable to parse any of the string %s with the ISO date-time parser"""% (string)
      assert next == len(string),"""Did not finish parsing string %s with the ISO date-time parser, remainder was %s, found was %s"""%( string, string [next:],children)
      assert children [0] == date,"""Returned different date for string %s than expected, got %s, expected %s"""% (string,children [0], date)
  def testISODate( self ):
    """Test the parsing of ISO date and time formats"""
    values = [
      ("2002-02-03", DateTime.DateTime( 2002, 2,3)),
      ("2002-02",DateTime.DateTime( 2002, 2)),
      ("2002",DateTime.DateTime( 2002)),
      ("2002-02-03T04:15", DateTime.DateTime( 2002, 2,3, 4,15)),
      ("2002-02-03T04:15:16", DateTime.DateTime( 2002, 2,3, 4,15, 16)),
      ("2002-02-03T04:15:16+00:00", DateTime.DateTime( 2002, 2,3, 4,15, 16)-tzOffset),
    ]
    p = Parser ("d:= ISO_date_time", "d")
    proc = iso_date.MxInterpreter()
    for string, date in values:
      success, children, next = p.parse( string, processor=proc)
      assert success, """Unable to parse any of the string %s with the ISO date-time parser"""% (string)
      assert next == len(string),"""Did not finish parsing string %s with the ISO date-time parser, remainder was %s, found was %s"""%( string, string [next:],children)
      assert children [0] == date,"""Returned different date for string %s than expected, got %s, expected %s"""% (string,children [0], date)
  def testProductionsStrict( self ):
    for string, production in [
      ("2002", "year"),
      ("02", "month"),
      ("02", "day"),
      ("24:00:00", "ISO_time"),
      ("02", "ISO_time"),
      (":", "time_separator"),
      ("02:02", "ISO_time"),
      ("02:02:02", "ISO_time"),
      ("2002-02-30", "ISO_date"),
      ("2002-02-30", "ISO_date_time"),
      ("02", "hour"),
      ("02", "minute"),
      ("02", "second"),
      ("20", "second"),

      ("+0500", "offset"),
      ("+00:00", "offset"),
      ("-", "offset_sign"),
      ("-00:00", "offset"),
      ("-04:00", "offset"),
      ("-0500", "offset"),
      ("02:13", "ISO_time"),
      ("02:13:16", "ISO_time"),
      ("2002-02-01T02:13-0500", "ISO_date_time"),
    ]:
      success, children, next = iso_date._p.parse( string,production)
      assert next == len(string), "couldn't parse %s as a %s"%( string, production)
    
  def testProductions2( self ):
    for string, production in [
      ("2002", "year"),
      ("02", "month"),
      ("02", "day"),
      ("24:00:00", "ISO_time_loose"),
      ("02", "ISO_time_loose"),
      (":", "time_separator"),
      ("02:02", "ISO_time_loose"),
      ("02:02:02", "ISO_time_loose"),
      ("2002-02-30", "ISO_date_loose"),
      ("2002-02-30", "ISO_date_time_loose"),
      ("2002-2-1", "ISO_date_time_loose"),
      ("02", "hour"),
      ("02", "minute"),
      ("2", "second"),
      ("02", "second"),
      ("20", "second"),
      ("20.", "second"),
      ("20.3", "second"),

      ("+0500", "offset"),
      ("+00:00", "offset"),
      ("-", "offset_sign"),
      ("-00:00", "offset"),
      ("-04:00", "offset"),
      ("-0500", "offset"),
      ("02:13", "ISO_time_loose"),
      ("02:13:16", "ISO_time_loose"),
      ("2002-2-1 2:13", "ISO_date_time_loose"),
      ("2002-2-1 2:13 -0500", "ISO_date_time_loose"),
      ("2002-2-1 2:13 -05:30", "ISO_date_time_loose"),
      ("2002-2-1 2:13 +05:30", "ISO_date_time_loose"),
      ("2002-2-1 2:13 +00:00", "ISO_date_time_loose"),
      
    ]:
      success, children, next = iso_date_loose._p.parse( string,production )
      assert next == len(string), "couldn't parse %s as a %s"%( string, production)
      
    
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.