-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
431c4ea
commit b42b2c4
Showing
6 changed files
with
130 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import experimental/[sexp, sexp_diff] | ||
import std/[tables, algorithm] | ||
|
||
proc sortmatches(s1, s2: seq[SexpNode], dir: SortOrder = Ascending): | ||
seq[tuple[key: (int, int), diff: seq[SexpMismatch]]] = | ||
|
||
var diffMap = TableRef[(int, int), seq[SexpMismatch]]() | ||
|
||
proc reportCmp(a, b: int): int = | ||
let diff = diff(s1[a], s2[b]) | ||
diffMap[(a, b)] = diff | ||
return diff.len | ||
|
||
let (expected, given, sorted) = sortedStablematch( | ||
s1.len, s2.len, reportCmp, dir) | ||
|
||
for (key, val) in sorted: | ||
result.add((key, diffMap[key])) | ||
|
||
proc sortmatches( | ||
s1, s2: seq[string], | ||
dir: SortOrder = Ascending | ||
): seq[tuple[key: (int, int), diff: seq[SexpMismatch]]] = | ||
var ss1, ss2: seq[SexpNode] | ||
for item in s1: ss1.add parseSexp(item) | ||
for item in s2: ss2.add parseSexp(item) | ||
return sortmatches(ss1, ss2, dir) | ||
|
||
|
||
proc matches(s1, s2: string): seq[SexpMismatch] = | ||
diff(s1.parseSexp(), s2.parseSexp()) | ||
|
||
proc assertEq[T](a, b: T) = | ||
doAssert a == b, "a was " & $a & ", b was " & $b | ||
|
||
block literal: | ||
let d = matches("(1)", "(2)") | ||
assertEq d.len, 1 | ||
assertEq(d[0].kind, smDifferentLiteral) | ||
|
||
block symbol: | ||
let d = matches("(A)", "(B)") | ||
assertEq d.len, 1 | ||
assertEq d[0].kind, smDifferentSymbol | ||
|
||
block match_all: | ||
let d = matches("(_)", "(Q)") | ||
assertEq d.len, 0 | ||
|
||
block match_keys: | ||
block ordered_unordered: | ||
for (m1, m2) in @[ | ||
("(:a b :c d)", "(:a q :c z)"), | ||
("(:a b :c d)", "(:c q :a z)") | ||
]: | ||
let d = matches(m1, m2) | ||
assertEq d.len, 2 | ||
# Mismatches are placed in the same order as they are used in the input | ||
# text | ||
assertEq d[0].path[0].key, "a" | ||
assertEq d[1].path[0].key, "c" | ||
assertEq d[0].kind, smDifferentSymbol | ||
assertEq d[1].kind, smDifferentSymbol | ||
|
||
block elements: | ||
let d = matches("(User :key 12)", "(Azer :id 14 :key 3)") | ||
assertEq d.len, 2 | ||
assertEq d[0].kind, smDifferentLiteral | ||
assertEq d[1].kind, smDifferentSymbol | ||
|
||
block weighed_matching: | ||
let d = sortmatches(@["(User)"], @["(User2)"]) | ||
assertEq d[0].key, (0, 0) | ||
assertEq d[0].diff.len, 1 | ||
assertEq d[0].diff[0].kind, smDifferentSymbol | ||
|
||
block hint_matching: | ||
let d = sortmatches(@[ | ||
"""(User :location ("tfile.nim" 11 7) :severity Hint :str "Another hint")""", | ||
"""(User :location ("tfile.nim" 8 _) :str "User Hint")""" | ||
], @[ | ||
"""(User :location ("tfile.nim" 8 _) :str "User Hint")""", | ||
"""(User :location ("tfile.nim" 9 6) :severity Hint :str "User hint")""" | ||
]) | ||
|
||
echo d | ||
|
||
# @[( | ||
# key: (0, 1), | ||
# diff: @[ | ||
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 1)], | ||
# kind: smDifferentLiteral, | ||
# expected: 11, | ||
# found: 9, | ||
# arraydiff: (target: @[], input: @[])), | ||
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 2)], | ||
# kind: smDifferentLiteral, | ||
# expected: 7, | ||
# found: 6, | ||
# arraydiff: (target: @[], input: @[])), | ||
# (path: @[(kind: spKey, key: "str")], | ||
# kind: smDifferentLiteral, | ||
# expected: "Another hint", | ||
# found: "User hint", | ||
# arraydiff: (target: @[], input: @[])) | ||
# ]), | ||
# ( | ||
# key: (0, 0), | ||
# diff: @[ | ||
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 1)], kind: smDifferentLiteral, expected: 11, found: 8, arraydiff: (target: @[], input: @[])), | ||
# (path: @[(kind: spKey, key: "location"), (kind: spIndex, index: 2)], kind: smKindMismatch, expected: 7, found: _, arraydiff: (target: @[], input: @[])), | ||
# (path: @[], kind: smMissingKey, key: "severity"), (path: @[(kind: spKey, key: "str")], kind: smDifferentLiteral, expected: "Another hint", found: "User Hint", arraydiff: (target: @[], input: @[])) | ||
# ]) | ||
# ] |