StableTemplatingUtils.java

1
package com.github.dakusui.actionunit.utils;
2
3
import java.util.Collection;
4
import java.util.LinkedList;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Optional;
8
import java.util.SortedMap;
9
import java.util.TreeMap;
10
import java.util.concurrent.atomic.AtomicInteger;
11
import java.util.concurrent.atomic.AtomicReference;
12
import java.util.function.BiConsumer;
13
import java.util.function.BinaryOperator;
14
import java.util.function.Function;
15
import java.util.function.IntFunction;
16
import java.util.stream.Collector;
17
import java.util.stream.Collectors;
18
import java.util.stream.IntStream;
19
20
import static com.github.dakusui.actionunit.utils.Checks.requireArgument;
21
import static java.util.Objects.requireNonNull;
22
import static java.util.stream.Collectors.toList;
23
24
public enum StableTemplatingUtils {
25
  ;
26
  public static String template(String template, Map<String, Object> mapping) {
27
    AtomicInteger lastPosition = new AtomicInteger(0);
28
    StringBuilder b = new StringBuilder();
29
    positionToPlaceHolder(template, mapping.keySet())
30 1 1. template : removed call to java/util/SortedMap::forEach → KILLED
        .forEach(chainBiConsumers(
31
            (Integer position, String placeHolder) -> b.append(
32
                template,
33
                lastPosition.get(),
34
                position),
35
            (Integer position, String placeHolder) -> b.append(mapping.get(placeHolder)),
36
            (Integer position, String placeHolder) -> lastPosition.accumulateAndGet(
37
                placeHolder.length(),
38 2 1. lambda$null$2 : Replaced integer addition with subtraction → KILLED
2. lambda$null$2 : replaced int return with 0 for com/github/dakusui/actionunit/utils/StableTemplatingUtils::lambda$null$2 → KILLED
                (i, j) -> position + j)
39
        ));
40 2 1. template : changed conditional boundary → SURVIVED
2. template : negated conditional → KILLED
    if (lastPosition.get() < template.length())
41
      b.append(template, lastPosition.get(), template.length());
42 1 1. template : replaced return value with "" for com/github/dakusui/actionunit/utils/StableTemplatingUtils::template → KILLED
    return b.toString();
43
  }
44
45
  public static SortedMap<String, Object> toMapping(IntFunction<String> placeHolderComposer, Object[] argValues) {
46
    AtomicInteger i = new AtomicInteger();
47 1 1. toMapping : replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::toMapping → KILLED
    return parameterPlaceHolders(placeHolderComposer, argValues.length)
48
        .stream()
49 2 1. lambda$toMapping$4 : replaced return value with "" for com/github/dakusui/actionunit/utils/StableTemplatingUtils::lambda$toMapping$4 → KILLED
2. lambda$toMapping$5 : replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::lambda$toMapping$5 → KILLED
        .collect(toLinkedHashMap(placeHolder -> placeHolder, placeHolder -> argValues[i.getAndIncrement()]));
50
  }
51
52
  static List<String> parameterPlaceHolders(IntFunction<String> placeHolderComposer, int numParameters) {
53 1 1. parameterPlaceHolders : replaced return value with Collections.emptyList for com/github/dakusui/actionunit/utils/StableTemplatingUtils::parameterPlaceHolders → KILLED
    return IntStream.range(0, numParameters)
54
        .mapToObj(placeHolderComposer)
55
        .collect(toList());
56
  }
57
58
  static SortedMap<Integer, String> positionToPlaceHolder(String formatString, Collection<String> placeHolders) {
59 1 1. positionToPlaceHolder : replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::positionToPlaceHolder → KILLED
    return new TreeMap<Integer, String>() {{
60
      for (Optional<PositionedPlaceHolder> positionedPlaceHolderOptional = findFirstPlaceHolderFrom(
61
          formatString,
62
          0,
63
          new LinkedList<>(placeHolders));
64 1 1. <init> : negated conditional → KILLED
           positionedPlaceHolderOptional.isPresent();
65
           positionedPlaceHolderOptional = findFirstPlaceHolderFrom(
66
               formatString,
67 1 1. <init> : Replaced integer addition with subtraction → TIMED_OUT
               positionedPlaceHolderOptional.get().position + positionedPlaceHolderOptional.get().placeHolder.length(),
68
               new LinkedList<>(placeHolders)
69
           )) {
70
        PositionedPlaceHolder positionedPlaceHolder = positionedPlaceHolderOptional.get();
71
        this.put(positionedPlaceHolder.position, positionedPlaceHolder.placeHolder);
72
      }
73
    }};
74
  }
75
76
  private static Optional<PositionedPlaceHolder> findFirstPlaceHolderFrom(
77
      String template,
78
      int from,
79
      Collection<String> remainingPlaceHolders) {
80
    String templateSubString = template.substring(from);
81
    List<String> notFound = new LinkedList<>();
82
    AtomicReference<Optional<PositionedPlaceHolder>> ret = new AtomicReference<>(Optional.empty());
83 1 1. findFirstPlaceHolderFrom : removed call to java/util/Collection::forEach → KILLED
    remainingPlaceHolders.forEach(
84
        (String s) -> {
85
          int position = templateSubString.indexOf(s);
86 2 1. lambda$findFirstPlaceHolderFrom$6 : changed conditional boundary → KILLED
2. lambda$findFirstPlaceHolderFrom$6 : negated conditional → KILLED
          if (position < 0)
87
            notFound.add(s);
88
          else {
89 1 1. lambda$findFirstPlaceHolderFrom$6 : Replaced integer addition with subtraction → KILLED
            PositionedPlaceHolder found = PositionedPlaceHolder.of(s, from + position);
90 3 1. lambda$findFirstPlaceHolderFrom$6 : changed conditional boundary → SURVIVED
2. lambda$findFirstPlaceHolderFrom$6 : negated conditional → KILLED
3. lambda$findFirstPlaceHolderFrom$6 : negated conditional → KILLED
            if (!ret.get().isPresent() || found.position < ret.get().get().position)
91 1 1. lambda$findFirstPlaceHolderFrom$6 : removed call to java/util/concurrent/atomic/AtomicReference::set → KILLED
              ret.set(Optional.of(found));
92
          }
93
        });
94
    remainingPlaceHolders.removeAll(notFound);
95 1 1. findFirstPlaceHolderFrom : replaced return value with Optional.empty for com/github/dakusui/actionunit/utils/StableTemplatingUtils::findFirstPlaceHolderFrom → KILLED
    return ret.get();
96
  }
97
98
99
  private static <T, K, U> Collector<T, ?, SortedMap<K, U>> toLinkedHashMap(Function<? super T, ? extends K> keyMapper,
100
      Function<? super T, ? extends U> valueMapper) {
101 1 1. toLinkedHashMap : replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::toLinkedHashMap → KILLED
    return Collectors.toMap(keyMapper, valueMapper, throwingMerger(), TreeMap::new);
102
  }
103
104
  private static <T> BinaryOperator<T> throwingMerger() {
105 1 1. throwingMerger : replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::throwingMerger → KILLED
    return (u, v) -> {
106
      throw new IllegalStateException(String.format("Duplicate key %s", u));
107
    };
108
  }
109
110
  @SafeVarargs
111
  private static <T, U> BiConsumer<T, U> chainBiConsumers(BiConsumer<T, U> c1, BiConsumer<T, U>... c2) {
112
    BiConsumer<T, U> ret = c1;
113
    for (BiConsumer<T, U> each : c2)
114
      ret = ret.andThen(each);
115 1 1. chainBiConsumers : replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::chainBiConsumers → KILLED
    return ret;
116
  }
117
118
  private static class PositionedPlaceHolder {
119
    final String placeHolder;
120
    final int    position;
121
122
    private PositionedPlaceHolder(String placeHolder, int position) {
123 3 1. lambda$new$0 : changed conditional boundary → SURVIVED
2. lambda$new$0 : replaced boolean return with true for com/github/dakusui/actionunit/utils/StableTemplatingUtils$PositionedPlaceHolder::lambda$new$0 → SURVIVED
3. lambda$new$0 : negated conditional → KILLED
      requireArgument(s -> s.length() > 0, requireNonNull(placeHolder));
124 3 1. lambda$new$1 : replaced boolean return with true for com/github/dakusui/actionunit/utils/StableTemplatingUtils$PositionedPlaceHolder::lambda$new$1 → SURVIVED
2. lambda$new$1 : changed conditional boundary → KILLED
3. lambda$new$1 : negated conditional → KILLED
      requireArgument(i -> i >= 0, position);
125
      this.placeHolder = placeHolder;
126
      this.position = position;
127
    }
128
129
    static PositionedPlaceHolder of(String placeHolder, int position) {
130 1 1. of : replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils$PositionedPlaceHolder::of → KILLED
      return new PositionedPlaceHolder(placeHolder, position);
131
    }
132
  }
133
}

