summaryrefslogtreecommitdiff
path: root/tests/FunctionalTests/selenium/jsmock
diff options
context:
space:
mode:
authorwei <>2005-12-10 11:49:29 +0000
committerwei <>2005-12-10 11:49:29 +0000
commit98215a603fb798cdb4178e49061977544aaa45b7 (patch)
tree282787037961f7466acbd174ce151cac6de71273 /tests/FunctionalTests/selenium/jsmock
parent25f9c45261aafd65477ad526e0b6ad3dca80803a (diff)
Initial import selenium functional test framework.
Diffstat (limited to 'tests/FunctionalTests/selenium/jsmock')
-rw-r--r--tests/FunctionalTests/selenium/jsmock/mock-tests.html205
-rw-r--r--tests/FunctionalTests/selenium/jsmock/mock.js124
2 files changed, 329 insertions, 0 deletions
diff --git a/tests/FunctionalTests/selenium/jsmock/mock-tests.html b/tests/FunctionalTests/selenium/jsmock/mock-tests.html
new file mode 100644
index 00000000..f0cc6758
--- /dev/null
+++ b/tests/FunctionalTests/selenium/jsmock/mock-tests.html
@@ -0,0 +1,205 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+"http://www.w3.org/TR/html4/loose.dtd">
+
+<!--
+Copyright 2004 ThoughtWorks, 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.
+-->
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>JsMock Tests</title>
+ <link rel="stylesheet" type="text/css" href="/jsunit/css/jsUnitStyle.css">
+<script language="JavaScript" type="text/javascript" src="/jsunit/app/jsUnitCore.js"></script>
+<script language="JavaScript" type="text/javascript" src="/jsmock/mock.js"></script>
+<script language="JavaScript" type="text/javascript">
+
+function testCallingExpectedFunctionShouldPass() {
+ var myMock = new Mock()
+ myMock.expects("aslak")
+ myMock.aslak()
+ myMock.verify()
+}
+
+function testAccessingExpectedPropertyShouldPass() {
+ var myMock = new Mock()
+ myMock.expectsProperty("hello").returns("world")
+ assertEquals("world", myMock.hello)
+}
+
+function testAccessingExpectedPropertyWithObjectShouldPass() {
+ var myMock = new Mock()
+ ob = [1,2]
+ myMock.expectsProperty("hello").returns(ob)
+ assertEquals(ob, myMock.hello)
+}
+
+function testCallingUnexpectedFunctionShouldFail() {
+ var myMock = new Mock()
+ try {
+ myMock.someMethod()
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because someMethod wasn't expected!")
+}
+
+function testNotCallingExpectedFunctionShouldFail() {
+ var myMock = new Mock()
+ myMock.expects("someMethod")
+ try {
+ myMock.verify()
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because someMethod wasn't called!")
+}
+
+function testCallingExpectedFunctionWithBadArgumentsShouldFail() {
+ var myMock = new Mock()
+ myMock.expects("someMethod", "foo")
+ try {
+ myMock.someMethod("bar")
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because bar wasn't the expected arg!")
+}
+
+function testCallingExpectedFunctionWithExpectedArgumentsShouldPass() {
+ var myMock = new Mock()
+ myMock.expects("someMethod", "foo")
+ myMock.expects("anotherMethod", "bar", "zap")
+ assertUndefined(myMock.someMethod("foo"))
+ assertUndefined(myMock.anotherMethod("bar", "zap"))
+}
+
+function testCallingExpectedFunctionWithTooFewArgumentsShouldFail() {
+ var myMock = new Mock()
+ myMock.expects("someMethod", "foo", "bar")
+ try {
+ myMock.someMethod("foo")
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because too few arguments were passed!")
+}
+
+function testCallingExpectedFunctionWithTooManyArgumentsShouldFail() {
+ var myMock = new Mock()
+ myMock.expects("someMethod", "foo")
+ try {
+ myMock.someMethod("foo", "bar")
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because too many arguments were passed!")
+}
+
+function testShouldCreateMockInstancesWithoutSideffects() {
+ var foo = new Mock()
+ var bar = new Mock()
+
+ foo.expects("foo")
+ bar.expects("bar")
+
+ try {
+ bar.foo()
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because an unexpected bar was called!")
+}
+
+function testCallingExpectedFunctionWithReturnShouldReturnValue() {
+ var myMock = new Mock()
+ myMock.expects("someMethod", "bar").returns("foo")
+ myMock.expects("theOtherMethod", "zap", "ping", "pong").returns("bang")
+ assertEquals("foo", myMock.someMethod("bar"))
+ assertEquals("bang", myMock.theOtherMethod("zap", "ping", "pong"))
+ myMock.verify()
+}
+
+function testCallingExpectedFunctionWithThrowsShouldThrowError() {
+ var myMock = new Mock();
+ myMock.expects("someMethod", "bar").andThrows("failure")
+ try {
+ myMock.someMethod("bar")
+ } catch (e) {
+ assertEquals("failure", e.message);
+ return
+ }
+ fail("Mock did not throw exception when required");
+}
+
+function testSettingExpectedPropertyShouldPass() {
+ var myMock = new Mock()
+ myMock.expectsProperty("foo", "bar")
+ myMock.foo = "bar"
+ myMock.verify()
+}
+
+function TODO_testSettingUnexpectedPropertyShouldFail() {
+ var myMock = new Mock()
+ myMock.foo = "bar"
+
+ try {
+ myMock.verify()
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because an unexpected property was set!")
+}
+
+function TODO_testShouldAllowExpectationOfSameFunctionWithDifferentArguments() {
+ var myMock = new Mock()
+ myMock.expects("aslak", "hello").returns("world")
+ myMock.expects("aslak", "bonjour").returns("monde")
+ assertEquals("world", myMock.aslak("hello"))
+ assertEquals("monde", myMock.aslak("bonjour"))
+ myMock.verify()
+}
+
+function TODO_testNotSettingExpectedPropertyShouldFail() {
+ var myMock = new Mock()
+ myMock.expectsProperty("foo", "bar")
+ try {
+ myMock.verify()
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because an expected property was not set!")
+}
+
+function TODO_testSettingExpectedPropertyWithUnexpectedValueShouldFail() {
+ var myMock = new Mock()
+ myMock.expectsProperty("foo", "bar")
+ myMock.foo="zap"
+ try {
+ myMock.verify()
+ } catch(expected) {
+ return
+ }
+ fail("Should fail because an expected property was set with unexpected value!")
+}
+
+</script>
+ </head>
+
+ <body>
+ <h1>JsMock Tests</h1>
+
+ <p>This page contains tests for JsMock. To see them, take a look at the source. To run them, load this file via JsUnit's testRunner.html</p>
+ </body>
+</html>
diff --git a/tests/FunctionalTests/selenium/jsmock/mock.js b/tests/FunctionalTests/selenium/jsmock/mock.js
new file mode 100644
index 00000000..8c0b4b9f
--- /dev/null
+++ b/tests/FunctionalTests/selenium/jsmock/mock.js
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2004 ThoughtWorks, 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.
+ *
+ */
+
+// A simple mock library for Javascript
+//
+// Original code by Aslak Hellesoy and Ji Wang
+
+Mock = function() {
+ this.expectedInvocations = {};
+ this.expectedArgs = {};
+ this.returnValues = {};
+ this.attrs = [];
+ this.expectedProperties = {};
+
+ this.expects = function() {
+ functionName = arguments[0];
+ this.expectedArgs[functionName] = [];
+ for(i = 1; i < arguments.length; i++) {
+ this.expectedArgs[functionName][i-1] = arguments[i];
+ }
+ javascriptCode = "this." + functionName + " = function() {\n" +
+ " // mark this function as \"executed\"\n" +
+ " this.expectedInvocations[\"" + functionName + "\"] = true;\n" +
+ " assertEquals(\"" + functionName + ": Wrong number of arguments.\", " + this.expectedArgs[functionName].length + ", arguments.length);\n" +
+ " for(i = 0; i < arguments.length; i++) {\n" +
+ " assertEquals(this.expectedArgs[\"" + functionName + "\"][i], arguments[i]);\n" +
+ " };\n" +
+ " var returnValue = this.returnValues[\"" + functionName + "\"];\n" +
+ " if (returnValue && returnValue.isMockError) { throw returnValue };\n" +
+ " return returnValue;\n" +
+ "}";
+ eval(javascriptCode);
+ // initially mark this function as "not yet executed"
+ this.expectedInvocations[functionName] = false;
+ this.attrs[this.attrs.length] = "dummy";
+ return new Returner(this, functionName);
+ };
+
+ this.expectsProperty = function() {
+ var propertyName = arguments[0];
+ if(arguments.length == 2) {
+ expectedPropertyValue = arguments[1];
+ this.expectedProperties[propertyName] = expectedPropertyValue;
+ this.attrs[this.attrs.length] = "dummy";
+ } else {
+ return new PropertySetter(this, propertyName);
+ }
+ };
+
+ this.verify = function() {
+ // loop over all expected invocations and see if they were called
+ for(var functionName in this.expectedInvocations) {
+ var wasCalled = this.expectedInvocations[functionName];
+ if(!wasCalled) {
+ fail("Expected function not called:" + functionName);
+ }
+ }
+ var currentAttrs = [];
+ var currentAttrCount = 0;
+
+ // verify that all expected properties are set
+// for(var attr in this) {
+// currentAttrs[currentAttrCount] = attr;
+// currentAttrCount++;
+// }
+// if(this.attrs.length < currentAttrCount) {
+// unexpectedAttr = currentAttrs[this.attrs.length]
+// fail("Unexpected property was set: " + unexpectedAttr + "=" + eval("this." + unexpectedAttr))
+// }
+
+ // verify that all expected properties are set with the right value
+// for(var attr in this.expectedProperties) {
+// if(this.expectedProperties[attr] != eval("this." + attr)) {
+// fail("Expected property was not set: " + attr + "=" + this.expectedProperties[attr])
+// }
+// }
+ };
+
+ var attrCount = 0;
+ for(var attr in this) {
+ this.attrs[attrCount] = attr;
+ attrCount++;
+ }
+};
+
+Returner = function(mock, functionName) {
+ this.mock = mock;
+ this.functionName = functionName;
+};
+
+Returner.prototype.returns = function(returnValue) {
+ this.mock.returnValues[this.functionName] = returnValue;
+};
+
+Returner.prototype.andThrows = function(message) {
+ var error = new Error(message);
+ error.isMockError = true;
+ this.mock.returnValues[this.functionName] = error;
+};
+
+PropertySetter = function(mock, propertyName) {
+ this.mock = mock;
+ this.propertyName = propertyName;
+};
+
+PropertySetter.prototype.returns = function(returnValue) {
+ var ref = new Object();
+ ref.value = returnValue;
+ eval("this.mock." + this.propertyName + "=ref.value");
+};