Skip to content

Commit

Permalink
Tests WIP 1
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 8, 2023
1 parent 2548842 commit 4878f20
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions test/with.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* --------------------
* livepack module
* Tests for `with`
* ------------------*/

/* eslint-disable strict, no-with */

// Imports

const support = require('./support/index.js'),
itSerializes = support.itSerializes.withOptions({strictEnv: false}),
{transpiledFiles} = support;

// Tests
describe('with statements', () => {
describe('outside serialized function', () => {
itSerializes('provides scope to function when no outer binding', {
in() {
with ({x: 123}) {
return () => x; // eslint-disable-line no-undef
}
},
out: '(a=>{with(a)return()=>x})({x:123})',
validate(fn) {
expect(fn).toBeFunction();
expect(fn()).toBe(123);
}
});

itSerializes('provides scope to function when outer binding', {
in() {
const x = 456;
with ({x: 123}) {
return () => x;
}
},
out: '(x=>a=>{with(a)return()=>x})(456)({x:123})',
validate(fn) {
expect(fn).toBeFunction();
expect(fn()).toBe(123);
}
});

itSerializes('allows access to outer binding', {
in() {
const x = 456;
with ({}) {
return () => x;
}
},
out: '(x=>a=>{with(a)return()=>x})(456)({})',
validate(fn) {
expect(fn).toBeFunction();
expect(fn()).toBe(456);
}
});

itSerializes('allows access to global', {
in() {
with ({}) {
return () => console;
}
},
out: '(a=>{with(a)return()=>console})({})',
validate(fn) {
expect(fn).toBeFunction();
expect(fn()).toBe(console);
}
});

itSerializes('Alters scope when `with` object property changed', {
in() {
const obj = {x: 123},
x = 456;
let fn;
with (obj) {
fn = (0, () => x);
}
return [fn, obj];
},
out: `(()=>{
const a={x:123};
return[
(x=>a=>{with(a)return()=>x})(456)(a),
a
]
})()`,
validate([fn, obj]) {
expect(fn).toBeFunction();
expect(fn()).toBe(123);
obj.x = 789;
expect(fn()).toBe(789);
}
});

itSerializes('Alters scope when `with` object property deleted', {
in() {
const obj = {x: 123},
x = 456;
let fn;
with (obj) {
fn = (0, () => x);
}
return [fn, obj];
},
out: `(()=>{
const a={x:123};
return[
(x=>a=>{with(a)return()=>x})(456)(a),
a
]
})()`,
validate([fn, obj]) {
expect(fn).toBeFunction();
expect(fn()).toBe(123);
delete obj.x;
expect(fn()).toBe(456);
}
});

itSerializes("does not disrupt instrumentation's internal functions", {
in() {
const x = 123;
with ({livepack_tracker: 1, livepack_getScopeId: 2}) {
const y = 456;
return () => [x, y];
}
},
out: '(x=>b=>{with(b)return a=>()=>[x,a]})(123)({livepack_tracker:1,livepack_getScopeId:2})(456)',
validate(fn) {
expect(fn).toBeFunction();
expect(fn()).toEqual([123, 456]);

// Check internal functions match the ones being tested for
expect(transpiledFiles[__filename]).toInclude(
// eslint-disable-next-line no-useless-concat
'const [livepack_tracker, livepack_getScopeId] = ' + 'require('
);
}
});
});
});

0 comments on commit 4878f20

Please sign in to comment.