From 340eb408f959554a08c3dc685ad0fcaa152a3f2a Mon Sep 17 00:00:00 2001 From: bja Date: Tue, 15 Aug 2017 07:00:35 +0100 Subject: [PATCH 1/2] Improve hinting for List, Dict and Sets by checking if the current item exists at a later index --- lib/comparer.ts | 30 ++++++++++++++++----- test/unit/lib/comparer.test.ts | 48 ++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/lib/comparer.ts b/lib/comparer.ts index ca5da9b..acb00c5 100644 --- a/lib/comparer.ts +++ b/lib/comparer.ts @@ -32,7 +32,7 @@ export class ComparerImp { } public diff(left: string, right: string): Difference { - let curly = /^\{.*}/; + let curly = /^{.*}/; let quote = /^".*"/; let round = /^\(.*\)/; let square = /^\[.*]/; @@ -73,14 +73,16 @@ export class ComparerImp { let l = left.indexOf(token) === -1 ? left : left.substring(1, left.length - 1); let r = right.indexOf(token) === -1 ? right : right.substring(1, right.length - 1); let valueWithoutToken; + let spacer: string; if (token === "\"") { valueWithoutToken = this.diffValue(l, r); + spacer = left !== right && l === r ? "^" : " "; } else { valueWithoutToken = this.diff(l, r); + spacer = left !== right && (l === r || l === "" || r === "") ? "^" : " "; } - let spacer = left !== right && l === r ? "^" : " "; let leftSpacer = left === l ? "" : spacer; let rightSpacer = right === r ? "" : spacer; @@ -137,17 +139,33 @@ export class ComparerImp { while (i <= leftMax || j <= rightMax) { let l = i > leftMax ? "" : leftList[i]; let r = j > rightMax ? "" : rightList[j]; + let isLastItem = i > leftMax && j === rightMax || j > rightMax && i === leftMax; + + let value: Difference; + + if (l === r) { + let noDifference = _.repeat(" ", l.length); + value = { left: noDifference, right: noDifference }; + i++; + j++; + } else if (rightList.indexOf(l, j) > 0) { + value = this.diff("", r); + j++; + } else if (leftList.indexOf(r, i) > 0) { + value = this.diff(l, ""); + i++; + } else { + value = this.diff(l, r); + i++; + j++; + } - let value = this.diff(l, r); - let isLastItem = i > leftMax && j === rightMax || j > rightMax && j === leftMax; let itemsExistInBothLists = l !== "" && r !== ""; let spacer = isLastItem || itemsExistInBothLists ? " " : "^"; let leftSpacer = value.left === "" ? "" : spacer; let rightSpacer = value.right === "" ? "" : spacer; acc.left += value.left + leftSpacer; acc.right += value.right + rightSpacer; - i++; - j++; } // correct empty list length; diff --git a/test/unit/lib/comparer.test.ts b/test/unit/lib/comparer.test.ts index 4a2942a..8c8f018 100644 --- a/test/unit/lib/comparer.test.ts +++ b/test/unit/lib/comparer.test.ts @@ -65,6 +65,14 @@ describe("lib compare", () => { expect(actual.right).to.equal("^ ^"); }); + it("should diff string value and empty string value", () => { + // act + let actual = comparer.diff("\"\"", "\"baz\""); + + // assert + expect(actual.left).to.equal(" "); + expect(actual.right).to.equal(" ^^^ "); + }); it("should not add hint to position of quotes in string value", () => { // act @@ -93,6 +101,42 @@ describe("lib compare", () => { expect(actual.right).to.equal(" ^ "); }); + it("should add hint for single missing item in list", () => { + // act + let actual = comparer.diff("[1,2,3]", "[1,3]"); + + // assert + expect(actual.left).to.equal(" ^ "); + expect(actual.right).to.equal(" "); + }); + + it("should add hint for missing items in both lists", () => { + // act + let actual = comparer.diff("[1,2,3]", "[1,3,4]"); + + // assert + expect(actual.left).to.equal(" ^ "); + expect(actual.right).to.equal(" ^ "); + }); + + it("should add hint for missing item in list with repeated value", () => { + // act + let actual = comparer.diff("[1,1]", "[1,2,1]"); + + // assert + expect(actual.left).to.equal(" "); + expect(actual.right).to.equal(" ^ "); + }); + + it("should add hint for missing tuple item in list", () => { + // act + let actual = comparer.diff("[(1,true),(2,false)]", "[(1,true)]"); + + // assert + expect(actual.left).to.equal(" ^^^^^^^^^ "); + expect(actual.right).to.equal(" "); + }); + it("should not add hint to position of brackets in list value", () => { // act let actual = comparer.diff("[1,2,3]", "[45,6,7]"); @@ -102,7 +146,7 @@ describe("lib compare", () => { expect(actual.right).to.equal(" ^^ ^ ^ "); }); - it("should add hint to differences in list values", () => { + it("should add hint to differences in list values when left is longer than right", () => { // act let actual = comparer.diff("[1,2,3]", "[5,6]"); @@ -111,7 +155,7 @@ describe("lib compare", () => { expect(actual.right).to.equal(" ^ ^ "); }); - it("should add hint to differences in list values", () => { + it("should add hint to differences in list values when right is longer than left", () => { // act let actual = comparer.diff("[1,2]", "[5,6,7]"); From 806ac865dabca68edb6a877a4ec97a724f813549 Mon Sep 17 00:00:00 2001 From: bja Date: Tue, 15 Aug 2017 07:10:28 +0100 Subject: [PATCH 2/2] Bump package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d1127c5..43f00ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lobo", - "version": "0.3.1", + "version": "0.3.2", "description": "Elm test runner", "keywords": [ "elm",