|
| 1 | +package com.metaobjects.generator.template; |
| 2 | + |
| 3 | +import com.metaobjects.generator.GeneratorBase; |
| 4 | +import com.metaobjects.loader.MetaDataLoader; |
| 5 | +import com.metaobjects.loader.MetaDataLoaderTestBase; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | + |
| 17 | +/** |
| 18 | + * Oracle: the shared template-codegen corpus, whose {@code expected/} tree names the file each |
| 19 | + * scope writes for the full model. A per-entity scope narrowed to one object writes that |
| 20 | + * object's file only; per-package and per-model files stay whole. |
| 21 | + */ |
| 22 | +public class TemplateScopeGeneratorSelectionTest extends MetaDataLoaderTestBase { |
| 23 | + |
| 24 | + private static Path corpus() { |
| 25 | + return Path.of(System.getProperty("user.dir")).resolve("../../..") |
| 26 | + .resolve("fixtures/template-codegen-conformance").normalize(); |
| 27 | + } |
| 28 | + |
| 29 | + private List<String> run(String scope, String pattern, List<String> entities) throws Exception { |
| 30 | + Path corpus = corpus(); |
| 31 | + MetaDataLoader loader = initLoader(List.of(corpus.resolve("metadata/meta.shop.json").toUri())); |
| 32 | + Path out = Files.createTempDirectory("tmpl-select-java"); |
| 33 | + String template = switch (scope) { |
| 34 | + case ScopeWalk.PER_ENTITY -> "entity"; |
| 35 | + case ScopeWalk.PER_PACKAGE -> "package"; |
| 36 | + default -> "model"; |
| 37 | + }; |
| 38 | + TemplateScopeGenerator gen = new TemplateScopeGenerator(); |
| 39 | + gen.setArgs(Map.of( |
| 40 | + TemplateScopeGenerator.ARG_TEMPLATE, template, |
| 41 | + TemplateScopeGenerator.ARG_SCOPE, scope, |
| 42 | + TemplateScopeGenerator.ARG_OUTPUT_PATTERN, pattern, |
| 43 | + TemplateScopeGenerator.ARG_TEMPLATES_DIR, corpus.resolve("templates").toString(), |
| 44 | + GeneratorBase.ARG_OUTPUTDIR, out.toString())); |
| 45 | + gen.setEntities(entities); |
| 46 | + gen.execute(loader); |
| 47 | + try (Stream<Path> s = Files.walk(out)) { |
| 48 | + return s.filter(Files::isRegularFile) |
| 49 | + .map(p -> out.relativize(p).toString()) |
| 50 | + .sorted() |
| 51 | + .collect(Collectors.toList()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test public void perEntityScopeWritesOnlyTheSelectedObject() throws Exception { |
| 56 | + assertEquals(List.of("Product.txt"), run(ScopeWalk.PER_ENTITY, "{name}.txt", List.of("Product"))); |
| 57 | + } |
| 58 | + |
| 59 | + @Test public void perEntityScopeWithNoSelectionWritesEveryObject() throws Exception { |
| 60 | + assertEquals(List.of("Order.txt", "Product.txt"), run(ScopeWalk.PER_ENTITY, "{name}.txt", List.of())); |
| 61 | + } |
| 62 | + |
| 63 | + @Test public void wholeModelScopesStayWholeUnderASelection() throws Exception { |
| 64 | + assertEquals(List.of("_model.txt"), run(ScopeWalk.PER_MODEL, "_model.txt", List.of("Product"))); |
| 65 | + assertEquals(List.of("shop/_package.txt"), |
| 66 | + run(ScopeWalk.PER_PACKAGE, "{package}/_package.txt", List.of("Product"))); |
| 67 | + } |
| 68 | +} |
0 commit comments