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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
package org.zwobble.couscous.tests.backends.csharp;
import org.junit.Test;
import org.zwobble.couscous.ast.*;
import org.zwobble.couscous.ast.identifiers.Identifier;
import org.zwobble.couscous.ast.sugar.SwitchNode;
import org.zwobble.couscous.backends.csharp.CsharpSerializer;
import org.zwobble.couscous.tests.TestIds;
import org.zwobble.couscous.types.ScalarType;
import org.zwobble.couscous.types.Types;
import static org.junit.Assert.assertEquals;
import static org.zwobble.couscous.ast.ArrayNode.array;
import static org.zwobble.couscous.ast.AssignmentNode.assign;
import static org.zwobble.couscous.ast.CastNode.cast;
import static org.zwobble.couscous.ast.ConstructorCallNode.constructorCall;
import static org.zwobble.couscous.ast.EnumNode.declareEnum;
import static org.zwobble.couscous.ast.ExceptionHandlerNode.exceptionHandler;
import static org.zwobble.couscous.ast.ExpressionStatementNode.expressionStatement;
import static org.zwobble.couscous.ast.FieldAccessNode.fieldAccess;
import static org.zwobble.couscous.ast.FormalArgumentNode.formalArg;
import static org.zwobble.couscous.ast.FormalTypeParameterNode.formalTypeParameter;
import static org.zwobble.couscous.ast.IfStatementNode.ifStatement;
import static org.zwobble.couscous.ast.InstanceReceiver.instanceReceiver;
import static org.zwobble.couscous.ast.LiteralNode.literal;
import static org.zwobble.couscous.ast.LocalVariableDeclarationNode.localVariableDeclaration;
import static org.zwobble.couscous.ast.MethodCallNode.methodCall;
import static org.zwobble.couscous.ast.MethodCallNode.staticMethodCall;
import static org.zwobble.couscous.ast.MethodSignature.signature;
import static org.zwobble.couscous.ast.Operations.*;
import static org.zwobble.couscous.ast.ReturnNode.returns;
import static org.zwobble.couscous.ast.TernaryConditionalNode.ternaryConditional;
import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference;
import static org.zwobble.couscous.ast.ThrowNode.throwNode;
import static org.zwobble.couscous.ast.TryNode.tryStatement;
import static org.zwobble.couscous.ast.VariableDeclaration.var;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.ast.WhileNode.whileLoop;
import static org.zwobble.couscous.ast.sugar.SwitchCaseNode.switchCase;
import static org.zwobble.couscous.types.TypeParameter.typeParameter;
import static org.zwobble.couscous.util.ExtraLists.list;
public class CsharpSerializerTests {
@Test
public void integerLiterals() {
String output = serialize(literal(42));
assertEquals("42", output);
}
@Test
public void booleanLiterals() {
String trueOutput = serialize(literal(true));
assertEquals("true", trueOutput);
String falseOutput = serialize(literal(false));
assertEquals("false", falseOutput);
}
@Test
public void charLiteralsUseSingleQuotes() {
assertEquals("'b'", serialize(literal('b')));
assertEquals("'\\''", serialize(literal('\'')));
}
@Test
public void charLiteralsAreEscaped() {
assertEquals("'\\n'", serialize(literal('\n')));
assertEquals("'\\r'", serialize(literal('\r')));
assertEquals("'\\t'", serialize(literal('\t')));
}
@Test
public void stringLiteralsUseDoubleQuotes() {
String output = serialize(literal("blah"));
assertEquals("\"blah\"", output);
}
@Test
public void typeLiteralUsesTypeOfOperator() {
String output = serialize(literal(ScalarType.topLevel("com.example.Example")));
assertEquals("typeof(com.example.Example)", output);
}
@Test
public void typeLiteralUsesAtSymbolToEscapeReservedIdentifiers() {
String output = serialize(literal(ScalarType.topLevel("internal")));
assertEquals("typeof(@internal)", output);
}
@Test
public void typeLiteralDoesntUseAtSymbolForReservedTypeIdentifiers() {
String output = serialize(literal(ScalarType.topLevel("string")));
assertEquals("typeof(string)", output);
}
@Test
public void variableReferenceWritesIdentifier() {
String output = serialize(reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))));
assertEquals("x", output);
}
@Test
public void identifierIsEscapedWithAtSymbolIfItsAReservedWord() {
String output = serialize(reference(var(TestIds.ANY_ID, "internal", ScalarType.topLevel("X"))));
assertEquals("@internal", output);
}
@Test
public void thisReferenceUsesThisKeyword() {
String output = serialize(thisReference(ScalarType.topLevel("X")));
assertEquals("this", output);
}
@Test
public void emptyArraySpecifiesTypeOfArray() {
String output = serialize(array(ScalarType.topLevel("X"), list()));
assertEquals("new X[] {}", output);
}
@Test
public void arrayElementsAreSeparatedByComma() {
String output = serialize(array(ScalarType.topLevel("X"), list(literal(1), literal(2))));
assertEquals("new X[] {1, 2}", output);
}
@Test
public void assignmentSeparatesTargetAndValueWithEqualsSign() {
String output = serialize(assign(
reference(var(TestIds.ANY_ID, "x", Types.BOOLEAN)),
literal(true)));
assertEquals("x = true", output);
}
@Test
public void ternaryConditionalWritesConditionAndBranches() {
String output = serialize(ternaryConditional(literal(true), literal(1), literal(2)));
assertEquals("true ? 1 : 2", output);
}
@Test
public void staticMethodCallWithNoArgumentsWritesStaticReceiver() {
String output = serialize(staticMethodCall(
ScalarType.topLevel("X"),
"y",
list(),
ScalarType.topLevel("Y")));
assertEquals("X.y()", output);
}
@Test
public void methodCallWithNoArgumentsWritesReceiver() {
String output = serialize(methodCall(
reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))),
"y",
list(),
ScalarType.topLevel("Y")));
assertEquals("x.y()", output);
}
@Test
public void methodCallWithArguments() {
String output = serialize(methodCall(
reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))),
"y",
list(literal(1), literal(2)),
ScalarType.topLevel("Y")));
assertEquals("x.y(1, 2)", output);
}
@Test
public void methodCallWithTypeParameters() {
String output = serialize(methodCall(
instanceReceiver(reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X")))),
"y",
list(ScalarType.topLevel("Z")),
list(),
ScalarType.topLevel("Y"),
signature("y", list(typeParameter(Identifier.forType("X").method("y"), "T")), list(), ScalarType.topLevel("Y"))));
assertEquals("x.y<Z>()", output);
}
@Test
public void constructorCallWithNoArguments() {
String output = serialize(constructorCall(
ScalarType.topLevel("X"),
list()));
assertEquals("new X()", output);
}
@Test
public void constructorCallWithArguments() {
String output = serialize(constructorCall(
ScalarType.topLevel("X"),
list(literal(1), literal(2))));
assertEquals("new X(1, 2)", output);
}
@Test
public void prefixExpression() {
String output = serialize(not(
reference(var(TestIds.ANY_ID, "x", Types.BOOLEAN))));
assertEquals("!x", output);
}
@Test
public void infixOperation() {
String output = serialize(integerAdd(literal(1), literal(2)));
assertEquals("1 + 2", output);
}
@Test
public void instanceOfIsWrittenUsingIsOperator() {
String output = serialize(instanceOf(
reference(var(TestIds.ANY_ID, "x", Types.OBJECT)),
ScalarType.topLevel("X")));
assertEquals("x is X", output);
}
@Test
public void infixOperationIsBracketedWhenNecessary() {
assertEquals("1 + 2 * 3", serialize(integerAdd(literal(1), integerMultiply(literal(2), literal(3)))));
assertEquals("(1 + 2) * 3", serialize(integerMultiply(integerAdd(literal(1), literal(2)), literal(3))));
}
@Test
public void fieldAccessSeparatesReceiverAndNameWithDot() {
String output = serialize(fieldAccess(
reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))),
"y",
ScalarType.topLevel("Y")));
assertEquals("x.y", output);
}
@Test
public void staticFieldAccessSeparatesReceiverAndNameWithDot() {
String output = serialize(fieldAccess(
ScalarType.topLevel("X"),
"y",
ScalarType.topLevel("Y")));
assertEquals("X.y", output);
}
@Test
public void castIsWrittenWithParenthesisedType() {
String output = serialize(cast(
reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))),
ScalarType.topLevel("Y")));
assertEquals("(Y) x", output);
}
@Test
public void returnStatementUsesReturnStatement() {
String output = serialize(returns(literal(true)));
assertEquals("return true;\n", output);
}
@Test
public void noValueIsSpecifiedWhenReturningUnit() {
String output = serialize(returns(LiteralNode.UNIT));
assertEquals("return;\n", output);
}
@Test
public void throwStatementIncludesValue() {
String output = serialize(throwNode(reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X")))));
assertEquals("throw x;\n", output);
}
@Test
public void expressionStatementWritesExpression() {
String output = serialize(expressionStatement(literal(true)));
assertEquals("true;\n", output);
}
@Test
public void localVariableDeclarationCanDeclareVariable() {
String output = serialize(localVariableDeclaration(
TestIds.ANY_ID,
"x",
ScalarType.topLevel("X"),
literal("[value]")));
assertEquals("X x = \"[value]\";\n", output);
}
@Test
public void builtinTypesAreNotMangled() {
String output = serialize(localVariableDeclaration(
TestIds.ANY_ID,
"x",
ScalarType.topLevel("string"),
literal("[value]")));
assertEquals("string x = \"[value]\";\n", output);
}
@Test
public void ifElseStatementPrintsBothBranches() {
String output = serialize(ifStatement(
literal(true),
list(returns(literal(1))),
list(returns(literal(2)))));
assertEquals("if (true) {\n return 1;\n} else {\n return 2;\n}\n", output);
}
@Test
public void elseBranchIsNotGeneratedIfEmpty() {
String output = serialize(ifStatement(
literal(true),
list(returns(literal(1))),
list()));
assertEquals("if (true) {\n return 1;\n}\n", output);
}
@Test
public void elseIfBranchesDontCauseNesting() {
String output = serialize(ifStatement(
literal(true),
list(returns(literal(1))),
list(ifStatement(
literal(false),
list(returns(literal(2))),
list(returns(literal(3)))))));
assertEquals("if (true) {\n return 1;\n} else if (false) {\n return 2;\n} else {\n return 3;\n}\n", output);
}
@Test
public void switchStatement() {
Node node = new SwitchNode(
literal(1),
list(
switchCase(literal(1), list(returns(literal(10)))),
switchCase(list(returns(literal(0))))
)
);
String output = serialize(node);
assertEquals("switch (1) {\n case 1:\n return 10;\n default:\n return 0;\n}\n", output);
}
@Test
public void whileLoopPrintsConditionAndBody() {
String output = serialize(whileLoop(
literal(true),
list(returns(literal(1)))));
assertEquals("while (true) {\n return 1;\n}\n", output);
}
@Test
public void statementBlockPrintsStatementsSurroundedByCurlyBraces() {
String output = serialize(new StatementBlockNode(list(
returns(literal(1))
)));
// TODO: remove leading space
assertEquals(" {\n return 1;\n}\n", output);
}
@Test
public void tryStatementCanHaveBodyAndMultipleHandlers() {
VariableDeclaration varX = var(TestIds.variable("x"), "x", ScalarType.topLevel("X"));
VariableDeclaration varY = var(TestIds.variable("y"), "y", ScalarType.topLevel("Y"));
String output = serialize(tryStatement(
list(returns(literal(1))),
list(
exceptionHandler(varX, list(returns(reference(varX)))),
exceptionHandler(varY, list(returns(reference(varY))))),
list()));
assertEquals("try {\n return 1;\n} catch (X x) {\n return x;\n} catch (Y y) {\n return y;\n}\n", output);
}
@Test
public void tryStatementCanHaveFinallyBody() {
String output = serialize(tryStatement(
list(),
list(),
list(returns(literal(1)))));
assertEquals("try {\n} finally {\n return 1;\n}\n", output);
}
@Test
public void methodCanHaveVoidReturnType() {
String output = serialize(MethodNode.staticMethod("nothing")
.returns(ScalarType.topLevel("void"))
.build());
assertEquals("public static void nothing() {\n}\n", output);
}
@Test
public void instanceMethodHasNoStaticKeword() {
String output = serialize(MethodNode.builder("nothing")
.returns(ScalarType.topLevel("void"))
.build());
assertEquals("public void nothing() {\n}\n", output);
}
@Test
public void methodWithReturnType() {
MethodNode methodNode = MethodNode.staticMethod("nothing")
.returns(ScalarType.topLevel("X"))
.build();
String output = serialize(methodNode);
assertEquals("public static X nothing() {\n}\n", output);
}
@Test
public void methodWithArguments() {
MethodNode methodNode = MethodNode.staticMethod("nothing")
.argument(formalArg(var(TestIds.variable("x"), "x", ScalarType.topLevel("X"))))
.argument(formalArg(var(TestIds.variable("y"), "y", ScalarType.topLevel("Y"))))
.returns(ScalarType.topLevel("void"))
.build();
String output = serialize(methodNode);
assertEquals("public static void nothing(X x, Y y) {\n}\n", output);
}
@Test
public void methodHasSerializedBody() {
MethodNode method = MethodNode.staticMethod("nothing")
.returns(ScalarType.topLevel("void"))
.statement(returns(literal(true)))
.build();
String output = serialize(method);
assertEquals("public static void nothing() {\n return true;\n}\n", output);
}
@Test
public void methodWithTypeParameters() {
MethodNode methodNode = MethodNode.staticMethod("nothing")
.typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "T"))
.typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "U"))
.returns(ScalarType.topLevel("void"))
.build();
String output = serialize(methodNode);
assertEquals("public static void nothing<T, U>() {\n}\n", output);
}
@Test
public void classIsInNamespace() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.build();
String output = serialize(classNode);
assertEquals("namespace com.example {\n internal class Example {\n }\n}\n", output);
}
@Test
public void namespacePartsInNamespaceDeclarationAreMangledInIfReserved() {
ClassNode classNode = ClassNode.builder("com.example.internal.Example")
.build();
String output = serialize(classNode);
assertEquals("namespace com.example.@internal {\n internal class Example {\n }\n}\n", output);
}
@Test
public void namespacePartsInNamespaceReferenceAreMangledInIfReserved() {
String output = serialize(localVariableDeclaration(
TestIds.ANY_ID,
"x",
ScalarType.topLevel("com.example.internal.Example"),
literal("[value]")));
assertEquals("com.example.@internal.Example x = \"[value]\";\n", output);
}
@Test
public void classWithTypeParametersIsSplitIntoStaticAndInstanceClasses() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.addTypeParameter("T")
.addTypeParameter("U")
.field("x", ScalarType.topLevel("X"))
.staticField("y", ScalarType.topLevel("Y"))
.build();
String output = serialize(classNode);
assertEquals(
"namespace com.example {" +
"\n internal static class Example {\n internal static Y y;\n }\n}" +
"\nnamespace com.example {" +
"\n internal class Example<T, U> {\n internal X x;\n }\n}\n", output);
}
@Test
public void classWithSuperTypes() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.addSuperType("com.example.Base")
.build();
String output = serialize(classNode);
assertEquals("namespace com.example {\n internal class Example : com.example.Base {\n }\n}\n", output);
}
@Test
public void staticConstructorHasSerializedBody() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.staticConstructor(list(expressionStatement(literal(true))))
.build();
String output = serialize(classNode);
assertEquals(
"namespace com.example {\n internal class Example {\n static Example() {\n true;\n }\n }\n}\n", output);
}
@Test
public void constructorHasSerializedBody() {
ConstructorNode constructor = ConstructorNode.constructor(
list(formalArg(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X")))),
list(expressionStatement(literal(true))));
ClassNode classNode = ClassNode.builder("com.example.Example")
.constructor(constructor)
.build();
String output = serialize(classNode);
assertEquals(
"namespace com.example {\n internal class Example {\n internal Example(X x) {\n true;\n }\n }\n}\n", output);
}
@Test
public void classWithFields() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.staticField("x", ScalarType.topLevel("X"))
.field("y", ScalarType.topLevel("Y"))
.build();
String output = serialize(classNode);
assertEquals("namespace com.example {\n internal class Example {\n internal static X x;\n internal Y y;\n }\n}\n", output);
}
@Test
public void interfaceIsInNamespace() {
Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example")).buildInterface();
String output = serialize(node);
assertEquals("namespace com.example {\n internal interface Example {\n }\n}\n", output);
}
@Test
public void interfaceWithTypeParameters() {
Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example"))
.addTypeParameter("T")
.addTypeParameter("U")
.buildInterface();
String output = serialize(node);
assertEquals("namespace com.example {\n internal interface Example<T, U> {\n }\n}\n", output);
}
@Test
public void interfaceWithSuperTypes() {
Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example"))
.addSuperType("com.example.Base")
.buildInterface();
String output = serialize(node);
assertEquals("namespace com.example {\n internal interface Example : com.example.Base {\n }\n}\n", output);
}
@Test
public void interfaceWithMethod() {
MethodNode method = MethodNode.builder("get").isAbstract().returns(ScalarType.topLevel("X")).build();
Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example"))
.method(method)
.buildInterface();
String output = serialize(node);
assertEquals("namespace com.example {\n internal interface Example {\n X get();\n }\n}\n", output);
}
@Test
public void interfaceWithMethodWithTypeParameters() {
MethodNode method = MethodNode.staticMethod("nothing")
.typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "T"))
.typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "U"))
.returns(ScalarType.topLevel("void"))
.build();
Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example"))
.method(method)
.buildInterface();
String output = serialize(node);
assertEquals("namespace com.example {\n internal interface Example {\n void nothing<T, U>();\n }\n}\n", output);
}
@Test
public void enumWithSimpleValues() {
Node node = declareEnum(ScalarType.topLevel("com.example.Example"), list("ONE", "TWO"));
String output = serialize(node);
assertEquals("namespace com.example {\n internal enum Example {\n ONE, TWO\n }\n}\n", output);
}
@Test
public void arrayTypeUsesSquareBrackets() {
String output = serialize(formalArg(var(TestIds.ANY_ID, "x", Types.array(ScalarType.topLevel("string")))));
assertEquals("string[] x", output);
}
private String serialize(Node node) {
return CsharpSerializer.serialize(node);
}
}
|