-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Swift/C++ interoperality test for struct
The struct object in question contains a std::string property whose type does not support direct conversion in Swift. Test case verifies that Swift could still modify this property as long as it does not have to initialize the value to be assigned. Also, the object with such property could be passed into other C++ functions to be read and modified. Bug: N/A Change-Id: I16ed68f6e934b8abd21142ecb7467f25ee94d989 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4261808 Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org> Reviewed-by: Stepan Khapugin <stkhapugin@chromium.org> Auto-Submit: Ginny Huang <ginnyhuang@chromium.org> Cr-Commit-Position: refs/heads/main@{#1106720}
- Loading branch information
ginnnnnnny
authored and
Chromium LUCI CQ
committed
Feb 17, 2023
1 parent
9b0d890
commit d829f36
Showing
4 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,7 @@ module Pointer { | |
requires cplusplus | ||
} | ||
|
||
module Struct { | ||
header "struct/struct.h" | ||
requires cplusplus | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef IOS_CHROME_TEST_SWIFT_INTEROP_STRUCT_STRUCT_H_ | ||
#define IOS_CHROME_TEST_SWIFT_INTEROP_STRUCT_STRUCT_H_ | ||
|
||
#include <string> | ||
|
||
// Boolean, but for foo. | ||
struct Foolean { | ||
Foolean() {} | ||
|
||
bool value; | ||
std::string description; | ||
|
||
static std::string GetDescriptionForValue(bool value) { | ||
return std::string(value ? "true" : "false"); | ||
} | ||
}; | ||
|
||
inline bool IsFooleanValid(Foolean foo) { | ||
return foo.description == Foolean::GetDescriptionForValue(foo.value); | ||
} | ||
|
||
#endif // IOS_CHROME_TEST_SWIFT_INTEROP_STRUCT_STRUCT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import Struct | ||
import XCTest | ||
|
||
class StructTest: XCTestCase { | ||
|
||
func testStruct() throws { | ||
var goodFoo = Foolean() | ||
var badFoo = Foolean() | ||
|
||
// Test calling static method from struct. | ||
goodFoo.value = true | ||
goodFoo.description = Foolean.GetDescriptionForValue(true) | ||
badFoo.value = false | ||
badFoo.description = Foolean.GetDescriptionForValue(true) | ||
|
||
// Test passing object defined in C++ and initialized in Swift to top level C++ functions. | ||
XCTAssertTrue(IsFooleanValid(goodFoo)) | ||
XCTAssertFalse(IsFooleanValid(badFoo)) | ||
} | ||
|
||
} |