-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2548842
commit 4878f20
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' | ||
); | ||
} | ||
}); | ||
}); | ||
}); |