Skip to content

Commit

Permalink
Fix legacy mocking failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Thevakumar-Luheerathan committed Nov 13, 2023
1 parent 90eacfe commit 3e192e3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,26 @@ public void generateMockFunctions(BLangPackage pkgNode) {
// Get the Mock Function map from the pkgNode
Map<String, String> mockFunctionMap = pkgNode.getTestablePkg().getMockFunctionNamesMap();

// Get the set of functions to generate
// Get all the imports symbols from the testable package
Set<String> mockFunctionSet = mockFunctionMap.keySet();
ArrayList<String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Running Tests with Coverage

function_mocking_legacy
[pass] testIntAdd
[pass] testIntMul


1 passing
2 passing
0 failing
0 skipped

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Running Tests with Coverage

function_mocking_legacy
[pass] testIntAdd
[pass] testIntMul


1 passing
2 passing
0 failing
0 skipped

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 3e192e3

Please sign in to comment.