/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
*
* 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.android.ide.eclipse.adt.internal.editors.formatting;
import com.android.ide.common.xml.XmlFormatPreferences;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.ReplaceEdit;
import junit.framework.TestCase;
@SuppressWarnings("javadoc")
public class AndroidXmlFormattingStrategyTest extends TestCase {
// In the given before document, replace in the range replaceStart to replaceEnd
// the formatted string, and assert that it's identical to the given after string
private void check(String before, int replaceStart, int replaceEnd, String formatted,
String expected, XmlFormatPreferences prefs)
throws MalformedTreeException, BadLocationException {
Document document = new Document();
document.set(before);
ReplaceEdit edit = AndroidXmlFormattingStrategy.createReplaceEdit(document, replaceStart,
replaceEnd, formatted, prefs);
assertNotNull(edit);
edit.apply(document);
String contents = document.get();
// Ensure that we don't have any mangled CRLFs
char prev = 0;
boolean haveCrlf = false;
for (int i = 0, n = contents.length(); i < n; i++) {
char c = contents.charAt(i);
if (c == '\r') {
haveCrlf = true;
}
if (!(c != '\r' || prev != '\r')) {
fail("Mangled document: Found adjacent \\r's starting at " + i
+ ": " + contents.substring(i - 1, Math.min(contents.length(), i + 10))
+ "...");
}
if (haveCrlf && c == '\n' && prev != '\r') {
fail("Mangled document: In a CRLF document, found \\n without preceeding \\r");
}
prev = c;
}
assertEquals(expected, contents);
}
// In the given before document, replace the range indicated by [ and ] with the given
// formatted string, and assert that it's identical to the given after string
private void check(
String before, String insert, String expected,
XmlFormatPreferences prefs)
throws MalformedTreeException, BadLocationException {
int replaceStart = before.indexOf('[');
assertTrue(replaceStart != -1);
before = before.substring(0, replaceStart) + before.substring(replaceStart + 1);
int replaceEnd = before.indexOf(']');
assertTrue(replaceEnd != -1);
before = before.substring(0, replaceEnd) + before.substring(replaceEnd + 1);
check(before, replaceStart, replaceEnd, insert, expected, prefs);
}
public void test1() throws Exception {
check(
// Before
"\n" +
"[ \n" +
" \n" +
"]\n" +
"\n",
// Insert
" \n" +
" \n",
// After
"\n" +
" \n" +
" \n" +
"\n" +
"\n",
XmlFormatPreferences.defaults());
}
public void test2() throws Exception {
XmlFormatPreferences prefs = XmlFormatPreferences.defaults();
prefs.removeEmptyLines = true;
check(
// Before
"\n" +
"\n" +
"\n" +
"[ \n" +
" \n" +
"]\n" +
"\n" +
"\n" +
"\n",
// Insert
" \n" +
" \n",
// After
"\n" +
" \n" +
" \n" +
"\n",
prefs);
}
public void test3() throws Exception {
XmlFormatPreferences prefs = XmlFormatPreferences.defaults();
prefs.removeEmptyLines = true;
check(
// Before
"\n" +
"\n" +
"\n" +
" [\n" +
" ]\n" +
"\n" +
"\n" +
"\n" +
"\n",
// Insert
" \n" +
" ",
// After
"\n" +
" \n" +
" \n" +
"\n",
prefs);
}
public void test4() throws Exception {
check(
"\n" +
"\n" +
" []\n" +
"\n" +
"\n",
// Insert
"\n" +
" \n",
// After
"\n" +
"\n" +
" \n" +
"\n" +
"\n",
XmlFormatPreferences.defaults());
}
public void testCrLf1() throws Exception {
check(
// Before
"\r\n" +
"[ \r\n" +
" \r\n" +
"]\r\n" +
"\r\n",
// Insert
" \r\n" +
" \r\n",
// After
"\r\n" +
" \r\n" +
" \r\n" +
"\r\n" +
"\r\n",
XmlFormatPreferences.defaults());
}
public void testCrLf2() throws Exception {
XmlFormatPreferences prefs = XmlFormatPreferences.defaults();
prefs.removeEmptyLines = true;
check(
// Before
"\r\n" +
"\r\n" +
"\r\n" +
"[ \r\n" +
" \r\n" +
"]\r\n" +
"\r\n" +
"\r\n" +
"\r\n",
// Insert
" \r\n" +
" \r\n",
// After
"\r\n" +
" \r\n" +
" \r\n" +
"\r\n",
prefs);
}
public void testCrLf3() throws Exception {
XmlFormatPreferences prefs = XmlFormatPreferences.defaults();
prefs.removeEmptyLines = true;
check(
// Before
"\r\n" +
"\r\n" +
"\r\n" +
" [\r\n" +
" ]\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"\r\n",
// Insert
" \r\n" +
" ",
// After
"\r\n" +
" \r\n" +
" \r\n" +
"\r\n",
prefs);
}
public void testCrlf4() throws Exception {
check(
"\r\n" +
"\r\n" +
" []\r\n" +
"\r\n" +
"\r\n",
// Insert
"\r\n" +
" \r\n",
// After
"\r\n" +
"\r\n" +
" \r\n" +
"\r\n" +
"\r\n",
XmlFormatPreferences.defaults());
}
}