addon-sdk/source/lib/method/test/common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/method/test/common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,272 @@
     1.4 +"use strict";
     1.5 +
     1.6 +var Method = require("../core")
     1.7 +
     1.8 +function type(value) {
     1.9 +  return Object.prototype.toString.call(value).
    1.10 +    split(" ").
    1.11 +    pop().
    1.12 +    split("]").
    1.13 +    shift().
    1.14 +    toLowerCase()
    1.15 +}
    1.16 +
    1.17 +var values = [
    1.18 +  null,                   // 0
    1.19 +  undefined,              // 1
    1.20 +  Infinity,               // 2
    1.21 +  NaN,                    // 3
    1.22 +  5,                      // 4
    1.23 +  {},                     // 5
    1.24 +  Object.create({}),      // 6
    1.25 +  Object.create(null),    // 7
    1.26 +  [],                     // 8
    1.27 +  /foo/,                  // 9
    1.28 +  new Date(),             // 10
    1.29 +  Function,               // 11
    1.30 +  function() {},          // 12
    1.31 +  true,                   // 13
    1.32 +  false,                  // 14
    1.33 +  "string"                // 15
    1.34 +]
    1.35 +
    1.36 +function True() { return true }
    1.37 +function False() { return false }
    1.38 +
    1.39 +var trues = values.map(True)
    1.40 +var falses = values.map(False)
    1.41 +
    1.42 +exports["test throws if not implemented"] = function(assert) {
    1.43 +  var method = Method("nope")
    1.44 +
    1.45 +  assert.throws(function() {
    1.46 +    method({})
    1.47 +  }, /not implement/i, "method throws if not implemented")
    1.48 +
    1.49 +  assert.throws(function() {
    1.50 +    method(null)
    1.51 +  }, /not implement/i, "method throws on null")
    1.52 +}
    1.53 +
    1.54 +exports["test all types inherit from default"] = function(assert) {
    1.55 +  var isImplemented = Method("isImplemented")
    1.56 +  isImplemented.define(function() { return true })
    1.57 +
    1.58 +  values.forEach(function(value) {
    1.59 +    assert.ok(isImplemented(value),
    1.60 +              type(value) + " inherits deafult implementation")
    1.61 +  })
    1.62 +}
    1.63 +
    1.64 +exports["test default can be implemented later"] = function(assert) {
    1.65 +  var isImplemented = Method("isImplemented")
    1.66 +  isImplemented.define(function() {
    1.67 +    return true
    1.68 +  })
    1.69 +
    1.70 +  values.forEach(function(value) {
    1.71 +    assert.ok(isImplemented(value),
    1.72 +              type(value) + " inherits deafult implementation")
    1.73 +  })
    1.74 +}
    1.75 +
    1.76 +exports["test dispatch not-implemented"] = function(assert) {
    1.77 +  var isDefault = Method("isDefault")
    1.78 +  values.forEach(function(value) {
    1.79 +    assert.throws(function() {
    1.80 +      isDefault(value)
    1.81 +    }, /not implement/, type(value) + " throws if not implemented")
    1.82 +  })
    1.83 +}
    1.84 +
    1.85 +exports["test dispatch default"] = function(assert) {
    1.86 +  var isDefault = Method("isDefault")
    1.87 +
    1.88 +  // Implement default
    1.89 +  isDefault.define(True)
    1.90 +  assert.deepEqual(values.map(isDefault), trues,
    1.91 +                   "all implementation inherit from default")
    1.92 +
    1.93 +}
    1.94 +
    1.95 +exports["test dispatch null"] = function(assert) {
    1.96 +  var isNull = Method("isNull")
    1.97 +
    1.98 +  // Implement default
    1.99 +  isNull.define(False)
   1.100 +  isNull.define(null, True)
   1.101 +  assert.deepEqual(values.map(isNull),
   1.102 +                   [ true ].
   1.103 +                   concat(falses.slice(1)),
   1.104 +                   "only null gets methods defined for null")
   1.105 +}
   1.106 +
   1.107 +exports["test dispatch undefined"] = function(assert) {
   1.108 +  var isUndefined = Method("isUndefined")
   1.109 +
   1.110 +  // Implement default
   1.111 +  isUndefined.define(False)
   1.112 +  isUndefined.define(undefined, True)
   1.113 +  assert.deepEqual(values.map(isUndefined),
   1.114 +                   [ false, true ].
   1.115 +                   concat(falses.slice(2)),
   1.116 +                   "only undefined gets methods defined for undefined")
   1.117 +}
   1.118 +
   1.119 +exports["test dispatch object"] = function(assert) {
   1.120 +  var isObject = Method("isObject")
   1.121 +
   1.122 +  // Implement default
   1.123 +  isObject.define(False)
   1.124 +  isObject.define(Object, True)
   1.125 +  assert.deepEqual(values.map(isObject),
   1.126 +                   [ false, false, false, false, false ].
   1.127 +                   concat(trues.slice(5, 13)).
   1.128 +                   concat([false, false, false]),
   1.129 +                   "all values except primitives inherit Object methods")
   1.130 +
   1.131 +}
   1.132 +
   1.133 +exports["test dispatch number"] = function(assert) {
   1.134 +  var isNumber = Method("isNumber")
   1.135 +  isNumber.define(False)
   1.136 +  isNumber.define(Number, True)
   1.137 +
   1.138 +  assert.deepEqual(values.map(isNumber),
   1.139 +                  falses.slice(0, 2).
   1.140 +                  concat(true, true, true).
   1.141 +                  concat(falses.slice(5)),
   1.142 +                  "all numbers inherit from Number method")
   1.143 +}
   1.144 +
   1.145 +exports["test dispatch string"] = function(assert) {
   1.146 +  var isString = Method("isString")
   1.147 +  isString.define(False)
   1.148 +  isString.define(String, True)
   1.149 +
   1.150 +  assert.deepEqual(values.map(isString),
   1.151 +                  falses.slice(0, 15).
   1.152 +                  concat(true),
   1.153 +                  "all strings inherit from String method")
   1.154 +}
   1.155 +
   1.156 +exports["test dispatch function"] = function(assert) {
   1.157 +  var isFunction = Method("isFunction")
   1.158 +  isFunction.define(False)
   1.159 +  isFunction.define(Function, True)
   1.160 +
   1.161 +  assert.deepEqual(values.map(isFunction),
   1.162 +                  falses.slice(0, 11).
   1.163 +                  concat(true, true).
   1.164 +                  concat(falses.slice(13)),
   1.165 +                  "all functions inherit from Function method")
   1.166 +}
   1.167 +
   1.168 +exports["test dispatch date"] = function(assert) {
   1.169 +  var isDate = Method("isDate")
   1.170 +  isDate.define(False)
   1.171 +  isDate.define(Date, True)
   1.172 +
   1.173 +  assert.deepEqual(values.map(isDate),
   1.174 +                  falses.slice(0, 10).
   1.175 +                  concat(true).
   1.176 +                  concat(falses.slice(11)),
   1.177 +                  "all dates inherit from Date method")
   1.178 +}
   1.179 +
   1.180 +exports["test dispatch RegExp"] = function(assert) {
   1.181 +  var isRegExp = Method("isRegExp")
   1.182 +  isRegExp.define(False)
   1.183 +  isRegExp.define(RegExp, True)
   1.184 +
   1.185 +  assert.deepEqual(values.map(isRegExp),
   1.186 +                  falses.slice(0, 9).
   1.187 +                  concat(true).
   1.188 +                  concat(falses.slice(10)),
   1.189 +                  "all regexps inherit from RegExp method")
   1.190 +}
   1.191 +
   1.192 +exports["test redefine for descendant"] = function(assert) {
   1.193 +  var isFoo = Method("isFoo")
   1.194 +  var ancestor = {}
   1.195 +  isFoo.implement(ancestor, function() { return true })
   1.196 +  var descendant = Object.create(ancestor)
   1.197 +  isFoo.implement(descendant, function() { return false })
   1.198 +
   1.199 +  assert.ok(isFoo(ancestor), "defined on ancestor")
   1.200 +  assert.ok(!isFoo(descendant), "overrided for descendant")
   1.201 +}
   1.202 +
   1.203 +exports["test on custom types"] = function(assert) {
   1.204 +  function Bar() {}
   1.205 +  var isBar = Method("isBar")
   1.206 +
   1.207 +  isBar.define(function() { return false })
   1.208 +  isBar.define(Bar, function() { return true })
   1.209 +
   1.210 +  assert.ok(!isBar({}), "object is get's default implementation")
   1.211 +  assert.ok(isBar(new Bar()), "Foo type objects get own implementation")
   1.212 +
   1.213 +  var isObject = Method("isObject")
   1.214 +  isObject.define(function() { return false })
   1.215 +  isObject.define(Object, function() { return true })
   1.216 +
   1.217 +  assert.ok(isObject(new Bar()), "foo inherits implementation from object")
   1.218 +
   1.219 +
   1.220 +  isObject.define(Bar, function() { return false })
   1.221 +
   1.222 +  assert.ok(!isObject(new Bar()),
   1.223 +            "implementation inherited form object can be overrided")
   1.224 +}
   1.225 +
   1.226 +
   1.227 +exports["test error types"] = function(assert) {
   1.228 +  var isError = Method("isError")
   1.229 +  isError.define(function() { return false })
   1.230 +  isError.define(Error, function() { return true })
   1.231 +
   1.232 +  assert.ok(isError(Error("boom")), "error is error")
   1.233 +  assert.ok(isError(TypeError("boom")), "type error is an error")
   1.234 +  assert.ok(isError(EvalError("boom")), "eval error is an error")
   1.235 +  assert.ok(isError(RangeError("boom")), "range error is an error")
   1.236 +  assert.ok(isError(ReferenceError("boom")), "reference error is an error")
   1.237 +  assert.ok(isError(SyntaxError("boom")), "syntax error is an error")
   1.238 +  assert.ok(isError(URIError("boom")), "URI error is an error")
   1.239 +}
   1.240 +
   1.241 +exports["test override define polymorphic method"] = function(assert) {
   1.242 +  var define = Method.define
   1.243 +  var implement = Method.implement
   1.244 +
   1.245 +  var fn = Method("fn")
   1.246 +  var methods = {}
   1.247 +  implement(define, fn, function(method, label, implementation) {
   1.248 +    methods[label] = implementation
   1.249 +  })
   1.250 +
   1.251 +  function foo() {}
   1.252 +
   1.253 +  define(fn, "foo-case", foo)
   1.254 +
   1.255 +  assert.equal(methods["foo-case"], foo, "define set property")
   1.256 +}
   1.257 +
   1.258 +exports["test override define via method API"] = function(assert) {
   1.259 +  var define = Method.define
   1.260 +  var implement = Method.implement
   1.261 +
   1.262 +  var fn = Method("fn")
   1.263 +  var methods = {}
   1.264 +  define.implement(fn, function(method, label, implementation) {
   1.265 +    methods[label] = implementation
   1.266 +  })
   1.267 +
   1.268 +  function foo() {}
   1.269 +
   1.270 +  define(fn, "foo-case", foo)
   1.271 +
   1.272 +  assert.equal(methods["foo-case"], foo, "define set property")
   1.273 +}
   1.274 +
   1.275 +require("test").run(exports)

mercurial