1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Tags: JDK1.5
// Copyright (C) 2006 Ito Kazumitsu (kaz@maczuka.gcd.org)
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
package gnu.testlet.java.util.regex.Matcher;
import gnu.testlet.*;
import java.util.regex.*;
public class hitEnd implements Testlet
{
private TestHarness harness;
// There seems to be some bug in gnu.java.util.regex and
// Pattern object cannot be reused for more than one matchers.
// So we compile the pattern string every time.
private Pattern pattern;
private String patternStr;
private Matcher matcher;
public void test (TestHarness harness)
{
this.harness = harness;
try
{
patternStr = "abcd";
testFind("xyzabcd", false);
testFind("XYZabcdxyz", false);
testFind("xyzabc", true);
testFind("xyzxyz", true);
testMatches("abcd", false);
testMatches("abc", true);
testMatches("abcdxyz", false);
testMatches("xyzabcd", false);
testMatches("xyz", false);
testLookingAt("abcd", false);
testLookingAt("abcdxyz", false);
testLookingAt("abc", true);
testLookingAt("xyzabcd", false);
patternStr = "abcd$";
testFind("xyzabcd", true);
testFind("XYZabcdxyz", true);
testFind("xyzabc", true);
testFind("xyzxyz", true);
testMatches("abcd", true);
testMatches("abc", true);
testMatches("abcdxyz", false);
testMatches("xyzabcd", false);
testMatches("xyz", false);
testLookingAt("abcd", true);
testLookingAt("abcdxyz", false);
testLookingAt("abc", true);
testLookingAt("xyzabcd", false);
patternStr = "a+b";
testFind("xyzaaab", false);
testFind("xyzaaabb", false);
testFind("xyzaaa", true);
testFind("xyzxyz", true);
testMatches("aaab", false);
testMatches("aaaa", true);
testMatches("aaabx", false);
testMatches("xaaab", false);
testLookingAt("aaab", false);
testLookingAt("aaaa", true);
testLookingAt("aaabxyz", false);
testLookingAt("xyzxyz", false);
patternStr = "(?:a+b)|(?:aa)";
testFind("xyzaaab", false);
testFind("xyzaa", true);
testFind("xyzaaa", true);
testFind("xyzaax", false);
testMatches("aaab", false);
testMatches("aaaa", true);
testMatches("aa", true);
testLookingAt("aaab", false);
testLookingAt("aaaa", true);
testLookingAt("aa", true);
testLookingAt("aax", false);
patternStr = "(?:aa)|(?:a+b)";
testFind("xyzaaab", false);
testFind("xyzaa", false);
testFind("xyzaaa", false);
testFind("xyzaax", false);
testMatches("aaab", false);
testMatches("aaaa", true);
testMatches("aa", false);
testLookingAt("aaab", false);
testLookingAt("aaaa", false);
testLookingAt("aa", false);
testLookingAt("aax", false);
}
catch(PatternSyntaxException pse)
{
harness.debug(pse);
harness.check(false, pse.toString());
}
}
private void testFind(String s, boolean expected)
{
pattern = Pattern.compile(patternStr);
matcher = pattern.matcher(s);
matcher.find();
boolean result = matcher.hitEnd();
if (result != expected) debugMsg(s, "find", expected, result);
harness.check(result == expected);
}
private void testMatches(String s, boolean expected)
{
pattern = Pattern.compile(patternStr);
matcher = pattern.matcher(s);
matcher.matches();
boolean result = matcher.hitEnd();
if (result != expected) debugMsg(s, "matches", expected, result);
harness.check(result == expected);
}
private void testLookingAt(String s, boolean expected)
{
pattern = Pattern.compile(patternStr);
matcher = pattern.matcher(s);
matcher.lookingAt();
boolean result = matcher.hitEnd();
if (result != expected) debugMsg(s, "lookingAt", expected, result);
harness.check(result == expected);
}
private void debugMsg(String s, String method, boolean expected, boolean result)
{
harness.debug("pattern=" + pattern.pattern() + " input=" + s + " method=" + method + " matcher=" + matcher + " expected=" + expected + " hitEnd=" + result);
}
}
|