Skip to content

Commit

Permalink
added testcases from vshaxe/vshaxe#632
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Nov 19, 2024
1 parent bcb4112 commit 03bac6c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/refactor/refactor/RefactorExtractMethodTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,39 @@ class RefactorExtractMethodTest extends RefactorTestBase {
];
checkRefactor(RefactorExtractMethod, {fileName: "testcases/methods/Main.hx", posStart: 341, posEnd: 439}, edits, async);
}

function testCalculateTotal(async:Async) {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/methods/Main.hx", "total = calculateTotalExtract(items, total);\n", 569, 720, true),
makeInsertTestEdit("testcases/methods/Main.hx",
"function calculateTotalExtract(items:Array<Item>, total:Float):Float {\n"
+ "// Selected code block to extract\n"
+ "\t\tfor (item in items) {\n"
+ "\t\t\tvar price = item.price;\n"
+ "\t\t\tvar quantity = item.quantity;\n"
+ "\t\t\ttotal += price * quantity;\n"
+ "\t\t}\n"
+ "return total;\n"
+ "}\n",
741, true),
];
checkRefactor(RefactorExtractMethod, {fileName: "testcases/methods/Main.hx", posStart: 568, posEnd: 720}, edits, async);
}

function testProcessUser(async:Async) {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/methods/Main.hx", "processUserExtract(name, age);\n", 844, 980, true),
makeInsertTestEdit("testcases/methods/Main.hx",
"function processUserExtract(name:String, age:Int):Void {\n"
+ "// Selected code block to extract\n"
+ "\t\tvar greeting = \"Hello, \" + name;\n"
+ "\t\tif (age < 18) {\n"
+ "\t\t\tgreeting += \" (minor)\";\n"
+ "\t\t}\n"
+ "\t\ttrace(greeting);\n"
+ "}\n",
994, true),
];
checkRefactor(RefactorExtractMethod, {fileName: "testcases/methods/Main.hx", posStart: 843, posEnd: 980}, edits, async);
}
}
37 changes: 37 additions & 0 deletions testcases/methods/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,41 @@ class Main {
trace("hello 3");
trace("hello 4");
}

public function calculateTotal(items:Array<Item>):Float {
var total:Float = 0;

// Selected code block to extract
for (item in items) {
var price = item.price;
var quantity = item.quantity;
total += price * quantity;
}

return total;
}

public function processUser(user:User) {
var name:String = user.name;
var age:Int = user.age;

// Selected code block to extract
var greeting = "Hello, " + name;
if (age < 18) {
greeting += " (minor)";
}
trace(greeting);

// ...
}
}

typedef Item = {
var price:Float;
var quantity:Float;
}

typedef User = {
var name:String;
var age:Int;
}

0 comments on commit 03bac6c

Please sign in to comment.