From 64c812ea6918a4479ad017743ff80aaa32febf85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20C=C3=A9bron?= Date: Mon, 14 Jul 2014 20:04:35 +0200 Subject: [PATCH] StringPattern: cleanup, add StringPatternTest --- .../com/intellij/patterns/StringPattern.java | 25 +---- .../patterns/StandardPatternsTest.java | 76 +++++++-------- .../intellij/patterns/StringPatternTest.java | 92 +++++++++++++++++++ 3 files changed, 129 insertions(+), 64 deletions(-) create mode 100644 platform/platform-tests/testSrc/com/intellij/patterns/StringPatternTest.java diff --git a/platform/core-api/src/com/intellij/patterns/StringPattern.java b/platform/core-api/src/com/intellij/patterns/StringPattern.java index 13e148d479c1..a8bab13ea6a3 100644 --- a/platform/core-api/src/com/intellij/patterns/StringPattern.java +++ b/platform/core-api/src/com/intellij/patterns/StringPattern.java @@ -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 { return with(new PatternCondition("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 { return with(new PatternCondition("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 { return with(new PatternCondition("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 { return with(new PatternCondition("containsChars") { @Override public boolean accepts(@NotNull final String str, final ProcessingContext context) { - for (int i=0, len=s.length(); i-1) return true; - } - return false; + return StringUtil.containsAnyChar(str, s); } }); } @@ -195,23 +192,11 @@ public class StringPattern extends ObjectPattern { }); } - @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 set) { - return super.oneOf(set); - } - @NotNull public static CharSequence newBombedCharSequence(@NotNull CharSequence sequence) { return new StringUtil.BombedCharSequence(sequence) { diff --git a/platform/platform-tests/testSrc/com/intellij/patterns/StandardPatternsTest.java b/platform/platform-tests/testSrc/com/intellij/patterns/StandardPatternsTest.java index 1e05add24302..1db3f319b250 100644 --- a/platform/platform-tests/testSrc/com/intellij/patterns/StandardPatternsTest.java +++ b/platform/platform-tests/testSrc/com/intellij/patterns/StandardPatternsTest.java @@ -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.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 filter = collection(String.class); assertTrue(filter.size(0).accepts(Arrays.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 filter = collection(String.class); assertTrue(filter.empty().accepts(Arrays.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 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)); } - - } diff --git a/platform/platform-tests/testSrc/com/intellij/patterns/StringPatternTest.java b/platform/platform-tests/testSrc/com/intellij/patterns/StringPatternTest.java new file mode 100644 index 000000000000..11a6eac867df --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/patterns/StringPatternTest.java @@ -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")); + } +}