|
| 1 | +"enable aexpr"; |
| 2 | +import chai, {expect} from 'src/external/chai.js'; |
| 3 | +import sinon from 'src/external/sinon-3.2.1.js'; |
| 4 | +import sinonChai from 'src/external/sinon-chai.js'; |
| 5 | +chai.use(sinonChai); |
| 6 | + |
| 7 | +import { PIReference, makeRef } from 'src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js'; |
| 8 | +import { uuid } from 'utils'; |
| 9 | + |
| 10 | +describe("PI", function() { |
| 11 | + |
| 12 | + it("is defined", () => { |
| 13 | + expect(PIReference).to.be.ok; |
| 14 | + expect(makeRef).to.be.ok; |
| 15 | + }); |
| 16 | + |
| 17 | + it("detects accesses", () => { |
| 18 | + class fourtyTwo extends PIReference { |
| 19 | + read() { |
| 20 | + return 42; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + expect(makeRef(fourtyTwo, { |
| 25 | + thisReference: this, |
| 26 | + evalFunction: str => eval(str) |
| 27 | + })``.access).to.equal(42) |
| 28 | + |
| 29 | + }); |
| 30 | + |
| 31 | + it("can access the `this` reference", () => { |
| 32 | + class prop extends PIReference { |
| 33 | + create(strings) { |
| 34 | + this.prop = strings.first; |
| 35 | + } |
| 36 | + read() { |
| 37 | + return this.thisReference[this.prop]; |
| 38 | + } |
| 39 | + write(value) { |
| 40 | + return this.thisReference[this.prop] = value; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // read |
| 45 | + const o = { |
| 46 | + foo: 17, |
| 47 | + func() { |
| 48 | + return makeRef(prop, { |
| 49 | + thisReference: this, |
| 50 | + evalFunction: str => eval(str) |
| 51 | + })`foo`.access; |
| 52 | + }, |
| 53 | + write(value) { |
| 54 | + makeRef(prop, { |
| 55 | + thisReference: this, |
| 56 | + evalFunction: str => eval(str) |
| 57 | + })`bar`.access = value; |
| 58 | + } |
| 59 | + }; |
| 60 | + expect(o.func()).to.equal(17) |
| 61 | + |
| 62 | + // write |
| 63 | + o.write(23) |
| 64 | + expect(o.bar).to.equal(23) |
| 65 | + }); |
| 66 | + |
| 67 | + it("can access locals with eval", () => { |
| 68 | + class local extends PIReference { |
| 69 | + create(strings) { |
| 70 | + this.local = strings.first; |
| 71 | + } |
| 72 | + read() { |
| 73 | + return this.evalFunction(this.local); |
| 74 | + } |
| 75 | + write(value) { |
| 76 | + const id = uuid(); |
| 77 | + self[id] = value; |
| 78 | + try { |
| 79 | + return this.evalFunction(this.local + ` = self['${id}']`); |
| 80 | + } finally { |
| 81 | + delete self[id]; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + let v1 = 'v1', v2 = 42; |
| 87 | + |
| 88 | + // read |
| 89 | + expect(makeRef(local, { |
| 90 | + thisReference: this, |
| 91 | + evalFunction: str => eval(str) |
| 92 | + })`v1`.access).to.equal('v1') |
| 93 | + |
| 94 | + // write |
| 95 | + makeRef(local, { |
| 96 | + thisReference: this, |
| 97 | + evalFunction: str => eval(str) |
| 98 | + })`v2`.access = "v2"; |
| 99 | + expect(v2).to.equal('v2'); |
| 100 | + |
| 101 | + // read/write |
| 102 | + makeRef(local, { |
| 103 | + thisReference: this, |
| 104 | + evalFunction: str => eval(str) |
| 105 | + })`v2`.access += '.2'; |
| 106 | + expect(v2).to.equal('v2.2'); |
| 107 | + }); |
| 108 | + |
| 109 | + it("ignore normal tagged template strings", () => { |
| 110 | + function foo() { |
| 111 | + return 'bar'; |
| 112 | + } |
| 113 | + |
| 114 | + expect(makeRef(foo, { |
| 115 | + thisReference: this, |
| 116 | + evalFunction: str => eval(str) |
| 117 | + })``.access).to.equal('bar') |
| 118 | + }); |
| 119 | + |
| 120 | +// it("inserts default constructors", () => { |
| 121 | +// class A { |
| 122 | +// constructor() { |
| 123 | +// this.prop = 42; |
| 124 | +// } |
| 125 | +// } |
| 126 | +// class B extends A {} |
| 127 | + |
| 128 | +// const b = new B(); |
| 129 | +// expect(b.prop).to.equal(42); |
| 130 | +// }); |
| 131 | + |
| 132 | +}); |
0 commit comments