555

tests.js 9.6KB

5 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. var assert = require('chai').assert
  2. const util = require('util');
  3. var utils = require("../bbhverse-pb-wip/utils")
  4. var js = utils.js;
  5. const proxiedDescriptor = js.proxiedDescriptor;
  6. const Ref = js.Ref;
  7. const valueBackedAccessor = js.valueBackedAccessor;
  8. var vValue = js.vValue;
  9. // var o = {}
  10. // Object.defineProperty(o, 'vV', proxiedDescriptor.create());
  11. // var x = { a: 5 }
  12. // o.vV = x;
  13. // console.dir(o.vV)
  14. // // console.log("" + (o.vV.current=== x))
  15. // assert.strictEqual(o.vV.current, x);
  16. // o.vV.revision = { a : '4'}
  17. // assert.strictEqual(o.vV.previous, x);
  18. console.dir(Reflect.ownKeys(Reflect));
  19. // Find proxyable types
  20. (()=>{
  21. utils.js.typeInstances.forEach((instance)=>{
  22. try {
  23. new Proxy(instance,{})
  24. js.proxyable.push(js.nativeType(instance))
  25. }
  26. catch(e){
  27. js.nonproxyable.push(js.nativeType(instance))
  28. // console.log('Proxy not possible for : ' + js.nativeType(instance))
  29. // console.log(e);
  30. }
  31. })
  32. return;
  33. })()
  34. console.dir('console.dir(js.proxyable)');
  35. console.dir(js.proxyable)
  36. console.dir('console.dir(js.nonproxyable)');
  37. console.dir(js.nonproxyable);
  38. // Getter Setter test.
  39. var vstore = '';
  40. var r = {}
  41. Object.defineProperty(r, 'p',{ get : function(){ return 'u got me : ' + vstore }, set : function(v){ vstore = v}, enumerable:true })
  42. console.dir(r)
  43. console.dir('***************-------- ')
  44. r.p = 'Pvalue'
  45. var ref = js.Ref.create(r, 'p')
  46. console.dir(ref)
  47. ref.y ='sadasdf'
  48. console.dir(ref)
  49. ref.assign('u set me')
  50. ref.x = 'asd'
  51. console.dir(ref)
  52. console.dir(r)
  53. r = {}
  54. ref = js.Ref.create(r, 'p')
  55. console.dir('***************-------- ')
  56. console.dir(ref)
  57. console.dir(typeof(ref))
  58. r.p = 'initialP'
  59. console.dir(ref)
  60. console.dir(typeof(ref))
  61. // PB : TODO -- Test
  62. // r.p preexisting value or with data descriptor
  63. // r.p with accessor that has side effects.
  64. // r.p is an object.
  65. r = { p : 'p'}
  66. ref = js.Ref.create(r, 'p')
  67. console.dir('***************-------- ')
  68. console.dir(ref)
  69. console.dir(typeof(ref))
  70. console.dir(typeof(new String('abc')))
  71. // console.dir('ref() -------- ')
  72. // console.dir(ref())
  73. // console.dir('ref() -------- ')
  74. ref.assign("P")
  75. console.dir(ref)
  76. // console.dir(Object.prototype.toString.call('astring'))
  77. console.dir(r)
  78. ref.assign("PP")
  79. console.dir(ref)
  80. console.dir(Object.prototype.toString.call(ref))
  81. console.dir(new String('asdfasdf'))
  82. console.dir(Object.prototype.toString.call(new String('asdfasdf')))
  83. // console.dir(Object.prototype.toString.call(new Proxy(new String('rawproxy'), {})))
  84. console.dir(r)
  85. r.p = "original replaced"
  86. console.dir(ref)
  87. console.dir(r)
  88. // PB : TODO -- enable when ref extensions are implemented.
  89. console.dir('ref.x')
  90. console.dir(ref.x)
  91. ref.x = 'X';
  92. console.dir(ref.x)
  93. console.dir(ref)
  94. console.dir(r)
  95. console.dir(ref.substring(2,5))
  96. r.p = 'x after pchanged'
  97. // ref.assign(Object.assign(new String('sdsd'), { p : r.p}) )
  98. console.dir(ref.x)
  99. console.dir(ref)
  100. console.dir(r)
  101. ref.assign(new String('ref.assign'));
  102. console.dir(ref.x)
  103. console.dir(ref)
  104. console.dir(r)
  105. var ns = new String('sdsd')
  106. console.dir(ns)
  107. ns.x = 'X'
  108. console.dir(ns)
  109. // ref.p = "PLOP"
  110. // console.dir(r)
  111. // console.dir(ref.p)
  112. // var o = js.Versioned.create(proxiedDescriptor['versioned current'])
  113. var o = js.Versioned.create()
  114. // o = 5; // This will destroy the reference to the prooxy received from Versioned.create.
  115. // we cannot override assignment operator...
  116. // therefore provision made for assign etc. methods for these types at the object level.
  117. // to achieve a switch to the underlying content for the reference.
  118. // property level assignment is possible through accessors or proxied descriptors.
  119. o.assign(new String('ddd'));
  120. console.dir('console.dir(o).......................')
  121. // Object.keys(o)
  122. console.dir(o)
  123. console.dir('ssss' + o)
  124. o.x = 'xtttt'
  125. console.dir(o)
  126. console.dir('sssfff : ' + o)
  127. console.dir('console.dir(o).......................')
  128. o.assign(new Number(5));
  129. console.dir('console.dir(o).......................')
  130. console.dir(o)
  131. console.dir('ssss' + o)
  132. console.dir(3 + o)
  133. console.dir('console.dir(o).......................')
  134. // throw 'done'
  135. o.assign(new Boolean(true));
  136. console.dir('console.dir(o).......................')
  137. console.dir('ssss' + true)
  138. console.dir(o)
  139. console.dir('ssss' + o)
  140. console.dir(3 + o)
  141. console.dir(3 + true)
  142. console.dir('console.dir(o).......................')
  143. // throw 'here'
  144. o = js.Versioned.create(o) // Doesn't work.
  145. var x = { a: 5 }
  146. o.vV = x;
  147. console.dir(o.vV)
  148. console.dir('console.dir(o).......................')
  149. console.dir(o)
  150. console.dir('console.dir(o).......................')
  151. console.dir(o.vV)
  152. // console.log("" + (o.vV.current=== x))
  153. console.log(assert.deepEqual(o.vV, x));
  154. // console.log(assert.strictEqual(o.vV, x, 'o being a Versioned o.vV and { a : 5 } found not strict equal'));
  155. console.log(assert.strictEqual(o.vV.current, x, 'but is not'));
  156. x = { a: 4 }
  157. o.vV.revision = x
  158. console.dir(o.vV)
  159. assert.deepEqual(o.vV, x);
  160. // assert.strictEqual(o.vV, x); // Strictequal will not match coz we get a proxy...
  161. // If we expect this to match use x = js.Versioned.create(x) which will be the proxy that will allways match.
  162. // PB : TODO -- We don't necessarily need to convert every property in x to a versioned property...
  163. assert.strictEqual(o.vV.current, x), 'but is not';
  164. o = js.Versioned.create()
  165. x = { a: 5 }
  166. o.vV = x;
  167. console.dir(o.vV)
  168. // console.log("" + (o.vV.current=== x))
  169. assert.deepEqual(o.vV, x, 'o.vV and { a : 5} found equal');
  170. // assert.strictEqual(o.vV.current, x), 'but is not';
  171. x = { a: 4 }
  172. o.vV = x
  173. console.dir(o.vV)
  174. assert.deepEqual(o.vV, x);
  175. var ten = { g : "10" }
  176. // proxiedDescriptor.ten = ten;
  177. var o1 = js.Versioned.create();
  178. // Object.defineProperty(o1, 'vV', proxiedDescriptor.create(o1, 'vV', null, proxiedDescriptor.current));
  179. // o1.vV = null;
  180. // initial uninitialized state...
  181. console.log('console.log(cloned.vV)')
  182. console.log(o1.vV)
  183. console.log(Object.prototype.toString.call(o1.vV))
  184. console.log('---------------------------------------------')
  185. console.log('console.dir(o1.vV)')
  186. console.dir(o1.vV)
  187. console.log(Object.prototype.toString.call(o1.vV))
  188. // throw 'done'
  189. console.log('---------------------------------------------')
  190. console.log('util.inspect(o1.vV)')
  191. console.log(util.inspect(o1.vV))
  192. console.log(Object.prototype.toString.call(o1.vV))
  193. console.log('---------------------------------------------')
  194. o1.vV = { d : 7 }
  195. console.dir(o1.vV)
  196. o1.vV = { e : 8 }
  197. console.dir(o1.vV)
  198. o1.vV = { f : "9 Nine" }
  199. console.dir(o1.vV)
  200. debugger
  201. o1.vV = ten
  202. var a = o1.vV;
  203. o1.vV = ten
  204. // console.log(util.inspect(o1.vV))
  205. // console.log((o1.vV === ten))
  206. console.dir(o1.vV)
  207. console.dir(o1.vV.g)
  208. assert.deepEqual(o1.vV, ten);
  209. assert.strictEqual(o1.vV, a); // Self identity via proxy === proxy. Instead of target === target which is effectively the same..
  210. console.dir('(ten == (+o1.vV))')
  211. // console.dir((ten == (+o1.vV))) // false Doesn't work. Throws exception when valueOf and toString return non primitives.
  212. console.dir((ten == o1.vV))
  213. console.dir((ten === o1.vV)) // false Doesn't work. coz getter always returns a proxy and not the target. The target indeed is === ten.
  214. // The options therefore as alternative or === which fails for proxies are as below.
  215. // Explicitly call valueOf or use a helper which gets the real target of the proxy
  216. console.dir('(o1.vV.valueOf() === ten)')
  217. console.dir((o1.vV.valueOf() === ten))
  218. assert.strictEqual(o1.vV.valueOf(), ten);
  219. assert.strictEqual(js.Proxied.getTarget(o1.vV), ten);
  220. // assert.strictEqual(o1.vV, ten);
  221. // Test descriptor
  222. var c = proxiedDescriptor.create(o1, 'dD', null, proxiedDescriptor.current);
  223. Object.defineProperty(o1, 'dD', c);
  224. var dD = Object.getOwnPropertyDescriptor(o1, "dD");
  225. console.dir('console.dir(c === dD)')
  226. console.dir(c === dD)
  227. console.log((dD.get === c.get) + ' ' + 'dD.get === c.get')
  228. var d = Object.getOwnPropertyDescriptor(o1, "vV");
  229. console.dir(d);
  230. console.dir('switching proxiedDescriptor type to composite revision')
  231. proxiedDescriptor.switchTo(d, proxiedDescriptor['composite revision'])
  232. o1.vV = { eleven : '11'}
  233. console.dir('o1.vV')
  234. console.dir(o1.vV)
  235. assert.deepEqual(o1.vV, { g : "10", eleven : '11' } );
  236. // Direct access test...
  237. var vV = o1.vV;
  238. vV.revision = { "12" : 12 }
  239. console.dir(vV);
  240. console.dir(vV.composite);
  241. assert.deepEqual(vV, o1.vV );
  242. var v = ['a', 'b', 'c'];
  243. var b = v.valueOf()
  244. console.log(v===b)
  245. console.dir(b)
  246. // Deref Tests.
  247. var proxT = { a : 'A', b : { bv : 'bv'}, c : ['e', 4, { x : 'x'}]}
  248. var prox = js.mutableProxy.create(proxT)
  249. prox = js.mutableProxy.create(prox)
  250. prox = js.mutableProxy.create(prox)
  251. console.dir(prox)
  252. console.dir(proxT === prox)
  253. prox = js.mutableProxy.create(prox)
  254. prox = js.mutableProxy.create(prox)
  255. prox = js.mutableProxy.create(prox)
  256. console.dir(js.dereference(prox))
  257. console.dir(proxT === js.dereference(prox))
  258. elxr = utils.elxr
  259. // var bitsToset = 0
  260. // for(var i=0; i<32; i++) {bitsToset = js.setBit(i, bitsToset)}
  261. // console.dir('' + elxr.toBitString(bitsToset) + ' (' + elxr.toBitString(bitsToset).length + ')')
  262. // var unusedmasks = []
  263. // console.dir(unusedmasks.pop())
  264. // elxr.generateBitMaskedSequence([4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29])
  265. // elxr.insertBits(16, [1,2])
  266. console.dir(elxr.bitIndices)
  267. console.dir(elxr.toBitString(elxr.bitIndices[31]))
  268. console.dir(elxr.toBitString(elxr.insertBitsForMask(16, 6)))
  269. var nwM = elxr.nextWithMask(6)
  270. console.dir(elxr.toBitString(nwM.next().value))
  271. console.dir(elxr.toBitString(nwM.next().value))
  272. console.dir(elxr.toBitString(nwM.next().value))
  273. console.dir(elxr.toBitString(nwM.next().value))