-
Notifications
You must be signed in to change notification settings - Fork 1
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
28 changed files
with
105 additions
and
52 deletions.
There are no files selected for viewing
File renamed without changes.
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,6 @@ | ||
import { Language } from "../language"; | ||
|
||
export interface ICodeBlock { | ||
ctx: any; | ||
language: Language | ||
} |
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,5 +1,3 @@ | ||
import { Language } from "../language"; | ||
|
||
export interface IFile { | ||
filePath: string | undefined; | ||
} |
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,10 @@ | ||
import { BlockStatementsContext } from '../../../../grammars-v4/java/java20/Java20Parser'; | ||
import { IMethod } from '../../../interface/IMethod'; | ||
import { JavaMethod } from '../JavaMethod'; | ||
|
||
export class MethodLength { | ||
staticcalculate(method: IMethod): number { | ||
calculate(method: JavaMethod): number { | ||
const block_statements_ctx: BlockStatementsContext = method.ctx.methodBody().block().blockStatements() | ||
const method_length = block_statements_ctx.stop.line - block_statements_ctx.start.line + 1 | ||
const method_length = block_statements_ctx.stop!.line - block_statements_ctx.start.line + 1 | ||
return method_length; | ||
} | ||
} | ||
|
||
export function calculateMethodLength(method: JavaMethod): number { | ||
const block_statements_ctx: BlockStatementsContext = method.ctx.methodBody().block().blockStatements() | ||
const method_length = block_statements_ctx.stop.line - block_statements_ctx.start.line + 1 | ||
return method_length; | ||
} |
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 5 additions & 13 deletions
18
tests/python/smells/complex_conditional/complex_conditional.py
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,16 +1,8 @@ | ||
def getSimpleStmtements(self): | ||
class Simple_stmtListener(PythonParserListener): | ||
def __init__(self) -> None: | ||
self.statements = [] | ||
def enterSimple_stmt(self, ctx: PythonParser.Simple_stmtContext): | ||
self.statements.append(ctx.getText()) | ||
# return super().enterSimple_stmt(ctx) | ||
|
||
listener = Simple_stmtListener() | ||
walker = ParseTreeWalker() | ||
walker.walk(listener, self.ctx) | ||
|
||
def complexConditional(): | ||
if 1 > 2 and 3 > 4 or 8>5 or 5 > 7 and 5 > 6 or 9 > 5 and 5 > 6 or 9 > 5: | ||
return True | ||
|
||
return listener.statements | ||
def notComplexConditional(): | ||
if a > b: | ||
return True | ||
|
27 changes: 14 additions & 13 deletions
27
tests/python/smells/complex_conditional/complex_conditional.test.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 |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import { File } from '../../../../python/file'; | ||
import { describe, expect, test, beforeAll } from '@jest/globals'; | ||
import { ComplexConditional } from '../../../../python/smells/complex_conditional'; | ||
import { ComplexConditional } from '../../../../codemetrica/language/python/smell/ComplexConditional'; | ||
import { ParseSource } from '../../../../codemetrica/ParseSource'; | ||
import { PyFile, PyFunction } from '../../../../codemetrica/language/python'; | ||
|
||
describe('ComplexConditional Detection', () => { | ||
let functions: PyFunction[]; | ||
|
||
beforeAll(() => { | ||
|
||
const file = ParseSource.fromFileSync('./tests/python/smells/complex_conditional/complex_conditional.py') as PyFile; | ||
functions = file.getFunctions(); | ||
}); | ||
|
||
test('should detect complex conditionals in the file', () => { | ||
let file = new File('./codemetrica/tests/python/smells/complex_conditional/complex_conditional.py'); | ||
let expressions = file.getExpressions(); | ||
let expressions = functions[0].getExpressions(); | ||
const complexConditionals = expressions.filter(e => ComplexConditional.detect(e)); | ||
expect(complexConditionals.length).toBeGreaterThan(0); | ||
expect(complexConditionals.length).toEqual(1); | ||
}); | ||
|
||
// test('should not detect complex conditionals when none exist', () => { | ||
// const emptyFile = new File('./empty_file.py'); // Assuming this file has no complex conditionals | ||
// const emptyExpressions = emptyFile.getExpressions(); | ||
// const complexConditionals = emptyExpressions.filter(e => ComplexConditional.detect(e)); | ||
|
||
// expect(complexConditionals.length).toBe(0); | ||
// }); | ||
test('should not detect complex conditionals when none exist', () => { | ||
let expressions = functions[1].getExpressions(); | ||
const complexConditionals = expressions.filter(e => ComplexConditional.detect(e)); | ||
expect(complexConditionals.length).toEqual(0); | ||
}); | ||
}); |
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,41 @@ | ||
def process_order(order_id, user, items): | ||
print(f"Processing order {order_id} for user {user}") | ||
|
||
valid_items = [] | ||
for item in items: | ||
if 'id' in item and 'price' in item and item['price'] > 0: | ||
valid_items.append(item) | ||
else: | ||
print(f"Item {item} is invalid") | ||
|
||
subtotal = sum(item['price'] for item in valid_items) | ||
print(f"Subtotal for order {order_id}: {subtotal}") | ||
|
||
discount = 0 | ||
if subtotal > 100: | ||
discount = subtotal * 0.1 # 10% discount for orders over $100 | ||
print(f"Discount applied: {discount}") | ||
|
||
TAX_RATE = 0.08 # 8% tax | ||
tax = (subtotal - discount) * TAX_RATE | ||
print(f"Tax calculated: {tax}") | ||
|
||
total = subtotal - discount + tax | ||
print(f"Final total for order {order_id}: {total}") | ||
|
||
# 6. Update inventory | ||
for item in valid_items: | ||
print(f"Updating inventory for item {item['id']}") | ||
|
||
# 7. Payment processing (simplified) | ||
print(f"Charging {total} to user {user}") | ||
|
||
# 8. Logging the order | ||
print(f"Order {order_id} processed for user {user}") | ||
print("-" * 40) | ||
|
||
|
||
def notComplexConditional(): | ||
if a > b: | ||
return a | ||
|
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,21 @@ | ||
import { describe, expect, test, beforeAll } from '@jest/globals'; | ||
import { LongMethod } from '../../../../codemetrica/smell/LongMethod'; | ||
import { ParseSource } from '../../../../codemetrica/ParseSource'; | ||
import { PyFile, PyFunction } from '../../../../codemetrica/language/python'; | ||
|
||
describe('ComplexConditional Detection', () => { | ||
let functions: PyFunction[]; | ||
|
||
beforeAll(() => { | ||
const file = ParseSource.fromFileSync('./tests/python/smells/long_method/long_method.py') as PyFile; | ||
functions = file.getFunctions(); | ||
}); | ||
|
||
test('should detect complex conditionals in the file', () => { | ||
expect(LongMethod.detect(functions[0])).toBe(true); | ||
}); | ||
|
||
test('should not detect complex conditionals when none exist', () => { | ||
expect(LongMethod.detect(functions[1])).toBe(false); | ||
}); | ||
}); |