Skip to content

Commit

Permalink
Adds Swift/C++ interoperality test for struct
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ios/chrome/test/swift_interop/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ source_set("swift_interop_tests_support_files") {
"pointer/pointer_returner.h",
"pointer/unique_ptr.cc",
"pointer/unique_ptr.h",
"struct/struct.h",
]
}

Expand All @@ -47,6 +48,7 @@ swift_source_set("swift_interop_tests") {
"pointer/object_passing_xctest.swift",
"pointer/pointer_xctest.swift",
"pointer/unique_ptr_xctest.swift",
"struct/struct_xctest.swift",
]
frameworks = [ "UIKit.framework" ]
deps = [
Expand Down
4 changes: 4 additions & 0 deletions ios/chrome/test/swift_interop/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ module Pointer {
requires cplusplus
}

module Struct {
header "struct/struct.h"
requires cplusplus
}
26 changes: 26 additions & 0 deletions ios/chrome/test/swift_interop/struct/struct.h
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_
25 changes: 25 additions & 0 deletions ios/chrome/test/swift_interop/struct/struct_xctest.swift
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))
}

}

0 comments on commit d829f36

Please sign in to comment.