CommandLineComposer.java

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
Location : apply
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction.givenEchoHello$whenPerformAction$thenFinishNormally(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer::apply → KILLED

2.2
Location : lambda$apply$0
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction.givenEchoHello$whenPerformAction$thenFinishNormally(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer::lambda$apply$0 → KILLED

54

1.1
Location : lambda$append$0
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.linux.CurlTest.test6(com.github.dakusui.actionunit.ut.actions.cmd.linux.CurlTest)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::lambda$append$0 → KILLED

2.2
Location : lambda$append$1
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::lambda$append$1 → KILLED

55

1.1
Location : append
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsCommander.givenCommanderObject$whenExerciseGetters$thenNoExceptionThrown(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsCommander)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::append → KILLED

59

1.1
Location : append
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
negated conditional → KILLED

62

1.1
Location : append
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsCommander.givenCommanderObject$whenExerciseGetters$thenNoExceptionThrown(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsCommander)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::append → KILLED

68

1.1
Location : lambda$appendVariable$2
Killed by : none
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::lambda$appendVariable$2 → SURVIVED

71

1.1
Location : appendVariable
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction.givenUnknownCommand$whenPerformAsAction$thenFailureIsThrown(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction)
negated conditional → KILLED

75

1.1
Location : appendVariable
Killed by : none
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::appendVariable → SURVIVED

79

1.1
Location : declareVariable
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextConsumer.givenEchoVariable_i_usingManuallyWrittenPlaceHolder$whenPerformAsContextConsumerInsideHelloWorldLoopExpectingUnknownKeyword$thenFailureIsThrown(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextConsumer)
negated conditional → KILLED

81

1.1
Location : declareVariable
Killed by : none
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::declareVariable → SURVIVED

85

1.1
Location : build
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::build → KILLED

88

1.1
Location : parameterPlaceHolderFactory
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction.givenEchoHello$whenPerformAction$thenFinishNormally(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::parameterPlaceHolderFactory → KILLED

93

1.1
Location : format
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.linux.CurlTest.test6(com.github.dakusui.actionunit.ut.actions.cmd.linux.CurlTest)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::format → KILLED

98

1.1
Location : compose
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::compose → KILLED

2.2
Location : lambda$compose$0
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$1::lambda$compose$0 → KILLED

104

1.1
Location : knownVariables
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction.givenEchoHello$whenPerformAction$thenFinishNormally(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsAction)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::knownVariables → KILLED

113

1.1
Location : clone
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::clone → KILLED

121

1.1
Location : toString
Killed by : none
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::toString → SURVIVED

126

1.1
Location : quoteWithApostrophe
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder::quoteWithApostrophe → KILLED

129

1.1
Location : apply
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2.composeLocalEchoCommandLine(com.github.dakusui.actionunit.ut.actions.cmd.UnixCommanderFactoryTest$WithCustomSshOptions2)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$2::apply → KILLED

134

1.1
Location : toString
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.linux.CurlTest.test6(com.github.dakusui.actionunit.ut.actions.cmd.linux.CurlTest)
replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommandLineComposer$Builder$2::toString → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3