Skip to content

Commit

Permalink
test expectayshuns
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMuller committed Aug 24, 2020
2 parents 5253684 + ffc6b7d commit c392304
Show file tree
Hide file tree
Showing 29 changed files with 1,874 additions and 353 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ All packages within this repository have the following scripts:
- `watch` - watches for file changes and builds them progressively, usually
running `tsc --watch`.
- `test` - executes all unit tests for the current package.
- `test:update` - executes all unit tests and overwrites snapshot expectations (those `.snap` files).
- `package` - emits publishable artifacts to `dist`.
- `lint` - run linter against source
- `lint:fix` - lint and auto-correct formatting issues when possible
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/integ-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@octokit/rest": "^18.0.3",
"dotenv": "^8.2.0",
"fs-extra": "^9.0.1",
"jest": "^26.4.1",
"jest": "^26.4.2",
"jsii": "^0.0.0",
"jsii-pacmak": "^0.0.0",
"jsii-rosetta": "^0.0.0",
Expand Down
20 changes: 18 additions & 2 deletions packages/@jsii/kernel/lib/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,8 @@ export class Kernel {

// save the old property under $jsii$super$<prop>$ so that property overrides
// can still access it via `super.<prop>`.
const prev = Object.getOwnPropertyDescriptor(obj, propertyName) ?? {
value: undefined,
const prev = getPropertyDescriptor(obj, propertyName) ?? {
value: obj[propertyName],
writable: true,
enumerable: true,
configurable: true,
Expand Down Expand Up @@ -656,6 +656,22 @@ export class Kernel {
});
},
});

function getPropertyDescriptor(
obj: any,
propertyName: string,
): PropertyDescriptor | undefined {
const direct = Object.getOwnPropertyDescriptor(obj, propertyName);
if (direct != null) {
return direct;
}
const proto = Object.getPrototypeOf(obj);
if (proto == null && proto !== Object.prototype) {
// We reached Object or the prototype chain root, all hope is lost!
return undefined;
}
return getPropertyDescriptor(proto, propertyName);
}
}

private _applyMethodOverride(
Expand Down
10 changes: 7 additions & 3 deletions packages/@jsii/kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@
"@types/node": "^10.17.28",
"@types/tar": "^4.0.3",
"eslint": "^7.7.0",
"jest": "^26.4.1",
"jest": "^26.4.2",
"jest-expect-message": "^1.0.2",
"jsii-build-tools": "^0.0.0",
"jsii-calc": "^0.0.0",
"prettier": "^2.0.5",
"ts-jest": "^26.2.0",
"typescript": "~3.9.7"
},
"jest": {
Expand All @@ -73,7 +74,10 @@
],
"testEnvironment": "node",
"testMatch": [
"**/?(*.)+(spec|test).js"
]
"**/?(*.)+(spec|test).ts"
],
"transform": {
"\\.tsx?$": "ts-jest"
}
}
}
63 changes: 63 additions & 0 deletions packages/@jsii/kernel/test/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,69 @@ defineTest('ANY serializer: ref', (sandbox) => {
});
});

defineTest('Override transitive property', (sandbox) => {
//////////
// GIVEN
//////////
const originalString = 'r00t';
const initialOverriddenPropValue = 'Overridden Value';
let propValue = initialOverriddenPropValue;
sandbox.callbackHandler = makeSyncCallbackHandler((callback) => {
const getOrSet = callback.get ?? callback.set;
// We don't expect to receive any other callback
expect(getOrSet).toBeDefined();
expect(getOrSet?.property).toBe('dynamicProperty');

if (callback.get) {
return propValue;
}
propValue = callback.set?.value;
return void 0;
});

//////////
// WHEN
//////////
const objref = sandbox.create({
fqn: 'jsii-calc.DynamicPropertyBearerChild',
args: [originalString],
overrides: [{ property: 'dynamicProperty' }],
});

//////////
// THEN
//////////

// Reading the "super" property
expect(sandbox.get({ objref, property: 'dynamicProperty' }).value).toBe(
originalString,
);
expect(sandbox.get({ objref, property: 'valueStore' }).value).toBe(
originalString,
);

// Setting the dynamicProperty value through the override
expect(
sandbox.invoke({ objref, method: 'overrideValue', args: ['N3W'] }).result,
).toBe(initialOverriddenPropValue);
// Checking the side effect happened on the override:
expect(propValue).toBe('N3W');
// Check the "super" property didn't change:
expect(sandbox.get({ objref, property: 'dynamicProperty' }).value).toBe(
originalString,
);
expect(sandbox.get({ objref, property: 'valueStore' }).value).toBe(
originalString,
);

// Set the "super" property now
sandbox.set({ objref, property: 'dynamicProperty', value: '' });
// Check the side effect made it to the storage
expect(sandbox.get({ objref, property: 'valueStore' }).value).toBe('');
// Check the overridden property didn't change...
expect(propValue).toBe('N3W');
});

