descriptor-tests.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. var Trait = require("sdk/deprecated/light-traits").Trait;
  6. var utils = require("./utils");
  7. var Data = utils.Data;
  8. var Method = utils.Method;
  9. var Accessor = utils.Accessor;
  10. var Required = utils.Required;
  11. var Conflict = utils.Conflict;
  12. function method() {}
  13. exports.Assert = require("./assert").Assert
  14. exports["test simple composition"] = function(assert) {
  15. var actual = Trait.compose(
  16. Trait({ a: 0, b: 1 }),
  17. { c: { value: 2 }, d: { value: method, enumerable: true } }
  18. );
  19. var expected = {
  20. a: Data(0),
  21. b: Data(1),
  22. c: Data(2, false, false, false),
  23. d: Method(method, true, false, false)
  24. };
  25. assert.equalTraits(actual, expected);
  26. };
  27. exports["test composition with conflict"] = function(assert) {
  28. var actual = Trait.compose(
  29. Trait({ a: 0, b: 1 }),
  30. {
  31. a: {
  32. value: 2,
  33. writable: true,
  34. configurable: true,
  35. enumerable: true
  36. },
  37. c: {
  38. value: method,
  39. configurable: true
  40. }
  41. }
  42. );
  43. var expected = {
  44. a: Conflict("a"),
  45. b: Data(1),
  46. c: Method(method, false, true, false)
  47. };
  48. assert.equalTraits(actual, expected);
  49. };
  50. exports["test identical props does not cause conflict"] = function(assert) {
  51. var actual = Trait.compose(
  52. {
  53. a: {
  54. value: 0,
  55. writable: true,
  56. configurable: true,
  57. enumerable: true
  58. },
  59. b: {
  60. value: 1
  61. }
  62. },
  63. Trait({
  64. a: 0,
  65. c: method
  66. })
  67. );
  68. var expected = {
  69. a: Data(0),
  70. b: Data(1, false, false, false),
  71. c: Method(method)
  72. }
  73. assert.equalTraits(actual, expected);
  74. };
  75. exports["test composition with identical required props"] = function(assert) {
  76. var actual = Trait.compose(
  77. Trait({ a: Trait.required, b: 1 }),
  78. { a: { required: true }, c: { value: method } }
  79. );
  80. var expected = {
  81. a: Required(),
  82. b: Data(1),
  83. c: Method(method, false, false, false)
  84. };
  85. assert.equalTraits(actual, expected);
  86. };
  87. exports["test composition satisfying a required prop"] = function(assert) {
  88. var actual = Trait.compose(
  89. Trait({ a: Trait.required, b: 1 }),
  90. { a: { value: method, enumerable: true } }
  91. );
  92. var expected = {
  93. a: Method(method, true, false, false),
  94. b: Data(1)
  95. };
  96. assert.equalTraits(actual, expected);
  97. };
  98. exports["test compose is neutral wrt conflicts"] = function(assert) {
  99. var actual = Trait.compose(
  100. Trait({ a: { value: 1 } }, Trait({ a: 2 })),
  101. { b: { value: 0, writable: true, configurable: true, enumerable: false } }
  102. );
  103. var expected = { a: Conflict("a"), b: Data(0, false) };
  104. assert.equalTraits(actual, expected);
  105. };
  106. exports["test conflicting prop overrides Trait.required"] = function(assert) {
  107. var actual = Trait.compose(
  108. Trait.compose(
  109. Trait({ a: 1 }),
  110. { a: { value: 2 } }
  111. ),
  112. { a: { value: Trait.required } }
  113. );
  114. var expected = { a: Conflict("a") };
  115. assert.equalTraits(actual, expected);
  116. };
  117. exports["test compose is commutative"] = function(assert) {
  118. var actual = Trait.compose(
  119. Trait({ a: 0, b: 1 }),
  120. { c: { value: 2 }, d: { value: method } }
  121. );
  122. var expected = Trait.compose(
  123. { c: { value: 2 }, d: { value: method } },
  124. Trait({ a: 0, b: 1 })
  125. );
  126. assert.equalTraits(actual, expected);
  127. }
  128. exports["test compose is commutative, also for required/conflicting props"] = function(assert) {
  129. var actual = Trait.compose(
  130. {
  131. a: { value: 0 },
  132. b: { value: 1 },
  133. c: { value: 3 },
  134. e: { value: Trait.required }
  135. },
  136. {
  137. c: { value: 2 },
  138. d: { get: method }
  139. }
  140. );
  141. var expected = Trait.compose(
  142. Trait({ c: 3 }),
  143. {
  144. c: { value: 2 },
  145. d: { get: method },
  146. a: { value: 0 },
  147. b: { value: 1 },
  148. e: { value: Trait.required },
  149. }
  150. );
  151. assert.equalTraits(actual, expected);
  152. };
  153. exports["test compose is associative"] = function(assert) {
  154. var actual = Trait.compose(
  155. {
  156. a: { value: 0 },
  157. b: { value: 1 },
  158. c: { value: 3 },
  159. d: { value: Trait.required }
  160. },
  161. Trait.compose(
  162. { c: { value: 3 }, d: { value: Trait.required } },
  163. { c: { value: 2 }, d: { value: method }, e: { value: "foo" } }
  164. )
  165. );
  166. var expected = Trait.compose(
  167. Trait.compose(
  168. {
  169. a: { value: 0 },
  170. b: { value: 1 },
  171. c: { value: 3 },
  172. d: { value: Trait.required }
  173. },
  174. {
  175. c: { value: 3 },
  176. d: { value: Trait.required }
  177. }
  178. ),
  179. {
  180. c: { value: 2 },
  181. d: { value: method },
  182. e: { value: "foo" }
  183. }
  184. );
  185. assert.equalTraits(actual, expected);
  186. };
  187. exports["test diamond import of same prop do not conflict"] = function(assert) {
  188. var actual = Trait.compose(
  189. Trait.compose(
  190. { b: { value: 2 } },
  191. { a: { value: 1, enumerable: true, configurable: true, writable: true } }
  192. ),
  193. Trait.compose(
  194. { c: { value: 3 } },
  195. Trait({ a: 1 })
  196. ),
  197. Trait({ d: 4 })
  198. );
  199. var expected = {
  200. a: Data(1),
  201. b: Data(2, false, false, false),
  202. c: Data(3, false, false, false),
  203. d: Data(4)
  204. };
  205. assert.equalTraits(actual, expected);
  206. };
  207. exports["test create simple"] = function(assert) {
  208. var o1 = Trait.compose(
  209. Trait({ a: 1 }),
  210. {
  211. b: {
  212. value: function() {
  213. return this.a;
  214. }
  215. }
  216. }
  217. ).create(Object.prototype);
  218. assert.equal(Object.getPrototypeOf(o1), Object.prototype, "o1 prototype");
  219. assert.equal(1, o1.a, "o1.a");
  220. assert.equal(1, o1.b(), "o1.b()");
  221. assert.equal(Object.keys(o1).length, 1, "Object.keys(o1).length === 2");
  222. };
  223. exports["test create with Array.prototype"] = function(assert) {
  224. var o2 = Trait.compose({}, {}).create(Array.prototype);
  225. assert.equal(Object.getPrototypeOf(o2), Array.prototype, "o2 prototype");
  226. };
  227. exports["test exception for incomplete required properties"] = function(assert) {
  228. assert.throws(function() {
  229. Trait({ foo: Trait.required }).create(Object.prototype)
  230. }, /Missing required property: `foo`/, "required prop error");
  231. }
  232. exports["test exception for unresolved conflicts"] = function(assert) {
  233. assert.throws(function() {
  234. Trait(Trait({ a: 0 }), Trait({ a: 1 })).create({})
  235. }, /Remaining conflicting property: `a`/, "conflicting prop error");
  236. }
  237. exports["test conflicting properties are present"] = function(assert) {
  238. var o5 = Object.create(Object.prototype, Trait.compose(
  239. { a: { value: 0 } },
  240. { a: { value: 1 } }
  241. ));
  242. assert.ok("a" in o5, "conflicting property present");
  243. assert.throws(function() {
  244. o5.a
  245. }, /Remaining conflicting property: `a`/, "conflicting prop access error");
  246. };
  247. exports["test diamond with conflicts"] = function(assert) {
  248. function makeT1(x) {
  249. return {
  250. m: {
  251. value: function() {
  252. return x
  253. }
  254. }
  255. };
  256. };
  257. function makeT2(x) {
  258. return Trait.compose(
  259. Trait({ t2: "foo" }),
  260. makeT1(x)
  261. );
  262. };
  263. function makeT3(x) {
  264. return Trait.compose(
  265. {
  266. t3: { value: "bar" }
  267. },
  268. makeT1(x)
  269. );
  270. };
  271. var T4 = Trait.compose(makeT2(5), makeT3(5));
  272. assert.throws(function() {
  273. T4.create(Object.prototype);
  274. }, /Remaining conflicting property: `m`/, "diamond prop conflict");
  275. };
  276. exports["test providing requirements through proto"] = function(assert) {
  277. var t = Trait.compose(
  278. {},
  279. { required: { required: true } }
  280. ).create({ required: "test" });
  281. assert.equal(t.required, "test", "property from proto is inherited");
  282. };
  283. if (module == require.main)
  284. require("test").run(exports);