Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremie Bresson2016-01-23 07:27:38 +0000
committerJeremie Bresson2016-01-23 07:27:38 +0000
commitb15d692552891c96627cde1a8652ae9216b57e99 (patch)
tree5306a1042482b19b0aee3105dc14b01ad190fd53 /org.eclipse.text.tests
parentc87d70c7a9444b9248ad407b0753bc62a0695518 (diff)
downloadeclipse.platform.text-b15d692552891c96627cde1a8652ae9216b57e99.tar.gz
eclipse.platform.text-b15d692552891c96627cde1a8652ae9216b57e99.tar.xz
eclipse.platform.text-b15d692552891c96627cde1a8652ae9216b57e99.zip
Bug 75981: [templates][preferences] Allow to specify format of date variable in templates
https://bugs.eclipse.org/bugs/show_bug.cgi?id=75981 Change-Id: I8d1e2661d8698e47ea92963ba6caab0edaf8ab31 Signed-off-by: Dani Megert <Daniel_Megert@ch.ibm.com> Signed-off-by: Jeremie Bresson <jbr@bsiag.com> Signed-off-by: Pramod Goyal <pramod.goyal@gmail.com> Signed-off-by: Thomas Reinhardt <thomas@reinhardt.com>
Diffstat (limited to 'org.eclipse.text.tests')
-rw-r--r--org.eclipse.text.tests/META-INF/MANIFEST.MF2
-rw-r--r--org.eclipse.text.tests/src/org/eclipse/text/tests/templates/GlobalTemplateVariablesDateTest.java134
-rwxr-xr-xorg.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java4
3 files changed, 139 insertions, 1 deletions
diff --git a/org.eclipse.text.tests/META-INF/MANIFEST.MF b/org.eclipse.text.tests/META-INF/MANIFEST.MF
index 6c2c821847f..2df2f20f11d 100644
--- a/org.eclipse.text.tests/META-INF/MANIFEST.MF
+++ b/org.eclipse.text.tests/META-INF/MANIFEST.MF
@@ -16,3 +16,5 @@ Require-Bundle:
org.junit;bundle-version="3.8.2"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Eclipse-BundleShape: dir
+Import-Package: com.ibm.icu.text,
+ com.ibm.icu.util
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/GlobalTemplateVariablesDateTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/GlobalTemplateVariablesDateTest.java
new file mode 100644
index 00000000000..08222f20817
--- /dev/null
+++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/GlobalTemplateVariablesDateTest.java
@@ -0,0 +1,134 @@
+/*******************************************************************************
+ * Copyright (c) 2016 BSI Business Systems Integration AG.
+ * 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:
+ * BSI Business Systems Integration AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.text.tests.templates;
+
+import com.ibm.icu.text.DateFormat;
+import com.ibm.icu.text.SimpleDateFormat;
+import com.ibm.icu.util.ULocale;
+
+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;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class GlobalTemplateVariablesDateTest extends TestCase {
+ public static Test suite() {
+ return new TestSuite(GlobalTemplateVariablesDateTest.class);
+ }
+
+ private TemplateTranslator fTranslator;
+
+ private DocumentTemplateContext fContext;
+
+ private TemplateContextType fType;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ fTranslator= new TemplateTranslator();
+
+ fType= new TemplateContextType();
+ fType.addResolver(new GlobalTemplateVariables.Date());
+
+ fContext= new DocumentTemplateContext(fType, new Document(), 0, 0);
+ }
+
+ public void testWithoutParameter() throws Exception {
+ TemplateBuffer buffer= fTranslator.translate("Today is ${date}!");
+ fType.resolve(buffer, fContext);
+
+ StringBuffer expected= new StringBuffer();
+ expected.append("Today is ");
+ expected.append(DateFormat.getDateInstance().format(new java.util.Date()));
+ expected.append("!");
+ assertBufferStringAndVariables(expected.toString(), buffer);
+ }
+
+ public void testOneParameter() throws Exception {
+ TemplateBuffer buffer= fTranslator.translate("This format ${d:date('dd MMM YYYY')} and not ${p:date('YYYY-MM-dd')}");
+ fType.resolve(buffer, fContext);
+
+ StringBuffer expected= new StringBuffer();
+ expected.append("This format ");
+ expected.append(new SimpleDateFormat("dd MMM YYYY").format(new java.util.Date()));
+ expected.append(" and not ");
+ expected.append(new SimpleDateFormat("YYYY-MM-dd").format(new java.util.Date()));
+ assertBufferStringAndVariables(expected.toString(), buffer);
+ }
+
+ public void testSimpleLocale() throws Exception {
+ TemplateBuffer buffer= fTranslator.translate("From ${d:date('dd MMM YYYY', 'fr')} to ${d}");
+ fType.resolve(buffer, fContext);
+
+ StringBuffer expected= new StringBuffer();
+ expected.append("From ");
+ expected.append(new SimpleDateFormat("dd MMM YYYY", ULocale.FRENCH).format(new java.util.Date()));
+ expected.append(" to ");
+ expected.append(new SimpleDateFormat("dd MMM YYYY", ULocale.FRENCH).format(new java.util.Date()));
+ assertBufferStringAndVariables(expected.toString(), buffer);
+ }
+
+ public void testComplexLocale() throws Exception {
+ TemplateBuffer buffer= fTranslator.translate("France ${d:date('EEEE dd MMMM YYYY', 'fr_FR')} and Germany ${p:date('EEEE dd. MMMM YYYY', 'de_DE')}");
+ fType.resolve(buffer, fContext);
+
+ StringBuffer expected= new StringBuffer();
+ expected.append("France ");
+ expected.append(new SimpleDateFormat("EEEE dd MMMM YYYY", ULocale.FRANCE).format(new java.util.Date()));
+ expected.append(" and Germany ");
+ expected.append(new SimpleDateFormat("EEEE dd. MMMM YYYY", ULocale.GERMANY).format(new java.util.Date()));
+ assertBufferStringAndVariables(expected.toString(), buffer);
+ }
+
+ public void testInvalidDateFormat() throws Exception {
+ TemplateBuffer buffer= fTranslator.translate("Today is ${d:date('invalid')}!");
+ fType.resolve(buffer, fContext);
+
+ StringBuffer expected= new StringBuffer();
+ expected.append("Today is ");
+ expected.append(DateFormat.getDateInstance().format(new java.util.Date()));
+ expected.append("!");
+ assertBufferStringAndVariables(expected.toString(), buffer);
+ }
+
+ public void testInvalidLocale() throws Exception {
+ TemplateBuffer buffer= fTranslator.translate("Today is ${d:date('YYYY-MM-dd', 'this_invalid_locale')}!");
+ fType.resolve(buffer, fContext);
+
+ StringBuffer expected= new StringBuffer();
+ expected.append("Today is ");
+ expected.append(new SimpleDateFormat("YYYY-MM-dd", new ULocale("this_invalid_locale")).format(new java.util.Date()));
+ expected.append("!");
+ 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 b053e7dfdab..df495d5a4a1 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation 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
@@ -7,6 +7,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
*******************************************************************************/
package org.eclipse.text.tests.templates;
@@ -26,6 +27,7 @@ public class TemplatesTestSuite {
TestSuite suite = new TestSuite("Test Suite for org.eclipse.jface.text.templates"); //$NON-NLS-1$
//$JUnit-BEGIN$
suite.addTest(TemplateTranslatorTest.suite());
+ suite.addTest(GlobalTemplateVariablesDateTest.suite());
//$JUnit-END$
return suite;

Back to the top