Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // var assert = require('assert');
  2. var assert = require('chai').assert
  3. const util = require('util');
  4. var utils = require("../bbhverse-pb-wip/utils")
  5. var js = utils.js;
  6. const proxiedDescriptor = js.proxiedDescriptor;
  7. const Ref = js.Ref;
  8. const valueBackedAccessor = js.valueBackedAccessor;
  9. describe('proxy', function() {
  10. it('is not possible for', function() {
  11. console.log(js.nonproxyable)
  12. assert.ok(true)
  13. });
  14. it('is possible for', function() {
  15. console.log(js.proxyable)
  16. assert.ok(true)
  17. });
  18. })
  19. describe('mutableproxy', function() {
  20. it('blank creation should be possible.', function() {
  21. var mutableProxy = js.mutableProxy.create();
  22. console.dir(mutableProxy)
  23. assert.ok(true)
  24. });
  25. it('target should be switchable', function() {
  26. var mutableProxy = js.mutableProxy.create({original : 'target at create time'});
  27. console.dir(mutableProxy)
  28. js.mutableProxy.setTarget({'switched' : 'switched'}, mutableProxy)
  29. assert.ok(true)
  30. });
  31. it('handler should be switchable', function() {
  32. var mutableProxy = js.mutableProxy.create({original : 'target at create time'});
  33. js.mutableProxy.setHandler({
  34. get : function(target, key){
  35. console.log('handler switch invoked : ')
  36. // console.dir(meta.target)
  37. // console.dir(arguments)
  38. var args = Array.prototype.slice.call(arguments, 0);
  39. args[0] = target;
  40. return target[key]
  41. return Reflect.apply(Reflect[key], target, args)
  42. // var args = Array.prototype.slice.call(arguments, 0);
  43. // args[0] = meta.target;
  44. // return Reflect[key](...args)
  45. }
  46. }, mutableProxy)
  47. console.dir(mutableProxy)
  48. assert.ok(true)
  49. });
  50. it('handler should be called', function() {
  51. var mutableProxy = js.mutableProxy.create();
  52. // console.dir(mutableProxy);
  53. // console.log(mutableProxy);
  54. // js.mutableProxy.setTarget({ switched : 'switched'}, mutableProxy)
  55. // console.dir(mutableProxy);
  56. // console.log(mutableProxy);
  57. var x = {}
  58. js.mutableProxy.setHandler({
  59. get : function(target, key){
  60. console.log('handler switch invoked : ')
  61. // console.dir(meta.target)
  62. // console.dir(arguments)
  63. var args = Array.prototype.slice.call(arguments, 0);
  64. args[0] = target;
  65. x = 'handler was called'
  66. return target[key]
  67. return Reflect.apply(Reflect[key], target, args)
  68. // var args = Array.prototype.slice.call(arguments, 0);
  69. // args[0] = meta.target;
  70. // return Reflect[key](...args)
  71. }
  72. }, mutableProxy)
  73. console.dir(mutableProxy);
  74. console.log(mutableProxy);
  75. js.mutableProxy.setTarget({ switched : 'handlerswitched'}, mutableProxy)
  76. console.dir(mutableProxy);
  77. console.log(mutableProxy);
  78. assert.strictEqual('handler was called', x), 'but is not';
  79. });
  80. })
  81. describe('proxiedDescriptor', function() {
  82. var o = {}
  83. Object.defineProperty(o, 'vV', proxiedDescriptor.create());
  84. it('on create should be undefined', function() {
  85. // console.log(o.vV)
  86. // console.dir(o.vV)
  87. assert.isUndefined(o.vV);
  88. });
  89. it('after revision current should be {a : 4}', function() {
  90. var x = { a: 5 }
  91. o.vV = x;
  92. console.dir(o.vV)
  93. // console.log("" + (o.vV.current=== x))
  94. assert.deepEqual(o.vV, x);
  95. // assert.strictEqual(o.vV.current, x), 'but is not';
  96. x = { a: 4 }
  97. o.vV.revision = x
  98. console.dir(o.vV)
  99. assert.deepEqual(o.vV, x);
  100. // assert.strictEqual(o.vV.previous, x), 'but is not';
  101. });
  102. it('valueBackedAccessor is unrevisioned unless revision is explicitly called.', function() {
  103. var vValue = js.vValue;
  104. var o1 = {}
  105. Object.defineProperty(o1, 'vV', valueBackedAccessor.create());
  106. // initial uninitialized state...
  107. console.log('console.log(cloned.vV)')
  108. console.log(o1.vV)
  109. console.log(Object.prototype.toString.call(o1.vV))
  110. console.log('---------------------------------------------')
  111. console.log('console.dir(o1.vV)')
  112. console.dir(o1.vV)
  113. console.log(Object.prototype.toString.call(o1.vV))
  114. // throw 'done'
  115. console.log('---------------------------------------------')
  116. console.log('util.inspect(o1.vV)')
  117. console.log(util.inspect(o1.vV))
  118. console.log(Object.prototype.toString.call(o1.vV))
  119. console.log('---------------------------------------------')
  120. o1.vV = { d : 7 }
  121. o1.vV = { e : 8 }
  122. o1.vV = { f : "9 Nine" }
  123. var ten = { g : "10" }
  124. o1.vV = ten
  125. // console.log(o1.vV === ten)
  126. assert.deepEqual(o1.vV, ten);
  127. assert.strictEqual(o1.vV.current, ten);
  128. assert.strictEqual(o1.vV, ten), 'but is not vV[Proxy] cannot be equal to original object. Use vv[proxy].current === original value for comparison. ';
  129. });
  130. it('proxiedDescriptor switch to composite revision', function() {
  131. var ten = { g : "10" }
  132. // proxiedDescriptor.ten = ten;
  133. var o1 = js.Versioned.create();
  134. // Object.defineProperty(o1, 'vV', proxiedDescriptor.create(proxiedDescriptor.current));
  135. // o1.vV = null;
  136. // initial uninitialized state...
  137. console.log('console.log(cloned.vV)')
  138. console.log(o1.vV)
  139. console.log(Object.prototype.toString.call(o1.vV))
  140. console.log('---------------------------------------------')
  141. console.log('console.dir(o1.vV)')
  142. console.dir(o1.vV)
  143. console.log(Object.prototype.toString.call(o1.vV))
  144. // throw 'done'
  145. console.log('---------------------------------------------')
  146. console.log('util.inspect(o1.vV)')
  147. console.log(util.inspect(o1.vV))
  148. console.log(Object.prototype.toString.call(o1.vV))
  149. console.log('---------------------------------------------')
  150. o1.vV = { d : 7 }
  151. console.dir(o1.vV)
  152. o1.vV = { e : 8 }
  153. console.dir(o1.vV)
  154. o1.vV = { f : "9 Nine" }
  155. console.dir(o1.vV)
  156. debugger
  157. o1.vV = ten
  158. var a = o1.vV;
  159. o1.vV = ten
  160. // console.log(util.inspect(o1.vV))
  161. // console.log((o1.vV === ten))
  162. console.dir(o1.vV)
  163. console.dir(o1.vV.g)
  164. assert.deepEqual(o1.vV, ten);
  165. assert.strictEqual(o1.vV, a); // Self identity via proxy === proxy. Instead of target === target which is effectively the same..
  166. console.dir((ten == (+o1.vV))) // false Doesn't work. Throws exception when valueOf and toString return non primitives.
  167. console.dir((ten === o1.vV)) // false Doesn't work. coz getter always returns a proxy and not the target. The target indeed is === ten.
  168. // The options therefore as alternative or === which fails for proxies are as below.
  169. // Explicitly call valueOf or use a helper which gets the real target of the proxy
  170. console.dir((o1.vV.valueOf() === ten))
  171. assert.strictEqual(o1.vV.valueOf(), ten);
  172. assert.strictEqual(js.Proxied.getTarget(o1.vV), ten);
  173. // assert.strictEqual(o1.vV, ten);
  174. var d = Object.getOwnPropertyDescriptor(o1, "vV");
  175. proxiedDescriptor.switchTo(d, proxiedDescriptor['composite revision'])
  176. o1.vV = { eleven : '11'}
  177. console.dir(o1.vV)
  178. assert.deepEqual(o1.vV, { g : "10", eleven : '11' } );
  179. });
  180. it('series of changes should match', function() {
  181. o.vV = { d : 7 }
  182. o.vV.revision = { b: 6 }
  183. o.vV.firstrevision = { c: 4 }
  184. // console.log("" + (o.vV.current=== x))
  185. // console.dir(o.vV)
  186. assert.deepEqual(o.vV.current, {b : 6} );
  187. o.vV.revision = { a : '4'}
  188. assert.deepEqual(o.vV.previous, {b : 6});
  189. });
  190. });
  191. describe('Ref', function() {
  192. var o = { a : "aaaaa", b : "bbbbb" }
  193. var ref = js.Ref.create(o,'a')
  194. // console.log('---------------------s assignment---')
  195. var aaa = ref;
  196. // console.log('---------------------e assignment---')
  197. // console.dir(aaa)
  198. it('ref should return transparent proxy', function() {
  199. assert.strictEqual(ref, aaa, 'but does not');
  200. });
  201. it('ref() should return referenced value', function() {
  202. assert.deepEqual(ref(), 'aaaaa', 'but does not');
  203. });
  204. });
  205. describe('exercise some calls', function() {
  206. it('series of calls should not throw exceptions.', function() {
  207. var cobj = { d : 'd', e : 5};
  208. var original = { a : 'a', b : 'b', cobj : cobj
  209. , arrayContainingObjects : [ { o: 'o', arr : [1,2]}, cobj ]
  210. , objectContainingArrays : { arr0 : ['o', 1, 'asd'], arr1 : [1,2]}
  211. };
  212. // PB : TODO -- Test accessor descriptor and valueBackedAccessorDescriptor cases.
  213. original.fobj = original.cobj;
  214. original.orig = original;
  215. Object.defineProperty(original, 'vba', js.valueBackedAccessor.create());
  216. original.vba = 'value set using setter'
  217. console.log('original.vba : ')
  218. console.dir(original.vba)
  219. console.dir(original)
  220. var d = Object.getOwnPropertyDescriptor(original, 'vba')
  221. // console.log('valueBackedAccessor.isValueBackedAccessor(d)' + js.valueBackedAccessor.isValueBackedAccessor(d));
  222. // console.log('valueBackedAccessor.isValueBackedAccessor(d)' + js.valueBackedAccessor.isValueBackedAccessor({}));
  223. var cloned = js.clone(false, original);
  224. console.log('cloned.vba : ')
  225. console.dir(cloned.vba)
  226. console.dir(cloned)
  227. console.log("isClone = " + (original !== cloned))
  228. console.log("isClone = " + (original.cobj !== cloned.cobj))
  229. console.log("isClone = " + (original.fobj !== cloned.fobj))
  230. console.log("isClone = " + (original.cobj === original.fobj))
  231. console.log("isClone = " + (cloned.cobj === cloned.fobj)) // Ref cloning.
  232. console.log("descriptorRetrivalYieldsSameDescriptor = " + (Object.getOwnPropertyDescriptor(original, 'a') === Object.getOwnPropertyDescriptor(original, "a")))
  233. var linkedUsingAssign = {}
  234. js.assign(linkedUsingAssign, cloned, cloned)
  235. console.log('linkedUsingAssign.vba : ')
  236. console.dir(linkedUsingAssign.vba)
  237. console.dir(linkedUsingAssign)
  238. // var a = {
  239. // valueOf : function(){ return 'I am a'}
  240. // , toString : function(){ return 'I am a'}
  241. // }
  242. // console.dir('' + a)
  243. // vValueHelper Tests
  244. // var proxiedDescriptor = js.proxiedDescriptor;
  245. var vValue = js.vValue;
  246. Object.defineProperty(cloned, 'vV', vValue.create());
  247. console.log('vValueHelper')
  248. // initial uninitialized state...
  249. console.log('console.log(cloned.vV)')
  250. console.log(cloned.vV)
  251. console.log(Object.prototype.toString.call(cloned.vV))
  252. console.log('---------------------------------------------')
  253. console.log('console.dir(cloned.vV)')
  254. console.dir(cloned.vV)
  255. console.log(Object.prototype.toString.call(cloned.vV))
  256. // throw 'done'
  257. console.log('---------------------------------------------')
  258. console.log('util.inspect(cloned.vV)')
  259. console.log(util.inspect(cloned.vV))
  260. console.log(Object.prototype.toString.call(cloned.vV))
  261. console.log('---------------------------------------------')
  262. cloned.vV = { d : 7 }
  263. // cloned.vV = { e : 8 }
  264. // cloned.vV = { f : "9 Nine" }
  265. // cloned.vV = { g : "10" }
  266. cloned.vV.revision = { a: 5 }
  267. cloned.vV.revision = { a: 6 }
  268. cloned.vV.firstrevision = { c: 4 }
  269. cloned.vV.previous = { c0: 'c0' }
  270. cloned.vV.previous = { a: '4' }
  271. console.log('console.log(cloned.vV)')
  272. console.log(cloned.vV)
  273. console.log('---------------------------------------------')
  274. console.dir('console.dir(cloned.vV)')
  275. console.dir(cloned.vV)
  276. console.log('---------------------------------------------')
  277. console.log('util.inspect(cloned.vV)')
  278. console.log(util.inspect(cloned.vV))
  279. console.log('---------------------------------------------')
  280. console.log('console.dir(cloned.vV.composite)')
  281. console.dir(cloned.vV.composite)
  282. console.dir(cloned.vV.previous)
  283. console.dir(cloned.vV.current)
  284. });
  285. });