Skip to content

Commit eb2cff3

Browse files
committed
Refactor coverage result array to assert duplicate entries are not added
1 parent cf6aa76 commit eb2cff3

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/main/java/org/truffleruby/stdlib/CoverageNodes.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
*/
1010
package org.truffleruby.stdlib;
1111

12-
import java.util.ArrayList;
13-
import java.util.List;
12+
import java.util.HashMap;
1413
import java.util.Map;
1514

1615
import org.jcodings.specific.UTF8Encoding;
@@ -60,14 +59,14 @@ public abstract static class CoverageResultNode extends CoreMethodArrayArguments
6059
@TruffleBoundary
6160
@Specialization
6261
protected RubyArray resultArray() {
63-
final List<RubyArray> results = new ArrayList<>();
6462

6563
final Map<Source, long[]> counts = getContext().getCoverageManager().getCounts();
6664

6765
if (counts == null) {
6866
throw new RaiseException(getContext(), coreExceptions().runtimeErrorCoverageNotEnabled(this));
6967
}
7068

69+
Map<String, RubyArray> results = new HashMap<>();
7170
for (Map.Entry<Source, long[]> source : counts.entrySet()) {
7271
final long[] countsArray = source.getValue();
7372

@@ -81,16 +80,18 @@ protected RubyArray resultArray() {
8180
}
8281
}
8382

84-
results.add(createArray(new Object[]{
83+
final String path = RubyContext.getPath(source.getKey());
84+
assert !results.containsKey(path) : "path already exists in coverage results";
85+
results.put(path, createArray(new Object[]{
8586
makeStringNode.executeMake(
86-
RubyContext.getPath(source.getKey()),
87+
path,
8788
UTF8Encoding.INSTANCE,
8889
CodeRange.CR_UNKNOWN),
8990
createArray(countsStore)
9091
}));
9192
}
9293

93-
return createArray(results.toArray());
94+
return createArray(results.values().toArray());
9495
}
9596

9697
}

0 commit comments

Comments
 (0)