package org.eclipse.xtext.xdoc; import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.HashMap; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EObject; import org.eclipse.xtend2.lib.StringConcatenation; import org.eclipse.xtext.junit4.InjectWith; import org.eclipse.xtext.junit4.XtextRunner; import org.eclipse.xtext.util.StopWatch; import org.eclipse.xtext.xbase.lib.CollectionLiterals; import org.eclipse.xtext.xbase.lib.Conversions; import org.eclipse.xtext.xbase.lib.Functions.Function0; import org.eclipse.xtext.xbase.lib.IntegerRange; import org.eclipse.xtext.xdoc.XdocInjectorProvider; 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 org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(XtextRunner.class) @InjectWith(XdocInjectorProvider.class) @SuppressWarnings("all") public class UtilityTest { private 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 = ((String[])Conversions.unwrapArray(CollectionLiterals.<String>newArrayList("grammar", "with", "import", "terminal", "returns"), String.class)); private String testCodeString = new Function0<String>() { public String apply() { StringConcatenation _builder = new StringConcatenation(); _builder.append("class Foo{"); _builder.newLine(); _builder.append("\t"); _builder.append("static int foo = 13;"); _builder.newLine(); _builder.append("}\t"); _builder.newLine(); _builder.newLine(); String _string = _builder.toString(); return _string; } }.apply(); private String expectationCodeString = new Function0<String>() { public String apply() { StringConcatenation _builder = new StringConcatenation(); _builder.append("class Foo{"); _builder.newLine(); _builder.append("\t"); _builder.append("static int foo = 13;"); _builder.newLine(); _builder.append("}"); String _string = _builder.toString(); return _string; } }.apply(); private CodeBlock testCodeBlock; @Before public void setUp() { LangDef _createLangDef = XdocFactory.eINSTANCE.createLangDef(); this.testLangDef = _createLangDef; EList<String> _keywords = this.testLangDef.getKeywords(); Iterables.<String>addAll(_keywords, ((Iterable<? extends String>)Conversions.doWrapArray(this.testKeyWords))); CodeBlock _createCodeBlock = XdocFactory.eINSTANCE.createCodeBlock(); this.testCodeBlock = _createCodeBlock; final Code testCode = XdocFactory.eINSTANCE.createCode(); testCode.setContents(this.testCodeString); EList<EObject> _contents = this.testCodeBlock.getContents(); _contents.add(testCode); } /** * TODO: replace Utils test * @Test * def testRemoveIndent() { * val result = StringFormatter::removeIndent(this.testCodeBlock) * 1.assertEquals(result.contents.size) * (result.contents.head instanceof Code).assertTrue * val resultCode = result.contents.head as Code * expectationCodeString.assertEquals(resultCode.contents) * } */ @Test public void testFormatCode() { final Utils utils = new Utils(); String code = ""; final StopWatch watch = new StopWatch(); IntegerRange _upTo = new IntegerRange(0, 1); for (final Integer i : _upTo) { LangDef _langDef = this.langDef(); String _formatCode = utils.formatCode("mein foo ist bar nicht baz.", _langDef); code = _formatCode; } watch.resetAndLog("keywords"); Assert.assertEquals("mein <span class=\"keyword\">foo</span> ist <span class=\"keyword\">bar</span> nicht <span class=\"keyword\">baz</span>.", code); } @Test public void testFormatCode_01() { final Utils utils = new Utils(); LangDef _langDef = this.langDef(); final String code = utils.formatCode("/* mein foo ist bar nicht baz.*/", _langDef); Assert.assertEquals("<span class=\"comment\">/* mein foo ist bar nicht baz.*/</span>", code); } @Test public void testFormatCode_02() { final Utils utils = new Utils(); LangDef _langDef = this.langDef(); final String code = utils.formatCode("\' mein foo ist bar nicht baz.\'", _langDef); Assert.assertEquals("<span class=\"string\">' mein foo ist bar nicht baz.'</span>", code); } @Test public void testFormatCode_03() { final Utils utils = new Utils(); final String code = utils.formatCode("\' mein foo ist bar nicht baz.", null); Assert.assertEquals("<span class=\"string\">' mein foo ist bar nicht baz.</span>", code); } @Test public void testFormatCode_04() { final Utils utils = new Utils(); final String code = utils.formatCode("/* mein foo ist bar nicht baz.", null); Assert.assertEquals("/* mein foo ist bar nicht baz.", code); } @Test public void testFormatCode_05() { final Utils utils = new Utils(); final String code = utils.formatCode("\'\\[mein\\]\'", null); Assert.assertEquals("<span class=\"string\">'\\[mein\\]'</span>", code); } public LangDef langDef() { final ArrayList<?> _cacheKey = CollectionLiterals.newArrayList(); final LangDef _result; synchronized (_createCache_langDef) { if (_createCache_langDef.containsKey(_cacheKey)) { return _createCache_langDef.get(_cacheKey); } LangDef _createLangDef = XdocFactory.eINSTANCE.createLangDef(); _result = _createLangDef; _createCache_langDef.put(_cacheKey, _result); } _init_langDef(_result); return _result; } private final HashMap<ArrayList<?>, LangDef> _createCache_langDef = CollectionLiterals.newHashMap(); private void _init_langDef(final LangDef it) { LangDef _langDef = this.langDef(); EList<String> _keywords = _langDef.getKeywords(); ArrayList<String> _newArrayList = CollectionLiterals.<String>newArrayList("foo", "bar", "baz", "dfsdf", "wweee", "dsfsd"); Iterables.<String>addAll(_keywords, _newArrayList); } }