Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(es/decorators): Split decorator-tests #9119

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import typescript from 'typescript'
import fs from 'fs'
import module from 'module'

const require = module.createRequire(import.meta.url)
const ts = fs.readFileSync('./tests/decorator-tests/decorator-tests.ts', 'utf8')

// Convert TypeScript to JavaScript for testing JavaScript VMs
console.log('Converting TypeScript to JavaScript...')

const program = typescript.createProgram({
rootNames: ['./tests/decorator-tests/decorator-tests.ts'],
options: { target: typescript.ScriptTarget.ESNext }
});
// console.log('program', program)
const mainFile = program.getSourceFile('./tests/decorator-tests/decorator-tests.ts')
// console.log('mainFile', mainFile)

const testVarStmt = mainFile.statements.find((s) => s.kind === typescript.SyntaxKind.VariableStatement)
// console.log('testVarStmt', testVarStmt)

const testVarDecl = testVarStmt.declarationList.declarations[0]
// console.log('testVarDecl', testVarDecl)

const properties = testVarDecl.initializer.properties
// console.log('properties', properties)

fs.mkdirSync(`tests/decorator-evanw-split`, { recursive: true });

for (const property of properties) {
let name = property.name.getText(mainFile)
const value = property.initializer.getText(mainFile)

name = name.replace(/['\(\)]+/g, '').replace(/[\: ]+/g, '-')

// console.log('name', name)
// console.log('value', value)

const code = `(${value})()`;
const transpiiled = typescript.transpile(code, { target: typescript.ScriptTarget.ESNext })

fs.writeFileSync(`tests/decorator-evanw-split/${name}.js`, transpiiled)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(() => {
const dec = (key, getName, setName) => (target, ctx) => {
assertEq(() => typeof target.get, 'function');
assertEq(() => typeof target.set, 'function');
assertEq(() => target.get.name, getName);
assertEq(() => target.set.name, setName);
assertEq(() => ctx.kind, 'accessor');
assertEq(() => ctx.name, key);
assertEq(() => ctx.static, false);
assertEq(() => ctx.private, false);
assertEq(() => ctx.access.has({ [key]: false }), true);
assertEq(() => ctx.access.has({ bar: true }), false);
assertEq(() => ctx.access.get({ [key]: 123 }), 123);
assertEq(() => {
const obj = {};
ctx.access.set(obj, 123);
return obj[key];
}, 123);
};
const bar = Symbol('bar');
const baz = Symbol();
class Foo {
@dec('foo', 'get foo', 'set foo')
accessor foo = 0;
@dec(bar, 'get [bar]', 'set [bar]')
accessor [bar] = 0;
@dec(baz, 'get ', 'set ')
accessor [baz] = 0;
}
var obj = new Foo;
obj.foo = 321;
assertEq(() => obj.foo, 321);
obj[bar] = 4321;
assertEq(() => obj[bar], 4321);
obj[baz] = 54321;
assertEq(() => obj[baz], 54321);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(() => {
let lateAsserts;
const dec = (target, ctx) => {
assertEq(() => typeof target.get, 'function');
assertEq(() => typeof target.set, 'function');
assertEq(() => target.get.name, 'get #foo');
assertEq(() => target.set.name, 'set #foo');
assertEq(() => ctx.kind, 'accessor');
assertEq(() => ctx.name, '#foo');
assertEq(() => ctx.static, false);
assertEq(() => ctx.private, true);
lateAsserts = () => {
assertEq(() => ctx.access.has(new Foo), true);
assertEq(() => ctx.access.has({}), false);
assertEq(() => ctx.access.get(new Foo), 0);
assertEq(() => {
const obj = new Foo;
ctx.access.set(obj, 123);
return get$foo(obj);
}, 123);
};
};
let get$foo;
let set$foo;
class Foo {
@dec
accessor #foo = 0;
static {
get$foo = x => x.#foo;
set$foo = (x, y) => { x.#foo = y; };
}
}
lateAsserts();
var obj = new Foo;
assertEq(() => set$foo(obj, 321), undefined);
assertEq(() => get$foo(obj), 321);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(() => {
let lateAsserts;
const dec = (target, ctx) => {
assertEq(() => typeof target.get, 'function');
assertEq(() => typeof target.set, 'function');
assertEq(() => target.get.name, 'get #foo');
assertEq(() => target.set.name, 'set #foo');
assertEq(() => ctx.kind, 'accessor');
assertEq(() => ctx.name, '#foo');
assertEq(() => ctx.static, true);
assertEq(() => ctx.private, true);
lateAsserts = () => {
assertEq(() => ctx.access.has(Foo), true);
assertEq(() => ctx.access.has({}), false);
assertEq(() => ctx.access.get(Foo), 0);
assertEq(() => {
ctx.access.set(Foo, 123);
return get$foo(Foo);
}, 123);
};
};
let get$foo;
let set$foo;
class Foo {
@dec
static accessor #foo = 0;
static {
get$foo = x => x.#foo;
set$foo = (x, y) => { x.#foo = y; };
}
}
lateAsserts();
assertEq(() => set$foo(Foo, 321), undefined);
assertEq(() => get$foo(Foo), 321);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(() => {
const dec = (key, getName, setName) => (target, ctx) => {
assertEq(() => typeof target.get, 'function');
assertEq(() => typeof target.set, 'function');
assertEq(() => target.get.name, getName);
assertEq(() => target.set.name, setName);
assertEq(() => ctx.kind, 'accessor');
assertEq(() => ctx.name, key);
assertEq(() => ctx.static, true);
assertEq(() => ctx.private, false);
assertEq(() => ctx.access.has({ [key]: false }), true);
assertEq(() => ctx.access.has({ bar: true }), false);
assertEq(() => ctx.access.get({ [key]: 123 }), 123);
assertEq(() => {
const obj = {};
ctx.access.set(obj, 123);
return obj[key];
}, 123);
};
const bar = Symbol('bar');
const baz = Symbol();
class Foo {
@dec('foo', 'get foo', 'set foo')
static accessor foo = 0;
@dec(bar, 'get [bar]', 'set [bar]')
static accessor [bar] = 0;
@dec(baz, 'get ', 'set ')
static accessor [baz] = 0;
}
Foo.foo = 321;
assertEq(() => Foo.foo, 321);
Foo[bar] = 4321;
assertEq(() => Foo[bar], 4321);
Foo[baz] = 54321;
assertEq(() => Foo[baz], 54321);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(() => {
let oldAddInitializer;
let got;
const dec = (target, ctx) => {
ctx.addInitializer(function (...args) {
got = { this: this, args };
});
if (oldAddInitializer)
assertThrows(() => oldAddInitializer(() => { }), TypeError);
assertThrows(() => ctx.addInitializer({}), TypeError);
oldAddInitializer = ctx.addInitializer;
};
class Foo {
@dec
@dec
accessor foo;
}
assertEq(() => got, undefined);
const instance = new Foo;
assertEq(() => got.this, instance);
assertEq(() => got.args.length, 0);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(() => {
let oldAddInitializer;
let got;
const dec = (target, ctx) => {
ctx.addInitializer(function (...args) {
got = { this: this, args };
});
if (oldAddInitializer)
assertThrows(() => oldAddInitializer(() => { }), TypeError);
assertThrows(() => ctx.addInitializer({}), TypeError);
oldAddInitializer = ctx.addInitializer;
};
class Foo {
@dec
@dec
accessor #foo;
}
assertEq(() => got, undefined);
const instance = new Foo;
assertEq(() => got.this, instance);
assertEq(() => got.args.length, 0);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(() => {
let oldAddInitializer;
let got;
const dec = (target, ctx) => {
ctx.addInitializer(function (...args) {
got = { this: this, args };
});
if (oldAddInitializer)
assertThrows(() => oldAddInitializer(() => { }), TypeError);
assertThrows(() => ctx.addInitializer({}), TypeError);
oldAddInitializer = ctx.addInitializer;
};
class Foo {
@dec
@dec
static accessor #foo;
}
assertEq(() => got.this, Foo);
assertEq(() => got.args.length, 0);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(() => {
let oldAddInitializer;
let got;
const dec = (target, ctx) => {
ctx.addInitializer(function (...args) {
got = { this: this, args };
});
if (oldAddInitializer)
assertThrows(() => oldAddInitializer(() => { }), TypeError);
assertThrows(() => ctx.addInitializer({}), TypeError);
oldAddInitializer = ctx.addInitializer;
};
class Foo {
@dec
@dec
static accessor foo;
}
assertEq(() => got.this, Foo);
assertEq(() => got.args.length, 0);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(() => {
assertThrows(() => {
const dec = (target, ctx) => {
return null;
};
class Foo {
@dec
accessor foo;
}
}, TypeError);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(() => {
assertThrows(() => {
const dec = (target, ctx) => {
return null;
};
class Foo {
@dec
accessor #foo;
}
}, TypeError);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(() => {
assertThrows(() => {
const dec = (target, ctx) => {
return null;
};
class Foo {
@dec
static accessor #foo;
}
}, TypeError);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(() => {
assertThrows(() => {
const dec = (target, ctx) => {
return null;
};
class Foo {
@dec
static accessor foo;
}
}, TypeError);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(() => {
let get;
let set;
const dec = (target, ctx) => {
function init(x) {
assertEq(() => this instanceof Foo, true);
return x + 1;
}
get = function () { return target.get.call(this) * 10; };
set = function (x) { target.set.call(this, x * 2); };
return { get, set, init };
};
class Foo {
@dec
accessor foo = 123;
}
assertEq(() => Object.getOwnPropertyDescriptor(Foo.prototype, 'foo').get, get);
assertEq(() => Object.getOwnPropertyDescriptor(Foo.prototype, 'foo').set, set);
var obj = new Foo;
assertEq(() => obj.foo, (123 + 1) * 10);
obj.foo = 321;
assertEq(() => obj.foo, (321 * 2) * 10);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(() => {
let get;
let set;
const dec = (target, ctx) => {
function init(x) {
assertEq(() => this instanceof Foo, true);
return x + 1;
}
get = function () { return target.get.call(this) * 10; };
set = function (x) { target.set.call(this, x * 2); };
return { get, set, init };
};
let get$foo;
let set$foo;
class Foo {
@dec
accessor #foo = 123;
static {
get$foo = x => x.#foo;
set$foo = (x, y) => { x.#foo = y; };
}
}
var obj = new Foo;
assertEq(() => get$foo(obj), (123 + 1) * 10);
assertEq(() => set$foo(obj, 321), undefined);
assertEq(() => get$foo(obj), (321 * 2) * 10);
})();
Loading
Loading