// =================================================================================================

const testNames: { [name: string]: boolean } = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/python-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"jsii-build-tools": "^0.0.0",
"jsii-calc": "^0.0.0",
"jsii-pacmak": "^0.0.0",
"ts-node": "^8.10.2",
"ts-node": "^9.0.0",
"typescript": "~3.9.7"
}
}
2 changes: 1 addition & 1 deletion packages/@jsii/python-runtime/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
black~=19.10b0
pytest~=6.0
pytest-mypy~=0.6
pytest-mypy~=0.7
pip~=20.2
setuptools~=49.6
wheel~=0.35
Expand Down
10 changes: 7 additions & 3 deletions packages/@jsii/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
"@types/jest": "^26.0.10",
"@types/node": "^10.17.28",
"eslint": "^7.7.0",
"jest": "^26.4.1",
"jest": "^26.4.2",
"jsii-build-tools": "^0.0.0",
"jsii-calc": "^0.0.0",
"prettier": "^2.0.5",
"source-map": "^0.7.3",
"source-map-loader": "^1.0.2",
"ts-jest": "^26.2.0",
"typescript": "~3.9.7",
"wasm-loader": "^1.3.0",
"webpack": "^4.44.1",
Expand Down Expand Up @@ -76,7 +77,10 @@
],
"testEnvironment": "node",
"testMatch": [
"**/?(*.)+(spec|test).js"
]
"**/?(*.)+(spec|test).ts"
],
"transform": {
"\\.tsx?$": "ts-jest"
}
}
}
2 changes: 1 addition & 1 deletion packages/@jsii/runtime/test/playback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function createRecords(): string {
...process.execArgv,
require.resolve('jest/bin/jest'),
'--coverage=false',
'test/kernel.test.js',
'test/kernel.test.ts',
],
{
env: { ...process.env, JSII_RECORD: records, JSII_NOSTACK: '1' },
Expand Down
4 changes: 2 additions & 2 deletions packages/@jsii/spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"@types/jest": "^26.0.10",
"@types/node": "^10.17.28",
"eslint": "^7.7.0",
"jest": "^26.4.1",
"jest": "^26.4.2",
"jsii-build-tools": "^0.0.0",
"prettier": "^2.0.5",
"typescript": "~3.9.7",
"typescript-json-schema": "^0.42.0"
"typescript-json-schema": "^0.43.0"
},
"jest": {
"collectCoverage": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/codemaker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@types/jest": "^26.0.10",
"@types/node": "^10.17.28",
"eslint": "^7.7.0",
"jest": "^26.4.1",
"jest": "^26.4.2",
"prettier": "^2.0.5",
"typescript": "~3.9.7"
},
Expand Down
33 changes: 33 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2785,3 +2785,36 @@ export abstract class BurriedAnonymousObject {
}

import { StaticConsumer } from '@scope/jsii-calc-base-of-base';

/**
* Ensures we can override a dynamic property that was inherited.
*/
export class DynamicPropertyBearer {
public constructor(public valueStore: string) {}

public get dynamicProperty(): string {
return this.valueStore;
}

public set dynamicProperty(value: string) {
this.valueStore = value;
}
}
export class DynamicPropertyBearerChild extends DynamicPropertyBearer {
public constructor(public readonly originalValue: string) {
super(originalValue);
}

/**
* Sets `this.dynamicProperty` to the new value, and returns the old value.
*
* @param newValue the new value to be set.
*
* @returns the old value that was set.
*/
public overrideValue(newValue: string): string {
const oldValue = this.dynamicProperty;
this.dynamicProperty = newValue;
return oldValue;
}
}
Loading

0 comments on commit c392304

Please sign in to comment.