Skip to content
This repository has been archived by the owner on Oct 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12 from benansell/improve-hinting
Browse files Browse the repository at this point in the history
Improve List, Dict, Set hinting
  • Loading branch information
benansell authored Aug 15, 2017
2 parents 0bd6082 + 806ac86 commit 0ed1102
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
30 changes: 24 additions & 6 deletions lib/comparer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ComparerImp {
}

public diff(left: string, right: string): Difference {
let curly = /^\{.*}/;
let curly = /^{.*}/;
let quote = /^".*"/;
let round = /^\(.*\)/;
let square = /^\[.*]/;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 = <Difference> { 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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lobo",
"version": "0.3.1",
"version": "0.3.2",
"description": "Elm test runner",
"keywords": [
"elm",
Expand Down
48 changes: 46 additions & 2 deletions test/unit/lib/comparer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]");
Expand All @@ -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]");

Expand All @@ -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]");

Expand Down

0 comments on commit 0ed1102

Please sign in to comment.