Jason Heim has uploaded a new change for review.

  https://gwt-review.googlesource.com/3580


Change subject: Files added
......................................................................

Files added

Change-Id: Ic30bd738a9e931d1f207ee3a86eabe52cbee7161
---
A user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMaker.java
A user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMakerTest.java
2 files changed, 113 insertions(+), 0 deletions(-)



diff --git a/user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMaker.java b/user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMaker.java
new file mode 100644
index 0000000..84e7ee0
--- /dev/null
+++ b/user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMaker.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.junit;
+
+import com.google.gwt.safehtml.client.SafeHtmlTemplates;
+import com.google.gwt.safehtml.shared.SafeHtmlUtils;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+
+/**
+ * Helper to make a fake implementation of any {@link SafeHtmlTemplates}
+ * interface via reflection, for use in JUnit tests. (This will not work in
+ * GWTTestCase.) All calls to the returned object return the method name
+ * followed by the passed parameters as a list surrounded by [].
+ * <p>
+ * Sample use:
+ *
+ * <pre>interface MyTemplates extends SafeHtmlTemplates {
+ * &#64;Template("<span class=\"{3}\">{0}: <a href=\"{1}\">{2}</a></span>") + * SafeHtml messageWithLink(SafeHtml message, String url, String linkText,
+ *     String style);
+ * }
+ *
+ * public void testWithArgs() {
+ * MyTemplates templates = FakeSafeHtmlTemplatesMaker.create(MyTemplates.class);
+ *   SafeHtml message = SafeHtmlUtils.fromString("message");
+ *   assertEquals("messageWithLink[message, url, link, style]",
+ * templates.messageWithLink(message, "url", "link", "style").asString());
+ * }
+ * </pre>
+ */
+public class FakeSafeHtmlTemplatesMaker implements InvocationHandler {
+ public static <T extends SafeHtmlTemplates> T create(Class<T> templatesClass) {
+    return templatesClass.cast(Proxy.newProxyInstance(
+        FakeSafeHtmlTemplatesMaker.class.getClassLoader(),
+        new Class[] {templatesClass},
+        new FakeSafeHtmlTemplatesMaker()));
+  }
+
+  public Object invoke(Object proxy, Method method, Object[] args)
+      throws Throwable {
+    String name = method.getName();
+
+    String result = (args == null || args.length == 0) ? name : name
+        + Arrays.asList(args);
+    return SafeHtmlUtils.fromString(result);
+  }
+}
diff --git a/user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMakerTest.java b/user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMakerTest.java
new file mode 100644
index 0000000..8170a4d
--- /dev/null
+++ b/user/test/com/google/gwt/junit/FakeSafeHtmlTemplatesMakerTest.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.junit;
+
+import com.google.gwt.safehtml.client.SafeHtmlTemplates;
+import com.google.gwt.safehtml.shared.SafeHtml;
+import com.google.gwt.safehtml.shared.SafeHtmlUtils;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests of FakeSafeHtmlTemplatesMaker.
+ */
+public class FakeSafeHtmlTemplatesMakerTest extends TestCase {
+  interface MyTemplates extends SafeHtmlTemplates {
+
+    @Template("<span class=\"someclass\">Some message here</span>")
+    SafeHtml mySimpleTemplate();
+
+    @Template("<span class=\"{3}\">{0}: <a href=\"{1}\">{2}</a></span>")
+    SafeHtml messageWithLink(SafeHtml message, String url, String linkText,
+        String style);
+  }
+
+  public void testSimple() {
+ MyTemplates templates = FakeSafeHtmlTemplatesMaker.create(MyTemplates.class); + assertEquals("mySimpleTemplate", templates.mySimpleTemplate().asString());
+  }
+
+  public void testArgs() {
+ MyTemplates templates = FakeSafeHtmlTemplatesMaker.create(MyTemplates.class);
+    SafeHtml message = SafeHtmlUtils.fromString("message");
+    assertEquals("messageWithLink[message, url, linkText, style]",
+ templates.messageWithLink(message, "url", "link", "style").asString());
+  }
+}

--
To view, visit https://gwt-review.googlesource.com/3580
To unsubscribe, visit https://gwt-review.googlesource.com/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic30bd738a9e931d1f207ee3a86eabe52cbee7161
Gerrit-PatchSet: 1
Gerrit-Project: gwt
Gerrit-Branch: master
Gerrit-Owner: Jason Heim <[email protected]>

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
--- You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to