-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Showing
6 changed files
with
109 additions
and
7 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
106 changes: 106 additions & 0 deletions
106
...twenty-server/src/modules/code-introspection/__tests__/code-introspection.service.spec.ts
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,106 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { CodeIntrospectionException } from 'src/modules/code-introspection/code-introspection.exception'; | ||
import { CodeIntrospectionService } from 'src/modules/code-introspection/code-introspection.service'; | ||
|
||
describe('CodeIntrospectionService', () => { | ||
let service: CodeIntrospectionService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CodeIntrospectionService], | ||
}).compile(); | ||
|
||
service = module.get<CodeIntrospectionService>(CodeIntrospectionService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('analyze', () => { | ||
it('should analyze a function declaration correctly', () => { | ||
const fileContent = ` | ||
function testFunction(param1: string, param2: number): void { | ||
console.log(param1, param2); | ||
} | ||
`; | ||
|
||
const result = service.analyze(fileContent); | ||
|
||
expect(result).toEqual([ | ||
{ name: 'param1', type: 'string' }, | ||
{ name: 'param2', type: 'number' }, | ||
]); | ||
}); | ||
|
||
it('should analyze an arrow function correctly', () => { | ||
const fileContent = ` | ||
const testArrowFunction = (param1: string, param2: number): void => { | ||
console.log(param1, param2); | ||
}; | ||
`; | ||
|
||
const result = service.analyze(fileContent); | ||
|
||
expect(result).toEqual([ | ||
{ name: 'param1', type: 'string' }, | ||
{ name: 'param2', type: 'number' }, | ||
]); | ||
}); | ||
|
||
it('should return an empty array for files without functions', () => { | ||
const fileContent = ` | ||
const x = 5; | ||
console.log(x); | ||
`; | ||
|
||
const result = service.analyze(fileContent); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should throw an exception for multiple function declarations', () => { | ||
const fileContent = ` | ||
function func1(param1: string) {} | ||
function func2(param2: number) {} | ||
`; | ||
|
||
expect(() => service.analyze(fileContent)).toThrow( | ||
CodeIntrospectionException, | ||
); | ||
expect(() => service.analyze(fileContent)).toThrow( | ||
'Only one function is allowed', | ||
); | ||
}); | ||
|
||
it('should throw an exception for multiple arrow functions', () => { | ||
const fileContent = ` | ||
const func1 = (param1: string) => {}; | ||
const func2 = (param2: number) => {}; | ||
`; | ||
|
||
expect(() => service.analyze(fileContent)).toThrow( | ||
CodeIntrospectionException, | ||
); | ||
expect(() => service.analyze(fileContent)).toThrow( | ||
'Only one arrow function is allowed', | ||
); | ||
}); | ||
|
||
it('should correctly analyze complex types', () => { | ||
const fileContent = ` | ||
function complexFunction(param1: string[], param2: { key: number }): Promise<boolean> { | ||
return Promise.resolve(true); | ||
} | ||
`; | ||
|
||
const result = service.analyze(fileContent); | ||
|
||
expect(result).toEqual([ | ||
{ name: 'param1', type: 'string[]' }, | ||
{ name: 'param2', type: '{ key: number; }' }, | ||
]); | ||
}); | ||
}); | ||
}); |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ntrospection/code-introspection.module.ts → ...ntrospection/code-introspection.module.ts
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