diff --git a/pom.xml b/pom.xml
index c8f36be..c21cd7b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
io.github.imagineDevit
giwt
- 0.1.8-SNAPSHOT
+ 0.1.9-SNAPSHOT
A JUnit based library that allow developers to write unit test on GIVEN-WHEN-THEN format
@@ -14,7 +14,7 @@
17
17
UTF-8
- 0.0.8
+ 0.0.9
giwt
@@ -62,11 +62,6 @@
junit-platform-testkit
1.9.2
-
- com.squareup
- javapoet
- 1.13.0
-
diff --git a/src/main/java/io/github/imagineDevit/giwt/TestCase.java b/src/main/java/io/github/imagineDevit/giwt/TestCase.java
index 487c871..6b8c4e3 100644
--- a/src/main/java/io/github/imagineDevit/giwt/TestCase.java
+++ b/src/main/java/io/github/imagineDevit/giwt/TestCase.java
@@ -157,6 +157,7 @@ protected WhenStmt whens(String message, WhenFn.S fn) {
this.whenFn = fn;
return new WhenStmt<>(this);
}
+
/**
* Adds a When statement to the current test case with the provided message and a function.
*
@@ -212,12 +213,12 @@ protected void run() {
this.givenRFn.run();
}
- this.andGivenFns.forEach(this.state::apply);
+ this.andGivenFns.forEach(this.state::consumeValue);
try {
if (this.whenFn != null) {
if (this.whenFn instanceof WhenFn.F, ?> gfn) {
- this.result = this.state.mapToResult((WhenFn.F) gfn);
+ this.result = TestCaseResult.of(((WhenFn.F) gfn).apply(this.state.value()));
} else if (this.whenFn instanceof WhenFn.R rfn) {
rfn.run();
} else if (this.whenFn instanceof WhenFn.C> cfn) {
@@ -230,7 +231,10 @@ protected void run() {
this.result = TestCaseResult.ofErr(e);
}
+ System.out.print(Utils.listExpectations());
+
this.thenFns.forEach(fn -> fn.accept(this.result));
+ System.out.println();
}
public record GivenStmt(TestCase testCase) {
diff --git a/src/main/java/io/github/imagineDevit/giwt/TestCaseContext.java b/src/main/java/io/github/imagineDevit/giwt/TestCaseContext.java
index a05faa5..3d943fb 100644
--- a/src/main/java/io/github/imagineDevit/giwt/TestCaseContext.java
+++ b/src/main/java/io/github/imagineDevit/giwt/TestCaseContext.java
@@ -4,9 +4,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
-import java.util.function.Consumer;
-import java.util.function.Function;
-import java.util.function.Supplier;
import static io.github.imagineDevit.giwt.core.utils.TextUtils.blue;
import static io.github.imagineDevit.giwt.core.utils.TextUtils.bold;
@@ -107,7 +104,7 @@ protected GCtx() {
*
* @param value the state value
*/
- public void setState(T value) {
+ protected void setState(T value) {
context.put(STATE, TestCaseCtxState.of(value));
}
@@ -146,16 +143,6 @@ protected WCtx toWCtx() {
return new WCtx<>(context);
}
- /**
- * Apply an action on the context state
- *
- * @param consumer the action to apply
- */
- public void applyOnState(Consumer consumer) {
- consumer.accept(getState().value());
- }
-
-
}
public static non-sealed class WCtx extends TestCaseContext {
@@ -165,40 +152,6 @@ protected WCtx(Map context) {
setResult(TestCaseCtxResult.empty());
}
- /**
- * Apply a function to the context state and set the context result with the returned value
- *
- * @param mapper the function to apply to the state
- */
- public void mapToResult(Function mapper) {
- setResult(getState().toResult(mapper));
- }
-
- /**
- * Apply an action on the context state
- *
- * @param consumer the action to apply
- */
- public void applyOnState(Consumer consumer) {
- consumer.accept(getState().value());
- }
-
- /**
- * Set the context state as the context result
- */
- public void setStateAsResult() {
- setResult(getState().toResult(t -> (R) t));
- }
-
- /**
- * Supply the context result
- *
- * @param supplier the supplier to get the result value
- */
- public void supplyResult(Supplier supplier) {
- setResult(TestCaseCtxResult.of(supplier.get()));
- }
-
/**
* Set the context result
*
diff --git a/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxResult.java b/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxResult.java
index 77b5074..652ddbe 100644
--- a/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxResult.java
+++ b/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxResult.java
@@ -7,18 +7,18 @@
/**
* A result of a test case with context
*
- * @param the result type
+ * @param the result type
* @author Henri Joel SEDJAME
* @since 0.1.0
*/
-public class TestCaseCtxResult extends ATestCaseResult {
+public class TestCaseCtxResult extends ATestCaseResult {
/**
* Private constructor that accepts a value
*
* @param value the value to be wrapped in the TestCaseCtxResult
*/
- private TestCaseCtxResult(T value) {
+ private TestCaseCtxResult(R value) {
super(value);
}
@@ -37,7 +37,7 @@ private TestCaseCtxResult(Exception e) {
* @param value the value to be wrapped in the TestCaseCtxResult
* @return a new TestCaseCtxResult instance with the given value
*/
- protected static TestCaseCtxResult of(T value) {
+ protected static TestCaseCtxResult of(R value) {
return new TestCaseCtxResult<>(value);
}
@@ -46,8 +46,8 @@ protected static TestCaseCtxResult of(T value) {
*
* @return a new TestCaseCtxResult instance with a null value
*/
- protected static TestCaseCtxResult empty() {
- return new TestCaseCtxResult<>(null);
+ protected static TestCaseCtxResult empty() {
+ return new TestCaseCtxResult<>((R) null);
}
/**
@@ -57,24 +57,36 @@ protected static TestCaseCtxResult empty() {
* @return a new TestCaseCtxResult instance with the given Exception
*/
protected static TestCaseCtxResult ofErr(Exception e) {
- return new TestCaseCtxResult<>(e);
+ return new TestCaseCtxResult<>(Objects.requireNonNull(e));
}
/**
* Method to get the result of the TestCaseCtxResult
*
- * @return a TestCaseResult instance with the value or Exception of this TestCaseCtxResult
+ * @return a TestCaseResult instance with the value or exception of this TestCaseCtxResult
*/
- protected TestCaseResult result() {
- return Objects.requireNonNull(value, "Result value is Null")
- .ok()
- .map(ResultValue.Ok::getValue)
- .map(TestCaseResult::of)
- .orElseGet(() ->
- TestCaseResult.ofErr(
- value.err().map(ResultValue.Err::getError).orElseThrow()
+ protected TestCaseResult result() {
+ return Objects.requireNonNull(value, "Result value is Null").ok()
+ .map(v -> TestCaseResult.of(v.getValue()))
+ .orElseGet(() -> TestCaseResult.ofErr(
+ value.err()
+ .map(ResultValue.Err::getError)
+ .orElseThrow(() -> new IllegalStateException("Unexpected error occurred while mapping result to TestCaseResult"))
)
);
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ TestCaseCtxResult> that = (TestCaseCtxResult>) o;
+ return Objects.equals(value, that.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(value);
+ }
+
}
\ No newline at end of file
diff --git a/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxState.java b/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxState.java
index 44c2b89..7d4307e 100644
--- a/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxState.java
+++ b/src/main/java/io/github/imagineDevit/giwt/TestCaseCtxState.java
@@ -2,8 +2,7 @@
import io.github.imagineDevit.giwt.core.ATestCaseState;
-import java.util.function.Function;
-import java.util.function.UnaryOperator;
+import java.util.Objects;
/**
* A state of a test case with context
@@ -42,29 +41,6 @@ protected static TestCaseCtxState empty() {
return new TestCaseCtxState<>(null);
}
- /**
- * Applies a function to the current value and returns a new TestCaseCtxState with the result.
- *
- * @param mapper the function to apply to the current value
- * @return a new TestCaseCtxState with the result of the function
- */
- protected TestCaseCtxState map(UnaryOperator mapper) {
- return new TestCaseCtxState<>(mapper.apply(this.value()));
- }
-
- /**
- * Applies a function to the current value and returns a new TestCaseCtxResult with the result.
- *
- * @param mapper the function to apply to the current value
- * @return a new TestCaseCtxResult with the result of the function
- */
- protected TestCaseCtxResult toResult(Function mapper) {
- try {
- return TestCaseCtxResult.of(mapper.apply(this.value()));
- } catch (Exception e) {
- return TestCaseCtxResult.ofErr(e);
- }
- }
/**
* Returns the current value.
@@ -74,4 +50,17 @@ protected TestCaseCtxResult toResult(Function mapper) {
protected T value() {
return value;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ TestCaseCtxState> that = (TestCaseCtxState>) o;
+ return Objects.equals(value, that.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(value);
+ }
}
\ No newline at end of file
diff --git a/src/main/java/io/github/imagineDevit/giwt/TestCaseResult.java b/src/main/java/io/github/imagineDevit/giwt/TestCaseResult.java
index f509a80..bbf9085 100644
--- a/src/main/java/io/github/imagineDevit/giwt/TestCaseResult.java
+++ b/src/main/java/io/github/imagineDevit/giwt/TestCaseResult.java
@@ -11,13 +11,13 @@
/**
* A result of a test case
*
- * @param the result type
+ * @param the result type
* @author Henri Joel SEDJAME
* @since 0.1.0
*/
-public class TestCaseResult extends ATestCaseResult implements JExpectable {
+public class TestCaseResult extends ATestCaseResult implements JExpectable {
- private final MutVal rValue = new MutVal<>();
+ private final MutVal rValue = new MutVal<>();
private final MutVal rError = new MutVal<>();
/**
@@ -25,7 +25,7 @@ public class TestCaseResult extends ATestCaseResult implements JExpectable
*
* @param value the initial value of the test case result
*/
- private TestCaseResult(T value) {
+ private TestCaseResult(R value) {
super(value);
}
@@ -35,7 +35,7 @@ private TestCaseResult(T value) {
* @param e the exception to be set as the value of the test case result
*/
private TestCaseResult(Throwable e) {
- super(e);
+ super(Objects.requireNonNull(e));
}
/**
@@ -44,7 +44,7 @@ private TestCaseResult(Throwable e) {
* @param value the initial value of the test case result
* @return a new TestCaseResult instance
*/
- protected static TestCaseResult of(T value) {
+ protected static TestCaseResult of(R value) {
return new TestCaseResult<>(value);
}
@@ -54,8 +54,8 @@ protected static TestCaseResult of(T value) {
* @param e the exception to be set as the value of the test case result
* @return a new TestCaseResult instance
*/
- protected static TestCaseResult ofErr(Throwable e) {
- return new TestCaseResult<>(e);
+ protected static TestCaseResult ofErr(Throwable e) {
+ return new TestCaseResult<>(Objects.requireNonNull(e));
}
/**
@@ -63,8 +63,8 @@ protected static TestCaseResult ofErr(Throwable e) {
*
* @return a new TestCaseResult instance with no initial value
*/
- protected static TestCaseResult empty() {
- return new TestCaseResult<>((T) null);
+ protected static TestCaseResult empty() {
+ return new TestCaseResult<>((R) null);
}
/**
@@ -74,30 +74,40 @@ protected static TestCaseResult empty() {
* @return a new TestCaseResult with the result of the function
* @throws IllegalStateException if the result is a failure
*/
- public TestCaseResult map(Function mapper) {
- return value.ok()
- .map(ResultValue.Ok::getValue)
- .map(v -> TestCaseResult.of(mapper.apply(v)))
+ public TestCaseResult map(Function mapper) {
+ return value.ok()
+ .map(v -> TestCaseResult.of(mapper.apply(v.getValue())))
.orElseThrow(() -> new IllegalStateException("Result is Failure"));
}
@Override
- public T resultValue() {
- return rValue.getOr(
- () -> Objects.requireNonNull(value, "Result value is Null")
- .ok()
- .orElseThrow(() -> new IllegalStateException("Result value is not an Ok"))
- .getValue()
+ public R resultValue() {
+ return rValue.getOr(() -> Objects.requireNonNull(value, "Result value is Null")
+ .ok()
+ .orElseThrow(() -> new IllegalStateException("Result is Failure"))
+ .getValue()
);
}
@Override
public Throwable resultError() {
- return rError.getOr(
- () -> Objects.requireNonNull(value, "Result value is Null")
- .err()
- .map(ATestCaseResult.ResultValue.Err::getError)
- .orElseThrow(() -> new IllegalStateException("Result value is not a Failure"))
+ return rError.getOr(() -> Objects.requireNonNull(value, "Result value is Null")
+ .err()
+ .orElseThrow(() -> new IllegalStateException("Result is Success"))
+ .getError()
);
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ TestCaseResult> that = (TestCaseResult>) o;
+ return Objects.equals(value, that.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(value);
+ }
}
\ No newline at end of file
diff --git a/src/main/java/io/github/imagineDevit/giwt/TestCaseState.java b/src/main/java/io/github/imagineDevit/giwt/TestCaseState.java
index d104f42..f4aed67 100644
--- a/src/main/java/io/github/imagineDevit/giwt/TestCaseState.java
+++ b/src/main/java/io/github/imagineDevit/giwt/TestCaseState.java
@@ -2,9 +2,8 @@
import io.github.imagineDevit.giwt.core.ATestCaseState;
-import java.util.Optional;
+import java.util.Objects;
import java.util.function.Consumer;
-import java.util.function.Function;
/**
* A state of a test case
@@ -43,16 +42,6 @@ protected static TestCaseState empty() {
return new TestCaseState<>(null);
}
- /**
- * Applies a function to the current value and returns a new TestCaseResult with the result.
- *
- * @param mapper the function to apply to the current value
- * @return a new TestCaseResult with the result of the function
- */
- protected TestCaseResult mapToResult(Function mapper) {
- return TestCaseResult.of(mapper.apply(value));
- }
-
/**
* Consumes the current value using a provided Consumer.
*
@@ -64,19 +53,24 @@ protected void consumeValue(Consumer consumer) {
}
/**
- * Returns an Optional containing the current value, or an empty Optional if the value is null.
+ * Returns the current value.
*
- * @return an Optional containing the current value
+ * @return the current value
*/
- protected Optional get() {
- return Optional.ofNullable(value);
+ protected T value() {
+ return value;
}
- /**
- * Applies a function to the current value.
- * @param fn the function to apply to the current value.
- */
- protected void apply(Consumer fn) {
- fn.accept(value);
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ TestCaseState> that = (TestCaseState>) o;
+ return Objects.equals(value, that.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(value);
}
}
\ No newline at end of file
diff --git a/src/main/java/io/github/imagineDevit/giwt/TestCaseWithContext.java b/src/main/java/io/github/imagineDevit/giwt/TestCaseWithContext.java
index a89d0b4..b18db5c 100644
--- a/src/main/java/io/github/imagineDevit/giwt/TestCaseWithContext.java
+++ b/src/main/java/io/github/imagineDevit/giwt/TestCaseWithContext.java
@@ -4,8 +4,10 @@
import io.github.imagineDevit.giwt.core.TestParameters;
import io.github.imagineDevit.giwt.core.report.TestCaseReport;
import io.github.imagineDevit.giwt.core.utils.Utils;
-import io.github.imagineDevit.giwt.statements.functions.context.CtxConsumer;
-import io.github.imagineDevit.giwt.statements.functions.context.ResCtxConsumer;
+import io.github.imagineDevit.giwt.statements.functions.context.AGCtxFn;
+import io.github.imagineDevit.giwt.statements.functions.context.GCtxFn;
+import io.github.imagineDevit.giwt.statements.functions.context.TCtxFn;
+import io.github.imagineDevit.giwt.statements.functions.context.WCtxFns;
import java.util.ArrayList;
import java.util.List;
@@ -20,19 +22,19 @@
public class TestCaseWithContext extends ATestCase, TestCaseCtxResult> {
private final TestCaseContext.GCtx ctx = new TestCaseContext.GCtx<>();
- private CtxConsumer> givenFn = null;
- private final List>> aGivenFns = new ArrayList<>();
- private final List> thenFns = new ArrayList<>();
- private CtxConsumer> whenFn;
+ private final List> aGivenFns = new ArrayList<>();
+ private final List> thenFns = new ArrayList<>();
+ private GCtxFn givenFn = null;
+ private WCtxFns whenFn;
protected TestCaseWithContext(String name, TestCaseReport.TestReport report, TestParameters.Parameter parameters) {
super(name, report, parameters);
}
- public GivenCtxStmt given(String message, CtxConsumer> fn) {
+ public GivenCtxStmt given(String message, GCtxFn fn) {
return runIfOpen(() -> {
this.addGivenMsg(message);
- this.givenFn = fn;
+ this.givenFn = fn;
return new GivenCtxStmt<>(this);
});
}
@@ -45,12 +47,12 @@ public GivenCtxStmt given(String message, T t) {
});
}
- protected void andGiven(String message, CtxConsumer> fn) {
+ protected void andGiven(String message, AGCtxFn fn) {
this.addAndGivenMsg(message);
this.aGivenFns.add(fn);
}
- public WhenCtxStmt when(String message, CtxConsumer> fn) {
+ public WhenCtxStmt when(String message, WCtxFns.WCtxFFn fn) {
return runIfOpen(() -> {
this.addWhenMsg(message);
this.whenFn = fn;
@@ -58,19 +60,19 @@ public WhenCtxStmt when(String message, CtxConsumer whenr(String message, CtxConsumer> fn) {
+ protected WhenCtxStmt whenr(String message, WCtxFns.WCtxSFn fn) {
this.addWhenMsg(message);
this.whenFn = fn;
return new WhenCtxStmt<>(this);
}
- protected ThenCtxStmt then(String message, ResCtxConsumer fn) {
+ protected ThenCtxStmt then(String message, TCtxFn fn) {
this.addThenMsg(message);
this.thenFns.add(fn);
return new ThenCtxStmt<>(this);
}
- protected void andThen(String message, ResCtxConsumer fn) {
+ protected void andThen(String message, TCtxFn fn) {
this.addAndThenMsg(message);
this.thenFns.add(fn);
}
@@ -80,16 +82,16 @@ protected void run() {
System.out.print(Utils.reportTestCase(name, givenMsgs, whenMsgs, thenMsgs, parameters));
- TestCaseContext.AGCtx aGctx;
- TestCaseContext.WCtx wctx;
+ TestCaseContext.AGCtx aGctx;
+ TestCaseContext.WCtx wctx;
if (this.givenFn != null) {
- this.givenFn.accept(this.ctx);
+ this.ctx.setState(this.givenFn.apply(this.ctx));
}
if (!this.aGivenFns.isEmpty()) {
aGctx = this.ctx.toAGCtx();
- this.aGivenFns.forEach(fn -> fn.accept(aGctx));
+ this.aGivenFns.forEach(fn -> fn.accept(aGctx, aGctx.getState().value()));
wctx = aGctx.toWCtx();
} else {
wctx = this.ctx.toWCtx();
@@ -97,23 +99,31 @@ protected void run() {
try {
- this.whenFn.accept(wctx);
+
+ if (this.whenFn instanceof WCtxFns.WCtxFFn fn) {
+ fn.accept(wctx);
+ } else if (this.whenFn instanceof WCtxFns.WCtxSFn fn) {
+ wctx.setResult(TestCaseCtxResult.of(fn.apply(wctx, wctx.getState().value())));
+ }
} catch (Exception e) {
wctx.setResult(TestCaseCtxResult.ofErr(e));
}
+
+ System.out.print(Utils.listExpectations());
var tctx = wctx.toTCtx();
this.thenFns.forEach(fn -> fn.accept(tctx, tctx.getResult()));
+ System.out.println();
}
public record GivenCtxStmt(TestCaseWithContext testCase) {
- public GivenCtxStmt and(String message, CtxConsumer> fn) {
+ public GivenCtxStmt and(String message, AGCtxFn fn) {
testCase.andGiven(message, fn);
return this;
}
- public WhenCtxStmt when(String message, CtxConsumer> fn) {
+ public WhenCtxStmt when(String message, WCtxFns.WCtxSFn fn) {
return testCase.whenr(message, fn);
}
@@ -121,7 +131,7 @@ public WhenCtxStmt when(String message, CtxConsumer(TestCaseWithContext testCase) {
- public ThenCtxStmt then(String message, ResCtxConsumer fn) {
+ public ThenCtxStmt then(String message, TCtxFn fn) {
return testCase.then(message, fn);
}
@@ -129,7 +139,7 @@ public ThenCtxStmt then(String message, ResCtxConsumer fn) {
public record ThenCtxStmt(TestCaseWithContext testCase) {
- public ThenCtxStmt and(String message, ResCtxConsumer fn) {
+ public ThenCtxStmt and(String message, TCtxFn fn) {
testCase.andThen(message, fn);
return this;
}
diff --git a/src/main/java/io/github/imagineDevit/giwt/expectations/JExpectable.java b/src/main/java/io/github/imagineDevit/giwt/expectations/JExpectable.java
index 762e266..cd74778 100644
--- a/src/main/java/io/github/imagineDevit/giwt/expectations/JExpectable.java
+++ b/src/main/java/io/github/imagineDevit/giwt/expectations/JExpectable.java
@@ -1,11 +1,15 @@
package io.github.imagineDevit.giwt.expectations;
import io.github.imagineDevit.giwt.core.expectations.*;
+import io.github.imagineDevit.giwt.core.expectations.Expectation.Should;
+import io.github.imagineDevit.giwt.core.expectations.Expectation.Should.ShouldFail;
import java.util.Arrays;
+import java.util.List;
/**
* [JExpectable] is an interface that extends [Expectable] and provides additional methods to verify expectations.
+ *
* @param the type of the result value
*/
public interface JExpectable extends Expectable {
@@ -15,8 +19,8 @@ public interface JExpectable extends Expectable {
*/
@Override
default OnFailureChain shouldFail(ExpectedToFail expectation) {
- expectation.doVerify(resultError());
- return new OnFailureChain<>(resultError());
+ var sh = new ShouldFail<>(this, List.of(expectation));
+ return new OnFailureChain<>(sh.verifyAndGet());
}
/**
@@ -25,7 +29,7 @@ default OnFailureChain shouldFail(ExpectedToFail expectation) {
* @param expectations the expectations to be checked
*/
default void shouldFail(ExpectedToFail... expectations) {
- Arrays.asList(expectations).forEach(f -> f.doVerify(resultError()));
+ new ShouldFail<>(this, Arrays.asList(expectations)).verify();
}
/**
@@ -85,13 +89,12 @@ default void shouldMatch(ExpectedToMatch... expectations) {
* @param expectations the expectations to be checked
*/
private void verify(Expectation.OnValue[] expectations) {
- T value = resultValue();
- Arrays.asList(expectations).forEach(b -> b.doVerify(value));
+ new Should.ShouldSucceed<>(this, Arrays.asList(expectations))
+ .verify();
}
private > OnValueChain verify(E expectation) {
- T value = resultValue();
- expectation.doVerify(value);
- return new OnValueChain<>(value, this);
+ var sc = new Should.ShouldSucceed<>(this, List.of(expectation));
+ return new OnValueChain<>(sc.verifyAndGet(), this);
}
}
diff --git a/src/main/java/io/github/imagineDevit/giwt/processors/TestProxyProcessor.java b/src/main/java/io/github/imagineDevit/giwt/processors/TestProxyProcessor.java
deleted file mode 100644
index c9c54a4..0000000
--- a/src/main/java/io/github/imagineDevit/giwt/processors/TestProxyProcessor.java
+++ /dev/null
@@ -1,182 +0,0 @@
-package io.github.imagineDevit.giwt.processors;
-
-import com.squareup.javapoet.*;
-import io.github.imagineDevit.giwt.core.annotations.ParametersDataName;
-
-import javax.annotation.processing.*;
-import javax.lang.model.SourceVersion;
-import javax.lang.model.element.*;
-import javax.lang.model.type.NoType;
-import javax.lang.model.type.TypeMirror;
-import javax.lang.model.util.Elements;
-import javax.tools.Diagnostic;
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
-
-/**
- * A processor to generate proxy classes for classes annotated with {@link io.github.imagineDevit.giwt.core.annotations.TestProxy}.
- *
- * @author Henri Joel SEDJAME
- * @see io.github.imagineDevit.giwt.core.annotations.TestProxy
- */
-
-@SupportedAnnotationTypes("io.github.imagineDevit.giwt.core.annotations.TestProxy")
-@SupportedSourceVersion(SourceVersion.RELEASE_17)
-public class TestProxyProcessor extends AbstractProcessor {
-
- private static final String DELEGATE = "delegate";
- private static final String INIT = "";
- private static final String P0 = "p0";
- private static final String TESTER = "TestProxy";
- private static final String PARAMS = "Params";
-
- private Messager messager;
- private Elements elementUtils;
-
- @Override
- public synchronized void init(ProcessingEnvironment processingEnv) {
- super.init(processingEnv);
- messager = processingEnv.getMessager();
- elementUtils = processingEnv.getElementUtils();
- }
-
- @Override
- public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
- annotations.forEach(annotation ->
- roundEnv.getElementsAnnotatedWith(annotation).forEach(this::buildFile));
- return false;
- }
-
- private void buildFile(Element element) {
- var packageName = elementUtils.getPackageOf(element).getQualifiedName().toString();
- var delegateName = element.getSimpleName().toString();
- var fileName = delegateName + TESTER;
-
- var type = TypeSpec.classBuilder(fileName)
- .addModifiers(Modifier.PUBLIC);
-
- var delegateField = FieldSpec
- .builder(TypeName.get(element.asType()), DELEGATE)
- .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
- .build();
-
- var constructor = MethodSpec.constructorBuilder()
- .addModifiers(Modifier.PUBLIC)
- .addParameter(TypeName.get(element.asType()), DELEGATE)
- .addStatement("this.$N = $N", DELEGATE, DELEGATE)
- .build();
-
- type
- .addField(delegateField)
- .addMethod(constructor);
-
- var getters = element.getEnclosedElements()
- .stream()
- .filter(e -> e.getKind().isField())
- .map(e -> "get" + capitalize(e.getSimpleName().toString()))
- .toList();
-
- Predicate predicate = e -> {
- var name = e.getSimpleName().toString();
- return !getters.contains(name)
- && !name.equals(INIT)
- && e.getModifiers().contains(Modifier.PUBLIC);
- };
-
- element.getEnclosedElements()
- .stream()
- .filter(e -> e instanceof ExecutableElement)
- .map(e -> (ExecutableElement) e)
- .filter(predicate)
- .forEach(e -> type.addMethod(buildMethod(type, e)));
-
- var file = JavaFile.builder(packageName, type.build()).build();
-
- try {
- file.writeTo(processingEnv.getFiler());
- } catch (Exception e) {
- messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
- }
-
- }
-
- private MethodSpec buildMethod(TypeSpec.Builder type, ExecutableElement element) {
- var methodName = element.getSimpleName().toString();
-
- TypeMirror elmtRetType = element.getReturnType();
-
- boolean isVoid = elmtRetType instanceof NoType;
-
- var returnType = isVoid ? TypeName.VOID : TypeName.get(elmtRetType);
-
- var method = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC);
-
- var parameters = element.getParameters();
-
- var returnTerm = isVoid ? "" : "return";
-
- switch (parameters.size()) {
- case 0 -> method.addStatement("$N this.$N.$N()", returnTerm, DELEGATE, methodName);
-
- case 1 -> {
- String parameter = parameters.get(0).getSimpleName().toString();
- method.addModifiers(Modifier.PUBLIC)
- .addParameter(TypeName.get(parameters.get(0).asType()), parameter)
- .addStatement("$N this.$N.$N($N)", returnTerm, DELEGATE, methodName, parameter);
- }
- default -> {
- var recordName = capitalize(Optional.ofNullable(element.getAnnotation(ParametersDataName.class))
- .map(ParametersDataName::value)
- .orElse(methodName) + PARAMS);
-
- type.addType(buildRecord(recordName, parameters));
-
- var paramValues = parameters
- .stream()
- .map(p -> "%s.%s".formatted(P0, p.getSimpleName().toString()))
- .collect(Collectors.joining(", "));
-
- method.addModifiers(Modifier.PUBLIC)
- .addParameter(ClassName.bestGuess(recordName), P0)
- .addStatement("$N this.$N.$N($N)", returnTerm, DELEGATE, methodName, paramValues);
- }
- }
-
- return method
- .returns(returnType)
- .build();
- }
-
- private TypeSpec buildRecord(String recordName, List extends VariableElement> parameters) {
-
- var fields = parameters
- .stream()
- .map(p -> FieldSpec.builder(TypeName.get(p.asType()), p.getSimpleName().toString())
- .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
- .build())
- .toList();
-
- var constructorParameters = parameters.stream()
- .map(p -> ParameterSpec.builder(TypeName.get(p.asType()), p.getSimpleName().toString()).build())
- .toList();
-
- var constructor = MethodSpec.constructorBuilder()
- .addModifiers(Modifier.PUBLIC)
- .addParameters(constructorParameters);
-
- parameters.forEach(p -> constructor.addStatement("this.$N = $N", p.getSimpleName(), p.getSimpleName()));
-
- return TypeSpec.classBuilder(recordName)
- .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
- .addFields(fields)
- .addMethod(constructor.build())
- .build();
- }
-
- private String capitalize(String s) {
- return s.substring(0, 1).toUpperCase() + s.substring(1);
- }
-}
diff --git a/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/AGCtxFn.java b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/AGCtxFn.java
new file mode 100644
index 0000000..32da5d8
--- /dev/null
+++ b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/AGCtxFn.java
@@ -0,0 +1,17 @@
+package io.github.imagineDevit.giwt.statements.functions.context;
+
+import io.github.imagineDevit.giwt.TestCaseContext;
+
+import java.util.function.BiConsumer;
+
+/**
+ * TextCase context consumer.
+ *
+ * @param type of the result value
+ * @param type of the state value
+ * @author Henri Joel SEDJAME
+ * @version 0.1.2
+ * @see TestCaseContext
+ */
+public interface AGCtxFn extends BiConsumer, T> {
+}
diff --git a/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/CtxConsumer.java b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/GCtxFn.java
similarity index 63%
rename from src/main/java/io/github/imagineDevit/giwt/statements/functions/context/CtxConsumer.java
rename to src/main/java/io/github/imagineDevit/giwt/statements/functions/context/GCtxFn.java
index 24b7527..0be5164 100644
--- a/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/CtxConsumer.java
+++ b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/GCtxFn.java
@@ -2,16 +2,16 @@
import io.github.imagineDevit.giwt.TestCaseContext;
-import java.util.function.Consumer;
+import java.util.function.Function;
/**
* TextCase context consumer.
*
* @param type of the result value
- * @param type of the context
+ * @param type of the state value
* @author Henri Joel SEDJAME
* @version 0.1.2
* @see TestCaseContext
*/
-public interface CtxConsumer> extends Consumer {
+public interface GCtxFn extends Function, T> {
}
diff --git a/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/ResCtxConsumer.java b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/TCtxFn.java
similarity index 78%
rename from src/main/java/io/github/imagineDevit/giwt/statements/functions/context/ResCtxConsumer.java
rename to src/main/java/io/github/imagineDevit/giwt/statements/functions/context/TCtxFn.java
index 159ae76..bb1fa39 100644
--- a/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/ResCtxConsumer.java
+++ b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/TCtxFn.java
@@ -13,5 +13,5 @@
* @version 0.1.2
* @see TestCaseContext
*/
-public interface ResCtxConsumer extends BiConsumer, TestCaseResult> {
+public interface TCtxFn extends BiConsumer, TestCaseResult> {
}
diff --git a/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/WCtxFns.java b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/WCtxFns.java
new file mode 100644
index 0000000..fe2080e
--- /dev/null
+++ b/src/main/java/io/github/imagineDevit/giwt/statements/functions/context/WCtxFns.java
@@ -0,0 +1,25 @@
+package io.github.imagineDevit.giwt.statements.functions.context;
+
+import io.github.imagineDevit.giwt.TestCaseContext;
+
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+
+/**
+ * TextCase with context state consumer.
+ *
+ * @param type of the state value
+ * @param type of the result value
+ * @author Henri Joel SEDJAME
+ * @version 0.1.2
+ * @see TestCaseContext
+ */
+
+sealed public interface WCtxFns {
+ non-sealed interface WCtxSFn extends WCtxFns, BiFunction, T, R> {
+ }
+
+ non-sealed interface WCtxFFn extends WCtxFns, Consumer> {
+ }
+}
+
diff --git a/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/src/main/resources/META-INF/services/javax.annotation.processing.Processor
deleted file mode 100644
index c5b1dad..0000000
--- a/src/main/resources/META-INF/services/javax.annotation.processing.Processor
+++ /dev/null
@@ -1 +0,0 @@
-io.github.imagineDevit.giwt.processors.TestProxyProcessor
\ No newline at end of file
diff --git a/src/test/java/io/github/imagineDevit/giwt/TestCaseCtxResultTest.java b/src/test/java/io/github/imagineDevit/giwt/TestCaseCtxResultTest.java
new file mode 100644
index 0000000..afa822c
--- /dev/null
+++ b/src/test/java/io/github/imagineDevit/giwt/TestCaseCtxResultTest.java
@@ -0,0 +1,108 @@
+package io.github.imagineDevit.giwt;
+
+
+import io.github.imagineDevit.giwt.core.annotations.Test;
+
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.equalTo;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.notNull;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToFail.withType;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToMatch.one;
+
+class TestCaseCtxResultTest {
+
+
+ @Test("create a TestCaseCtxResult with a given value")
+ void of(TestCase> tc) {
+ tc
+ .given(" a not null number 1 ", 1)
+ .when("a TestCaseCtxResult is created using 'of' method factory", (i) -> {
+ return TestCaseCtxResult.of(i);
+ })
+ .then("the value of the TestCaseCtxResult should be 1", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseCtxResult.of(1)));
+ });
+ }
+
+ @Test("create a TestCaseCtxResult with a null value")
+ void of2(TestCase> tc) {
+ tc
+ .given(" a null number", () -> null)
+ .when("a TestCaseCtxResult is created using 'of' method factory", (i) -> {
+ return TestCaseCtxResult.of(i);
+ })
+ .then("the value of the TestCaseCtxResult should be null", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseCtxResult.of(null)));
+ });
+ }
+
+ @Test("create a TestCaseCtxResult with a given exception")
+ void ofErr(TestCase> tc) {
+ tc.withContext()
+ .given(" a not null exception ", new Exception())
+ .when("a TestCaseCtxResult is created using 'ofErr' method factory", (ctx, e) -> TestCaseCtxResult.ofErr(e))
+ .then("the value of the TestCaseCtxResult should be the exception", (ctx, result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseCtxResult.ofErr(ctx.getState().value())))
+ );
+ }
+
+ @Test("create a TestCaseCtxResult with a null exception")
+ void ofErr2(TestCase> tc) {
+ tc
+ .given(" a null exception ", () -> null)
+ .when("a TestCaseCtxResult is created using 'ofErr' method factory", (e) -> {
+ return TestCaseCtxResult.ofErr(e);
+ })
+ .then("the value of the TestCaseCtxResult should be the exception", (result) ->
+ result
+ .shouldFail(withType(NullPointerException.class))
+ );
+ }
+
+ @Test("create a empty TestCaseCtxResult")
+ void empty(TestCase> tc) {
+ tc
+ .when("a TestCaseCtxResult is created using 'empty' method factory", () -> TestCaseCtxResult.empty())
+ .then("the value of the TestCaseCtxResult should be null", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseCtxResult.of(null)));
+ });
+ }
+
+ @Test("get the result of a TestCaseCtxResult")
+ void result(TestCase, TestCaseResult> tc) {
+
+ tc
+ .given("a TestCaseCtxResult with a value", () -> TestCaseCtxResult.of(1))
+ .when("the result method is called", (result) -> {
+ return result.result();
+ })
+ .then("the result should be a TestCaseResult with the same value", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseResult.of(1)));
+ });
+ }
+
+ @Test("get the result of a TestCaseCtxResult with an exception")
+ void result2(TestCase, TestCaseResult> tc) {
+
+ tc
+ .given("a TestCaseCtxResult with an exception", () -> TestCaseCtxResult.ofErr(new Exception()))
+ .when("the result method is called", (result) -> {
+ return result.result();
+ })
+ .then("the result should be a TestCaseResult with the same exception", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and()
+ .shouldMatch(one("Expected to have error of type Exception", e -> e.resultError() instanceof Exception));
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/io/github/imagineDevit/giwt/TestCaseCtxStateTest.java b/src/test/java/io/github/imagineDevit/giwt/TestCaseCtxStateTest.java
new file mode 100644
index 0000000..42ce3e9
--- /dev/null
+++ b/src/test/java/io/github/imagineDevit/giwt/TestCaseCtxStateTest.java
@@ -0,0 +1,48 @@
+package io.github.imagineDevit.giwt;
+
+
+import io.github.imagineDevit.giwt.core.annotations.Test;
+
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.equalTo;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.notNull;
+
+class TestCaseCtxStateTest {
+
+ @Test("create a TestCaseCtxState with a not null given value")
+ void of(TestCase> tc) {
+ tc
+ .given(" a not null number 1 ", 1)
+ .when("a TestCaseState is created using 'of' method factory", (i) -> {
+ return TestCaseCtxState.of(i);
+ })
+ .then("the value of the TestCaseState should be 1", (result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseCtxState.of(1))));
+ }
+
+ @Test("create a TestCaseCtxState with a null value")
+ void of2(TestCase> tc) {
+ tc
+ .given(" a not null number", () -> null)
+ .when("a TestCaseState is created using 'of' method factory", (i) -> {
+ return TestCaseCtxState.of(i);
+ })
+ .then("the value of the TestCaseState should be null", (result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseCtxState.of(null))));
+ }
+
+ @Test("create a TestCaseCtxState with no initial value")
+ void empty(TestCase> tc) {
+ tc
+ .when("a TestCaseState is created using 'empty' method factory",
+ () -> TestCaseCtxState.empty()
+ )
+ .then("the value of the TestCaseState should be null", (result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseCtxState.of(null))));
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/io/github/imagineDevit/giwt/TestCaseResultTest.java b/src/test/java/io/github/imagineDevit/giwt/TestCaseResultTest.java
new file mode 100644
index 0000000..8756a63
--- /dev/null
+++ b/src/test/java/io/github/imagineDevit/giwt/TestCaseResultTest.java
@@ -0,0 +1,165 @@
+package io.github.imagineDevit.giwt;
+
+
+import io.github.imagineDevit.giwt.core.annotations.Test;
+
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.equalTo;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.notNull;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToFail.withMessage;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToFail.withType;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToMatch.one;
+
+class TestCaseResultTest {
+
+
+ @Test("create a TestCaseResult with a given value")
+ void of(TestCase> tc) {
+ tc
+ .given(" a not null number 1 ", 1)
+ .when("a TestCaseResult is created using 'of' method factory", (i) -> {
+ return TestCaseResult.of(i);
+ })
+ .then("the value of the TestCaseResult should be 1", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseResult.of(1)));
+ });
+ }
+
+ @Test("create a TestCaseResult with a null value")
+ void of2(TestCase> tc) {
+ tc
+ .given(" a null number", () -> null)
+ .when("a TestCaseResult is created using 'of' method factory", (i) -> {
+ return TestCaseResult.of(i);
+ })
+ .then("the value of the TestCaseResult should be null", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseResult.of(null)));
+ });
+ }
+
+ @Test("create a TestCaseResult with a given exception")
+ void ofErr(TestCase> tc) {
+ tc.withContext()
+ .given(" a not null exception ", new Exception())
+ .when("a TestCaseResult is created using 'ofErr' method factory", (ctx, e) -> TestCaseResult.ofErr(e))
+ .then("the value of the TestCaseResult should be the exception", (ctx, result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseResult.ofErr(ctx.getState().value())))
+ );
+ }
+
+ @Test("create a TestCaseResult with a null exception")
+ void ofErr2(TestCase> tc) {
+ tc
+ .given(" a null exception ", () -> null)
+ .when("a TestCaseResult is created using 'ofErr' method factory", (e) -> {
+ return TestCaseResult.ofErr(e);
+ })
+ .then("the value of the TestCaseResult should be the exception", (result) ->
+ result
+ .shouldFail(withType(NullPointerException.class))
+ );
+ }
+
+ @Test("create a empty TestCaseResult")
+ void empty(TestCase> tc) {
+ tc
+ .when("a TestCaseResult is created using 'empty' method factory", () -> TestCaseResult.empty())
+ .then("the value of the TestCaseResult should be null", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseResult.of(null)));
+ });
+ }
+
+ @Test("map a TestCaseResult with a value")
+ void map(TestCase, TestCaseResult> tc) {
+ tc
+ .given(" a TestCaseResult with a value of 1 ", TestCaseResult.of(1))
+ .when("the value of the TestCaseResult is mapped to a string", (result) -> {
+ return result.map(Object::toString);
+ })
+ .then("the value of the TestCaseResult should be '1'", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseResult.of("1")));
+ });
+ }
+
+ @Test("map a TestCaseResult with an exception")
+ void map2(TestCase, TestCaseResult> tc) {
+ tc
+ .given(" a TestCaseResult with a error ", TestCaseResult.ofErr(new Exception()))
+ .when("the value of the TestCaseResult is mapped to a string", (result) -> {
+ return result.map(Object::toString);
+ })
+ .then("the TestCaseResult should fail", (result) -> {
+ result
+ .shouldFail(withType(IllegalStateException.class))
+ .and(withMessage("Result is Failure"));
+ });
+ }
+
+
+ @Test("get the value of a TestCaseResult with a value")
+ void resultValue(TestCase, Integer> tc) {
+ tc
+ .given(" a TestCaseResult with a value of 1 ", TestCaseResult.of(1))
+ .when("the value of the TestCaseResult is retrieved", (result) -> {
+ return result.resultValue();
+ })
+ .then("the value should be 1", (value) -> {
+ value
+ .shouldBe(notNull())
+ .and(equalTo(1));
+ });
+ }
+
+ @Test("try get the value of a TestCaseResult with an exception")
+ void resultValue2(TestCase, Integer> tc) {
+ tc
+ .given(" a TestCaseResult with an exception", TestCaseResult.ofErr(new Exception()))
+ .when("the value of the TestCaseResult is retrieved", (result) -> {
+ return result.resultValue();
+ })
+ .then("the value should fail", (value) -> {
+ value
+ .shouldFail(withType(IllegalStateException.class))
+ .and(withMessage("Result is Failure"));
+ });
+ }
+
+
+ @Test("get the error of a TestCaseResult with an exception")
+ void resultError(TestCase, Throwable> tc) {
+ tc
+ .given(" a TestCaseResult with an exception", TestCaseResult.ofErr(new Exception()))
+ .when("the error of the TestCaseResult is retrieved", (result) -> {
+ return result.resultError();
+ })
+ .then("the error should be the exception", (result) -> {
+ result
+ .shouldBe(notNull())
+ .and()
+ .shouldMatch(one("expected to instance of Exception", e -> e instanceof Exception));
+ });
+ }
+
+ @Test("try get the error of a TestCaseResult with a value")
+ void resultError2(TestCase, Throwable> tc) {
+ tc
+ .given(" a TestCaseResult with a value", TestCaseResult.of(1))
+ .when("the value of the TestCaseResult is retrieved", (result) -> {
+ return result.resultError();
+ })
+ .then("the value should fail", (result) -> {
+ result
+ .shouldFail(withType(IllegalStateException.class))
+ .and(withMessage("Result is Success"));
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/io/github/imagineDevit/giwt/TestCaseStateTest.java b/src/test/java/io/github/imagineDevit/giwt/TestCaseStateTest.java
new file mode 100644
index 0000000..ccf0b26
--- /dev/null
+++ b/src/test/java/io/github/imagineDevit/giwt/TestCaseStateTest.java
@@ -0,0 +1,48 @@
+package io.github.imagineDevit.giwt;
+
+
+import io.github.imagineDevit.giwt.core.annotations.Test;
+
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.equalTo;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.notNull;
+
+class TestCaseStateTest {
+
+ @Test("create a TestCaseState with a not null given value")
+ void of(TestCase> tc) {
+ tc
+ .given(" a not null number 1 ", 1)
+ .when("a TestCaseState is created using 'of' method factory", (i) -> {
+ return TestCaseState.of(i);
+ })
+ .then("the value of the TestCaseState should be 1", (result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseState.of(1))));
+ }
+
+ @Test("create a TestCaseState with a null value")
+ void of2(TestCase> tc) {
+ tc
+ .given(" a not null number", () -> null)
+ .when("a TestCaseState is created using 'of' method factory", (i) -> {
+ return TestCaseState.of(i);
+ })
+ .then("the value of the TestCaseState should be null", (result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseState.of(null))));
+ }
+
+ @Test("create a TestCaseState with no initial value")
+ void empty(TestCase> tc) {
+ tc
+ .when("a TestCaseState is created using 'empty' method factory",
+ () -> TestCaseState.empty()
+ )
+ .then("the value of the TestCaseState should be null", (result) ->
+ result
+ .shouldBe(notNull())
+ .and(equalTo(TestCaseState.of(null))));
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/io/github/imagineDevit/giwt/tests/MyTest.java b/src/test/java/io/github/imagineDevit/giwt/tests/MyTest.java
index 5f5da20..0744fe0 100644
--- a/src/test/java/io/github/imagineDevit/giwt/tests/MyTest.java
+++ b/src/test/java/io/github/imagineDevit/giwt/tests/MyTest.java
@@ -8,7 +8,8 @@
import java.util.ArrayList;
import java.util.List;
-import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.*;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.equalTo;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.notNull;
import static io.github.imagineDevit.giwt.core.expectations.ExpectedToFail.withMessage;
import static io.github.imagineDevit.giwt.core.expectations.ExpectedToFail.withType;
import static io.github.imagineDevit.giwt.core.expectations.ExpectedToHave.anItemEqualTo;
@@ -75,15 +76,14 @@ void test4(TestCase testCase) {
result -> result
.shouldFail(withType(IllegalStateException.class))
.and(withMessage("Oups"))
-
);
}
@Test("test case with context")
void test5(TestCase testCase) {
testCase.withContext()
- .given("the state is set to 1", ctx -> ctx.setState(1))
- .when("result is set to state + 1", ctx -> ctx.mapToResult(one -> one + 1))
+ .given("the state is set to 1", 1)
+ .when("result is set to state + 1", (ctx, state) -> state + 1)
.then("the result should be 3", (ctx, result) -> result.shouldBe(notNull(), equalTo(2)));
}
@@ -91,16 +91,16 @@ void test5(TestCase testCase) {
void test6(TestCase, List> testCase) {
testCase.withContext()
.given("an empty list", new ArrayList<>())
- .and("element is stored as context variable", ctx -> ctx.setVar("var", "element"))
- .when("an element is added to the list", ctx -> {
- ctx.applyOnState(list -> list.add(ctx.getVar("var")));
- ctx.setStateAsResult();
+ .and("element is stored as context variable", (ctx, state) -> ctx.setVar("var", "element"))
+ .when("an element is added to the list", (ctx, state) -> {
+ state.add(ctx.getVar("var"));
+ return state;
})
.then("the result should be not null", (ctx, result) -> result.shouldBe(notNull()))
.and("the result should have a single item equal to 'element'",
(ctx, result) -> result
- .shouldHave(size(1))
- .and(anItemEqualTo("element"))
+ .shouldHave(size(1))
+ .and(anItemEqualTo("element"))
);
}
diff --git a/src/test/java/io/github/imagineDevit/giwt/tests/TestEngine.java b/src/test/java/io/github/imagineDevit/giwt/tests/TestEngine.java
index 70ef5d3..d100d89 100644
--- a/src/test/java/io/github/imagineDevit/giwt/tests/TestEngine.java
+++ b/src/test/java/io/github/imagineDevit/giwt/tests/TestEngine.java
@@ -3,13 +3,14 @@
import io.github.imagineDevit.giwt.TestCase;
import io.github.imagineDevit.giwt.core.GiwtTestEngine;
import io.github.imagineDevit.giwt.core.annotations.Test;
-import io.github.imagineDevit.giwt.core.expectations.ExpectedToMatch;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.junit.platform.testkit.engine.Events;
+import java.util.Map;
+
import static io.github.imagineDevit.giwt.core.expectations.ExpectedToBe.notNull;
-import static io.github.imagineDevit.giwt.core.expectations.ExpectedToMatch.matching;
+import static io.github.imagineDevit.giwt.core.expectations.ExpectedToMatch.all;
class TestEngine {
@@ -29,11 +30,13 @@ void verify(TestCase tc) {
list of resulting events should contain 8 events
9 of them should be successful
1 of them should be skipped
- """, events -> events.shouldMatch(ExpectedToMatch.all(
- matching("9 tests started", e -> e.started().count() == 9),
- matching("8 tests succeeded", e -> e.succeeded().count() == 8),
- matching("1 test skipped", e -> e.skipped().count() == 1)
- ))
+ """, events -> events.shouldMatch(all(
+ Map.of(
+ "9 tests started", e -> e.started().count() == 9,
+ "8 tests succeeded", e -> e.succeeded().count() == 8,
+ "1 test skipped", e -> e.skipped().count() == 1
+ ))
+ )
);