Mutations

30

1.1
Location : template
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
removed call to java/util/SortedMap::forEach → KILLED

38

1.1
Location : lambda$null$2
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
Replaced integer addition with subtraction → KILLED

2.2
Location : lambda$null$2
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
replaced int return with 0 for com/github/dakusui/actionunit/utils/StableTemplatingUtils::lambda$null$2 → KILLED

40

1.1
Location : template
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : template
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenNoVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
negated conditional → KILLED

42

1.1
Location : template
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenNoVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
replaced return value with "" for com/github/dakusui/actionunit/utils/StableTemplatingUtils::template → KILLED

47

1.1
Location : toMapping
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/utils/StableTemplatingUtils::toMapping → KILLED

49

1.1
Location : lambda$toMapping$4
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextFunction.givenEchoVariable_i$whenPerformAsContextFunctionInsideHelloWorldLoop$thenFinishesNormally(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextFunction)
replaced return value with "" for com/github/dakusui/actionunit/utils/StableTemplatingUtils::lambda$toMapping$4 → KILLED

2.2
Location : lambda$toMapping$5
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextFunction.givenEchoVariable_i$whenPerformAsContextFunctionInsideHelloWorldLoop$thenFinishesNormally(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextFunction)
replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::lambda$toMapping$5 → KILLED

