|
| 1 | +package twg2.parser.test; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | +import java.util.function.Function; |
| 7 | + |
| 8 | +import lombok.val; |
| 9 | + |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import twg2.arrays.ArrayUtil; |
| 14 | +import twg2.parser.Inclusion; |
| 15 | +import twg2.parser.text.CharParserCondition; |
| 16 | +import twg2.parser.text.CharPrecondition; |
| 17 | +import twg2.parser.text.StringBoundedParserBuilder; |
| 18 | +import twg2.parser.textParser.TextParser; |
| 19 | +import twg2.parser.textParser.TextParserImpl; |
| 20 | +import twg2.parser.textParserUtils.EscapeSequences; |
| 21 | +import twg2.text.stringUtils.StringCompare; |
| 22 | +import twg2.text.stringUtils.StringIndex; |
| 23 | +import checks.CheckTask; |
| 24 | +import checks.TestData; |
| 25 | +import checks.TestDataObj; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author TeamworkGuy2 |
| 29 | + * @since 2016-0-0 |
| 30 | + */ |
| 31 | +public class MiscStringTests { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void startsWithTest() { |
| 35 | + List<TestData<String, String>> startsWithStrs = Arrays.asList( |
| 36 | + TestDataObj.matchFalse("this a lz3", "thisa"), |
| 37 | + TestDataObj.matchTrue("this a lz3", "this"), |
| 38 | + TestDataObj.matchFalse("a", "ab"), |
| 39 | + TestDataObj.matchTrue("a", "a"), |
| 40 | + TestDataObj.matchFalse("", "a"), |
| 41 | + TestDataObj.matchTrue("", "") |
| 42 | + ); |
| 43 | + |
| 44 | + for(TestData<String, String> test : startsWithStrs) { |
| 45 | + Assert.assertTrue(test.isShouldInputEqualExpected() == |
| 46 | + StringCompare.startsWith(test.getInput().toCharArray(), 0, test.getExpected().toCharArray(), 0)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + @Test |
| 52 | + public void indexOfTest() { |
| 53 | + String searchString = "this 32a this 32.1f is_a string"; |
| 54 | + String[] strs = new String[] {"32a", "32z", " ", " ", "this", "string"}; |
| 55 | + Integer[] expect = { 5, -1, 4, -1, 0, 25 }; |
| 56 | + char[] searchChars = searchString.toCharArray(); |
| 57 | + |
| 58 | + CheckTask.assertTests(strs, expect, (str) -> StringIndex.indexOf(searchChars, 0, str, 0)); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Test |
| 63 | + public void lineBufferTest() { |
| 64 | + String line = "Abbb abab [very awesome] 132\n" + |
| 65 | + "few 142345 52132"; |
| 66 | + |
| 67 | + TextParser tool = TextParserImpl.of(line); |
| 68 | + StringBuilder dst = new StringBuilder(); |
| 69 | + |
| 70 | + tool.nextIf('A', dst); |
| 71 | + tool.nextIf((c) -> (c == 'b'), 2, dst); |
| 72 | + check(tool.nextIf('b', dst), 1, "could not read 'b'"); |
| 73 | + check(tool.nextIf(' ', dst), 1, "could not read ' '"); |
| 74 | + check(tool.nextIf('a', 'b', 0, dst), 4, "could not read 'a' or 'b'"); |
| 75 | + tool.nextIf((c) -> (true), 0, dst); |
| 76 | + |
| 77 | + tool.nextIf((c) -> (true), 3, dst); |
| 78 | + tool.nextIf((c) -> (ArrayUtil.indexOf(new char[] {'1', '2', '3', '4', '5', ' '}, c) > -1) , 0, dst); |
| 79 | + tool.nextIf((c) -> (ArrayUtil.indexOf(new char[] {'1', '2', '3', '4', '5', ' '}, c) > -1) , 0, dst); |
| 80 | + |
| 81 | + Assert.assertEquals("parsed: '" + dst.toString() + "', does not match: '" + line + "'", line, dst.toString()); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @Test |
| 86 | + public void stringBoundedSegmentParserTest() throws IOException { |
| 87 | + |
| 88 | + // single-character start and end markers and single-character escape markers |
| 89 | + String[] strs = new String[] { "\"a \\\" b \\\"", "\"\" !", "alpha", "\"a \n\\\"\\\" z\" echo" }; |
| 90 | + String[] expect = new String[] { "\"a \" b \"", "\"\"", "", "\"a \n\"\" z\"" }; |
| 91 | + |
| 92 | + CharPrecondition parser1 = new StringBoundedParserBuilder("stringBoundedSegmentParserTest").addStartEndNotPrecededByMarkers("string literal", '"', '\\', '"', Inclusion.INCLUDE).build(); |
| 93 | + |
| 94 | + Function<String, String> escSeqDecoder = EscapeSequences.unicodeEscapeDecoder(); |
| 95 | + CheckTask.assertTests(strs, expect, (s, i) -> { |
| 96 | + val dst = new StringBuilder(); |
| 97 | + //Assert.assertTrue("i=" + i + " first char '" + s.charAt(0) + "' of '" + s + "'", parser1.isMatch(s.charAt(0))); |
| 98 | + CharParserCondition cond = parser1.createParser(); |
| 99 | + cond.readConditional(TextParserImpl.of(s), dst); |
| 100 | + return escSeqDecoder.apply(dst.toString()); |
| 101 | + }); |
| 102 | + |
| 103 | + // TODO reimplement string markers |
| 104 | + // multi-character start and end markers and multi-character escape markers |
| 105 | + //strs = new String[] { "to /**string @@/** and @@**/", "/**/", "alpha", "@@/**start \n@@/**@@**/ end**/ echo" }; |
| 106 | + //expect = new String[] { "/**string @@/** and **/", "/**/", "", "/**start \n@@/****/ end**/" }; |
| 107 | + /* |
| 108 | + settings.setEscapeString("@@") |
| 109 | + .setHandleEscapes(true) |
| 110 | + .setReadMultiline(true); |
| 111 | + input = new ParserHelper(); |
| 112 | + */ |
| 113 | + //StringBoundedParser parser2 = new StringBoundedParser(settings, input, new String[] {"/**"}, new String[] {"**/"}); |
| 114 | + /* |
| 115 | + Check.assertTests(strs, expect, "", "mismatch", (s) -> { |
| 116 | + dst.setLength(0); |
| 117 | + return parser2.readElement(LineBufferImpl.of(s), dst).toString(); |
| 118 | + }); |
| 119 | + */ |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + private static void check(int input, int expected, String message) { |
| 124 | + Assert.assertEquals(message + " (input: " + input + ", expected: " + expected + ")", expected, input); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments