diff options
author | Lars Vogel | 2016-01-31 09:33:24 +0000 |
---|---|---|
committer | Lars Vogel | 2016-02-09 09:37:10 +0000 |
commit | 9e178f2b274b2e8d498ab3c9d86df474fa0ab76a (patch) | |
tree | 18cbf65a69ae102a6e742eea0be0c602fa622dac /org.eclipse.text.tests | |
parent | 4cffb0013023a237c3d43ffa763649b07f7b61fe (diff) | |
download | eclipse.platform.text-9e178f2b274b2e8d498ab3c9d86df474fa0ab76a.tar.gz eclipse.platform.text-9e178f2b274b2e8d498ab3c9d86df474fa0ab76a.tar.xz eclipse.platform.text-9e178f2b274b2e8d498ab3c9d86df474fa0ab76a.zip |
Bug 486903 - [Templates] Allow specifying a default value in case theI20160209-0800
word_selection is empty
Change-Id: I5a5c2e77f65aa45474362838aa71fc351779dcd7
Signed-off-by: Lars Vogel <Lars.Vogel@vogella.com>
Diffstat (limited to 'org.eclipse.text.tests')
2 files changed, 83 insertions, 3 deletions
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java new file mode 100644 index 00000000000..040683c6539 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2016 vogella GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Lars Vogel <Lars.Vogel@vogella.com> - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests.templates; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.templates.DocumentTemplateContext; +import org.eclipse.jface.text.templates.GlobalTemplateVariables; +import org.eclipse.jface.text.templates.TemplateBuffer; +import org.eclipse.jface.text.templates.TemplateContextType; +import org.eclipse.jface.text.templates.TemplateTranslator; +import org.eclipse.jface.text.templates.TemplateVariable; + +public class TemplateVariablesWordSelectionTest { + + private TemplateTranslator fTranslator; + + private DocumentTemplateContext fContext; + + private TemplateContextType fType; + + @Before + public void setUp() { + fTranslator= new TemplateTranslator(); + + fType= new TemplateContextType(); + fType.addResolver(new GlobalTemplateVariables.WordSelection()); + + fContext= new DocumentTemplateContext(fType, new Document(), 0, 0); + } + + @Test + public void testWithoutParameter() throws Exception { + TemplateBuffer buffer= fTranslator.translate("Selected word is ${word_selection}!"); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("Selected word is !"); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + @Test + public void testWithParameter() throws Exception { + TemplateBuffer buffer= fTranslator.translate("No selection results in the ${w:word_selection('default')} text."); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("No selection results in the default text."); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + /** + * Ensures that {@link TemplateBuffer#getString()} equals the expected String and that all + * {@link TemplateBuffer#getVariables()} are resolved and unambiguous. + * + * @param expected expected result + * @param buffer the template buffer + */ + private static void assertBufferStringAndVariables(String expected, TemplateBuffer buffer) { + assertEquals(expected, buffer.getString()); + for (TemplateVariable v : buffer.getVariables()) { + assertTrue(v.isResolved()); + assertTrue(v.isUnambiguous()); + } + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java index 5160075d68b..422bf62be70 100755 --- a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Jeremie Bresson <jbr@bsiag.com> - Allow to specify format for date variable - https://bugs.eclipse.org/75981 - * Lars Vogel <Lars.Vogel@vogella.com> - Bug 486889 + * Lars Vogel <Lars.Vogel@vogella.com> - Bug 486889, 486903 *******************************************************************************/ package org.eclipse.text.tests.templates; @@ -22,8 +22,9 @@ import org.junit.runners.Suite.SuiteClasses; * */ @RunWith(Suite.class) -@SuiteClasses({ - TemplateTranslatorTest.class, +@SuiteClasses({ + TemplateTranslatorTest.class, + TemplateVariablesWordSelectionTest.class, GlobalTemplateVariablesDateTest.class}) public class TemplatesTestSuite { // intentionally empty |