53

1.1
Location : parameterPlaceHolders
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)
replaced return value with Collections.emptyList for com/github/dakusui/actionunit/utils/StableTemplatingUtils::parameterPlaceHolders → KILLED

59

1.1
Location : positionToPlaceHolder
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenNoVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::positionToPlaceHolder → KILLED

64

1.1
Location : <init>
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenNoVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
negated conditional → KILLED

67

1.1
Location : <init>
Killed by : none
Replaced integer addition with subtraction → TIMED_OUT

83

1.1
Location : findFirstPlaceHolderFrom
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
removed call to java/util/Collection::forEach → KILLED

86

1.1
Location : lambda$findFirstPlaceHolderFrom$6
Killed by : com.github.dakusui.actionunit.ut.ContextFunctionsUnitTest$GivenPrintablePredicate.whenPrinted$thenFormattedCorrectly(com.github.dakusui.actionunit.ut.ContextFunctionsUnitTest$GivenPrintablePredicate)
changed conditional boundary → KILLED

2.2
Location : lambda$findFirstPlaceHolderFrom$6
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
negated conditional → KILLED

89

1.1
Location : lambda$findFirstPlaceHolderFrom$6
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
Replaced integer addition with subtraction → KILLED

90

1.1
Location : lambda$findFirstPlaceHolderFrom$6
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : lambda$findFirstPlaceHolderFrom$6
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
negated conditional → KILLED

3.3
Location : lambda$findFirstPlaceHolderFrom$6
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenTwoVariables$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
negated conditional → KILLED

91

1.1
Location : lambda$findFirstPlaceHolderFrom$6
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
removed call to java/util/concurrent/atomic/AtomicReference::set → KILLED

95

1.1
Location : findFirstPlaceHolderFrom
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
replaced return value with Optional.empty for com/github/dakusui/actionunit/utils/StableTemplatingUtils::findFirstPlaceHolderFrom → KILLED

101

1.1
Location : toLinkedHashMap
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/utils/StableTemplatingUtils::toLinkedHashMap → KILLED

105

1.1
Location : throwingMerger
Killed by : com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextFunction.givenEchoVariable_i$whenPerformAsContextFunctionInsideHelloWorldLoop$thenFinishesNormally(com.github.dakusui.actionunit.ut.actions.cmd.CmdTest$AsContextFunction)
replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::throwingMerger → KILLED

115

1.1
Location : chainBiConsumers
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenNoVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils::chainBiConsumers → KILLED

123

1.1
Location : lambda$new$0
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : lambda$new$0
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
negated conditional → KILLED

3.3
Location : lambda$new$0
Killed by : none
replaced boolean return with true for com/github/dakusui/actionunit/utils/StableTemplatingUtils$PositionedPlaceHolder::lambda$new$0 → SURVIVED

124

1.1
Location : lambda$new$1
Killed by : com.github.dakusui.actionunit.ut.ContextFunctionsUnitTest$GivenPrintablePredicate.whenPrinted$thenFormattedCorrectly(com.github.dakusui.actionunit.ut.ContextFunctionsUnitTest$GivenPrintablePredicate)
changed conditional boundary → KILLED

2.2
Location : lambda$new$1
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
negated conditional → KILLED

3.3
Location : lambda$new$1
Killed by : none
replaced boolean return with true for com/github/dakusui/actionunit/utils/StableTemplatingUtils$PositionedPlaceHolder::lambda$new$1 → SURVIVED

130

1.1
Location : of
Killed by : com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest.givenOneVariable$whenTemplating$thenTemplated(com.github.dakusui.actionunit.ut.utils.StableTemplatingUtilsUnitTest)
replaced return value with null for com/github/dakusui/actionunit/utils/StableTemplatingUtils$PositionedPlaceHolder::of → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3