StringPattern: cleanup, add StringPatternTest

This commit is contained in:
Yann Cébron
2014-07-14 20:04:35 +02:00
parent 7fa1b28fd0
commit 64c812ea69
3 changed files with 129 additions and 64 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public class StringPattern extends ObjectPattern<String, StringPattern> {
return with(new PatternCondition<String>("startsWith") {
@Override
public boolean accepts(@NotNull final String str, final ProcessingContext context) {
return str.startsWith(s);
return StringUtil.startsWith(str, s);
}
});
}
@@ -66,7 +66,7 @@ public class StringPattern extends ObjectPattern<String, StringPattern> {
return with(new PatternCondition<String>("endsWith") {
@Override
public boolean accepts(@NotNull final String str, final ProcessingContext context) {
return str.endsWith(s);
return StringUtil.endsWith(str, s);
}
});
}
@@ -76,7 +76,7 @@ public class StringPattern extends ObjectPattern<String, StringPattern> {
return with(new PatternCondition<String>("contains") {
@Override
public boolean accepts(@NotNull final String str, final ProcessingContext context) {
return str.contains(s);
return StringUtil.contains(str, s);
}
});
@@ -87,10 +87,7 @@ public class StringPattern extends ObjectPattern<String, StringPattern> {
return with(new PatternCondition<String>("containsChars") {
@Override
public boolean accepts(@NotNull final String str, final ProcessingContext context) {
for (int i=0, len=s.length(); i<len; i++) {
if (str.indexOf(s.charAt(i))>-1) return true;
}
return false;
return StringUtil.containsAnyChar(str, s);
}
});
}
@@ -195,23 +192,11 @@ public class StringPattern extends ObjectPattern<String, StringPattern> {
});
}
@Override
@NotNull
public StringPattern oneOf(@NonNls final String... values) {
return super.oneOf(values);
}
@NotNull
public StringPattern oneOfIgnoreCase(@NonNls final String... values) {
return with(new CaseInsensitiveValuePatternCondition("oneOfIgnoreCase", values));
}
@Override
@NotNull
public StringPattern oneOf(@NonNls final Collection<String> set) {
return super.oneOf(set);
}
@NotNull
public static CharSequence newBombedCharSequence(@NotNull CharSequence sequence) {
return new StringUtil.BombedCharSequence(sequence) {
@@ -1,11 +1,23 @@
/*
* Copyright (c) 2000-2007 JetBrains s.r.o. All Rights Reserved.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.patterns;
import com.intellij.openapi.util.Key;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.util.ProcessingContext;
import junit.framework.TestCase;
import java.util.Arrays;
@@ -14,37 +26,15 @@ import static com.intellij.patterns.StandardPatterns.*;
/**
* @author peter
*/
public class StandardPatternsTest extends UsefulTestCase {
public class StandardPatternsTest extends TestCase {
public void testNull() throws Throwable {
public void testNull() {
assertTrue(object().isNull().accepts(null));
assertFalse(object().isNull().accepts(""));
assertFalse(string().isNull().accepts(""));
assertTrue(string().isNull().accepts(null));
}
public void testString() throws Throwable {
final ElementPattern pattern = string();
assertFalse(string().accepts(new Object()));
assertTrue(pattern.accepts(""));
}
public void testStartsWith() throws Throwable {
final ElementPattern pattern = string().startsWith("abc");
assertFalse(pattern.accepts(""));
assertTrue(pattern.accepts("abcd"));
assertTrue(pattern.accepts("abc"));
assertFalse(string().startsWith("abc").accepts(new Object()));
}
public void testEndsWith() throws Throwable {
final ElementPattern pattern = string().endsWith("abc");
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("abcd"));
assertTrue(pattern.accepts("abc"));
}
private static void checkPrefixSuffix(final ElementPattern pattern) {
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("abcd"));
@@ -53,23 +43,23 @@ public class StandardPatternsTest extends UsefulTestCase {
assertFalse(pattern.accepts("abcdab"));
}
public void testPrefixSuffix() throws Throwable {
public void testPrefixSuffix() {
checkPrefixSuffix(string().endsWith("abc").startsWith("abc"));
}
public void testAnd1() throws Throwable {
public void testAnd1() {
checkPrefixSuffix(and(string().endsWith("abc"), string().startsWith("abc")));
}
public void testAnd2() throws Throwable {
public void testAnd2() {
checkPrefixSuffix(string().endsWith("abc").and(string().startsWith("abc")));
}
public void testOr1() throws Throwable {
public void testOr1() {
checkOr(or(string().endsWith("abc"), string().startsWith("abc")));
}
public void testNot1() throws Throwable {
public void testNot1() {
final ElementPattern pattern = not(or(string().endsWith("abc"), string().startsWith("abc")));
assertTrue(pattern.accepts(""));
assertTrue(pattern.accepts("xxx"));
@@ -90,7 +80,7 @@ public class StandardPatternsTest extends UsefulTestCase {
assertTrue(filterFactory.accepts("abcdab"));
}
public void testEquals() throws Throwable {
public void testEquals() {
final Object foo = new Object();
final Object bar = new Object();
ElementPattern objectPattern = object().equalTo(foo);
@@ -103,15 +93,14 @@ public class StandardPatternsTest extends UsefulTestCase {
}
public void testAll() throws Throwable {
public void testAll() {
ElementPattern pattern = collection(String.class).all(string().startsWith("abc"));
assertTrue(pattern.accepts(Arrays.asList("abc")));
assertTrue(pattern.accepts(Arrays.asList("abc", "abcd")));
assertFalse(pattern.accepts(Arrays.asList("abc", "bcd")));
}
public void testAtLeastOne() throws Throwable {
public void testAtLeastOne() {
ElementPattern pattern = collection(String.class).atLeastOne(string().startsWith("abc"));
assertTrue(pattern.accepts(Arrays.asList("abc")));
assertTrue(pattern.accepts(Arrays.asList("abc", "abcd")));
@@ -120,15 +109,16 @@ public class StandardPatternsTest extends UsefulTestCase {
assertTrue(pattern.accepts(Arrays.asList("bc", "abc")));
}
public void testFilter() throws Throwable {
ElementPattern pattern = collection(String.class).filter(string().endsWith("x"), collection(String.class).all(string().startsWith("abc")));
public void testFilter() {
ElementPattern pattern =
collection(String.class).filter(string().endsWith("x"), collection(String.class).all(string().startsWith("abc")));
assertTrue(pattern.accepts(Arrays.asList("abc")));
assertTrue(pattern.accepts(Arrays.asList("abc", "abcd")));
assertFalse(pattern.accepts(Arrays.asList("bcx", "bcd")));
assertTrue(pattern.accepts(Arrays.asList("abcx", "abc")));
}
public void testFirst() throws Throwable {
public void testFirst() {
ElementPattern pattern = collection(String.class).first(string().startsWith("abc"));
assertFalse(pattern.accepts(Arrays.<String>asList()));
assertTrue(pattern.accepts(Arrays.asList("abc")));
@@ -138,7 +128,7 @@ public class StandardPatternsTest extends UsefulTestCase {
assertFalse(pattern.accepts(Arrays.asList("bc", "abc")));
}
public void testLast() throws Throwable {
public void testLast() {
//collection(String.class)
ElementPattern pattern = collection(String.class).last(string().startsWith("abc"));
@@ -150,7 +140,7 @@ public class StandardPatternsTest extends UsefulTestCase {
assertTrue(pattern.accepts(Arrays.asList("bc", "abc")));
}
public void testSize() throws Throwable {
public void testSize() {
final CollectionPattern<String> filter = collection(String.class);
assertTrue(filter.size(0).accepts(Arrays.<String>asList()));
assertFalse(filter.size(0).accepts(Arrays.asList("abc")));
@@ -165,7 +155,7 @@ public class StandardPatternsTest extends UsefulTestCase {
assertTrue(filter.size(2).accepts(Arrays.asList("abc", "abc")));
}
public void testEmpty() throws Throwable {
public void testEmpty() {
final CollectionPattern<String> filter = collection(String.class);
assertTrue(filter.empty().accepts(Arrays.<String>asList()));
assertFalse(filter.empty().accepts(Arrays.asList("abc")));
@@ -176,7 +166,7 @@ public class StandardPatternsTest extends UsefulTestCase {
assertTrue(not(filter.empty()).accepts(Arrays.asList("abc", "abc")));
}
public void testSave() throws Throwable {
public void testSave() {
Key<String> key = Key.create("abc");
final ProcessingContext context = new ProcessingContext();
assertFalse(string().contains("abc").save(key).accepts(null));
@@ -189,6 +179,4 @@ public class StandardPatternsTest extends UsefulTestCase {
assertTrue(string().contains("abc").save(key).accepts(s, context));
assertSame(s, context.get(key));
}
}
@@ -0,0 +1,92 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.patterns;
import junit.framework.TestCase;
import static com.intellij.patterns.StandardPatterns.string;
public class StringPatternTest extends TestCase {
public void testString() {
final ElementPattern pattern = string();
assertFalse(string().accepts(new Object()));
assertTrue(pattern.accepts(""));
}
public void testStartsWith() {
final ElementPattern pattern = string().startsWith("abc");
assertFalse(pattern.accepts(""));
assertTrue(pattern.accepts("abcd"));
assertTrue(pattern.accepts("abc"));
assertFalse(string().startsWith("abc").accepts(new Object()));
}
public void testEndsWith() {
final ElementPattern pattern = string().endsWith("abc");
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("abcd"));
assertTrue(pattern.accepts("abc"));
}
public void testLongerThan() {
final ElementPattern pattern = string().longerThan(2);
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("01"));
assertTrue(pattern.accepts("012"));
}
public void testShorterThan() {
final ElementPattern pattern = string().shorterThan(2);
assertTrue(pattern.accepts(""));
assertTrue(pattern.accepts("1"));
assertFalse(pattern.accepts("12"));
}
public void testWithLength() {
final ElementPattern pattern = string().withLength(2);
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("1"));
assertTrue(pattern.accepts("12"));
}
public void testContains() {
final ElementPattern pattern = string().contains("abc");
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("acb"));
assertFalse(pattern.accepts("ABC"));
assertTrue(pattern.accepts("01abcd"));
}
public void testContainsChars() {
final ElementPattern pattern = string().containsChars("abc");
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("ABC"));
assertTrue(pattern.accepts("01a"));
assertTrue(pattern.accepts("01c"));
assertTrue(pattern.accepts("01b"));
}
public void testOneOfIgnoreCase() {
final ElementPattern pattern = string().oneOfIgnoreCase("a", "B");
assertFalse(pattern.accepts(""));
assertFalse(pattern.accepts("ab"));
assertFalse(pattern.accepts("d01x"));
assertTrue(pattern.accepts("A"));
assertTrue(pattern.accepts("b"));
}
}