|
| 1 | +package cz.habarta.typescript.generator; |
| 2 | + |
| 3 | +import cz.habarta.typescript.generator.compiler.ModelCompiler; |
| 4 | +import cz.habarta.typescript.generator.compiler.ModelCompiler.TransformationPhase; |
| 5 | +import cz.habarta.typescript.generator.compiler.ModelTransformer; |
| 6 | +import cz.habarta.typescript.generator.compiler.SymbolTable; |
| 7 | +import cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures; |
| 8 | +import cz.habarta.typescript.generator.emitter.TsModel; |
| 9 | +import cz.habarta.typescript.generator.parser.BeanModel; |
| 10 | +import cz.habarta.typescript.generator.parser.Jackson2Parser; |
| 11 | +import cz.habarta.typescript.generator.parser.Model; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import org.hamcrest.CoreMatchers; |
| 16 | +import org.junit.Assert; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class ExtensionTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testBeforeTsExtension() throws Exception { |
| 23 | + final Settings settings = TestUtils.settings(); |
| 24 | + |
| 25 | + settings.extensions.add(new Extension() { |
| 26 | + |
| 27 | + @Override |
| 28 | + public EmitterExtensionFeatures getFeatures() { |
| 29 | + return new EmitterExtensionFeatures(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public List<TransformerDefinition> getTransformers() { |
| 34 | + return Collections.singletonList(new TransformerDefinition(TransformationPhase.BeforeTsModel, new ModelTransformer() { |
| 35 | + |
| 36 | + @Override |
| 37 | + public TsModel transformModel(SymbolTable symbolTable, TsModel model) { |
| 38 | + return model; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Model transformModel(SymbolTable symbolTable, Model model) { |
| 43 | + List<BeanModel> beans = new ArrayList<>(model.getBeans()); |
| 44 | + |
| 45 | + BeanModel implementationBean = model.getBean(Implementation.class); |
| 46 | + BeanModel beanWithComments = implementationBean.withComments(Collections.singletonList("My new comment")); |
| 47 | + |
| 48 | + beans.remove(implementationBean); |
| 49 | + beans.add(beanWithComments); |
| 50 | + |
| 51 | + return new Model(beans, model.getEnums(), model.getJaxrsApplication()); |
| 52 | + } |
| 53 | + })); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + final Jackson2Parser jacksonParser = new Jackson2Parser(settings, new DefaultTypeProcessor()); |
| 58 | + final Model model = jacksonParser.parseModel(Implementation.class); |
| 59 | + final ModelCompiler modelCompiler = new TypeScriptGenerator(settings).getModelCompiler(); |
| 60 | + |
| 61 | + final TsModel result = modelCompiler.javaToTypeScript(model); |
| 62 | + |
| 63 | + Assert.assertEquals(1, result.getBean(Implementation.class).getComments().size()); |
| 64 | + Assert.assertThat(result.getBean(Implementation.class).getComments().get(0), CoreMatchers.containsString("My new comment")); |
| 65 | + } |
| 66 | + |
| 67 | + private static class Implementation { } |
| 68 | + |
| 69 | +} |
0 commit comments