Skip to content

Commit 552e9ad

Browse files
committed
Added test for mapOf
1 parent 1430907 commit 552e9ad

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

src/tools/schema2kotlin.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ abstract class Abstract${particleName} : ${this.opts.wasm ? 'WasmParticleImpl' :
138138
}
139139
}
140140

141-
class KotlinGenerator implements ClassGenerator {
141+
export class KotlinGenerator implements ClassGenerator {
142142
fields: string[] = [];
143143
fieldVals: string[] = [];
144144
setFields: string[] = [];
@@ -201,10 +201,19 @@ class KotlinGenerator implements ClassGenerator {
201201

202202
}
203203

204-
private mapOf(items: string[], indent: number): string {
205-
if (items.length === 0) return `emptyMap()`;
204+
mapOf(items: string[]): string {
205+
switch (items.length) {
206+
case 0:
207+
return `emptyMap()`;
208+
case 1:
209+
return `mapOf(${items[0]})`;
210+
default:
211+
return `\
212+
mapOf(
213+
${this.leftPad(items.join(',\n'), 4)}
214+
)`;
215+
}
206216

207-
return `mapOf(${items.join(',\n' + ' '.repeat(indent))})`;
208217
}
209218

210219
createSchema(schemaHash: string): string {
@@ -213,8 +222,8 @@ class KotlinGenerator implements ClassGenerator {
213222
Schema(
214223
listOf(${schemaNames.join(',\n' + ' '.repeat(8))}),
215224
SchemaFields(
216-
singletons = ${this.mapOf(this.singletonSchemaFields, 12)},
217-
collections = ${this.mapOf(this.collectionSchemaFields, 12)}
225+
singletons = ${this.leftPad(this.mapOf(this.singletonSchemaFields), 8, true)},
226+
collections = ${this.leftPad(this.mapOf(this.collectionSchemaFields), 8, true)}
218227
),
219228
"${schemaHash}"
220229
)`;

src/tools/tests/goldens/generated-schemas.jvm.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,12 @@ class Gold_Data_Spec() : EntitySpec<Gold_Data> {
199199
val schema = Schema(
200200
listOf(),
201201
SchemaFields(
202-
singletons = mapOf("num" to FieldType.Number,
202+
singletons = mapOf(
203+
"num" to FieldType.Number,
203204
"txt" to FieldType.Text,
204205
"lnk" to FieldType.Text,
205-
"flg" to FieldType.Boolean),
206+
"flg" to FieldType.Boolean
207+
),
206208
collections = emptyMap()
207209
),
208210
"d8058d336e472da47b289eafb39733f77eadb111"

src/tools/tests/schema2kotlin-test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2020 Google Inc. All rights reserved.
4+
* This code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt
6+
* Code distributed by Google as part of this project is also
7+
* subject to an additional IP rights grant found at
8+
* http://polymer.github.io/PATENTS.txt
9+
*/
10+
11+
12+
import {assert} from '../../platform/chai-node.js';
13+
import {KotlinGenerator} from '../schema2kotlin.js';
14+
import {SchemaNode} from '../schema2graph.js';
15+
import {Schema} from '../../runtime/schema.js';
16+
17+
18+
describe('schema2wasm', () => {
19+
describe('kotlin-generator', () => {
20+
it('when no items are present, it creates an empty map', () => {
21+
const ktGen = new KotlinGenerator(new SchemaNode(new Schema([], {}), 'dummyNode'), {arg: '', _: []});
22+
23+
const actual = ktGen.mapOf([]);
24+
25+
assert.strictEqual('emptyMap()', actual);
26+
});
27+
it('when one item is present, it creates a single-line map', () => {
28+
const ktGen = new KotlinGenerator(new SchemaNode(new Schema([], {}), 'dummyNode'), {arg: '', _: []});
29+
30+
const actual = ktGen.mapOf([`"a" to "b"`]);
31+
32+
assert.strictEqual('mapOf("a" to "b")', actual);
33+
});
34+
it('when multiple items are present, it creates a multi-line map', () => {
35+
const ktGen = new KotlinGenerator(new SchemaNode(new Schema([], {}), 'dummyNode'), {arg: '', _: []});
36+
37+
const actual = ktGen.mapOf([`"a" to "b"`, `"b" to "c"`]);
38+
39+
assert.strictEqual(`\
40+
mapOf(
41+
"a" to "b",
42+
"b" to "c"
43+
)`, actual);
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)