Skip to content

Commit 5e47f35

Browse files
committed
jextract fix variable test
1 parent 2ae6bbf commit 5e47f35

File tree

1 file changed

+149
-123
lines changed

1 file changed

+149
-123
lines changed

Tests/JExtractSwiftTests/VariableImportTests.swift

Lines changed: 149 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -34,138 +34,164 @@ final class VariableImportTests {
3434
"""
3535

3636
@Test("Import: var counter: Int")
37-
func variable_() throws {
37+
func variable_int() throws {
3838
let st = Swift2JavaTranslator(
3939
javaPackage: "com.example.swift",
40-
swiftModuleName: "__FakeModule"
40+
swiftModuleName: "FakeModule"
4141
)
4242
st.log.logLevel = .error
4343

44-
try st.analyze(file: "Fake.swift", text: class_interfaceFile)
45-
46-
let identifier = "counterInt"
47-
let varDecl: ImportedVariable? =
48-
st.importedTypes.values.compactMap {
49-
$0.variables.first {
50-
$0.identifier == identifier
51-
}
52-
}.first
53-
guard let varDecl else {
54-
fatalError("Cannot find: \(identifier)")
55-
}
56-
57-
let output = CodePrinter.toString { printer in
58-
st.printVariableDowncallMethods(&printer, varDecl)
59-
}
60-
61-
assertOutput(
62-
dump: true,
63-
output,
64-
expected:
65-
"""
66-
// ==== --------------------------------------------------
67-
// counterInt
68-
private static class counterInt {
69-
public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
70-
/* -> */SWIFT_INT,
71-
SWIFT_POINTER
72-
);
73-
public static final MemorySegment ADDR_GET = __FakeModule.findOrThrow("g");
74-
public static final MethodHandle HANDLE_GET = Linker.nativeLinker().downcallHandle(ADDR_GET, DESC_GET);
75-
public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
76-
SWIFT_INT,
77-
SWIFT_POINTER
78-
);
79-
public static final MemorySegment ADDR_SET = __FakeModule.findOrThrow("s");
80-
public static final MethodHandle HANDLE_SET = Linker.nativeLinker().downcallHandle(ADDR_SET, DESC_SET);
81-
}
82-
/**
83-
* Function descriptor for:
84-
*
85-
*/
86-
public static FunctionDescriptor counterInt$get$descriptor() {
87-
return counterInt.DESC_GET;
88-
}
89-
/**
90-
* Downcall method handle for:
91-
*
92-
*/
93-
public static MethodHandle counterInt$get$handle() {
94-
return counterInt.HANDLE_GET;
95-
}
96-
/**
97-
* Address for:
98-
*
99-
*/
100-
public static MemorySegment counterInt$get$address() {
101-
return counterInt.ADDR_GET;
102-
}
103-
/**
104-
* Function descriptor for:
105-
*
106-
*/
107-
public static FunctionDescriptor counterInt$set$descriptor() {
108-
return counterInt.DESC_SET;
109-
}
110-
/**
111-
* Downcall method handle for:
112-
*
113-
*/
114-
public static MethodHandle counterInt$set$handle() {
115-
return counterInt.HANDLE_SET;
116-
}
117-
/**
118-
* Address for:
119-
*
120-
*/
121-
public static MemorySegment counterInt$set$address() {
122-
return counterInt.ADDR_SET;
123-
}
124-
/**
125-
* Downcall to Swift:
126-
*
127-
*/
128-
public static long getCounterInt(java.lang.foreign.MemorySegment self$) {
129-
var mh$ = counterInt.HANDLE_GET;
44+
try assertOutput(
45+
st, input: class_interfaceFile, .java,
46+
detectChunkByInitialLines: 7,
47+
expectedChunks:
48+
[
49+
"""
50+
private static class counterInt {
51+
public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
52+
/* -> */SWIFT_INT,
53+
SWIFT_POINTER
54+
);
55+
public static final MemorySegment ADDR_GET =
56+
FakeModule.findOrThrow("swiftjava_FakeModule_MySwiftClass_counterInt$1");
57+
public static final MethodHandle HANDLE_GET = Linker.nativeLinker().downcallHandle(ADDR_GET, DESC_GET);
58+
public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
59+
SWIFT_INT,
60+
SWIFT_POINTER
61+
);
62+
public static final MemorySegment ADDR_SET =
63+
FakeModule.findOrThrow("swiftjava_FakeModule_MySwiftClass_counterInt__$1");
64+
public static final MethodHandle HANDLE_SET = Linker.nativeLinker().downcallHandle(ADDR_SET, DESC_SET);
65+
}
66+
""",
67+
"""
68+
/**
69+
* Function descriptor for:
70+
* {@snippet lang=swift :
71+
* public var counterInt: Int
72+
* }
73+
*/
74+
public static FunctionDescriptor counterInt$get$descriptor() {
75+
return counterInt.DESC_GET;
76+
}
77+
""",
78+
"""
79+
/**
80+
* Downcall method handle for:
81+
* {@snippet lang=swift :
82+
* public var counterInt: Int
83+
* }
84+
*/
85+
public static MethodHandle counterInt$get$handle() {
86+
return counterInt.HANDLE_GET;
87+
}
88+
""",
89+
"""
90+
/**
91+
* Address for:
92+
* {@snippet lang=swift :
93+
* public var counterInt: Int
94+
* }
95+
*/
96+
public static MemorySegment counterInt$get$address() {
97+
return counterInt.ADDR_GET;
98+
}
99+
""",
100+
"""
101+
/**
102+
* Function descriptor for:
103+
* {@snippet lang=swift :
104+
* public var counterInt: Int
105+
* }
106+
*/
107+
public static FunctionDescriptor counterInt$set$descriptor() {
108+
return counterInt.DESC_SET;
109+
}
110+
"""
111+
,
112+
"""
113+
/**
114+
* Downcall method handle for:
115+
* {@snippet lang=swift :
116+
* public var counterInt: Int
117+
* }
118+
*/
119+
public static MethodHandle counterInt$set$handle() {
120+
return counterInt.HANDLE_SET;
121+
}
122+
""",
123+
"""
124+
/**
125+
* Address for:
126+
* {@snippet lang=swift :
127+
* public var counterInt: Int
128+
* }
129+
*/
130+
public static MemorySegment counterInt$set$address() {
131+
return counterInt.ADDR_SET;
132+
}
133+
""",
134+
"""
135+
/**
136+
* Downcall to Swift:
137+
* {@snippet lang=swift :
138+
* public var counterInt: Int
139+
* }
140+
*/
141+
public static long getCounterInt(java.lang.foreign.MemorySegment self$) {
142+
var mh$ = counterInt.HANDLE_GET;
143+
try {
144+
if (TRACE_DOWNCALLS) {
145+
traceDowncall(self$);
146+
}
147+
return (long) mh$.invokeExact(self$);
148+
} catch (Throwable ex$) {
149+
throw new AssertionError("should not reach here", ex$);
150+
}
151+
}
152+
""",
153+
"""
154+
/**
155+
* Downcall to Swift:
156+
* {@snippet lang=swift :
157+
* public var counterInt: Int
158+
* }
159+
*/
160+
public long getCounterInt() {
161+
return (long) getCounterInt($memorySegment());
162+
}
163+
""",
164+
"""
165+
/**
166+
* Downcall to Swift:
167+
* {@snippet lang=swift :
168+
* public var counterInt: Int
169+
* }
170+
*/
171+
public static void setCounterInt(long newValue, java.lang.foreign.MemorySegment self$) {
172+
var mh$ = counterInt.HANDLE_SET;
130173
try {
131-
if (TRACE_DOWNCALLS) {
132-
traceDowncall(self$);
133-
}
134-
return (long) mh$.invokeExact(self$);
174+
if (TRACE_DOWNCALLS) {
175+
traceDowncall(newValue, self$);
176+
}
177+
mh$.invokeExact(newValue, self$);
135178
} catch (Throwable ex$) {
136-
throw new AssertionError("should not reach here", ex$);
179+
throw new AssertionError("should not reach here", ex$);
137180
}
138-
}
139-
/**
140-
* Downcall to Swift:
141-
*
142-
*/
143-
public long getCounterInt() {
144-
return (long) getCounterInt($memorySegment());
145-
}
146-
/**
147-
* Downcall to Swift:
148-
*
149-
*/
150-
public static void setCounterInt(long newValue, java.lang.foreign.MemorySegment self$) {
151-
var mh$ = counterInt.HANDLE_SET;
152-
try {
153-
if (TRACE_DOWNCALLS) {
154-
traceDowncall(newValue, self$);
155-
}
156-
mh$.invokeExact(newValue, self$);
157-
} catch (Throwable ex$) {
158-
throw new AssertionError("should not reach here", ex$);
159181
}
160-
}
161-
/**
162-
* Downcall to Swift:
163-
*
164-
*/
165-
public void setCounterInt(long newValue) {
166-
setCounterInt(newValue, $memorySegment());
167-
}
168-
"""
182+
""",
183+
"""
184+
/**
185+
* Downcall to Swift:
186+
* {@snippet lang=swift :
187+
* public var counterInt: Int
188+
* }
189+
*/
190+
public void setCounterInt(long newValue) {
191+
setCounterInt(newValue, $memorySegment());
192+
}
193+
"""
194+
]
169195
)
170196
}
171197
}

0 commit comments

Comments
 (0)