1 | package com.github.dakusui.actionunit.actions.cmd; | |
2 | ||
3 | import com.github.dakusui.actionunit.actions.ContextVariable; | |
4 | import com.github.dakusui.actionunit.core.Context; | |
5 | import com.github.dakusui.actionunit.core.context.ContextFunction; | |
6 | import com.github.dakusui.actionunit.exceptions.ActionException; | |
7 | import com.github.dakusui.actionunit.utils.StableTemplatingUtils; | |
8 | ||
9 | import java.util.ArrayList; | |
10 | import java.util.Formattable; | |
11 | import java.util.Formatter; | |
12 | import java.util.LinkedList; | |
13 | import java.util.List; | |
14 | import java.util.function.BiFunction; | |
15 | import java.util.function.Function; | |
16 | import java.util.function.IntFunction; | |
17 | ||
18 | import static java.util.Objects.requireNonNull; | |
19 | import static java.util.stream.Collectors.joining; | |
20 | ||
21 | public interface CommandLineComposer extends Function<ContextVariable[], BiFunction<Context, Object[], String>>, Formattable { | |
22 | @Override | |
23 | default BiFunction<Context, Object[], String> apply(ContextVariable[] variables) { | |
24 |
2
1. apply : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer::apply → KILLED 2. lambda$apply$0 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer::lambda$apply$0 → KILLED |
return (context, argValues) -> StableTemplatingUtils.template( |
25 | compose(context), | |
26 | StableTemplatingUtils.toMapping(this.parameterPlaceHolderFactory().apply(variables), argValues) | |
27 | ); | |
28 | } | |
29 | ||
30 | @Override | |
31 | default void formatTo(Formatter formatter, int flags, int width, int precision) { | |
32 | formatter.format(format()); | |
33 | } | |
34 | ||
35 | Function<ContextVariable[], IntFunction<String>> parameterPlaceHolderFactory(); | |
36 | ||
37 | String format(); | |
38 | ||
39 | String compose(Context context); | |
40 | ||
41 | class Builder implements Cloneable { | |
42 | private final Function<ContextVariable[], IntFunction<String>> parameterPlaceHolderFactory; | |
43 | private List<ContextVariable> knownVariables; | |
44 | private List<Function<Context, String>> tokens; | |
45 | ||
46 | public Builder(Function<ContextVariable[], IntFunction<String>> parameterPlaceHolderFactory) { | |
47 | this.parameterPlaceHolderFactory = requireNonNull(parameterPlaceHolderFactory); | |
48 | this.tokens = new LinkedList<>(); | |
49 | this.knownVariables = new LinkedList<>(); | |
50 | } | |
51 | ||
52 | public Builder append(String text, boolean quoted) { | |
53 | requireNonNull(text); | |
54 |
2
1. lambda$append$0 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::lambda$append$0 → KILLED 2. lambda$append$1 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::lambda$append$1 → KILLED |
ContextFunction<String> func = ContextFunction.of(() -> text, c -> text); |
55 |
1
1. append : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::append → KILLED |
return append(func, quoted); |
56 | } | |
57 | ||
58 | public Builder append(Function<Context, String> func, boolean quoted) { | |
59 |
1
1. append : negated conditional → KILLED |
if (quoted) |
60 | func = quoteWithApostrophe(func); | |
61 | this.tokens.add(func); | |
62 |
1
1. append : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::append → KILLED |
return this; |
63 | } | |
64 | ||
65 | @SuppressWarnings("UnusedReturnValue") | |
66 | public Builder appendVariable(ContextVariable variable, boolean quoted) { | |
67 | Function<Context, String> func = ContextFunction.of( | |
68 |
1
1. lambda$appendVariable$2 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::lambda$appendVariable$2 → SURVIVED |
() -> "${" + variable.variableName() + "}", |
69 | variable::resolve | |
70 | ); | |
71 |
1
1. appendVariable : negated conditional → KILLED |
if (quoted) { |
72 | func = quoteWithApostrophe(func); | |
73 | } | |
74 | this.tokens.add(func); | |
75 |
1
1. appendVariable : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::appendVariable → SURVIVED |
return this.declareVariable(variable); |
76 | } | |
77 | ||
78 | public Builder declareVariable(ContextVariable contextVariable) { | |
79 |
1
1. declareVariable : negated conditional → KILLED |
if (!knownVariables.contains(contextVariable)) |
80 | this.knownVariables.add(contextVariable); | |
81 |
1
1. declareVariable : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::declareVariable → SURVIVED |
return this; |
82 | } | |
83 | ||
84 | public CommandLineComposer build() { | |
85 |
1
1. build : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::build → KILLED |
return new CommandLineComposer() { |
86 | @Override | |
87 | public Function<ContextVariable[], IntFunction<String>> parameterPlaceHolderFactory() { | |
88 |
1
1. parameterPlaceHolderFactory : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::parameterPlaceHolderFactory → KILLED |
return parameterPlaceHolderFactory; |
89 | } | |
90 | ||
91 | @Override | |
92 | public String format() { | |
93 |
1
1. format : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::format → KILLED |
return tokens.stream().map(Object::toString).collect(joining()); |
94 | } | |
95 | ||
96 | @Override | |
97 | public String compose(Context context) { | |
98 |
2
1. compose : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::compose → KILLED 2. lambda$compose$0 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::lambda$compose$0 → KILLED |
return tokens.stream().map(each -> each.apply(context)).collect(joining()); |
99 | } | |
100 | }; | |
101 | } | |
102 | ||
103 | public ContextVariable[] knownVariables() { | |
104 |
1
1. knownVariables : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::knownVariables → KILLED |
return knownVariables.toArray(new ContextVariable[0]); |
105 | } | |
106 | ||
107 | @Override | |
108 | public CommandLineComposer.Builder clone() { | |
109 | try { | |
110 | CommandLineComposer.Builder ret = (Builder) super.clone(); | |
111 | ret.knownVariables = new ArrayList<>(this.knownVariables); | |
112 | ret.tokens = new ArrayList<>(this.tokens); | |
113 |
1
1. clone : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::clone → KILLED |
return ret; |
114 | } catch (CloneNotSupportedException e) { | |
115 | throw ActionException.wrap(e); | |
116 | } | |
117 | } | |
118 | ||
119 | @Override | |
120 | public String toString() { | |
121 |
1
1. toString : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::toString → SURVIVED |
return String.format("Builder:%s(vars=%s)", tokens, knownVariables); |
122 | } | |
123 | ||
124 | ||
125 | private static Function<Context, String> quoteWithApostrophe(Function<Context, String> func) { | |
126 |
1
1. quoteWithApostrophe : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::quoteWithApostrophe → KILLED |
return func.andThen(new Function<String, String>() { |
127 | @Override | |
128 | public String apply(String s) { | |
129 |
1
1. apply : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$2::apply → KILLED |
return CommanderUtils.quoteWithApostropheForShell(s); |
130 | } | |
131 | ||
132 | @Override | |
133 | public String toString() { | |
134 |
1
1. toString : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$2::toString → KILLED |
return "'"; |
135 | } | |
136 | }); | |
137 | } | |
138 | } | |
139 | } | |
Mutations | ||
24 |
1.1 2.2 |
|
54 |
1.1 2.2 |
|
55 |
1.1 |
|
59 |
1.1 |
|
62 |
1.1 |
|
68 |
1.1 |
|
71 |
1.1 |
|
75 |
1.1 |
|
79 |
1.1 |
|
81 |
1.1 |
|
85 |
1.1 |
|
88 |
1.1 |
|
93 |
1.1 |
|
98 |
1.1 2.2 |
|
104 |
1.1 |
|
113 |
1.1 |
|
121 |
1.1 |
|
126 |
1.1 |
|
129 |
1.1 |
|
134 |
1.1 |