1 | package com.github.dakusui.actionunit.actions; | |
2 | ||
3 | import com.github.dakusui.actionunit.core.Action; | |
4 | import com.github.dakusui.actionunit.core.Context; | |
5 | ||
6 | import java.util.Formatter; | |
7 | import java.util.Optional; | |
8 | import java.util.function.Consumer; | |
9 | import java.util.function.Function; | |
10 | ||
11 | import static com.github.dakusui.actionunit.core.ActionSupport.simple; | |
12 | import static com.github.dakusui.actionunit.core.context.FormattableConsumer.nopConsumer; | |
13 | import static com.github.dakusui.actionunit.utils.InternalUtils.toStringIfOverriddenOrNoname; | |
14 | import static com.github.dakusui.printables.PrintableFunctionals.*; | |
15 | import static java.util.Objects.requireNonNull; | |
16 | ||
17 | /** | |
18 | * An interface to access a context variable safely. | |
19 | * An instance of this interface corresponds to single context variable. | |
20 | * | |
21 | * @param <V> A type of variable through which this action interacts with another. | |
22 | * @see Context | |
23 | */ | |
24 | public interface With<V> extends Contextful<V> { | |
25 | /** | |
26 | * Returns a "close" action which takes care of "clean up" | |
27 | * | |
28 | * @return A "close" action. | |
29 | */ | |
30 | Optional<Action> close(); | |
31 | ||
32 | @Override | |
33 | default void accept(Visitor visitor) { | |
34 |
1
1. accept : removed call to com/github/dakusui/actionunit/core/Action$Visitor::visit → KILLED |
visitor.visit(this); |
35 | } | |
36 | ||
37 | @Override | |
38 | default void formatTo(Formatter formatter, int flags, int width, int precision) { | |
39 | formatter.format("with:" + variableName() + ":" + toStringIfOverriddenOrNoname(valueSource())); | |
40 | } | |
41 | ||
42 | class Builder<V> extends Contextful.Builder<Builder<V>, With<V>, V, V> { | |
43 | ||
44 | public Builder(String variableName, Function<Context, V> function) { | |
45 | super(variableName, function); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Creates an action that updates the context variable. | |
50 | * A current value of the context variable is given to the `function` and the result | |
51 | * of the function is written back to the context. | |
52 | * | |
53 | * @param function A function to compute a new value of the context variable. | |
54 | * @return An action that updates the context variable. | |
55 | */ | |
56 | public Action updateContextVariableWith(Function<V, V> function) { | |
57 |
1
1. updateContextVariableWith : replaced return value with null for com/github/dakusui/actionunit/actions/With$Builder::updateContextVariableWith → KILLED |
return simple( |
58 | "update:" + variableName() + "*", | |
59 | printableConsumer((Context c) -> variableUpdateFunction(function).apply(c)).describe(toStringIfOverriddenOrNoname(function))); | |
60 | } | |
61 | ||
62 | ||
63 | public With<V> build() { | |
64 |
1
1. build : replaced return value with null for com/github/dakusui/actionunit/actions/With$Builder::build → KILLED |
return build(nopConsumer()); |
65 | } | |
66 | ||
67 | public With<V> build(Consumer<V> finisher) { | |
68 |
1
1. build : replaced return value with null for com/github/dakusui/actionunit/actions/With$Builder::build → KILLED |
return new Impl<>(this.variableName(), this.internalVariableName(), this.valueSource(), this.action(), finisher); |
69 | } | |
70 | ||
71 | public <W> Builder<W> nest(Function<V, W> function) { | |
72 |
1
1. nest : replaced return value with null for com/github/dakusui/actionunit/actions/With$Builder::nest → KILLED |
return new Builder<>(nextVariableName(variableName()), function(function)); |
73 | } | |
74 | protected static String nextVariableName(String variableName) { | |
75 | requireNonNull(variableName); | |
76 |
5
1. nextVariableName : changed conditional boundary → SURVIVED 2. nextVariableName : changed conditional boundary → SURVIVED 3. nextVariableName : negated conditional → SURVIVED 4. nextVariableName : negated conditional → SURVIVED 5. nextVariableName : negated conditional → SURVIVED |
if (variableName.length() == 1 && 'a' <= variableName.charAt(0) && variableName.charAt(0) <= 'z') |
77 |
2
1. nextVariableName : Replaced integer addition with subtraction → SURVIVED 2. nextVariableName : replaced return value with "" for com/github/dakusui/actionunit/actions/With$Builder::nextVariableName → SURVIVED |
return Character.toString((char) (variableName.charAt(0) + 1)); |
78 |
1
1. nextVariableName : negated conditional → NO_COVERAGE |
if (variableName.matches(".*_[1-9][0-9]*$")) { |
79 |
1
1. nextVariableName : Replaced integer addition with subtraction → NO_COVERAGE |
int index = Integer.parseInt(variableName.replaceAll(".*_", "")) + 1; |
80 |
1
1. nextVariableName : replaced return value with "" for com/github/dakusui/actionunit/actions/With$Builder::nextVariableName → NO_COVERAGE |
return variableName.replaceAll("_[1-9][0-9]*$", "_" + index); |
81 | } | |
82 |
1
1. nextVariableName : replaced return value with "" for com/github/dakusui/actionunit/actions/With$Builder::nextVariableName → NO_COVERAGE |
return variableName + "_1"; |
83 | } | |
84 | private Function<Context, V> variableUpdateFunction(Function<V, V> function) { | |
85 |
1
1. variableUpdateFunction : replaced return value with null for com/github/dakusui/actionunit/actions/With$Builder::variableUpdateFunction → KILLED |
return printableFunction( |
86 | (Context context) -> { | |
87 | V ret = function.apply(resolveValue(context)); | |
88 | context.assignTo(internalVariableName(), ret); | |
89 |
1
1. lambda$variableUpdateFunction$1 : replaced return value with null for com/github/dakusui/actionunit/actions/With$Builder::lambda$variableUpdateFunction$1 → SURVIVED |
return ret; |
90 | }) | |
91 | .describe(toStringIfOverriddenOrNoname(function)); | |
92 | } | |
93 | ||
94 | private static class Impl<V> extends Base<V, V> implements With<V> { | |
95 | private final Action end; | |
96 | ||
97 | public Impl(String variableName, String internalVariableName, Function<Context, V> valueSource, Action action, Consumer<V> finisher) { | |
98 | super(variableName, internalVariableName, valueSource, action); | |
99 | this.end = simple(String.format("done:%s", finisher), | |
100 | printableConsumer((Context context) -> { | |
101 | V variable = context.valueOf(internalVariableName); | |
102 |
1
1. lambda$new$0 : removed call to com/github/dakusui/actionunit/core/Context::unassign → SURVIVED |
context.unassign(internalVariableName); // Un-assign first. Otherwise, finisher may fail. |
103 |
1
1. lambda$new$0 : negated conditional → SURVIVED |
if (finisher != null) |
104 |
1
1. lambda$new$0 : removed call to java/util/function/Consumer::accept → SURVIVED |
finisher.accept(variable); |
105 | }).describe(String.format("cleanUp:%s", variableName))); | |
106 | } | |
107 | ||
108 | @Override | |
109 | public Optional<Action> close() { | |
110 |
1
1. close : replaced return value with Optional.empty for com/github/dakusui/actionunit/actions/With$Builder$Impl::close → SURVIVED |
return Optional.ofNullable(end); |
111 | } | |
112 | ||
113 | } | |
114 | } | |
115 | } | |
Mutations | ||
34 |
1.1 |
|
57 |
1.1 |
|
64 |
1.1 |
|
68 |
1.1 |
|
72 |
1.1 |
|
76 |
1.1 2.2 3.3 4.4 5.5 |
|
77 |
1.1 2.2 |
|
78 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
82 |
1.1 |
|
85 |
1.1 |
|
89 |
1.1 |
|
102 |
1.1 |
|
103 |
1.1 |
|
104 |
1.1 |
|
110 |
1.1 |