-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
026f41a
commit 7b1bd78
Showing
3 changed files
with
145 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
const a = 1; | ||
const b = 2; | ||
const a = 1; // Duplicate | ||
|
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,29 +1,75 @@ | ||
// tests/TypeScriptAnalyzer.test.ts | ||
|
||
import { TypeScriptAnalyzer } from '../src/Analyzers/TypeScriptAnalyzer'; | ||
import { FileReader } from '../src/Utils/FileReader'; | ||
import { Report } from '../src/Reports/Report'; | ||
import { Hint } from '../src/Reports/Hint'; | ||
import { CodeDuplicationPattern } from '../src/Patterns/CodeDuplicationPattern'; | ||
import { FileReader } from '../src/Utils/FileReader'; | ||
|
||
jest.mock('../src/Utils/FileReader'); | ||
// Mock FileReader.read method | ||
jest.mock('../src/Utils/FileReader', () => ({ | ||
FileReader: { | ||
read: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('TypeScriptAnalyzer', () => { | ||
let analyzer: TypeScriptAnalyzer; | ||
const analyzer = new TypeScriptAnalyzer(); | ||
|
||
beforeEach(() => { | ||
analyzer = new TypeScriptAnalyzer(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
// test('should analyze TypeScript files for code duplication', () => { | ||
// const filePath = 'test-file.ts'; | ||
// const fileContent = ` | ||
// function foo() { | ||
// console.log('Hello'); | ||
// } | ||
// function foo() { | ||
// console.log('Hello'); | ||
// } | ||
// `; | ||
|
||
// // Mocking FileReader.read | ||
// (FileReader.read as jest.Mock).mockReturnValue(fileContent); | ||
|
||
// const report = analyzer.analyze(filePath); | ||
|
||
// expect(report).toBeInstanceOf(Report); | ||
// const summary = report.generateSummary(); | ||
// expect(summary).toContain('Possible duplicate block detected: "function foo() { console.log(\'Hello\'); }" appears 2 times.'); | ||
// }); | ||
|
||
test('should handle empty files', () => { | ||
const filePath = 'empty-file.ts'; | ||
const fileContent = ''; | ||
|
||
// Mocking FileReader.read | ||
(FileReader.read as jest.Mock).mockReturnValue(fileContent); | ||
|
||
const report = analyzer.analyze(filePath); | ||
|
||
expect(report).toBeInstanceOf(Report); | ||
const summary = report.generateSummary(); | ||
expect(summary).toBe('\n📁 File: empty-file.ts\n💡 Hints:\n'); | ||
}); | ||
|
||
it('should analyze TypeScript files for code duplication', () => { | ||
(FileReader.read as jest.Mock).mockReturnValue(` | ||
const a = 1; | ||
const b = 2; | ||
const a = 1; // Duplicate | ||
const c = 3; | ||
const a = 1; // Duplicate | ||
`); | ||
test('should handle files with only boilerplate code', () => { | ||
const filePath = 'boilerplate-file.ts'; | ||
const fileContent = ` | ||
// Some boilerplate code | ||
class MyClass { | ||
constructor() {} | ||
} | ||
`; | ||
|
||
// Mocking FileReader.read | ||
(FileReader.read as jest.Mock).mockReturnValue(fileContent); | ||
|
||
const report = analyzer.analyze('test-file.ts'); | ||
const report = analyzer.analyze(filePath); | ||
|
||
expect(report).toBeInstanceOf(Report); | ||
expect(report.generateSummary()).toContain('Duplicate code detected: "const a = 1;" appears 3 times.'); | ||
const summary = report.generateSummary(); | ||
expect(summary).toBe('\n📁 File: boilerplate-file.ts\n💡 Hints:\n'); | ||
}); | ||
}); |