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