").append("\n *\n * ").append(linkToAccessor).append("\n *
\n");
- sb.append(
- " * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n");
- sb.append(
- " * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link "
- + MATCHERS + "#both(org.hamcrest.Matcher)}.\n");
- sb.append(" *\n");
- param.ifPresent(
- t -> Arrays.stream(t.split("\n")).forEach(l -> sb.append(" * @param ").append(l).append(".\n")));
- sb.append(" * @return the DSL to continue the construction of the matcher.\n");
- see.ifPresent(t -> sb.append(" * @see ").append(t).append("\n"));
- sb.append(" */");
- return sb.toString();
- }
-
- public static String buildImplementation(String declaration, String body) {
- return String.format("@Override\npublic %1$s {\n %2$s\n}\n", declaration, body.replaceAll("\\R", "\n" + " "));
- }
-
- public static String buildDsl(String javadoc, String declaration) {
- return String.format("%1$s\n%2$s;\n", javadoc.replaceAll("\\R", "\n"), declaration);
- }
-
- public static String buildDefaultDsl(AbstractFieldDescription fieldDescription, String javadoc, String declaration,
- String innerMatcher) {
- return String.format("%1$s\ndefault %2$s{\n return %3$s(%4$s);\n}", javadoc.replaceAll("\\R", "\n"),
- declaration, fieldDescription.getFieldName(), innerMatcher);
-
- }
-
-}
+/**
+ * Powerunit - A JDK1.8 test framework
+ * Copyright (C) 2014 Mathieu Boretti.
+ *
+ * This file is part of Powerunit
+ *
+ * Powerunit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Powerunit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Powerunit. If not, see
\n" + " *\n" + " * %2$s\n" + + " *
\n"
+ + " * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n"
+ + " * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n"
+ + " * (See for instance {@link " + MATCHERS + "#both(org.hamcrest.Matcher)}.\n" + " *\n" + "%3$s"
+ + " * @return the DSL to continue the construction of the matcher.\n" + "%4$s */";
+
+ public static BuilderDeclaration of(AbstractFieldDescription fieldDescription) {
+ return new Builder(fieldDescription);
+ }
+
+ public static class Builder implements BuilderDeclaration, BuilderJavadoc, BuilderImplementation {
+ private final AbstractFieldDescription fieldDescription;
+ private String declaration;
+ private String javadoc;
+
+ private Builder(AbstractFieldDescription fieldDescription) {
+ this.fieldDescription = fieldDescription;
+ }
+
+ @Override
+ public BuilderImplementation withJavaDoc(Optional \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */");
-
- assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.of("add"), Optional.empty(),
- Optional.empty())).is(
- "/**\n * Add a validation on the field `fn` add.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */");
-
- assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.of("p is p"),
- Optional.empty())).is(
- "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @param p is p.\n * @return the DSL to continue the construction of the matcher.\n */");
-
- assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.empty(),
- Optional.of("other"))).is(
- "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n * @see other\n */");
- }
-
- @Test(fastFail = false)
- public void testDefaultImplementationV1() {
- FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a")
- .withDefaultJavaDoc().havingImplementation("b");
- assertThat(fieldDSLMethod.asDSLMethod()).is(
- "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\n drm fn(a a);\n");
- assertThat(fieldDSLMethod.asImplementationMethod()).is("@Override\npublic drm fn(a a) {\n b\n}\n");
- }
-
- @Test(fastFail = false)
- public void testDefaultDSLV1() {
- FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a")
- .withDefaultJavaDoc().havingDefault("m(a)");
- assertThat(fieldDSLMethod.asDSLMethod()).is(
- "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\ndefault drm fn(a a){\n return fn(m(a));\n}");
- assertThat(fieldDSLMethod.asImplementationMethod()).is("");
- }
-
-}
+package ch.powerunit.extensions.matchers.provideprocessor.fields;
+
+import static org.mockito.Mockito.when;
+
+import java.util.Optional;
+
+import org.mockito.Mock;
+
+import ch.powerunit.Rule;
+import ch.powerunit.Test;
+import ch.powerunit.TestRule;
+import ch.powerunit.TestSuite;
+
+public class FieldDSLMethodBuilderTest implements TestSuite {
+ @Rule
+ public final TestRule rules = mockitoRule().around(before(this::prepareMock));
+
+ @Mock
+ private AbstractFieldDescription fieldDescription;
+
+ private void prepareMock() {
+ when(fieldDescription.getFieldName()).thenReturn("fn");
+ when(fieldDescription.getFieldAccessor()).thenReturn("fa");
+ when(fieldDescription.getFullyQualifiedNameEnclosingClassOfField()).thenReturn("fqnecof");
+ when(fieldDescription.getFieldType()).thenReturn("ft");
+ when(fieldDescription.getDefaultReturnMethod()).thenReturn("drm");
+ }
+
+ @Test
+ public void testBuildImplementationOneLine() {
+ assertThatBiFunction(FieldDSLMethodBuilder::buildImplementation, "name", "ligne1")
+ .is("@Override\npublic name {\n ligne1\n}\n");
+ }
+
+ @Test
+ public void testBuildImplementationTwoLine() {
+ assertThatBiFunction(FieldDSLMethodBuilder::buildImplementation, "name", "ligne1\nligne2")
+ .is("@Override\npublic name {\n ligne1\n ligne2\n}\n");
+ }
+
+ @Test
+ public void testBuildDsl() {
+ assertThatBiFunction(FieldDSLMethodBuilder::buildDsl, "javadoc", "declaration").is("javadoc\ndeclaration;\n");
+ }
+
+ @Test(fastFail = false)
+ public void testGetJavaDocFor() {
+ assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.empty(),
+ Optional.empty())).is(
+ "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */");
+
+ assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.of("add"), Optional.empty(),
+ Optional.empty())).is(
+ "/**\n * Add a validation on the field `fn` add.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */");
+
+ assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.of("p is p"),
+ Optional.empty())).is(
+ "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @param p is p.\n * @return the DSL to continue the construction of the matcher.\n */");
+
+ assertThat(FieldDSLMethodBuilder.getJavaDocFor(fieldDescription, Optional.empty(), Optional.empty(),
+ Optional.of("other"))).is(
+ "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n * @see other\n */");
+ }
+
+ @Test(fastFail = false)
+ public void testDefaultImplementationV1() {
+ FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a")
+ .withDefaultJavaDoc().havingImplementation("b");
+ assertThat(fieldDSLMethod.asDSLMethod()).is(
+ "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\n drm fn(a a);\n");
+ assertThat(fieldDSLMethod.asImplementationMethod()).is("@Override\npublic drm fn(a a) {\n b\n}\n");
+ }
+
+ @Test(fastFail = false)
+ public void testDefaultDSLV1() {
+ FieldDSLMethod fieldDSLMethod = FieldDSLMethodBuilder.of(fieldDescription).withDeclaration("a a")
+ .withDefaultJavaDoc().havingDefault("m(a)");
+ assertThat(fieldDSLMethod.asDSLMethod()).is(
+ "/**\n * Add a validation on the field `fn`.\n * \n *\n * {@link fqnecof#fa This field is accessed by using this approach}.\n * \n * In case method specifing a matcher on a fields are used several times, only the last setted matcher will be used. \n * When several control must be done on a single field, hamcrest itself provides a way to combine several matchers.\n * (See for instance {@link org.hamcrest.Matchers#both(org.hamcrest.Matcher)}.\n *\n * @return the DSL to continue the construction of the matcher.\n */\ndefault drm fn(a a){\n return fn(m(a));\n}");
+ assertThat(fieldDSLMethod.asImplementationMethod()).is("");
+ }
+
+}