📄 testmisc.java
字号:
+ "<books><book id='book1'/><book id='book2'/></books> " + "for $book in $books/book return <p>{string($book/@id)}</p>", "<p>book1</p><p>book2</p>"); evalTest("for $n in children(<a>xx<b/>yy</a>) return $n instance of node()", "true true true"); evalTest("for $n in children(<a>xx<b/>yy</a>) return $n instance of text ( )", "true false true"); evalTest("for $n in children(<a>xx<b/>yy</a>) return $n instance of element(a)", "false false false"); evalTest("for $n in <a>xx<b/>yy</a>/node() return $n instance of element(b)", "false true false"); // FIXME: evalTest("<a>xx<b/>yy</a>/node() instance of node()", "false"); evalTest("<a>xx<b/>yy</a>/node() instance of node()?", "false"); evalTest("<a>xx<b/>yy</a>/node() instance of node()+", "true"); evalTest("<a>xx<b/>yy</a>/node() instance of node()*", "true"); evalTest("<a>xx<b/>yy</a>/node() instance of item()+", "true"); evalTest("(3,4,5) instance of item()+", "true"); evalTest("('a','b') instance of string+", "true"); evalTest("(2,3) instance of string?", "false"); evalTest("(2,3) instance of string+", "false"); evalTest("() instance of string?", "true"); evalTest("() instance of string+", "false"); evalTest("() instance of string*", "true"); evalTest("('2') instance of string?", "true"); evalTest("('2') instance of string+", "true"); evalTest("('2') instance of string*", "true"); evalTest("('2','3') instance of string?", "false"); evalTest("('2','3') instance of string+", "true"); evalTest("('2','3') instance of string*", "true"); evalTest("declare namespace Int='class:java.lang.Integer';\n" + "Int:toHexString(266)", "10a"); evalTest("declare namespace File='class:java.io.File';\n" + "declare function local:make-file ($x as string) {File:new($x)};\n" + "declare function local:parent ($x) {java.io.File:getParent($x)};\n" + "local:parent(local:make-file('dir/mine.txt'))", "dir"); evalTest("java.lang.Integer:toHexString(255)", "ff"); // String functions evalTest("substring('motor car', 6)", "car"); evalTest("substring('metadata', 4, 3)", "ada"); // evalTest("substring('metadata', -INF, 3)", "met"); evalTest("(1 to 20)[. mod 5 = 0]", "5 10 15 20"); evalTest("(1 to 20)[. mod 5 ge 3]", "3 4 8 9 13 14 18 19"); evalTest("1,(99 to 0),3", "1 3"); evalTest("-10 to -2", "-10 -9 -8 -7 -6 -5 -4 -3 -2"); String some_elements = "let $top := <top><a/><b/><c/><d/></top>," + " $a:=$top/a, $b:=$top/b, $c:=$top/c, $d:=$top/d return "; evalNodeNames(some_elements+"($b, $a) union ($a, $b)", "a;b;"); evalNodeNames(some_elements+"($b, $a) union ($b, $c)", "a;b;c;"); evalNodeNames(some_elements+"($b, $a) intersect ($a, $b)", "a;b;"); evalNodeNames(some_elements+"($b, $a) intersect ($b, $c)", "b;"); evalNodeNames(some_elements+"($b, $a) except ($a, $b)", ""); evalNodeNames(some_elements+"($b, $a) except ($b, $c)", "a;"); evalNodeNames(some_elements+"($b, $a, $b, $d) intersect ($b, $d)", "b;d;"); evalNodeNames(some_elements+"($b, $a, $b, $d) except ($b, $d)", "a;"); evalNodeNames(some_elements+"($b, $a, $b, $d) except ()", "a;b;d;"); // Check for catching errors: evalTest("+ +", "*** syntax error - <string>:1:4: missing expression [XPST0003]"); evalTest("declare namespace x1='XXX", "*** caught SyntaxException - <string>:1:22: " + "unexpected end-of-file in string starting here [XPST0003]"); evalTest("unescaped-data('<?--->'),let $x:=unescaped-data('an &oslash;') return <b>{unescaped-data('<![CDATA[saw]]>')} {$x}</b>", "<?---><b><![CDATA[saw]]> an ø</b>"); evalTestIdAttrs("doc('outline.xml')/book/part/chapter/ancestor::*", "b1;P1;"); evalTestIdAttrs("doc('outline.xml')/book/part/" +"chapter/ancestor-or-self::node()", ";b1;P1;c1;c2;"); evalTestIdAttrs("doc('outline.xml')//" +"section[@id='s1']/following-sibling::*", "s2;s3;"); evalTestIdAttrs("doc('outline.xml')//chapter/self::*", "c1;c2;"); evalTestIdAttrs("doc('outline.xml')//" +"para[@id='p31']/preceding::*", "s1;s11;s2;"); evalTestIdAttrs("doc('outline.xml')//" +"section[@id='s5']/preceding-sibling::*", "s4;"); evalTestIdAttrs("doc('outline.xml')//" +"chapter[@id='c1']/following::*", "c2;s4;s5;"); evalTestIdAttrs("doc('outline.xml')//" +"section[@id='s1']/(/book)", "b1;"); evalTestIdAttrs("doc('outline.xml')//" +"section[@id='s1']/(//chapter)", "c1;c2;"); evalTest("declare namespace XQuery = 'class:gnu.xquery.lang.XQuery';" + "XQuery:eval-with-focus(XQuery:getInstance()," + " '<r pos=\"{position()}\">{.}</r>', (<b/>, 3))", "<r pos=\"1\"><b /></r><r pos=\"2\">3</r>"); evalTest("declare namespace XQuery = 'class:gnu.xquery.lang.XQuery';" + "XQuery:eval-with-focus(XQuery:getInstance()," + " '<r pos=\"{position()}\">{.}</r>', <b/>, 3, 4)", "<r pos=\"3\"><b /></r>"); Object r; String e = "<r pos='{position()}' size='{last()}'>{.}</r>"; try { r = toString(interp.evalWithFocus(e, interp.eval("2,3,4"))); } catch (Throwable ex) { r = ex; } matchTest(e, r, "<r pos=\"1\" size=\"3\">2</r>" + "<r pos=\"2\" size=\"3\">3</r>" + "<r pos=\"3\" size=\"3\">4</r>"); try { r = toString(interp.evalWithFocus(e, interp.eval("<b/>"), 4, 10)); } catch (Throwable ex) { r = ex; } matchTest(e, r, "<r pos=\"4\" size=\"10\"><b/></r>"); printSummary(); } public static boolean printSummary () { System.out.println("# of expected passes " + expectedPasses); if (expectedFailures > 0) System.out.println("# of expected failures " + expectedFailures); if (unexpectedPasses > 0) System.out.println("# of unexpected passes " + unexpectedPasses); if (unexpectedFailures > 0) System.out.println("# of unexpected failures " + unexpectedFailures); return unexpectedFailures != 0; } /** True if the two string match, ignoring unquoted white-space. */ public static boolean matches(String str1, String str2) { int i = 0; int j = 0; char quote = 0; for (;;) { char x, y; for (;;) { if (i >= str1.length()) { x = 0; break; } x = str1.charAt(i++); if (quote != 0 || ! Character.isWhitespace(x)) break; } for (;;) { if (j >= str2.length()) { y = 0; break; } y = str2.charAt(j++); if (quote != 0 || ! Character.isWhitespace(y)) break; } if (x != y) return false; if (x == 0) return true; if (x == '\'' || x == '\"') { if (quote == 0) quote = x; else if (x == quote) quote = 0; } } } private static void evalTestIdAttrs(String expr, String expected) { evalTest("for $x in (" + expr + ") return (string($x/@id),';')", expected); } private static void evalNodeNames(String expr, String expected) { evalTest("for $node in (" + expr + ") return concat(node-name($node),';')", expected); } public static void evalTest(String expr, String expected) { Object result; try { result = eval(expr); } catch (Throwable ex) { result = ex; } matchTest(expr, result, expected); } public static String toString (Object value) { CharArrayOutPort wr = new CharArrayOutPort(); gnu.xml.XMLPrinter xp = new gnu.xml.XMLPrinter(wr); xp.writeObject(value); xp.close(); return wr.toString(); } public static void matchTest(String expr, Object returned, String expected) { String result; Throwable throwable; if (returned instanceof Throwable) { if (returned instanceof WrappedException) { throwable = ((WrappedException) returned).getException(); if (throwable != null) returned = throwable; } throwable = (Throwable) returned; // throwable.printStackTrace(); if (returned instanceof SyntaxException) result = "*** caught SyntaxException - " + ((SyntaxException) returned).getMessages().getErrors(); else result = "*** caught " + returned.getClass().getName() + " ***"; } else { result = returned.toString(); throwable = null; } boolean failureExpected = failureExpectedNext != null; if (matches(expected, result)) { if (failureExpected) unexpectedPasses++; else expectedPasses++; if (verbose || failureExpected) System.out.println((failureExpected ? "XPASS: \"" : "PASS: \"") + expr + "\" evaluated to \"" + result + "\""); } else { if (failureExpectedNext != null) expectedFailures++; else unexpectedFailures++; if (verbose || ! failureExpected) System.out.println((failureExpected ? "XFAIL: \"" : "FAIL: \"") + expr + "\" evaluated to \"" + result + "\" but expected \"" + expected + "\""); if (verbose && ! failureExpected && throwable != null) throwable.printStackTrace(System.out); } failureExpectedNext = null; } public static String eval(String expr) throws Throwable { CharArrayOutPort out = new CharArrayOutPort(); InPort in = new CharArrayInPort(expr); SourceMessages messages = new SourceMessages(); Compilation comp = interp.parse(in, messages, Language.PARSE_IMMEDIATE); SourceError firstError = messages.getErrors(); if (firstError != null) return "*** syntax error - " + firstError; CallContext ctx = CallContext.getInstance(); gnu.lists.Consumer save = ctx.consumer; try { ctx.consumer = interp.getOutputConsumer(out); ModuleExp mod = comp.getModule(); mod.setName("atInteractiveLevel"); // FIXME ModuleExp.evalModule(env, ctx, comp, null, null); } finally { ctx.consumer = save; } String result = new String(out.toCharArray()); out.close(); return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -