Search in sources :

Example 1 with EnumConstantsExtension

use of cz.habarta.typescript.generator.ext.EnumConstantsExtension in project typescript-generator by vojtechhabarta.

the class EnumConstantsExtensionTest method testSorting.

@Test
public void testSorting() {
    final Settings settings = TestUtils.settings();
    settings.sortDeclarations = false;
    settings.newline = "\n";
    settings.outputFileType = TypeScriptFileType.implementationFile;
    settings.outputKind = TypeScriptOutputKind.global;
    settings.jsonLibrary = JsonLibrary.jackson2;
    settings.extensions.add(new EnumConstantsExtension());
    Assert.assertNotEquals(new TypeScriptGenerator(settings).generateTypeScript(Input.from(Emotions.class, Direction.class)), new TypeScriptGenerator(settings).generateTypeScript(Input.from(Direction.class, Emotions.class)));
    settings.sortDeclarations = true;
    Assert.assertEquals(new TypeScriptGenerator(settings).generateTypeScript(Input.from(Emotions.class, Direction.class)), new TypeScriptGenerator(settings).generateTypeScript(Input.from(Direction.class, Emotions.class)));
}
Also used : EnumConstantsExtension(cz.habarta.typescript.generator.ext.EnumConstantsExtension) Test(org.junit.Test)

Example 2 with EnumConstantsExtension

use of cz.habarta.typescript.generator.ext.EnumConstantsExtension in project typescript-generator by vojtechhabarta.

the class EnumConstantsExtensionTest method testInTypeScriptGenerator.

@Test
public void testInTypeScriptGenerator() {
    final Settings settings = new Settings();
    settings.newline = "\n";
    settings.outputFileType = TypeScriptFileType.implementationFile;
    settings.outputKind = TypeScriptOutputKind.global;
    settings.jsonLibrary = JsonLibrary.jackson2;
    settings.extensions.add(new EnumConstantsExtension());
    final String actual = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Direction.class));
    Assert.assertTrue(actual.contains("const Direction"));
    Assert.assertTrue(actual.contains("North"));
}
Also used : EnumConstantsExtension(cz.habarta.typescript.generator.ext.EnumConstantsExtension) Direction(cz.habarta.typescript.generator.EnumTest.Direction) Test(org.junit.Test)

Example 3 with EnumConstantsExtension

use of cz.habarta.typescript.generator.ext.EnumConstantsExtension in project typescript-generator by vojtechhabarta.

the class EnumConstantsExtensionTest method basicTest.

@Test
public void basicTest() {
    final List<String> lines = new ArrayList<>();
    final EmitterExtension.Writer writer = new EmitterExtension.Writer() {

        @Override
        public void writeIndentedLine(String line) {
            lines.add(line);
        }
    };
    final Settings settings = new Settings();
    settings.sortDeclarations = true;
    final TypeProcessor typeProcessor = new DefaultTypeProcessor();
    final Model model = new Jackson2Parser(settings, typeProcessor).parseModel(Direction.class);
    final TsModel tsModel = new ModelCompiler(settings, typeProcessor).javaToTypeScript(model);
    new EnumConstantsExtension().emitElements(writer, settings, false, tsModel);
    String indent = settings.indentString;
    Assert.assertEquals(7, lines.size());
    Assert.assertEquals("", lines.get(0));
    Assert.assertEquals("const Direction = {", lines.get(1));
    Assert.assertEquals(indent + "North: <Direction>\"North\",", lines.get(2));
    Assert.assertEquals(indent + "East: <Direction>\"East\",", lines.get(3));
    Assert.assertEquals(indent + "South: <Direction>\"South\",", lines.get(4));
    Assert.assertEquals(indent + "West: <Direction>\"West\",", lines.get(5));
    Assert.assertEquals("}", lines.get(6));
}
Also used : Jackson2Parser(cz.habarta.typescript.generator.parser.Jackson2Parser) EmitterExtension(cz.habarta.typescript.generator.emitter.EmitterExtension) ArrayList(java.util.ArrayList) TsModel(cz.habarta.typescript.generator.emitter.TsModel) EnumConstantsExtension(cz.habarta.typescript.generator.ext.EnumConstantsExtension) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) Model(cz.habarta.typescript.generator.parser.Model) TsModel(cz.habarta.typescript.generator.emitter.TsModel) Test(org.junit.Test)

Aggregations

EnumConstantsExtension (cz.habarta.typescript.generator.ext.EnumConstantsExtension)3 Test (org.junit.Test)3 Direction (cz.habarta.typescript.generator.EnumTest.Direction)1 ModelCompiler (cz.habarta.typescript.generator.compiler.ModelCompiler)1 EmitterExtension (cz.habarta.typescript.generator.emitter.EmitterExtension)1 TsModel (cz.habarta.typescript.generator.emitter.TsModel)1 Jackson2Parser (cz.habarta.typescript.generator.parser.Jackson2Parser)1 Model (cz.habarta.typescript.generator.parser.Model)1 ArrayList (java.util.ArrayList)1