Skip to content

Commit e8cea66

Browse files
committed
Add unit tests for @JavaImplementation
1 parent 77fbe08 commit e8cea66

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftJavaMacros
16+
import SwiftSyntax
17+
import SwiftSyntaxBuilder
18+
import SwiftSyntaxMacros
19+
import SwiftSyntaxMacrosTestSupport
20+
import XCTest
21+
22+
class JavaImplementationMacroTests: XCTestCase {
23+
static let javaImplementationMacros: [String: any Macro.Type] = [
24+
"JavaImplementation": JavaImplementationMacro.self,
25+
"JavaMethod": JavaMethodMacro.self,
26+
]
27+
28+
func testJNIIdentifierEscaping() throws {
29+
assertMacroExpansion(
30+
"""
31+
@JavaImplementation("org.swift.example.Hello_World")
32+
extension HelloWorld {
33+
@JavaMethod
34+
func test_method() -> Int32 {
35+
return 42
36+
}
37+
}
38+
""",
39+
expandedSource: """
40+
41+
extension HelloWorld {
42+
func test_method() -> Int32 {
43+
return 42
44+
}
45+
}
46+
47+
@_cdecl("Java_org_swift_example_Hello_1World_test_1method")
48+
func __macro_local_11test_methodfMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
49+
let obj = HelloWorld(javaThis: thisObj, environment: environment!)
50+
return obj.test_method()
51+
.getJNIValue(in: environment)
52+
}
53+
""",
54+
macros: Self.javaImplementationMacros
55+
)
56+
}
57+
58+
func testJNIIdentifierEscapingWithDots() throws {
59+
assertMacroExpansion(
60+
"""
61+
@JavaImplementation("com.example.test.MyClass")
62+
extension MyClass {
63+
@JavaMethod
64+
func simpleMethod() -> Int32 {
65+
return 1
66+
}
67+
}
68+
""",
69+
expandedSource: """
70+
71+
extension MyClass {
72+
func simpleMethod() -> Int32 {
73+
return 1
74+
}
75+
}
76+
77+
@_cdecl("Java_com_example_test_MyClass_simpleMethod")
78+
func __macro_local_12simpleMethodfMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
79+
let obj = MyClass(javaThis: thisObj, environment: environment!)
80+
return obj.simpleMethod()
81+
.getJNIValue(in: environment)
82+
}
83+
""",
84+
macros: Self.javaImplementationMacros
85+
)
86+
}
87+
88+
func testJNIIdentifierEscapingStaticMethod() throws {
89+
assertMacroExpansion(
90+
"""
91+
@JavaImplementation("org.example.Utils")
92+
extension Utils {
93+
@JavaMethod
94+
static func static_helper(environment: JNIEnvironment) -> String {
95+
return "hello"
96+
}
97+
}
98+
""",
99+
expandedSource: """
100+
101+
extension Utils {
102+
static func static_helper(environment: JNIEnvironment) -> String {
103+
return "hello"
104+
}
105+
}
106+
107+
@_cdecl("Java_org_example_Utils_static_1helper")
108+
func __macro_local_13static_helperfMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> String.JNIType {
109+
return Utils.static_helper(environment: environment)
110+
.getJNIValue(in: environment)
111+
}
112+
""",
113+
macros: Self.javaImplementationMacros
114+
)
115+
}
116+
117+
func testJNIIdentifierEscapingMultipleMethods() throws {
118+
assertMacroExpansion(
119+
"""
120+
@JavaImplementation("test.Class_With_Underscores")
121+
extension ClassWithUnderscores {
122+
@JavaMethod
123+
func method_one() -> Int32 {
124+
return 1
125+
}
126+
127+
@JavaMethod
128+
func method_two() -> Int32 {
129+
return 2
130+
}
131+
}
132+
""",
133+
expandedSource: """
134+
135+
extension ClassWithUnderscores {
136+
func method_one() -> Int32 {
137+
return 1
138+
}
139+
func method_two() -> Int32 {
140+
return 2
141+
}
142+
}
143+
144+
@_cdecl("Java_test_Class_1With_1Underscores_method_1one")
145+
func __macro_local_10method_onefMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
146+
let obj = ClassWithUnderscores(javaThis: thisObj, environment: environment!)
147+
return obj.method_one()
148+
.getJNIValue(in: environment)
149+
}
150+
151+
@_cdecl("Java_test_Class_1With_1Underscores_method_1two")
152+
func __macro_local_10method_twofMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) -> Int32.JNIType {
153+
let obj = ClassWithUnderscores(javaThis: thisObj, environment: environment!)
154+
return obj.method_two()
155+
.getJNIValue(in: environment)
156+
}
157+
""",
158+
macros: Self.javaImplementationMacros
159+
)
160+
}
161+
162+
func testJNIIdentifierEscapingVoidReturn() throws {
163+
assertMacroExpansion(
164+
"""
165+
@JavaImplementation("org.example.Processor")
166+
extension Processor {
167+
@JavaMethod
168+
func process_data() {
169+
// do nothing
170+
}
171+
}
172+
""",
173+
expandedSource: """
174+
175+
extension Processor {
176+
func process_data() {
177+
// do nothing
178+
}
179+
}
180+
181+
@_cdecl("Java_org_example_Processor_process_1data")
182+
func __macro_local_12process_datafMu_(environment: UnsafeMutablePointer<JNIEnv?>!, thisObj: jobject) {
183+
let obj = Processor(javaThis: thisObj, environment: environment!)
184+
return obj.process_data()
185+
}
186+
""",
187+
macros: Self.javaImplementationMacros
188+
)
189+
}
190+
}

0 commit comments

Comments
 (0)