forked from NagRock/ts-mockito
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
7 changed files
with
217 additions
and
101 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
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
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
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 |
---|---|---|
@@ -1,17 +1,34 @@ | ||
|
||
export class ObjectPropertyCodeRetriever { | ||
public get(object: any, propertyName: string): string { | ||
const descriptor = Object.getOwnPropertyDescriptor(object, propertyName); | ||
if (!descriptor) { | ||
// property is defined in prototype but has no descriptor (it comes from abstract class and was not override) | ||
return ""; | ||
public static getObject(object: any) { | ||
const props = Object.getOwnPropertyNames(object); | ||
return `class ${object.constructor.name} { | ||
${props.map(prop => { | ||
let result = ''; | ||
const descriptor = Object.getOwnPropertyDescriptor(object, prop); | ||
if (descriptor.get) { | ||
result += ` | ||
${descriptor.get.toString()} | ||
`; | ||
} | ||
if (descriptor.set) { | ||
result += ` | ||
${descriptor.set.toString()} | ||
`; | ||
} | ||
if (!descriptor.get && !descriptor.set && typeof object[prop] === 'function') { | ||
const propName = prop === 'constructor' ? 'mock_constructor' : ''; | ||
const fnStr = String(object[prop]); | ||
result += ` | ||
${propName ? `${propName}=` : ''}${fnStr} | ||
`; | ||
} | ||
return result; | ||
}).join(` | ||
`)} | ||
} | ||
const accessorsCodes = []; | ||
if (descriptor.get) { | ||
accessorsCodes.push(descriptor.get.toString()); | ||
} | ||
if (descriptor.set) { | ||
accessorsCodes.push(descriptor.set.toString()); | ||
} | ||
return accessorsCodes.join(" ") || String(object[propertyName]); | ||
`; | ||
} | ||
|
||
} |
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
Oops, something went wrong.