Skip to content

Commit 5899523

Browse files
committed
ANDROID-16874 New comparator where order is not important
1 parent 238f589 commit 5899523

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

loggerazzi/src/main/java/com/telefonica/loggerazzi/LogComparator.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,30 @@ class DefaultLogComparator<LogType> : LogComparator<LogType> {
2121

2222
return compareResult.toString().takeIf { it.isNotEmpty() }
2323
}
24-
}
24+
}
25+
26+
@Suppress("unused")
27+
class AnyOrderLogComparator<LogType> : LogComparator<LogType> {
28+
override fun compare(recorded: List<LogType>, golden: List<LogType>): String? {
29+
val missing = findDifference(golden, recorded)
30+
val extra = findDifference(recorded, golden)
31+
32+
if (missing.isEmpty() && extra.isEmpty()) {
33+
return null
34+
}
35+
36+
val result = StringBuilder()
37+
if (missing.isNotEmpty()) {
38+
result.appendLine("Missing entries (in golden but not in recorded): $missing")
39+
}
40+
if (extra.isNotEmpty()) {
41+
result.appendLine("Extra entries (in recorded but not in golden): $extra")
42+
}
43+
44+
return result.toString().trimEnd()
45+
}
46+
47+
private fun findDifference(first: List<LogType>, second: List<LogType>): List<LogType> {
48+
return first.toMutableList().apply { removeAll(second) }
49+
}
50+
}

0 commit comments

Comments
 (0)