Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafed committed Oct 16, 2024
1 parent 15b82f3 commit 1225f0c
Show file tree
Hide file tree
Showing 28 changed files with 105 additions and 52 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions codemetrica/interface/ICodeBlock.ts
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
}
2 changes: 0 additions & 2 deletions codemetrica/interface/IFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Language } from "../language";

export interface IFile {
filePath: string | undefined;
}
2 changes: 1 addition & 1 deletion codemetrica/interface/IFunction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICodeBlock } from './IFile';
import { ICodeBlock } from './ICodeBlock';
import { IParameter } from './IParameter';

export interface IFunction extends ICodeBlock{
Expand Down
11 changes: 2 additions & 9 deletions codemetrica/language/java/metric/MethodLength.ts
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;
}
4 changes: 2 additions & 2 deletions codemetrica/language/python/PyClass.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PyCodeBlock } from "./PyCodeBlock";
import { PyFunction } from "./PyFunction";
import { Class_defContext, Function_defContext } from "../../../grammars-v4/python/python3_12_1/PythonParser.js";
import PythonParserVisitor from "../../../grammars-v4/python/python3_12_1/PythonParserVisitor";
import { Class_defContext, Function_defContext } from "../../../grammars-v4/python/python3_12/PythonParser.js";
import PythonParserVisitor from "../../../grammars-v4/python/python3_12/PythonParserVisitor";
import { IClass } from "../../interface/IClass";

export class PyClass extends PyCodeBlock<Class_defContext> implements IClass {
Expand Down
7 changes: 4 additions & 3 deletions codemetrica/language/python/PyCodeBlock.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { AtomContext, Simple_stmtContext, Except_blockContext, Except_star_blockContext, ExpressionContext, Match_stmtContext, Star_expressionContext } from "../../../grammars-v4/python/python3_12_1/PythonParser";
import PythonParserVisitor from "../../../grammars-v4/python/python3_12_1/PythonParserVisitor";
import { AtomContext, Simple_stmtContext, Except_blockContext, Except_star_blockContext, ExpressionContext, Match_stmtContext, Star_expressionContext } from "../../../grammars-v4/python/python3_12/PythonParser";
import PythonParserVisitor from "../../../grammars-v4/python/python3_12/PythonParserVisitor";
import { ParserRuleContext } from 'antlr4';
import { Language } from "../../language";
import { ICodeBlock } from "../../interface/ICodeBlock";

export abstract class PyCodeBlock<T extends ParserRuleContext>{
export abstract class PyCodeBlock<T extends ParserRuleContext> implements ICodeBlock{
ctx: T
language: Language

Expand Down
4 changes: 2 additions & 2 deletions codemetrica/language/python/PyFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PythonParserVisitor from "../../../grammars-v4/python/python3_12_1/PythonParserVisitor";
import { Class_defContext, File_inputContext, Function_defContext } from "../../../grammars-v4/python/python3_12_1/PythonParser.js";
import PythonParserVisitor from "../../../grammars-v4/python/python3_12/PythonParserVisitor";
import { Class_defContext, File_inputContext, Function_defContext } from "../../../grammars-v4/python/python3_12/PythonParser.js";
import { PyCodeBlock } from "./PyCodeBlock";
import { PyClass } from "./PyClass";
import { PyFunction } from "./PyFunction";
Expand Down
2 changes: 1 addition & 1 deletion codemetrica/language/python/PyFunction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PyCodeBlock } from "./PyCodeBlock";
import { Function_defContext } from "../../../grammars-v4/python/python3_12_1/PythonParser";
import { Function_defContext } from "../../../grammars-v4/python/python3_12/PythonParser";
import { IParameter } from "../../interface/IParameter";
import { IFunction } from "../../interface/IFunction";
import { IMethod } from "../../interface/IMethod";
Expand Down
4 changes: 2 additions & 2 deletions codemetrica/language/python/PyParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import antlr4 from 'antlr4';
import PythonLexer from '../../../grammars-v4/python/python3_12_1/PythonLexer';
import PythonParser from '../../../grammars-v4/python/python3_12_1/PythonParser';
import PythonLexer from '../../../grammars-v4/python/python3_12/PythonLexer';
import PythonParser from '../../../grammars-v4/python/python3_12/PythonParser';

export function parsePythonSource(sourceCode: string) {
const inputStream = new antlr4.InputStream(sourceCode) as unknown as antlr4.CharStream;;
Expand Down
4 changes: 2 additions & 2 deletions codemetrica/language/python/smell/ComplexConditional.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PythonParserVisitor from "../../../../grammars-v4/python/python3_12_1/PythonParserVisitor";
import PythonParserVisitor from "../../../../grammars-v4/python/python3_12/PythonParserVisitor";
import { Thresholds } from '../../../Thresholds';
import { ExpressionContext } from "../../../../grammars-v4/python/python3_12_1/PythonParser";
import { ExpressionContext } from "../../../../grammars-v4/python/python3_12/PythonParser";

export class ComplexConditional {
static detect(ctx: ExpressionContext): boolean {
Expand Down
4 changes: 2 additions & 2 deletions codemetrica/smell/longMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { calculateMethodLength as javaCalculateMethodLength } from "../language/
import { MethodLength as PyMethodLength } from "../language/python/metric/MethodLength";
import { Thresholds } from "../Thresholds";

type MethodLengthCalculator = (method: IMethod | IFunction) => number;
type MethodLengthCalculator = (method: any) => number;

export class LongMethod {
static detect(method: IMethod | IFunction): boolean {
Expand All @@ -16,7 +16,7 @@ export class LongMethod {
case Language.JAVA:
return javaCalculateMethodLength;
default:
return () => 0;
throw new Error(`Unsupported language: ${language}`);
}
}

Expand Down
18 changes: 5 additions & 13 deletions tests/python/smells/complex_conditional/complex_conditional.py
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 tests/python/smells/complex_conditional/complex_conditional.test.ts
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);
});
});
41 changes: 41 additions & 0 deletions tests/python/smells/long_method/long_method.py
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

21 changes: 21 additions & 0 deletions tests/python/smells/long_method/long_method.test.ts
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);
});
});

0 comments on commit 1225f0c

Please sign in to comment.