From efb644e6a94dc2a9c95bd93ef39865545c87f89b Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 23 Sep 2023 21:43:56 +0300 Subject: [PATCH] Add the create method for the TestCommandFactory. --- .../tests/factories/TestCommandFactory.java | 42 +++++++++- .../factories/TestCommandFactorySpec.groovy | 76 ++++++++++++++++++- 2 files changed, 114 insertions(+), 4 deletions(-) diff --git a/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java b/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java index d4d3274..4bf3b69 100644 --- a/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java +++ b/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java @@ -2,8 +2,12 @@ package space.kklochko.simple_jbdd.tests.factories; import space.kklochko.simple_jbdd.annotations.Given; import space.kklochko.simple_jbdd.annotations.Then; +import space.kklochko.simple_jbdd.annotations.Title; import space.kklochko.simple_jbdd.annotations.When; import space.kklochko.simple_jbdd.tests.Test; +import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand; +import space.kklochko.simple_jbdd.tests.commands.SimpleTestCommand; +import space.kklochko.simple_jbdd.tests.commands.decorators.BlockDecorator; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @@ -11,9 +15,41 @@ import java.util.Map; import java.util.TreeMap; public class TestCommandFactory { - public void create(T input) { - Class aClass = input.getClass(); - Map test_methods = this.getTestMethods(aClass); + public AbstractTestCommand create(Test input) { + Class aClass = input.getClass(); + Map test_methods = this.getTestMethods(aClass); + + AbstractTestCommand aTestCommand = createTest(aClass, input); + + if(test_methods.containsKey("Then")) { + Method then = test_methods.get("Then"); + String label = then.getAnnotation(Then.class).value(); + aTestCommand = new BlockDecorator(label, aTestCommand, then, "Then"); + } + + if(test_methods.containsKey("When")) { + Method when = test_methods.get("When"); + String label = when.getAnnotation(When.class).value(); + aTestCommand = new BlockDecorator(label, aTestCommand, when, "When"); + } + + if(test_methods.containsKey("Given")) { + Method when = test_methods.get("Given"); + String label = when.getAnnotation(Given.class).value(); + aTestCommand = new BlockDecorator(label, aTestCommand, when, "Given"); + } + + return aTestCommand; + } + + protected AbstractTestCommand createTest(Class aClass, Test input) { + Title aTitle = aClass.getAnnotation(Title.class); + String title = aClass.getName(); + + if(aTitle != null) + title = aTitle.value(); + + return new SimpleTestCommand(title, input); } protected Map getTestMethods(Class aClass) { diff --git a/src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy b/src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy index baba5db..32d5a03 100644 --- a/src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy +++ b/src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy @@ -1,12 +1,22 @@ package space.kklochko.simple_jbdd.tests.factories +import space.kklochko.simple_jbdd.test_examples.tests.SimpleGivenWhenThenTest import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTest -import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory; +import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTestWithoutTitle +import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory +import spock.lang.Narrative; import spock.lang.Specification +import spock.lang.Subject +import spock.lang.Title +@Narrative("""The factory must generate a test command, so +those tests check if factory generate them right. +""") +@Title("Unit test for TestCommandFactory") class TestCommandFactorySpec extends Specification { def "If the map of methods have one test block."() { given: "I have a TestCommandFactory object" + @Subject def factory = new TestCommandFactory(); and: "I also have a SimpleThenTest object" @@ -18,5 +28,69 @@ class TestCommandFactorySpec extends Specification { then: "the map size is 1 method" 1 == map.size() } + + def "createTest() sets the title to a class name if no Title annotation."() { + given: "I have a test factory" + @Subject + def factory = new TestCommandFactory() + + and: "I have a test class without Title" + def testClass = new SimpleThenTestWithoutTitle() + + when: "I build a test" + def test = factory.create(testClass) + + then: "The test have title as the test class" + testClass.class.getName() == test.getLabels()['Title'] + } + + def "createTest() sets the title to a value of the Title annotation."() { + given: "I have a test factory" + @Subject + def factory = new TestCommandFactory() + + and: "I have a test class with Title" + def testClass = new SimpleThenTest() + + and: "and the title of the annotation" + def titleClass = space.kklochko.simple_jbdd.annotations.Title.class + def title = testClass.getClass().getAnnotation(titleClass).value() + + when: "I build a test" + def test = factory.create(testClass) + + then: "The test have title as the test class" + title == test.getLabels()['Title'] + } + + def "createTest() build a Test with only Then."() { + given: "I have a test factory" + @Subject + def factory = new TestCommandFactory() + + and: "I have a test class with Title" + def testClass = new SimpleThenTest() + + when: "I build a test" + def test = factory.create(testClass) + + then: "The test have title and then method" + ['Title', 'Then'].toSet() == test.getLabels().keySet() + } + + def "createTest() build a Test with Given-When-Then."() { + given: "I have a test factory" + @Subject + def factory = new TestCommandFactory() + + and: "I have a test class with Title and Given-When-Then" + def testClass = new SimpleGivenWhenThenTest() + + when: "I build a test" + def test = factory.create(testClass) + + then: "The test have title and then method" + ['Title', 'Given', 'When', 'Then'].toSet() == test.getLabels().keySet() + } }