From 7a8c2d2d594edcb1c16e277c88693b0c349affac Mon Sep 17 00:00:00 2001 From: ptmcg Date: Sun, 14 Aug 2016 13:40:32 +0000 Subject: reformat test comments for runTests; example of using results from runTests in wordsToNum.py git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@420 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b --- src/examples/datetimeParseActions.py | 24 ++++++++++++---- src/examples/simpleSQL.py | 55 +++++++++++++++++++++++++----------- src/examples/wordsToNum.py | 12 ++++++-- 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/examples/datetimeParseActions.py b/src/examples/datetimeParseActions.py index 34b9d2b..e42d2c6 100644 --- a/src/examples/datetimeParseActions.py +++ b/src/examples/datetimeParseActions.py @@ -38,9 +38,15 @@ date_expr.setParseAction(convertToDatetime) date_expr.runTests("""\ 2000/1/1 - 2000/13/1 # invalid month - 1900/2/29 # 1900 was not a leap year - 2000/2/29 # but 2000 was + + # invalid month + 2000/13/1 + + # 1900 was not a leap year + 1900/2/29 + + # but 2000 was + 2000/2/29 """) @@ -50,7 +56,13 @@ date_expr.ignore(pythonStyleComment) date_expr.runTests("""\ 2000-01-01 - 2000-13-01 # invalid month - 1900-02-29 # 1900 was not a leap year - 2000-02-29 # but 2000 was + + # invalid month + 2000-13-01 + + # 1900 was not a leap year + 1900-02-29 + + # but 2000 was + 2000-02-29 """) \ No newline at end of file diff --git a/src/examples/simpleSQL.py b/src/examples/simpleSQL.py index 66dc18c..7bbd4cd 100644 --- a/src/examples/simpleSQL.py +++ b/src/examples/simpleSQL.py @@ -16,9 +16,9 @@ FROM = Keyword("from", caseless=True) WHERE = Keyword("where", caseless=True) ident = Word( alphas, alphanums + "_$" ).setName("identifier") -columnName = ( delimitedList( ident, ".", combine=True ) ).addParseAction(upcaseTokens) +columnName = ( delimitedList( ident, ".", combine=True ) ).setName("column name").addParseAction(upcaseTokens) columnNameList = Group( delimitedList( columnName ) ) -tableName = ( delimitedList( ident, ".", combine=True ) ).addParseAction(upcaseTokens) +tableName = ( delimitedList( ident, ".", combine=True ) ).setName("table name").addParseAction(upcaseTokens) tableNameList = Group( delimitedList( tableName ) ) whereExpression = Forward() @@ -56,17 +56,40 @@ oracleSqlComment = "--" + restOfLine simpleSQL.ignore( oracleSqlComment ) if __name__ == "__main__": - simpleSQL.runTests("""\ - SELECT * from XYZZY, ABC - select * from SYS.XYZZY - Select A from Sys.dual - Select A,B,C from Sys.dual - Select A, B, C from Sys.dual - Select A, B, C from Sys.dual, Table2 - Xelect A, B, C from Sys.dual - Select A, B, C frox Sys.dual - Select - Select &&& frox Sys.dual - Select A from Sys.dual where a in ('RED','GREEN','BLUE') - Select A from Sys.dual where a in ('RED','GREEN','BLUE') and b in (10,20,30) - Select A,b from table1,table2 where table1.id eq table2.id -- test out comparison operators""") + simpleSQL.runTests("""\ + + # multiple tables + SELECT * from XYZZY, ABC + + # dotted table name + select * from SYS.XYZZY + + Select A from Sys.dual + + Select A,B,C from Sys.dual + + Select A, B, C from Sys.dual, Table2 + + # FAIL - invalid SELECT keyword + Xelect A, B, C from Sys.dual + + # FAIL - invalid FROM keyword + Select A, B, C frox Sys.dual + + # FAIL - incomplete statement + Select + + # FAIL - incomplete statement + Select * from + + # FAIL - invalid column + Select &&& frox Sys.dual + + # where clause + Select A from Sys.dual where a in ('RED','GREEN','BLUE') + + # compound where clause + Select A from Sys.dual where a in ('RED','GREEN','BLUE') and b in (10,20,30) + + # where clause with comparison operator + Select A,b from table1,table2 where table1.id eq table2.id""") diff --git a/src/examples/wordsToNum.py b/src/examples/wordsToNum.py index 60c7c3d..7cebbff 100644 --- a/src/examples/wordsToNum.py +++ b/src/examples/wordsToNum.py @@ -78,8 +78,16 @@ numWords.ignore(Literal("-")) numWords.ignore(CaselessLiteral("and")) def test(s,expected): - print ("Expecting %s" % expected) - numWords.runTests(s) + try: + fail_expected = (expected is None) + success, results_tup = numWords.runTests(s, failureTests=fail_expected) + assert success, "Failed test!" + if not fail_expected: + teststr, results = results_tup[0] + observed = results[0] + assert expected == observed, "incorrect parsed value, {} -> {}, should be {}".format(teststr, observed, expected) + except Exception as exc: + print("{}: {}".format(type(exc).__name__, exc)) test("one hundred twenty hundred", None) test("one hundred and twennty", None) -- cgit v1.2.1