|
| 1 | +/* |
| 2 | + * Catroid: An on-device visual programming system for Android devices |
| 3 | + * Copyright (C) 2010-2020 The Catrobat Team |
| 4 | + * (<http://developer.catrobat.org/credits>) |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * An additional term exception under section 7 of the GNU Affero |
| 12 | + * General Public License, version 3, is available at |
| 13 | + * http://developer.catrobat.org/license_additional_term |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +package org.catrobat.catroid.test.ui.regexassistant; |
| 25 | + |
| 26 | +import android.app.Activity; |
| 27 | + |
| 28 | +import org.catrobat.catroid.utils.HtmlRegexExtractor; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.assertNull; |
| 34 | + |
| 35 | +public class HtmlRegexExtractorTest { |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp() { |
| 39 | + Activity context = new Activity(); |
| 40 | + htmlExtractor = new HtmlRegexExtractor(context); |
| 41 | + } |
| 42 | + private HtmlRegexExtractor htmlExtractor; |
| 43 | + |
| 44 | + @Test(expected = IllegalArgumentException.class) |
| 45 | + public void testFindKeywordWithEmptyKeyword() { |
| 46 | + htmlExtractor.findKeyword("", "abc"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testFindKeywordWithWrongKeyword() { |
| 51 | + assertNull(htmlExtractor.findKeyword("def", "abc")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testFindOneKeywordInLongWord() { |
| 56 | + assertEquals("abc", htmlExtractor.findKeyword("abc", "abcdefg")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testFindKeywordInSentence() { |
| 61 | + assertEquals("abc", htmlExtractor.findKeyword("abc", "Wer are looking for the abc " |
| 62 | + + "statement in long text")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testFindKeywordInSentenceWithSpaceInKeyword() { |
| 67 | + assertEquals("ab c", htmlExtractor.findKeyword("ab c", "Wer are looking for the " |
| 68 | + + "ab c statement in long text")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testFindKeywordInSentenceWithSpaceInKeywordAndTextWithNBSP() { |
| 73 | + assertEquals("ab c", htmlExtractor.findKeyword("ab c", "Wer are looking for " |
| 74 | + + "the " |
| 75 | + + "ab c statement in long text")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testKeywordWithNonBreakingSpace() { |
| 80 | + assertEquals("Key Word", htmlExtractor.findKeyword("Key Word", "Key Word")); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testKeywordWithTag() { |
| 85 | + assertEquals("Key <i>Word", htmlExtractor.findKeyword("Key Word", "Key " |
| 86 | + + "<i>Word</i>")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testFalseKeywordOrder() { |
| 91 | + assertNull(htmlExtractor.findKeyword("Key Word", "Word Key")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testRegexAsKeyword() { |
| 96 | + assertNull("A regular expression should not be found inside a html text that " |
| 97 | + + "doesn't contain it literally", |
| 98 | + htmlExtractor.findKeyword("[A-Z]", "ABCDE")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testRegexAsKeywordAndInText() { |
| 103 | + assertEquals("[A-Z]", htmlExtractor.findKeyword("[A-Z]", "[A-Z]+.*")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testFindKeywordSmallestOccurrence() { |
| 108 | + assertEquals("Hello World", htmlExtractor.findKeyword("Hello World", |
| 109 | + "Hello Banana Animal Text Hello World Ape")); |
| 110 | + } |
| 111 | +} |
0 commit comments