Skip to content

Commit

Permalink
Ensure that conversions are defined for lambdas defined in struct
Browse files Browse the repository at this point in the history
In the case of Swift language, when lambda was defined in
a structure, then the code could not compile because of
missing conversion functions. Interestingly, that was not
the case for classes.

After investigation it turned out, that the root cause is
related to missing inclusion of SwiftLambdaConversion
template in SwiftStructConversion.

This change:
 - adds missing inclusion of lambda conversions in the
   mentioned template (SwiftStructConversion.mustache)
 - implements functional tests for Swift to check that
   the fix works as expected
 - implements functional tests for Dart and Android to
   also cover the tested case in these technologies
 - updates the reference files for smoke tests related
   to definition of nested fields in structs

Signed-off-by: Patryk Wrobel <[email protected]>
  • Loading branch information
pwrobeldev committed Oct 23, 2024
1 parent 57ec8d8 commit 90c5060
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2019 HERE Europe B.V.
* Copyright (C) 2016-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -160,4 +160,12 @@ public void callJavaOverloadedLambdaInCpp() {

assertEquals("42", result);
}

@Test
public void callJavaLambdaFromCppForLambdaDefinedInStruct() {
StructWithLambda.LambdaCallback callback = (String arg) -> arg;
String result = StructWithLambda.invokeCallback(callback);

assertEquals("some callback argument", result);
}
}
9 changes: 8 additions & 1 deletion functional-tests/functional/dart/test/Lambdas_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2016-2020 HERE Europe B.V.
// Copyright (C) 2016-2024 HERE Europe B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,4 +130,11 @@ void main() {

expect(result, isNull);
});
_testSuite.test("Call Dart lambda in C++ for lambda defined in struct", () {
final callback = (String? arg) => arg;

final result = StructWithLambda.invokeCallback(callback);

expect(result!, "some callback argument");
});
}
8 changes: 7 additions & 1 deletion functional-tests/functional/input/lime/Lambdas.lime
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2016-2019 HERE Europe B.V.
# Copyright (C) 2016-2024 HERE Europe B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,6 +61,12 @@ class LambdasDeclarationOrder {
}
}

struct StructWithLambda {
lambda LambdaCallback = (String?) -> String?

static fun invoke_callback(callback: LambdaCallback?): String?
}

interface LambdasInterface {
lambda TakeScreenshotCallback = (Blob?) -> Void

Expand Down
14 changes: 13 additions & 1 deletion functional-tests/functional/input/src/cpp/Lambdas.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2016-2019 HERE Europe B.V.
// Copyright (C) 2016-2024 HERE Europe B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,11 @@

#include "test/CallOverloadedLambda.h"
#include "test/ClassWithInternalLambda.h"
#include "test/StructWithLambda.h"
#include "test/Lambdas.h"

#include <functional>

namespace test
{

Expand Down Expand Up @@ -152,4 +155,13 @@ CallOverloadedLambda::invoke_overloaded_lambda(const OverloadedLambda& lambda,
return lambda(value);
}

std::optional<std::string>
StructWithLambda::invoke_callback(const std::optional<LambdaCallback>& callback) {
if (callback) {
return std::invoke(*callback, "some callback argument");
}

return {};
}

}
12 changes: 10 additions & 2 deletions functional-tests/functional/swift/Tests/LambdasTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2016-2019 HERE Europe B.V.
// Copyright (C) 2016-2024 HERE Europe B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -128,6 +128,13 @@ class LambdasTests: XCTestCase {
XCTAssertNil(result)
}

func testCallingSwiftLambdaFromCppForLambdaDefinedInStruct() {
let callback = { (arg: String?) in return arg }
let result = StructWithLambda.invokeCallback(callback: callback)

XCTAssertEqual(result, "some callback argument");
}

static var allTests = [
("testCppLambdaInSwift", testCppLambdaInSwift),
("testSwiftLambdaInCpp", testSwiftLambdaInCpp),
Expand All @@ -143,6 +150,7 @@ class LambdasTests: XCTestCase {
("testCppNullableLambdaInSwiftWithValue", testCppNullableLambdaInSwiftWithValue),
("testCppNullableLambdaInSwiftWithNil", testCppNullableLambdaInSwiftWithNil),
("testSwiftNullableLambdaInCppWithValue", testSwiftNullableLambdaInCppWithValue),
("testSwiftNullableLambdaInCppWithNil", testSwiftNullableLambdaInCppWithNil)
("testSwiftNullableLambdaInCppWithNil", testSwiftNullableLambdaInCppWithNil),
("testCallingSwiftLambdaFromCppForLambdaDefinedInStruct", testCallingSwiftLambdaFromCppForLambdaDefinedInStruct)
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{!!
!
! Copyright (C) 2016-2019 HERE Europe B.V.
! Copyright (C) 2016-2024 HERE Europe B.V.
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -95,6 +95,9 @@ Optionals
{{#structs}}
{{>swift/SwiftStructConversion}}{{!!
}}{{/structs}}
{{#lambdas}}
{{>swift/SwiftLambdaConversion}}{{!!
}}{{/lambdas}}
{{#enumerations}}
{{>swift/SwiftEnumConversion}}{{!!
}}{{/enumerations}}
Loading

0 comments on commit 90c5060

Please sign in to comment.