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 pull request #43 from roypeled/refactor-code-parser-to-ast-from…
…-regex Refactor code parser to ast from regex
- Loading branch information
Showing
8 changed files
with
6,232 additions
and
4,243 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,32 @@ | ||
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 getObject(object: any) { | ||
const props = Object.getOwnPropertyNames(object); | ||
return `class Prototype { | ||
${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' : prop; | ||
result += ` | ||
${propName} = ${String(object[prop])} | ||
`; | ||
} | ||
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
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