From 9e178f2b274b2e8d498ab3c9d86df474fa0ab76a Mon Sep 17 00:00:00 2001 From: Lars Vogel Date: Sun, 31 Jan 2016 10:33:24 +0100 Subject: Bug 486903 - [Templates] Allow specifying a default value in case the word_selection is empty Change-Id: I5a5c2e77f65aa45474362838aa71fc351779dcd7 Signed-off-by: Lars Vogel --- .../TemplateVariablesWordSelectionTest.java | 79 ++++++++++++++++++++++ .../text/tests/templates/TemplatesTestSuite.java | 7 +- 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java (limited to 'org.eclipse.text.tests') 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 - 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 - Allow to specify format for date variable - https://bugs.eclipse.org/75981 - * Lars Vogel - Bug 486889 + * Lars Vogel - 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 -- cgit v1.2.3