From 03bac6ca8392f8a35166db812c04cad19ce3c914 Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Wed, 20 Nov 2024 00:44:01 +0100 Subject: [PATCH] added testcases from vshaxe/vshaxe#632 --- .../refactor/RefactorExtractMethodTest.hx | 35 ++++++++++++++++++ testcases/methods/Main.hx | 37 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/test/refactor/refactor/RefactorExtractMethodTest.hx b/test/refactor/refactor/RefactorExtractMethodTest.hx index 3dc06bd..fdb4d23 100644 --- a/test/refactor/refactor/RefactorExtractMethodTest.hx +++ b/test/refactor/refactor/RefactorExtractMethodTest.hx @@ -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 = [ + makeReplaceTestEdit("testcases/methods/Main.hx", "total = calculateTotalExtract(items, total);\n", 569, 720, true), + makeInsertTestEdit("testcases/methods/Main.hx", + "function calculateTotalExtract(items:Array, 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 = [ + 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); + } } diff --git a/testcases/methods/Main.hx b/testcases/methods/Main.hx index 7f3a30f..d4f850f 100644 --- a/testcases/methods/Main.hx +++ b/testcases/methods/Main.hx @@ -27,4 +27,41 @@ class Main { trace("hello 3"); trace("hello 4"); } + + public function calculateTotal(items:Array):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; }