test-traits.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. const { Trait } = require('sdk/deprecated/traits');
  6. exports['test:simple compose'] = function(assert) {
  7. let List = Trait.compose({
  8. _list: null,
  9. constructor: function List() {
  10. this._list = [];
  11. },
  12. list: function list() this._list.slice(0),
  13. add: function add(item) this._list.push(item),
  14. remove: function remove(item) {
  15. let list = this._list;
  16. let index = list.indexOf(item);
  17. if (0 <= index) list.slice(index, 1);
  18. }
  19. });
  20. assert.notEqual(undefined, List, 'should not be undefined');
  21. assert.equal('function', typeof List, 'type should be function');
  22. assert.equal(
  23. Trait.compose,
  24. List.compose,
  25. 'should inherit static compose'
  26. );
  27. assert.equal(
  28. Trait.override,
  29. List.override,
  30. 'should inherit static override'
  31. );
  32. assert.equal(
  33. Trait.required,
  34. List.required,
  35. 'should inherit static required'
  36. );
  37. assert.equal(
  38. Trait.resolve,
  39. List.resolve,
  40. 'should inherit static resolve'
  41. );
  42. assert.ok(
  43. !('_list' in List.prototype),
  44. 'should not expose private API'
  45. );
  46. }
  47. exports['test: compose trait instance and create instance'] = function(assert) {
  48. let List = Trait.compose({
  49. constructor: function List(options) {
  50. this._list = [];
  51. this._public.publicMember = options.publicMember;
  52. },
  53. _privateMember: true,
  54. get privateMember() this._privateMember,
  55. get list() this._list.slice(0),
  56. add: function add(item) this._list.push(item),
  57. remove: function remove(item) {
  58. let list = this._list
  59. let index = list.indexOf(item)
  60. if (0 <= index) list.slice(index, 1)
  61. }
  62. });
  63. let list = List({ publicMember: true });
  64. assert.equal('object', typeof list, 'should return an object')
  65. assert.equal(
  66. true,
  67. list instanceof List,
  68. 'should be instance of a List'
  69. );
  70. assert.equal(
  71. undefined,
  72. list._privateMember,
  73. 'instance should not expose private API'
  74. );
  75. assert.equal(
  76. true,
  77. list.privateMember,
  78. 'privates are accessible by public API'
  79. );
  80. list._privateMember = false;
  81. assert.equal(
  82. true,
  83. list.privateMember,
  84. 'property changes on instance must not affect privates'
  85. );
  86. assert.ok(
  87. !('_list' in list),
  88. 'instance should not expose private members'
  89. );
  90. assert.equal(
  91. true,
  92. list.publicMember,
  93. 'public members are exposed'
  94. )
  95. assert.equal(
  96. 'function',
  97. typeof list.add,
  98. 'should be function'
  99. )
  100. assert.equal(
  101. 'function',
  102. typeof list.remove,
  103. 'should be function'
  104. );
  105. list.add(1);
  106. assert.equal(
  107. 1,
  108. list.list[0],
  109. 'exposed public API should be able of modifying privates'
  110. )
  111. };
  112. exports['test:instances must not be hackable'] = function(assert) {
  113. let SECRET = 'There is no secret!',
  114. secret = null;
  115. let Class = Trait.compose({
  116. _secret: null,
  117. protect: function(data) this._secret = data
  118. });
  119. let i1 = Class();
  120. i1.protect(SECRET);
  121. assert.equal(
  122. undefined,
  123. (function() this._secret).call(i1),
  124. 'call / apply can\'t access private state'
  125. );
  126. let proto = Object.getPrototypeOf(i1);
  127. try {
  128. proto.reveal = function() this._secret;
  129. secret = i1.reveal();
  130. } catch(e) {}
  131. assert.notEqual(
  132. SECRET,
  133. secret,
  134. 'public __proto__ changes should not affect privates'
  135. );
  136. secret = null;
  137. let Class2 = Trait.compose({
  138. _secret: null,
  139. protect: function(data) this._secret = data
  140. });
  141. let i2 = Class2();
  142. i2.protect(SECRET);
  143. try {
  144. Object.prototype.reveal = function() this._secret;
  145. secret = i2.reveal();
  146. } catch(e) {}
  147. assert.notEqual(
  148. SECRET,
  149. secret,
  150. 'Object.prototype changes must not affect instances'
  151. );
  152. }
  153. exports['test:instanceof'] = function(assert) {
  154. const List = Trait.compose({
  155. // private API:
  156. _list: null,
  157. // public API
  158. constructor: function List() {
  159. this._list = []
  160. },
  161. get length() this._list.length,
  162. add: function add(item) this._list.push(item),
  163. remove: function remove(item) {
  164. let list = this._list;
  165. let index = list.indexOf(item);
  166. if (0 <= index) list.slice(index, 1);
  167. }
  168. });
  169. assert.ok(List() instanceof List, 'Must be instance of List');
  170. assert.ok(new List() instanceof List, 'Must be instance of List');
  171. };
  172. exports['test:privates are unaccessible'] = function(assert) {
  173. const List = Trait.compose({
  174. // private API:
  175. _list: null,
  176. // public API
  177. constructor: function List() {
  178. this._list = [];
  179. },
  180. get length() this._list.length,
  181. add: function add(item) this._list.push(item),
  182. remove: function remove(item) {
  183. let list = this._list;
  184. let index = list.indexOf(item);
  185. if (0 <= index) list.slice(index, 1);
  186. }
  187. });
  188. let list = List();
  189. assert.ok(!('_list' in list), 'no privates on instance');
  190. assert.ok(
  191. !('_list' in List.prototype),
  192. 'no privates on prototype'
  193. );
  194. };
  195. exports['test:public API can access private API'] = function(assert) {
  196. const List = Trait.compose({
  197. // private API:
  198. _list: null,
  199. // public API
  200. constructor: function List() {
  201. this._list = [];
  202. },
  203. get length() this._list.length,
  204. add: function add(item) this._list.push(item),
  205. remove: function remove(item) {
  206. let list = this._list;
  207. let index = list.indexOf(item);
  208. if (0 <= index) list.slice(index, 1);
  209. }
  210. });
  211. let list = List();
  212. list.add('test');
  213. assert.equal(
  214. 1,
  215. list.length,
  216. 'should be able to add element and access it from public getter'
  217. );
  218. };
  219. exports['test:required'] = function(assert) {
  220. const Enumerable = Trait.compose({
  221. list: Trait.required,
  222. forEach: function forEach(consumer) {
  223. return this.list.forEach(consumer);
  224. }
  225. });
  226. try {
  227. let i = Enumerable();
  228. assert.fail('should throw when creating instance with required properties');
  229. } catch(e) {
  230. assert.equal(
  231. 'Error: Missing required property: list',
  232. e.toString(),
  233. 'required prop error'
  234. );
  235. }
  236. };
  237. exports['test:compose with required'] = function(assert) {
  238. const List = Trait.compose({
  239. // private API:
  240. _list: null,
  241. // public API
  242. constructor: function List() {
  243. this._list = [];
  244. },
  245. get length() this._list.length,
  246. add: function add(item) this._list.push(item),
  247. remove: function remove(item) {
  248. let list = this._list;
  249. let index = list.indexOf(item);
  250. if (0 <= index) list.slice(index, 1);
  251. }
  252. });
  253. const Enumerable = Trait.compose({
  254. list: Trait.required,
  255. forEach: function forEach(consumer) {
  256. return this.list.forEach(consumer);
  257. }
  258. });
  259. const EnumerableList = Enumerable.compose({
  260. get list() this._list.slice(0)
  261. }, List);
  262. let array = [1,2, 'ab']
  263. let l = EnumerableList(array);
  264. array.forEach(function(element) l.add(element));
  265. let number = 0;
  266. l.forEach(function(element, index) {
  267. number ++;
  268. assert.equal(array[index], element, 'should mach array element')
  269. });
  270. assert.equal(
  271. array.length,
  272. number,
  273. 'should perform as many asserts as elements in array'
  274. );
  275. };
  276. exports['test:resolve'] = function(assert) {
  277. const List = Trait.compose({
  278. // private API:
  279. _list: null,
  280. // public API
  281. constructor: function List() {
  282. this._list = [];
  283. },
  284. get length() this._list.length,
  285. add: function add(item) this._list.push(item),
  286. remove: function remove(item) {
  287. let list = this._list;
  288. let index = list.indexOf(item);
  289. if (0 <= index) list.slice(index, 1);
  290. }
  291. });
  292. const Range = List.resolve({
  293. constructor: null,
  294. add: '_add',
  295. }).compose({
  296. min: null,
  297. max: null,
  298. get list() this._list.slice(0),
  299. constructor: function Range(min, max) {
  300. this.min = min;
  301. this.max = max;
  302. this._list = [];
  303. },
  304. add: function(item) {
  305. if (item <= this.max && item >= this.min)
  306. this._add(item)
  307. }
  308. });
  309. let r = Range(0, 10);
  310. assert.equal(
  311. 0,
  312. r.min,
  313. 'constructor must have set min'
  314. );
  315. assert.equal(
  316. 10,
  317. r.max,
  318. 'constructor must have set max'
  319. );
  320. assert.equal(
  321. 0,
  322. r.length,
  323. 'should not contain any elements'
  324. );
  325. r.add(5);
  326. assert.equal(
  327. 1,
  328. r.length,
  329. 'should add `5` to list'
  330. );
  331. r.add(12);
  332. assert.equal(
  333. 1,
  334. r.length,
  335. 'should not add `12` to list'
  336. );
  337. };
  338. exports['test:custom iterator'] = function(assert) {
  339. let Sub = Trait.compose({
  340. foo: "foo",
  341. bar: "bar",
  342. baz: "baz",
  343. __iterator__: function() {
  344. yield 1;
  345. yield 2;
  346. yield 3;
  347. }
  348. });
  349. let (i = 0, sub = Sub()) {
  350. for (let item in sub)
  351. assert.equal(++i, item, "iterated item has the right value");
  352. };
  353. };
  354. require('sdk/test').run(exports);