
package cz.habarta.typescript.generator;

import java.util.*;
import static org.junit.Assert.*;
import org.junit.Test;


public class CustomTypeMappingTest {

    @Test
    public void test() {
        final Settings settings = TestUtils.settings();
        settings.quotes = "'";
        settings.referencedFiles.add("../src/test/ts/my-custom-types.d.ts");
        settings.importDeclarations.add("import * as myModule from '../src/test/ts/my-module.d.ts'");
        settings.customTypeMappings.put("java.util.Date", "MyDate");
        settings.customTypeMappings.put("java.util.Calendar", "myModule.MyCalendar");
//        new TypeScriptGenerator(settings).generateTypeScript(Input.from(CustomTypesUsage.class), Output.to(new File("target/CustomTypeMappingTest.d.ts")));
        final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(CustomTypesUsage.class));
        assertTrue(output.contains("/// <reference path='../src/test/ts/my-custom-types.d.ts' />"));
        assertTrue(output.contains("import * as myModule from '../src/test/ts/my-module.d.ts';"));
        assertTrue(output.contains("date1: MyDate;"));
        assertTrue(output.contains("calendar1: myModule.MyCalendar;"));
    }

    private static class CustomTypesUsage {
        public Date date1;
        public Calendar calendar1;
    }

}

@@ -47,19 +47,339 @@
 ort 
-java.util.*
+com.fasterxml.jackson.core.JsonGenerator;%0Aimport com.fasterxml.jackson.databind.SerializerProvider;%0Aimport com.fasterxml.jackson.databind.annotation.JsonSerialize;%0Aimport com.fasterxml.jackson.databind.ser.std.StdSerializer;%0Aimport java.io.IOException;%0Aimport java.util.Calendar;%0Aimport java.util.Collections;%0Aimport java.util.Date
 ;%0Aim
@@ -1612,16 +1612,16 @@
  date1;%0A
-
         
@@ -1654,10 +1654,2156 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testEnumAsMap() throws Exception %7B%0A//        final ObjectMapper objectMapper = new ObjectMapper();%0A//        final String json = objectMapper.writeValueAsString(MyEnum.MY_FIRST_VALUE);%0A//        System.out.println(json);%0A%0A        final Settings settings = TestUtils.settings();%0A        settings.customTypeMappings = Collections.singletonMap(%22cz.habarta.typescript.generator.CustomTypeMappingTest$MyEnum%22, %22%7B code: string, definition: string %7D%22);%0A        final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(MyInterfUsingEnum.class));%0A        System.out.println(output);%0A        assertTrue(output.contains(%22someValue: %7B code: string, definition: string %7D%22));%0A    %7D%0A%0A    @JsonSerialize(using = CodedValueSerializer.class)%0A    public interface CodedValue %7B%0A        String getCode();%0A        String getDefinition();%0A    %7D%0A%0A    public enum MyEnum implements CodedValue %7B%0A%0A        MY_FIRST_VALUE(%22A0%22, %22Some description%22);%0A%0A        private final String code;%0A        private final String definition;%0A%0A        private MyEnum(String code, String definition) %7B%0A            this.code = code;%0A            this.definition = definition;%0A        %7D%0A%0A        @Override%0A        public String getCode() %7B%0A            return code;%0A        %7D%0A%0A        @Override%0A        public String getDefinition() %7B%0A            return definition;%0A        %7D%0A    %7D%0A%0A    public interface MyInterfUsingEnum %7B%0A        public MyEnum getSomeValue();%0A    %7D%0A%0A    public static class CodedValueSerializer extends StdSerializer%3CCodedValue%3E %7B%0A%0A        private static final long serialVersionUID = 1L;%0A%0A        public CodedValueSerializer() %7B%0A            super(CodedValue.class);%0A        %7D%0A%0A        public CodedValueSerializer(Class%3CCodedValue%3E t) %7B%0A            super(t);%0A        %7D%0A%0A        @Override%0A        public void serialize(CodedValue value, JsonGenerator gen, SerializerProvider provider) throws IOException %7B%0A            gen.writeStartObject();%0A            gen.writeStringField(%22code%22, value.getCode());%0A            gen.writeStringField(%22definition%22, value.getDefinition());%0A            gen.writeEndObject();%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Test for custom enum mapping as JSON object (#327)