Created
February 29, 2012 04:07
-
-
Save s-hiroshi/1937587 to your computer and use it in GitHub Desktop.
JavaScript > tddjs > for my study of tddjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) Copyright (c) 2010-2011, Christian Johansen | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | |
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | |
Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/** | |
* observe 観察者 | |
* observable 観察対象者 観察者に通知する | |
* @function observe | |
* @function hasObserver | |
* @function notify | |
* @param event | |
* _observers ヘルパー | |
*/ | |
/* | |
* name space | |
*/ | |
var ns = {}; | |
/* Observer, Observable */ | |
(function() { | |
// ヘルパー | |
function _observers(observable, event) { | |
if (!observable.observers) { | |
// not observers set object | |
observable.observers = {}; | |
} | |
if (!observable.observers[event]) { | |
observable.observers[event] = []; | |
} | |
return observable.observers[event]; | |
} | |
function observe(event, observer) { | |
if (typeof observer != "function") { | |
throw new TypeError("observer is not function"); | |
} | |
// イベントの観察者を設定 | |
_observers(this, event).push(observer); | |
} | |
function hasObserver(event, observer) { | |
var observers = _observers(this, event); | |
for (var i = 0, l = observers.length; i < l; i++) { | |
if (observers[i] == observer) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function notify(event) { | |
var observers = _observers(this, event); | |
var args = Array.prototype.slice.call(arguments, 1); | |
for (var i = 0, l = observers.length; i < l; i++) { | |
try { | |
observers[i].apply(this, args); | |
} catch (e) {} | |
} | |
} | |
ns.observable = { | |
observe: observe, | |
hasObserver: hasObserver, | |
notify: notify | |
}; | |
}()); | |
/* QUnit */ | |
module("observer pattern 1", { | |
setup: function() { // 初期化処理 | |
this.observable = Object.create(ns.observable); | |
}, | |
teardown: function() { | |
// 終了処理 | |
} | |
}); | |
test("test sequence observer add, notify", function() { | |
var observer1 = function() { | |
observer1.called = true; | |
}; | |
var observer2 = function() { | |
observer2.called = true; | |
}; | |
var observer3 = function() { | |
observer3.called = true; | |
} | |
// eventイベントに観察者を設定 | |
this.observable.observe("event", observer1); | |
this.observable.observe("event", observer2); | |
equal(observer1, this.observable.observers["event"][0], 'add observer1'); | |
equal(observer2, this.observable.observers["event"][1], 'add observer2'); | |
// 通知を受けていない状態 | |
equal(undefined, observer1.called, 'observer1 is not notify') | |
equal(undefined, observer2.called, 'observer2 is not notify'); | |
// 通知 | |
this.observable.notify("event"); | |
// 通知を受けた状態へ遷移 | |
ok(observer1.called, 'observer1 is notify'); | |
ok(observer2.called, 'observer2 is notify'); | |
}); | |
module("observer pattern 2", { | |
setup: function() { // 初期化処理 | |
this.observable = Object.create(ns.observable); | |
}, | |
teardown: function() { | |
// 終了処理 | |
} | |
}); | |
test("test should pass through arguments", function() { | |
var actual; | |
this.observable.observe("event", function() { | |
actual = arguments; | |
}); | |
this.observable.notify("event", "String", 1, 32); | |
notDeepEqual(["String", 1, 32], actual, 'notDeepEqual is ok but operator == , === is false both.'); | |
// ==, ===ともにfalse, equalもfalse | |
}); | |
module("observer pattern 3", { | |
setup: function() { // 初期化処理 | |
this.observable = Object.create(ns.observable); | |
}, | |
teardown: function() { | |
// 終了処理 | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1 id="qunit-header">QUnit example</h1> | |
<h2 id="qunit-banner"></h2> | |
<div id="qunit-testrunner-toolbar"></div> | |
<h2 id="qunit-userAgent"></h2> | |
<ol id="qunit-tests"></ol> | |
<div id="qunit-fixture">test markup, will be hidden</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment