forked from kevinvandenbreemen/SwiftJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgentests.rb
More file actions
executable file
·327 lines (237 loc) · 9.69 KB
/
gentests.rb
File metadata and controls
executable file
·327 lines (237 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env ruby
#
# Generates bindings and implementions for test cde.
#
# This file is a bit of a mess frankly and you may need
# to alternate between running this and genhello.sh
# a few times to get new types to compile cleanly.
#
types = ["boolean", "byte", "char", "short", "int", "long", "float", "double", "String", "TestListener"];
swiftTypes = {
'boolean' => 'Bool',
'byte' => 'Int8',
'char' => 'UInt16',
'short' => 'Int16',
'int' => 'Int',
'long' => 'Int64',
'float' => 'Float',
'double' => 'Double',
'String' => 'String',
'TestListener' => 'SwiftHelloTest_TestListener'
}
arrayException = {
'int' => 'Int32'
}
referenceException = {
'boolean' => 'true',
'String' => '"123"',
'TestListener' => 'SwiftTestListener()'
}
referenceExceptionJava = {
'boolean' => 'true',
'String' => '"123"',
'TestListener' => 'new SwiftTestListener()'
}
java = File.open( "src/org/swiftjava/SwiftTest.java", "w" )
java2 = File.open( "src/com/johnholdsworth/swiftbindings/SwiftHelloTest.java", "w" )
java.puts( <<JAVA )
package org.swiftjava;
// auto generated by ../../../gentests.rb
import com.johnholdsworth.swiftbindings.SwiftHelloTest.TestListener;
public class SwiftTest {
public TestListener loopback;
public SwiftTest() {
}
JAVA
java2.puts( <<JAVA )
// auto generated by ../../../../gentests.rb
package com.johnholdsworth.swiftbindings;
import com.johnholdsworth.swiftbindings.SwiftHelloTypes.TextListener;
import com.johnholdsworth.swiftbindings.SwiftHelloTypes.ListenerMap;
import com.johnholdsworth.swiftbindings.SwiftHelloTypes.ListenerMapList;
public interface SwiftHelloTest {
public interface TestListener {
public void setLoopback( TestListener loopback );
JAVA
swift = File.open( "org_swiftjava/test_body.swift", "w" )
swift2 = File.open( "com_johnholdsworth/Sources/SwiftHelloTestImpl.swift", "w" )
swift2.puts( <<SWIFT )
// auto generated by ../../gentests.rb
import java_swift
import Foundation
public class SwiftTestListener: SwiftHelloTest_TestListenerBase {
var loopback: SwiftHelloTest_TestListener?
SWIFT
for type in types
java.puts( <<JAVA )
public static #{type} #{type}FieldStatic;
public #{type} #{type}Field;
public static #{type} #{type}MethodStatic( #{type} arg ) {
return arg;
}
public #{type} #{type}Method( #{type} arg ) {
return loopback != null ? loopback.#{type}Method( arg ) : arg;
}
public static #{type} #{type}ArrayFieldStatic[];
public #{type} #{type}ArrayField[];
public static #{type}[] #{type}ArrayMethodStatic( #{type} arg[] ) {
return arg;
}
public #{type}[] #{type}ArrayMethod( #{type} arg[] ) {
return loopback != null ? loopback.#{type}ArrayMethod( arg ) : arg;
}
public static #{type} #{type}2dArrayFieldStatic[][];
public #{type} #{type}2dArrayField[][];
public static #{type}[][] #{type}2dArrayMethodStatic( #{type} arg[][] ) {
return arg;
}
public #{type}[][] #{type}2dArrayMethod( #{type} arg[][] ) {
return loopback != null ? loopback.#{type}2dArrayMethod( arg ) : arg;
}
JAVA
java2.puts( <<JAVA )
public #{type} #{type}Method( #{type} arg );
public #{type}[] #{type}ArrayMethod( #{type} arg[] );
public #{type}[][] #{type}2dArrayMethod( #{type} arg[][] );
JAVA
swift.puts( <<SWIFT )
if true {
let reference: #{swiftTypes[type]} = #{referenceException[type] || '123'}
let referenceArray = [#{arrayException[type] || ''}(reference)]
let reference2dArray = [referenceArray]
SwiftTest.#{type}FieldStatic = reference
XCTAssertEqual( SwiftTest.#{type}FieldStatic, reference )
// instance.#{type}Field = reference
// XCTAssertEqual( instance.#{type}Field, reference )
SwiftTest.#{type}ArrayFieldStatic = referenceArray
XCTAssertEqual( SwiftTest.#{type}ArrayFieldStatic, referenceArray )
// instance.#{type}ArrayField = referenceArray
// XCTAssertEqual( instance.#{type}ArrayField, referenceArray )
SwiftTest.#{type}2dArrayFieldStatic = reference2dArray
XCTAssertEqual( SwiftTest.#{type}2dArrayFieldStatic[0], reference2dArray[0] )
// instance.#{type}2dArrayField = reference2dArray
// XCTAssertEqual( instance.#{type}2dArrayField[0], reference2dArray[0] )
// XCTAssertEqual( SwiftTest.#{type}MethodStatic( reference ), reference )
XCTAssertEqual( instance.#{type}Method( reference ), reference )
// XCTAssertEqual( SwiftTest.#{type}ArrayMethodStatic( referenceArray ), referenceArray )
XCTAssertEqual( instance.#{type}ArrayMethod( referenceArray ), referenceArray )
// XCTAssertEqual( SwiftTest.#{type}2dArrayMethodStatic( reference2dArray )[0], reference2dArray[0] )
XCTAssertEqual( instance.#{type}2dArrayMethod( reference2dArray )[0], reference2dArray[0] )
}
SWIFT
opt = type == "String" || type == "TestListener" ? "?" : ""
atype = arrayException[type] || swiftTypes[type]
swift2.puts( <<SWIFT )
override public func #{type}Method( arg: #{swiftTypes[type]}#{opt} ) -> #{swiftTypes[type]}#{opt} {
return loopback?.#{type}Method( arg: arg ) ?? arg
}
override public func #{type}ArrayMethod( arg: [#{atype}]? ) -> [#{atype}]? {
return loopback?.#{type}ArrayMethod( arg: arg ) ?? arg
}
override public func #{type}2dArrayMethod( arg: [[#{atype}]]? ) -> [[#{atype}]]? {
return loopback?.#{type}2dArrayMethod( arg: arg ) ?? arg
}
SWIFT
end
java2.puts( <<JAVA )
public ListenerMap testMap( ListenerMap arg );
public ListenerMapList testMapList( ListenerMapList arg );
}
public static class TestResponderAdapter implements TestListener {
public TestListener loopback;
public void setLoopback( TestListener loopback ) {
this.loopback = loopback;
}
JAVA
swift2.puts( <<SWIFT )
}
public class SwiftTestResponder {
static var tcount = 0
public func respond( to responder: SwiftHelloTest_TestListener ) {
SwiftTestResponder.tcount += 1
NSLog("Swift -> Java \\(SwiftTestResponder.tcount)...")
SWIFT
for type in types
java2.puts( <<JAVA )
public #{type} #{type}Method( #{type} arg ) {
return loopback != null ? loopback.#{type}Method( arg ) : arg;
}
public #{type}[] #{type}ArrayMethod( #{type} arg[] ) {
return loopback != null ? loopback.#{type}ArrayMethod( arg ) : arg;
}
public #{type}[][] #{type}2dArrayMethod( #{type} arg[][] ) {
return loopback != null ? loopback.#{type}2dArrayMethod( arg ) : arg;
}
JAVA
swift2.puts( <<SWIFT )
if true {
let reference: #{swiftTypes[type]} = #{referenceException[type] || '123'}
let referenceArray = [#{arrayException[type] || ''}(reference)]
let reference2dArray = [referenceArray]
let response = responder.#{type}Method( arg: reference )
let responseArray = responder.#{type}ArrayMethod( arg: referenceArray )!
_ = responder.#{type}2dArrayMethod( arg: reference2dArray )
SWIFT
if swiftTypes[type] != "SwiftHelloTest_TestListener" then
swift2.puts( <<SWIFT )
if response != reference {
NSLog("#{swiftTypes[type]}: \\(String(describing: response)) != \\(reference)")
}
if responseArray != referenceArray {
NSLog("#{swiftTypes[type]}: \\(responseArray) != \\(referenceArray)")
}
SWIFT
end
swift2.puts( <<SWIFT )
}
SWIFT
end
java2.puts( <<JAVA )
public ListenerMap testMap( ListenerMap arg ) {
return loopback != null ? loopback.testMap( arg ) : arg;
}
public ListenerMapList testMapList( ListenerMapList arg ) {
return loopback != null ? loopback.testMapList( arg ) : arg;
}
}
public static class SwiftTestListener extends TestResponderAdapter {
static int tcount = 0;
public void respond( TestListener responder ) {
tcount += 1;
System.out.println("Java -> Swift "+tcount+"...");
JAVA
for type in types
java2.puts( <<SWIFT )
if ( true ) {
#{type} reference = #{referenceExceptionJava[type] || '123'};
#{type} referenceArray[] = new #{type} [] {reference};
#{type} reference2dArray[][] = new #{type} [][] {referenceArray};
#{type} response = responder.#{type}Method( reference );
#{type} responseArray[] = responder.#{type}ArrayMethod( referenceArray );
#{type} response2dArray[][] = responder.#{type}2dArrayMethod( reference2dArray );
SWIFT
if swiftTypes[type] != "SwiftHelloTest_TestListener" && type != "String" then
java2.puts( <<SWIFT )
if ( response != reference ) {
System.out.println("#{swiftTypes[type]}: "+response+" != "+reference);
}
if ( responseArray[0] != referenceArray[0] ) {
System.out.println("#{swiftTypes[type]}: "+responseArray[0]+" != "+referenceArray[0]);
}
if ( response2dArray[0][0] != reference2dArray[0][0] ) {
System.out.println("#{swiftTypes[type]}: "+response2dArray[0][0]+" != "+reference2dArray[0][0]);
}
SWIFT
end
java2.puts( <<SWIFT )
}
SWIFT
end
swift2.puts( " }\n\n}\n" )
swift2.close()
swift.close()
java2.puts( " }\n\n }\n\n}\n" )
java2.close()
java.puts( "}\n" )
java.close()
system( "./genjar.sh && ./genswift.sh org/swiftjava/SwiftTest swiftjava.jar" );