1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// State tracker 17class Tracker { 18 static lambdaCalled = false; 19 static blockExecuted = false; 20 static reset() { 21 Tracker.lambdaCalled = false; 22 Tracker.blockExecuted = false; 23 } 24} 25 26// Test class 27class TestClass { 28 // Method with mandatory lambda 29 withMandatoryLambda(a: number, f: () => void) { 30 f(); 31 } 32 33 // Method with optional lambda 34 withOptionalLambda(a?: number, f?: () => void) { 35 f?.(); 36 } 37} 38 39// Test function 40function testFunction(a: number, f: () => void) { 41 f(); 42} 43 44function main() { 45 /* Scenario 1: Class method with mandatory parameter */ 46 Tracker.reset(); 47 new TestClass().withMandatoryLambda(1, () => { 48 Tracker.lambdaCalled = true; 49 }) 50 { 51 Tracker.blockExecuted = true; 52 } 53 assertTrue(Tracker.lambdaCalled, "Scenario 1: Lambda should be executed"); 54 assertTrue(Tracker.blockExecuted, "Scenario 1: Block should be executed"); 55 56 /* Scenario 2: Class method with optional parameter */ 57 Tracker.reset(); 58 new TestClass().withOptionalLambda(1, () => { 59 Tracker.lambdaCalled = true; 60 }) 61 { 62 Tracker.blockExecuted = true; 63 } 64 assertTrue(Tracker.lambdaCalled, "Scenario 2: Lambda should be executed"); 65 assertTrue(Tracker.blockExecuted, "Scenario 2: Block should be executed"); 66 67 /* Scenario 3: Function call with mandatory parameter */ 68 Tracker.reset(); 69 testFunction(1, () => { 70 Tracker.lambdaCalled = true; 71 }) 72 { 73 Tracker.blockExecuted = true; 74 } 75 assertTrue(Tracker.lambdaCalled, "Scenario 3: Lambda should be executed"); 76 assertTrue(Tracker.blockExecuted, "Scenario 3: Block should be executed"); 77 78 /* Scenario 4: No lambda + block */ 79 Tracker.reset(); 80 new TestClass().withOptionalLambda(1) // No lambda parameter 81 { 82 Tracker.blockExecuted = true; 83 } 84 assertTrue(Tracker.blockExecuted, "Boundary Test: Block should be executed"); 85 assertEQ(Tracker.lambdaCalled, false) 86 87 /* Scenario 5: only trailing lambda */ 88 Tracker.reset(); 89 new TestClass().withOptionalLambda() // No lambda parameter 90 { 91 Tracker.lambdaCalled = true; 92 } 93 assertTrue(Tracker.lambdaCalled, "Boundary Test: Lambda should be executed"); 94 assertEQ(Tracker.blockExecuted, false) 95} 96