1 /*
2  * Copyright (C) 2011, 2013-2016 The JavaParser Team.
3  *
4  * This file is part of JavaParser.
5  *
6  * JavaParser can be used either under the terms of
7  * a) the GNU Lesser General Public License as published by
8  *     the Free Software Foundation, either version 3 of the License, or
9  *     (at your option) any later version.
10  * b) the terms of the Apache License
11  *
12  * You should have received a copy of both licenses in LICENCE.LGPL and
13  * LICENCE.APACHE. Please refer to those files for details.
14  *
15  * JavaParser 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 Lesser General Public License for more details.
19  */
20 package com.github.javaparser;
21 
22 import static com.github.javaparser.UnicodeEscapeProcessingProviderTest.*;
23 import static org.junit.jupiter.api.Assertions.*;
24 
25 import java.io.IOException;
26 import java.util.Arrays;
27 import java.util.Iterator;
28 import java.util.List;
29 
30 import org.junit.jupiter.api.Test;
31 
32 import com.github.javaparser.UnicodeEscapeProcessingProvider.PositionMapping;
33 
34 /**
35  * Test case for {@link PositionMapping}.
36  *
37  * @author <a href="mailto:[email protected]">Bernhard Haumacher</a>
38  */
39 @SuppressWarnings("javadoc")
40 public class PositionMappingTest {
41 
42 	@Test
testNoMapping()43 	public void testNoMapping() throws IOException {
44 		List<List<String>> input = lines(
45 			line("Hello World !\n"),
46 			line("Next Line\r"),
47 			line("Third Line\r\n"),
48 			line("Fourth Line."));
49 		String inputText = text(input);
50 		UnicodeEscapeProcessingProvider provider = provider(inputText);
51 		String outputText = process(provider);
52 		assertEquals(inputText, outputText);
53 		PositionMapping mapping = provider.getPositionMapping();
54 		assertTrue(mapping.isEmpty());
55 		assertEquals(4, provider.getInputCounter().getLine());
56 		assertEquals(4, provider.getOutputCounter().getLine());
57 		assertSame(PositionMapping.PositionUpdate.NONE, mapping.lookup(new Position(10000, 1)));
58 	}
59 
60 	@Test
testEncodedLineFeed()61 	public void testEncodedLineFeed() throws IOException {
62 		List<List<String>> input = lines(
63 			line("B", "\\u000A", "C"));
64 		List<List<String>> output = lines(
65 			line("B", "\n"),
66 			line("C"));
67 
68 		checkConvert(input, output);
69 	}
70 
71 	@Test
testComplexMapping()72 	public void testComplexMapping() throws IOException {
73 		List<List<String>> input = lines(
74 			// Character positions:
75 			//                      111    1 11111    1222    2 2222     2
76 			//    1    2 34567    89012    3 45678    9012    3 45678    9
77 			line("H", "\\u00E4", "llo W", "\\u00F6", "rld!", "\\u000A", "123 N", "\\u00E4", "xt Line", "\\u000D", "Third Line", "\r\n"),
78 			line("Fo", "\\u00FC", "rth Line."));
79 		List<List<String>> output = lines(
80 			line("H", "ä", "llo W", "ö", "rld!", "\n"),
81 			line("123 N", "ä", "xt Line", "\r"),
82 			line("Third Line", "\r\n"),
83 			line("Fo", "ü", "rth Line."));
84 
85 		checkConvert(input, output);
86 	}
87 
checkConvert(List<List<String>> input, List<List<String>> output)88 	private void checkConvert(List<List<String>> input,
89 			List<List<String>> output) throws IOException {
90 		UnicodeEscapeProcessingProvider provider = provider(text(input));
91 		String decoded = process(provider);
92 		assertEquals(text(output), decoded);
93 
94 		PositionMapping mapping = provider.getPositionMapping();
95 
96 		// Coarse grained test.
97 		assertEquals(input.size(), provider.getInputCounter().getLine());
98 		assertEquals(output.size(), provider.getOutputCounter().getLine());
99 
100 		// Fine grained test.
101 		int inPosLine = 1;
102 		int inPosColumn = 1;
103 		int outPosLine = 1;
104 		int outPosColumn = 1;
105 		Iterator<List<String>> outLineIt = output.iterator();
106 		List<String> outLine = outLineIt.next();
107 		Iterator<String> outPartIt = outLine.iterator();
108 		String outPart = outPartIt.next();
109 		boolean outFinished = false;
110 		for (List<String> inLine : input) {
111 			for (String inPart : inLine) {
112 				assertFalse(outFinished);
113 
114 				Position inPos = new Position(inPosLine, inPosColumn);
115 				Position outPos = new Position(outPosLine, outPosColumn);
116 				Position transfomedOutPos = mapping.transform(outPos);
117 
118 				assertEquals(inPos, transfomedOutPos,
119 					"Position mismatch at '" + outPart + "' " + outPos + " -> '" + inPart + "' " + inPos + ".");
120 
121 				outPosColumn += outPart.length();
122 				inPosColumn += inPart.length();
123 
124 				if (!outPartIt.hasNext()) {
125 					if (outLineIt.hasNext()) {
126 						outPartIt = outLineIt.next().iterator();
127 						outPosLine ++;
128 						outPosColumn = 1;
129 
130 						outPart = outPartIt.next();
131 					} else {
132 						outFinished = true;
133 					}
134 				} else {
135 					outPart = outPartIt.next();
136 				}
137 			}
138 
139 			inPosColumn = 1;
140 			inPosLine++;
141 		}
142 	}
143 
text(List<List<String>> input)144 	private static String text(List<List<String>> input) {
145 		StringBuilder result = new StringBuilder();
146 		for (List<String> line : input) {
147 			for (String part : line) {
148 				result.append(part);
149 			}
150 		}
151 		return result.toString();
152 	}
153 
154 	@SafeVarargs
line(String ....parts)155 	private static List<String> line(String ...parts) {
156 		return Arrays.asList(parts);
157 	}
158 
159 	@SafeVarargs
lines(List<String> ....lines)160 	private static List<List<String>> lines(List<String> ...lines) {
161 		return Arrays.asList(lines);
162 	}
163 
164 }
165