common.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. "use strict";
  2. var Method = require("../core")
  3. function type(value) {
  4. return Object.prototype.toString.call(value).
  5. split(" ").
  6. pop().
  7. split("]").
  8. shift().
  9. toLowerCase()
  10. }
  11. var values = [
  12. null, // 0
  13. undefined, // 1
  14. Infinity, // 2
  15. NaN, // 3
  16. 5, // 4
  17. {}, // 5
  18. Object.create({}), // 6
  19. Object.create(null), // 7
  20. [], // 8
  21. /foo/, // 9
  22. new Date(), // 10
  23. Function, // 11
  24. function() {}, // 12
  25. true, // 13
  26. false, // 14
  27. "string" // 15
  28. ]
  29. function True() { return true }
  30. function False() { return false }
  31. var trues = values.map(True)
  32. var falses = values.map(False)
  33. exports["test throws if not implemented"] = function(assert) {
  34. var method = Method("nope")
  35. assert.throws(function() {
  36. method({})
  37. }, /not implement/i, "method throws if not implemented")
  38. assert.throws(function() {
  39. method(null)
  40. }, /not implement/i, "method throws on null")
  41. }
  42. exports["test all types inherit from default"] = function(assert) {
  43. var isImplemented = Method("isImplemented")
  44. isImplemented.define(function() { return true })
  45. values.forEach(function(value) {
  46. assert.ok(isImplemented(value),
  47. type(value) + " inherits deafult implementation")
  48. })
  49. }
  50. exports["test default can be implemented later"] = function(assert) {
  51. var isImplemented = Method("isImplemented")
  52. isImplemented.define(function() {
  53. return true
  54. })
  55. values.forEach(function(value) {
  56. assert.ok(isImplemented(value),
  57. type(value) + " inherits deafult implementation")
  58. })
  59. }
  60. exports["test dispatch not-implemented"] = function(assert) {
  61. var isDefault = Method("isDefault")
  62. values.forEach(function(value) {
  63. assert.throws(function() {
  64. isDefault(value)
  65. }, /not implement/, type(value) + " throws if not implemented")
  66. })
  67. }
  68. exports["test dispatch default"] = function(assert) {
  69. var isDefault = Method("isDefault")
  70. // Implement default
  71. isDefault.define(True)
  72. assert.deepEqual(values.map(isDefault), trues,
  73. "all implementation inherit from default")
  74. }
  75. exports["test dispatch null"] = function(assert) {
  76. var isNull = Method("isNull")
  77. // Implement default
  78. isNull.define(False)
  79. isNull.define(null, True)
  80. assert.deepEqual(values.map(isNull),
  81. [ true ].
  82. concat(falses.slice(1)),
  83. "only null gets methods defined for null")
  84. }
  85. exports["test dispatch undefined"] = function(assert) {
  86. var isUndefined = Method("isUndefined")
  87. // Implement default
  88. isUndefined.define(False)
  89. isUndefined.define(undefined, True)
  90. assert.deepEqual(values.map(isUndefined),
  91. [ false, true ].
  92. concat(falses.slice(2)),
  93. "only undefined gets methods defined for undefined")
  94. }
  95. exports["test dispatch object"] = function(assert) {
  96. var isObject = Method("isObject")
  97. // Implement default
  98. isObject.define(False)
  99. isObject.define(Object, True)
  100. assert.deepEqual(values.map(isObject),
  101. [ false, false, false, false, false ].
  102. concat(trues.slice(5, 13)).
  103. concat([false, false, false]),
  104. "all values except primitives inherit Object methods")
  105. }
  106. exports["test dispatch number"] = function(assert) {
  107. var isNumber = Method("isNumber")
  108. isNumber.define(False)
  109. isNumber.define(Number, True)
  110. assert.deepEqual(values.map(isNumber),
  111. falses.slice(0, 2).
  112. concat(true, true, true).
  113. concat(falses.slice(5)),
  114. "all numbers inherit from Number method")
  115. }
  116. exports["test dispatch string"] = function(assert) {
  117. var isString = Method("isString")
  118. isString.define(False)
  119. isString.define(String, True)
  120. assert.deepEqual(values.map(isString),
  121. falses.slice(0, 15).
  122. concat(true),
  123. "all strings inherit from String method")
  124. }
  125. exports["test dispatch function"] = function(assert) {
  126. var isFunction = Method("isFunction")
  127. isFunction.define(False)
  128. isFunction.define(Function, True)
  129. assert.deepEqual(values.map(isFunction),
  130. falses.slice(0, 11).
  131. concat(true, true).
  132. concat(falses.slice(13)),
  133. "all functions inherit from Function method")
  134. }
  135. exports["test dispatch date"] = function(assert) {
  136. var isDate = Method("isDate")
  137. isDate.define(False)
  138. isDate.define(Date, True)
  139. assert.deepEqual(values.map(isDate),
  140. falses.slice(0, 10).
  141. concat(true).
  142. concat(falses.slice(11)),
  143. "all dates inherit from Date method")
  144. }
  145. exports["test dispatch RegExp"] = function(assert) {
  146. var isRegExp = Method("isRegExp")
  147. isRegExp.define(False)
  148. isRegExp.define(RegExp, True)
  149. assert.deepEqual(values.map(isRegExp),
  150. falses.slice(0, 9).
  151. concat(true).
  152. concat(falses.slice(10)),
  153. "all regexps inherit from RegExp method")
  154. }
  155. exports["test redefine for descendant"] = function(assert) {
  156. var isFoo = Method("isFoo")
  157. var ancestor = {}
  158. isFoo.implement(ancestor, function() { return true })
  159. var descendant = Object.create(ancestor)
  160. isFoo.implement(descendant, function() { return false })
  161. assert.ok(isFoo(ancestor), "defined on ancestor")
  162. assert.ok(!isFoo(descendant), "overrided for descendant")
  163. }
  164. exports["test on custom types"] = function(assert) {
  165. function Bar() {}
  166. var isBar = Method("isBar")
  167. isBar.define(function() { return false })
  168. isBar.define(Bar, function() { return true })
  169. assert.ok(!isBar({}), "object is get's default implementation")
  170. assert.ok(isBar(new Bar()), "Foo type objects get own implementation")
  171. var isObject = Method("isObject")
  172. isObject.define(function() { return false })
  173. isObject.define(Object, function() { return true })
  174. assert.ok(isObject(new Bar()), "foo inherits implementation from object")
  175. isObject.define(Bar, function() { return false })
  176. assert.ok(!isObject(new Bar()),
  177. "implementation inherited form object can be overrided")
  178. }
  179. exports["test error types"] = function(assert) {
  180. var isError = Method("isError")
  181. isError.define(function() { return false })
  182. isError.define(Error, function() { return true })
  183. assert.ok(isError(Error("boom")), "error is error")
  184. assert.ok(isError(TypeError("boom")), "type error is an error")
  185. assert.ok(isError(EvalError("boom")), "eval error is an error")
  186. assert.ok(isError(RangeError("boom")), "range error is an error")
  187. assert.ok(isError(ReferenceError("boom")), "reference error is an error")
  188. assert.ok(isError(SyntaxError("boom")), "syntax error is an error")
  189. assert.ok(isError(URIError("boom")), "URI error is an error")
  190. }
  191. exports["test override define polymorphic method"] = function(assert) {
  192. var define = Method.define
  193. var implement = Method.implement
  194. var fn = Method("fn")
  195. var methods = {}
  196. implement(define, fn, function(method, label, implementation) {
  197. methods[label] = implementation
  198. })
  199. function foo() {}
  200. define(fn, "foo-case", foo)
  201. assert.equal(methods["foo-case"], foo, "define set property")
  202. }
  203. exports["test override define via method API"] = function(assert) {
  204. var define = Method.define
  205. var implement = Method.implement
  206. var fn = Method("fn")
  207. var methods = {}
  208. define.implement(fn, function(method, label, implementation) {
  209. methods[label] = implementation
  210. })
  211. function foo() {}
  212. define(fn, "foo-case", foo)
  213. assert.equal(methods["foo-case"], foo, "define set property")
  214. }
  215. require("test").run(exports)