parseexpr.test

来自「tcl是工具命令语言」· TEST 代码 · 共 654 行 · 第 1/3 页

TEST
654
字号
# This file contains a collection of tests for the procedures in the# file tclParseExpr.c.  Sourcing this file into Tcl runs the tests and# generates output for errors.  No output means no errors were found.## Copyright (c) 1997 Sun Microsystems, Inc.# Copyright (c) 1998-1999 by Scriptics Corporation.## See the file "license.terms" for information on usage and redistribution# of this file, and for a DISCLAIMER OF ALL WARRANTIES.## RCS: @(#) $Id: parseExpr.test,v 1.10 2003/02/16 01:36:32 msofer Exp $if {[lsearch [namespace children] ::tcltest] == -1} {    package require tcltest    namespace import -force ::tcltest::*}# Note that the Tcl expression parser (tclParseExpr.c) does not check# the semantic validity of the expressions it parses. It does not check,# for example, that a math function actually exists, or that the operands# of "<<" are integers.if {[info commands testexprparser] == {}} {    puts "This application hasn't been compiled with the \"testexprparser\""    puts "command, so I can't test the Tcl expression parser."    ::tcltest::cleanupTests    return }# Some tests only work if wide integers (>32bit) are not found to be# integers at all.set ::tcltest::testConstraints(wideIntegerUnparsed) \	[expr {-1 == 0xffffffff}]test parseExpr-1.1 {Tcl_ParseExpr procedure, computing string length} {    testexprparser [bytestring "1+2\0 +3"] -1} {- {} 0 subexpr 1+2 5 operator + 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 {}}test parseExpr-1.2 {Tcl_ParseExpr procedure, computing string length} {    testexprparser "1  + 2" -1} {- {} 0 subexpr {1  + 2} 5 operator + 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 {}}test parseExpr-1.3 {Tcl_ParseExpr procedure, error getting initial lexeme} {wideIntegerUnparsed} {    list [catch {testexprparser {12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-1.4 {Tcl_ParseExpr procedure, error in conditional expression} {    list [catch {testexprparser {foo+} -1} msg] $msg} {1 {syntax error in expression "foo+": variable references require preceding $}}test parseExpr-1.5 {Tcl_ParseExpr procedure, lexemes after the expression} {    list [catch {testexprparser {1+2 345} -1} msg] $msg} {1 {syntax error in expression "1+2 345": extra tokens at end of expression}}test parseExpr-2.1 {ParseCondExpr procedure, valid test subexpr} {    testexprparser {2>3? 1 : 0} -1} {- {} 0 subexpr {2>3? 1 : 0} 11 operator ? 0 subexpr 2>3 5 operator > 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 1 1 text 1 0 subexpr 0 1 text 0 0 {}}test parseExpr-2.2 {ParseCondExpr procedure, error in test subexpr} {    list [catch {testexprparser {0 || foo} -1} msg] $msg} {1 {syntax error in expression "0 || foo": variable references require preceding $}}test parseExpr-2.3 {ParseCondExpr procedure, next lexeme isn't "?"} {    testexprparser {1+2} -1} {- {} 0 subexpr 1+2 5 operator + 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 {}}test parseExpr-2.4 {ParseCondExpr procedure, next lexeme is "?"} {    testexprparser {1+2 ? 3 : 4} -1} {- {} 0 subexpr {1+2 ? 3 : 4} 11 operator ? 0 subexpr 1+2 5 operator + 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-2.5 {ParseCondExpr procedure, bad lexeme after "?"} {wideIntegerUnparsed} {    list [catch {testexprparser {1+2 ? 12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-2.6 {ParseCondExpr procedure, valid "then" subexpression} {    testexprparser {1? 3 : 4} -1} {- {} 0 subexpr {1? 3 : 4} 7 operator ? 0 subexpr 1 1 text 1 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-2.7 {ParseCondExpr procedure, error in "then" subexpression} {    list [catch {testexprparser {1? fred : martha} -1} msg] $msg} {1 {syntax error in expression "1? fred : martha": variable references require preceding $}}test parseExpr-2.8 {ParseCondExpr procedure, lexeme after "then" subexpr isn't ":"} {    list [catch {testexprparser {1? 2 martha 3} -1} msg] $msg} {1 {syntax error in expression "1? 2 martha 3": missing colon from ternary conditional}}test parseExpr-2.9 {ParseCondExpr procedure, valid "else" subexpression} {    testexprparser {27||3? 3 : 4&&9} -1} {- {} 0 subexpr {27||3? 3 : 4&&9} 15 operator ? 0 subexpr 27||3 5 operator || 0 subexpr 27 1 text 27 0 subexpr 3 1 text 3 0 subexpr 3 1 text 3 0 subexpr 4&&9 5 operator && 0 subexpr 4 1 text 4 0 subexpr 9 1 text 9 0 {}}test parseExpr-2.10 {ParseCondExpr procedure, error in "else" subexpression} {    list [catch {testexprparser {1? 2 : martha} -1} msg] $msg} {1 {syntax error in expression "1? 2 : martha": variable references require preceding $}}test parseExpr-3.1 {ParseLorExpr procedure, valid logical and subexpr} {    testexprparser {1&&2 || 3} -1} {- {} 0 subexpr {1&&2 || 3} 9 operator || 0 subexpr 1&&2 5 operator && 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-3.2 {ParseLorExpr procedure, error in logical and subexpr} {    list [catch {testexprparser {1&&foo || 3} -1} msg] $msg} {1 {syntax error in expression "1&&foo || 3": variable references require preceding $}}test parseExpr-3.3 {ParseLorExpr procedure, next lexeme isn't "||"} {    testexprparser {1&&2? 1 : 0} -1} {- {} 0 subexpr {1&&2? 1 : 0} 11 operator ? 0 subexpr 1&&2 5 operator && 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 1 1 text 1 0 subexpr 0 1 text 0 0 {}}test parseExpr-3.4 {ParseLorExpr procedure, next lexeme is "||"} {    testexprparser {1&&2 || 3} -1} {- {} 0 subexpr {1&&2 || 3} 9 operator || 0 subexpr 1&&2 5 operator && 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-3.5 {ParseLorExpr procedure, bad lexeme after "||"} {wideIntegerUnparsed} {    list [catch {testexprparser {1&&2 || 12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-3.6 {ParseLorExpr procedure, valid RHS subexpression} {    testexprparser {1&&2 || 3 || 4} -1} {- {} 0 subexpr {1&&2 || 3 || 4} 13 operator || 0 subexpr {1&&2 || 3} 9 operator || 0 subexpr 1&&2 5 operator && 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-3.7 {ParseLorExpr procedure, error in RHS subexpression} {    list [catch {testexprparser {1&&2 || 3 || martha} -1} msg] $msg} {1 {syntax error in expression "1&&2 || 3 || martha": variable references require preceding $}}test parseExpr-4.1 {ParseLandExpr procedure, valid LHS "|" subexpr} {    testexprparser {1|2 && 3} -1} {- {} 0 subexpr {1|2 && 3} 9 operator && 0 subexpr 1|2 5 operator | 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-4.2 {ParseLandExpr procedure, error in LHS "|" subexpr} {    list [catch {testexprparser {1&&foo && 3} -1} msg] $msg} {1 {syntax error in expression "1&&foo && 3": variable references require preceding $}}test parseExpr-4.3 {ParseLandExpr procedure, next lexeme isn't "&&"} {    testexprparser {1|2? 1 : 0} -1} {- {} 0 subexpr {1|2? 1 : 0} 11 operator ? 0 subexpr 1|2 5 operator | 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 1 1 text 1 0 subexpr 0 1 text 0 0 {}}test parseExpr-4.4 {ParseLandExpr procedure, next lexeme is "&&"} {    testexprparser {1|2 && 3} -1} {- {} 0 subexpr {1|2 && 3} 9 operator && 0 subexpr 1|2 5 operator | 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-4.5 {ParseLandExpr procedure, bad lexeme after "&&"} {wideIntegerUnparsed} {    list [catch {testexprparser {1|2 && 12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-4.6 {ParseLandExpr procedure, valid RHS subexpression} {    testexprparser {1|2 && 3 && 4} -1} {- {} 0 subexpr {1|2 && 3 && 4} 13 operator && 0 subexpr {1|2 && 3} 9 operator && 0 subexpr 1|2 5 operator | 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-4.7 {ParseLandExpr procedure, error in RHS subexpression} {    list [catch {testexprparser {1|2 && 3 && martha} -1} msg] $msg} {1 {syntax error in expression "1|2 && 3 && martha": variable references require preceding $}}test parseExpr-5.1 {ParseBitOrExpr procedure, valid LHS "^" subexpr} {    testexprparser {1^2 | 3} -1} {- {} 0 subexpr {1^2 | 3} 9 operator | 0 subexpr 1^2 5 operator ^ 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-5.2 {ParseBitOrExpr procedure, error in LHS "^" subexpr} {    list [catch {testexprparser {1|foo | 3} -1} msg] $msg} {1 {syntax error in expression "1|foo | 3": variable references require preceding $}}test parseExpr-5.3 {ParseBitOrExpr procedure, next lexeme isn't "|"} {    testexprparser {1^2? 1 : 0} -1} {- {} 0 subexpr {1^2? 1 : 0} 11 operator ? 0 subexpr 1^2 5 operator ^ 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 1 1 text 1 0 subexpr 0 1 text 0 0 {}}test parseExpr-5.4 {ParseBitOrExpr procedure, next lexeme is "|"} {    testexprparser {1^2 | 3} -1} {- {} 0 subexpr {1^2 | 3} 9 operator | 0 subexpr 1^2 5 operator ^ 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-5.5 {ParseBitOrExpr procedure, bad lexeme after "|"} {wideIntegerUnparsed} {    list [catch {testexprparser {1^2 | 12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-5.6 {ParseBitOrExpr procedure, valid RHS subexpression} {    testexprparser {1^2 | 3 | 4} -1} {- {} 0 subexpr {1^2 | 3 | 4} 13 operator | 0 subexpr {1^2 | 3} 9 operator | 0 subexpr 1^2 5 operator ^ 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-5.7 {ParseBitOrExpr procedure, error in RHS subexpression} {    list [catch {testexprparser {1^2 | 3 | martha} -1} msg] $msg} {1 {syntax error in expression "1^2 | 3 | martha": variable references require preceding $}}test parseExpr-6.1 {ParseBitXorExpr procedure, valid LHS "&" subexpr} {    testexprparser {1&2 ^ 3} -1} {- {} 0 subexpr {1&2 ^ 3} 9 operator ^ 0 subexpr 1&2 5 operator & 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-6.2 {ParseBitXorExpr procedure, error in LHS "&" subexpr} {    list [catch {testexprparser {1^foo ^ 3} -1} msg] $msg} {1 {syntax error in expression "1^foo ^ 3": variable references require preceding $}}test parseExpr-6.3 {ParseBitXorExpr procedure, next lexeme isn't "^"} {    testexprparser {1&2? 1 : 0} -1} {- {} 0 subexpr {1&2? 1 : 0} 11 operator ? 0 subexpr 1&2 5 operator & 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 1 1 text 1 0 subexpr 0 1 text 0 0 {}}test parseExpr-6.4 {ParseBitXorExpr procedure, next lexeme is "^"} {    testexprparser {1&2 ^ 3} -1} {- {} 0 subexpr {1&2 ^ 3} 9 operator ^ 0 subexpr 1&2 5 operator & 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-6.5 {ParseBitXorExpr procedure, bad lexeme after "^"} {wideIntegerUnparsed} {    list [catch {testexprparser {1&2 ^ 12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-6.6 {ParseBitXorExpr procedure, valid RHS subexpression} {    testexprparser {1&2 ^ 3 ^ 4} -1} {- {} 0 subexpr {1&2 ^ 3 ^ 4} 13 operator ^ 0 subexpr {1&2 ^ 3} 9 operator ^ 0 subexpr 1&2 5 operator & 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-6.7 {ParseBitXorExpr procedure, error in RHS subexpression} {    list [catch {testexprparser {1&2 ^ 3 ^ martha} -1} msg] $msg} {1 {syntax error in expression "1&2 ^ 3 ^ martha": variable references require preceding $}}test parseExpr-7.1 {ParseBitAndExpr procedure, valid LHS equality subexpr} {    testexprparser {1==2 & 3} -1} {- {} 0 subexpr {1==2 & 3} 9 operator & 0 subexpr 1==2 5 operator == 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-7.2 {ParseBitAndExpr procedure, error in LHS equality subexpr} {    list [catch {testexprparser {1!=foo & 3} -1} msg] $msg} {1 {syntax error in expression "1!=foo & 3": variable references require preceding $}}test parseExpr-7.3 {ParseBitAndExpr procedure, next lexeme isn't "&"} {    testexprparser {1==2? 1 : 0} -1} {- {} 0 subexpr {1==2? 1 : 0} 11 operator ? 0 subexpr 1==2 5 operator == 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 1 1 text 1 0 subexpr 0 1 text 0 0 {}}test parseExpr-7.4 {ParseBitAndExpr procedure, next lexeme is "&"} {    testexprparser {1>2 & 3} -1} {- {} 0 subexpr {1>2 & 3} 9 operator & 0 subexpr 1>2 5 operator > 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-7.5 {ParseBitAndExpr procedure, bad lexeme after "&"} {wideIntegerUnparsed} {    list [catch {testexprparser {1==2 & 12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-7.6 {ParseBitAndExpr procedure, valid RHS subexpression} {    testexprparser {1<2 & 3 & 4} -1} {- {} 0 subexpr {1<2 & 3 & 4} 13 operator & 0 subexpr {1<2 & 3} 9 operator & 0 subexpr 1<2 5 operator < 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-7.7 {ParseBitAndExpr procedure, error in RHS subexpression} {    list [catch {testexprparser {1==2 & 3>2 & martha} -1} msg] $msg} {1 {syntax error in expression "1==2 & 3>2 & martha": variable references require preceding $}}test parseExpr-8.1 {ParseEqualityExpr procedure, valid LHS relational subexpr} {    testexprparser {1<2 == 3} -1} {- {} 0 subexpr {1<2 == 3} 9 operator == 0 subexpr 1<2 5 operator < 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-8.2 {ParseEqualityExpr procedure, error in LHS relational subexpr} {    list [catch {testexprparser {1>=foo == 3} -1} msg] $msg} {1 {syntax error in expression "1>=foo == 3": variable references require preceding $}}test parseExpr-8.3 {ParseEqualityExpr procedure, next lexeme isn't "==" or "!="} {    testexprparser {1<2? 1 : 0} -1} {- {} 0 subexpr {1<2? 1 : 0} 11 operator ? 0 subexpr 1<2 5 operator < 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 1 1 text 1 0 subexpr 0 1 text 0 0 {}}test parseExpr-8.4 {ParseEqualityExpr procedure, next lexeme is "==" or "!="} {    testexprparser {1<2 == 3} -1} {- {} 0 subexpr {1<2 == 3} 9 operator == 0 subexpr 1<2 5 operator < 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-8.5 {ParseEqualityExpr procedure, next lexeme is "==" or "!="} {    testexprparser {1<2 != 3} -1} {- {} 0 subexpr {1<2 != 3} 9 operator != 0 subexpr 1<2 5 operator < 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}test parseExpr-8.6 {ParseEqualityExpr procedure, bad lexeme after "==" or "!="} {wideIntegerUnparsed} {    list [catch {testexprparser {1<2 == 12345678901234567890} -1} msg] $msg} {1 {integer value too large to represent}}test parseExpr-8.7 {ParseEqualityExpr procedure, valid RHS subexpression} {    testexprparser {1<2 == 3 == 4} -1} {- {} 0 subexpr {1<2 == 3 == 4} 13 operator == 0 subexpr {1<2 == 3} 9 operator == 0 subexpr 1<2 5 operator < 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 subexpr 4 1 text 4 0 {}}test parseExpr-8.8 {ParseEqualityExpr procedure, error in RHS subexpression} {    list [catch {testexprparser {1<2 == 3 != martha} -1} msg] $msg} {1 {syntax error in expression "1<2 == 3 != martha": variable references require preceding $}}test parseExpr-9.1 {ParseRelationalExpr procedure, valid LHS shift subexpr} {    testexprparser {1<<2 < 3} -1

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?