package org.eclipse.xtext.xdoc.tests;
import java.util.Arrays;
import org.eclipse.xtext.util.StopWatch;
import org.eclipse.xtext.xdoc.generator.util.Utils;
import org.eclipse.xtext.xdoc.xdoc.Code;
import org.eclipse.xtext.xdoc.xdoc.CodeBlock;
import org.eclipse.xtext.xdoc.xdoc.LangDef;
import org.eclipse.xtext.xdoc.xdoc.XdocFactory;
import templates.util.StringFormatter;
import junit.framework.TestCase;
public class UtilityTest extends TestCase {
String testStringSplitting =
" grammar org.eclipse.xtext.common.Terminals <br />\n" +
" hidden(WS, ML_COMMENT, SL_COMMENT)<br />\n" +
"import \"http://www.eclipse.org/emf/2002/Ecore\" as ecore<br />\n" +
"terminal ID : <br />\n" +
" '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* ;<br />\n" +
"terminal INT returns ecore::EInt: ('0'..'9')+ ;<br />\n" +
"terminal STRING : <br />\n" +
" '\"' ( '\\\\' ('b'|'t'|'n'|'f'|'r'|'\"'|\"'\"|'\\\\') | !('\\'|'\"') )* '\"' |<br />\n" +
" \"'\" ( '\\\\' ('b'|'t'|'n'|'f'|'r'|'\"'|\"'\"|'\\\\') | !('\\\\'|\"'\") )* \"'\"; <br />\n" +
"terminal ML_COMMENT : '/*' -> '*/' ;<br />\n" +
"terminal SL_COMMENT : '//' !('\\n'|'\\r')* ('\\r'? '\\n')? ;<br />\n" +
"terminal WS : (' '|'\\t'|&apos\\;\\r'|'\\n')+ ;<br />\n" +
"terminal ANY_OTHER: . ;";
private LangDef testLangDef;
private String[] testKeyWords = {"grammar", "with", "import", "terminal", "returns", };
private String testCodeString = "\tclass Foo{\n"+
"\t\tstatic int foo = 13;\n" +
"\t}\t\n\n ";
private String expectationCodeString = "class Foo{\n\tstatic int foo = 13;\n}";
private CodeBlock testCodeBlock;
@Override
protected void setUp() throws Exception {
super.setUp();
this.testLangDef = XdocFactory.eINSTANCE.createLangDef();
testLangDef.getKeywords().addAll(Arrays.asList(testKeyWords));
this.testCodeBlock = XdocFactory.eINSTANCE.createCodeBlock();
Code testCode = XdocFactory.eINSTANCE.createCode();
testCode.setContents(testCodeString);
testCodeBlock.getContents().add(testCode);
}
public void testSplitting() throws Exception {
String text = testStringSplitting;
String[] toks;
do {
toks = StringFormatter.splitToNext(text);
if(toks.length == 3){
assertFalse("infinite loop: " + toks[2] + " == " + text, toks[2].equals(text));
text = toks[2];
}
} while(toks.length == 3);
}
public void testRemoveIndent() throws Exception {
CodeBlock result = StringFormatter.removeIndent(this.testCodeBlock);
assertEquals(1, result.getContents().size());
assertTrue(result.getContents().get(0) instanceof Code);
Code resultCode = (Code) result.getContents().get(0);
assertEquals(expectationCodeString, resultCode.getContents());
}
public void testFormatCode() throws Exception {
LangDef langDef = XdocFactory.eINSTANCE.createLangDef();
langDef.getKeywords().add("foo");
langDef.getKeywords().add("bar");
langDef.getKeywords().add("baz");
langDef.getKeywords().add("dfsdf");
langDef.getKeywords().add("wweee");
langDef.getKeywords().add("dsfsd");
final Utils utils = new Utils();
String code = null;
StopWatch watch = new StopWatch();
for (int i = 0; i < 2 ;i++) {
code = utils.formatCode("mein foo ist bar nicht baz.", langDef);
}
watch.resetAndLog("keywords");
assertEquals("mein <span class=\"keyword\">foo</span> ist <span class=\"keyword\">bar</span> nicht <span class=\"keyword\">baz</span>.", code);
}
public void testFormatCode_01() throws Exception {
LangDef langDef = XdocFactory.eINSTANCE.createLangDef();
langDef.getKeywords().add("foo");
langDef.getKeywords().add("bar");
langDef.getKeywords().add("baz");
langDef.getKeywords().add("dfsdf");
langDef.getKeywords().add("wweee");
langDef.getKeywords().add("dsfsd");
final Utils utils = new Utils();
String code = utils.formatCode("/* mein foo ist bar nicht baz.*/", langDef);
assertEquals("<span class=\"comment\">/* mein foo ist bar nicht baz.*/</span>", code);
}
public void testFormatCode_02() throws Exception {
LangDef langDef = XdocFactory.eINSTANCE.createLangDef();
langDef.getKeywords().add("foo");
langDef.getKeywords().add("bar");
langDef.getKeywords().add("baz");
langDef.getKeywords().add("dfsdf");
langDef.getKeywords().add("wweee");
langDef.getKeywords().add("dsfsd");
final Utils utils = new Utils();
String code = utils.formatCode("' mein foo ist bar nicht baz.'", langDef);
assertEquals("<span class=\"string\">' mein foo ist bar nicht baz.'</span>", code);
}
public void testFormatCode_03() throws Exception {
final Utils utils = new Utils();
String code = utils.formatCode("' mein foo ist bar nicht baz.", null);
assertEquals("<span class=\"string\">' mein foo ist bar nicht baz.</span>", code);
}
public void testFormatCode_04() throws Exception {
final Utils utils = new Utils();
String code = utils.formatCode("/* mein foo ist bar nicht baz.", null);
assertEquals("/* mein foo ist bar nicht baz.", code);
}
public void testFormatCode_05() throws Exception {
final Utils utils = new Utils();
String code = utils.formatCode("'\\[mein\\]'", null);
assertEquals("<span class=\"string\">'\\[mein\\]'</span>", code);
}
}