From 3e192e397efd45accbff2aa6615f441805830e44 Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Mon, 13 Nov 2023 15:09:07 +0530 Subject: [PATCH] Fix legacy mocking failure --- .../compiler/desugar/MockDesugar.java | 18 +++++++++++++++++- .../MockTest-testFunctionMockingLegacy.txt | 3 ++- .../MockTest-testFunctionMockingLegacy.txt | 3 ++- .../legacy-function-mocking-tests/main.bal | 6 ++++++ .../modules/moduleB/main.bal | 19 +++++++++++++++++++ .../tests/test.bal | 13 +++++++++++++ 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/modules/moduleB/main.bal diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/MockDesugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/MockDesugar.java index 7aa3b1e6f8bc..5bd74cd596cd 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/MockDesugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/MockDesugar.java @@ -115,10 +115,26 @@ public void generateMockFunctions(BLangPackage pkgNode) { // Get the Mock Function map from the pkgNode Map mockFunctionMap = pkgNode.getTestablePkg().getMockFunctionNamesMap(); - // Get the set of functions to generate + // Get all the imports symbols from the testable package Set mockFunctionSet = mockFunctionMap.keySet(); ArrayList importsList = new ArrayList<>(); for (BLangImportPackage importPkg : pkgNode.getTestablePkg().getImports()) { + if (importPkg.symbol == null) { + continue; + } + if (!importPkg.symbol.toString().contains(testPackageSymbol)) { + importsList.add(importPkg.symbol.toString()); + } + } + + // Get all the imports from the current package + for (BLangImportPackage importPkg : pkgNode.getImports()) { + if (importPkg.symbol == null) { + continue; + } + if (importsList.contains(importPkg.symbol.toString())) { + continue; + } if (!importPkg.symbol.toString().contains(testPackageSymbol)) { importsList.add(importPkg.symbol.toString()); } diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/unix/MockTest-testFunctionMockingLegacy.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/MockTest-testFunctionMockingLegacy.txt index b808cc17140d..0a844a841be6 100644 --- a/tests/testerina-integration-test/src/test/resources/command-outputs/unix/MockTest-testFunctionMockingLegacy.txt +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/MockTest-testFunctionMockingLegacy.txt @@ -5,9 +5,10 @@ Running Tests with Coverage function_mocking_legacy [pass] testIntAdd + [pass] testIntMul - 1 passing + 2 passing 0 failing 0 skipped diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/MockTest-testFunctionMockingLegacy.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/MockTest-testFunctionMockingLegacy.txt index 814ae209234f..184ca4ac709c 100644 --- a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/MockTest-testFunctionMockingLegacy.txt +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/MockTest-testFunctionMockingLegacy.txt @@ -5,9 +5,10 @@ Running Tests with Coverage function_mocking_legacy [pass] testIntAdd + [pass] testIntMul - 1 passing + 2 passing 0 failing 0 skipped diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/main.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/main.bal index b4fa8ef0efe7..93d497a8f64e 100644 --- a/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/main.bal +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/main.bal @@ -13,7 +13,13 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. +import function_mocking_legacy.moduleB; function intAdd(int a, int b) returns int { return a + b; } + +public function intMul3Num(int a, int b, int c) returns int { + int result = moduleB:intMul(a, b); + return moduleB:intMul(result, c); +} diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/modules/moduleB/main.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/modules/moduleB/main.bal new file mode 100644 index 000000000000..d280ec2672d1 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/modules/moduleB/main.bal @@ -0,0 +1,19 @@ +// Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +public function intMul(int a, int b) returns int { + return a * b; +} diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/tests/test.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/tests/test.bal index 8a6f59255541..889172e23a30 100644 --- a/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/tests/test.bal +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/legacy-function-mocking-tests/tests/test.bal @@ -33,8 +33,21 @@ function intSubMock(int a, int b) returns int { return 0; } +@test:Mock { + moduleName: "function_mocking_legacy.moduleB", + functionName: "intMul" +} +function intMulMock(int a, int b) returns int { + return 1; +} + @test:Config {} function testIntAdd() { test:assertEquals(intAdd(3, 7), 5); test:assertEquals(moduleA:intSub(3, 7), 0); } + +@test:Config {} +function testIntMul() { + test:assertEquals(intMul3Num(3, 7, 2), 1); +}