From: Manolo Carrasco Date: Tue, 12 Apr 2011 22:51:23 +0000 (+0000) Subject: tests for jsCache, permit null values and zero as key, tostring X-Git-Tag: release-1.3.2~411 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=55f5dbb606bf0343edb4b77f02ebb06953c4313b;p=gwtquery.git tests for jsCache, permit null values and zero as key, tostring --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java index 2422b8ab..fad4b1ca 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java @@ -27,11 +27,11 @@ public class JsCache extends JavaScriptObject { } public final native void delete(int name) /*-{ - delete this[name]; + delete this[name]; }-*/; public final native void delete(String name) /*-{ - delete this[name]; + delete this[name]; }-*/; public final native boolean exists(String name) /*-{ @@ -39,15 +39,15 @@ public class JsCache extends JavaScriptObject { }-*/; public final native boolean exists(int id) /*-{ - return !!this[id]; + return !!this[id]; }-*/; public final native T get(int id) /*-{ - return this[id] || null; + return this[id] || null; }-*/; public final native T get(String id) /*-{ - return this[id] || null; + return this[id] || null; }-*/; public final JsCache getCache(int id) { @@ -55,48 +55,47 @@ public class JsCache extends JavaScriptObject { } public final native double getDouble(int id) /*-{ - return this[id] || 0; + return this[id] || 0; }-*/; public final native double getDouble(String id) /*-{ - return this[id] || 0; + return this[id] || 0; }-*/; public final native int getInt(int id) /*-{ - return this[id] || 0; + return this[id] || 0; }-*/; public final native int getInt(String id) /*-{ - return this[id] || 0; + return this[id] || 0; }-*/; public final native String getString(int id) /*-{ - return this[id]; + return this[id]; }-*/; public final native String getString(String id) /*-{ - return this[id] || null; + return this[id] || null; }-*/; public final native boolean isEmpty() /*-{ - var foo = ""; - for (foo in this) - break; - return !foo; + var foo = ""; + for (foo in this) break; + return !foo; }-*/; public final native void put(int id, Object obj) /*-{ - if (obj && id >=0 ) this[id] = obj; + this[id] = obj; }-*/; public final native void put(String id, Object obj) /*-{ - if (obj && id) this[id] = obj; + this[id] = obj; }-*/; public final native int length() /*-{ - if (this.length) return this.lenght; - var ret = 0; - for (k in this) ret ++; + if (this.length) return this.length; + var key, ret = 0; + for (key in this) ret ++; return ret; }-*/; @@ -132,16 +131,17 @@ public class JsCache extends JavaScriptObject { return ret; } - public final native Object[] elemImpl() /*-{ - var key, ret=[]; - for(key in this) ret.push(this[key]); - return ret; - }-*/; - - - public final native JsArrayString keysImpl() /*-{ + public final String tostring() { + String ret = getClass().getName() + "{ "; + for (String k: keys()){ + ret += k + "=" + get(k) + " "; + } + return ret + "}"; + } + + private final native JsArrayString keysImpl() /*-{ var key, keys=[]; - for(key in this) keys.push("" + key); + for(key in this) keys.push(String(key)); return keys; }-*/; } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java new file mode 100644 index 00000000..748fb49f --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2011, The gwtquery team. + * + * 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.query.client; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.junit.client.GWTTestCase; +import com.google.gwt.query.client.js.JsCache; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.RootPanel; + +/** + * Test class for testing js classes. + */ +public class GQueryJsTest extends GWTTestCase { + + static Element e = null; + + static HTML testPanel = null; + + public String getModuleName() { + return "com.google.gwt.query.Query"; + } + + public void gwtSetUp() { + if (e == null) { + testPanel = new HTML(); + RootPanel.get().add(testPanel); + e = testPanel.getElement(); + e.setId("core-tst"); + } else { + e.setInnerHTML(""); + } + } + + public void testJsCache() { + String[] slist = new String[]{"A", "B", "C"}; + + JsCache c = JsCache.create(); + assertTrue(c.isEmpty()); + for (int i=0; i < slist.length; i++) { + c.put(i, slist[i]); + } + assertFalse(c.isEmpty()); + assertEquals(3, c.length()); + assertEquals(slist[1], c.get(1)); + for (int i=0; i < slist.length; i++) { + c.put(slist[i], slist[i]); + } + assertEquals(6, c.length()); + assertEquals(slist[1], c.get(1)); + assertEquals(slist[1], c.get(slist[1])); + c.put(1, null); + c.put("X", "X"); + assertNull(c.get(1)); + assertEquals(slist[2], c.get(2)); + assertEquals(7, c.length()); + assertEquals(7, c.keys().length); + assertEquals(7, c.elements().length); + + assertTrue(c.exists(2)); + assertFalse(c.exists(3)); + assertTrue(c.exists("X")); + assertFalse(c.exists("V")); + + c.delete(2); + c.delete("C"); + assertEquals(5, c.length()); + + c.put(-1, "N"); + assertEquals(6, c.length()); + assertEquals("N", c.get(-1)); + } +}