Skip to content

Commit

Permalink
feat: removes diff from TextDiff validator
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Jul 8, 2019
1 parent 48251fd commit a95de94
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 55 deletions.
43 changes: 12 additions & 31 deletions lib/validators/text-diff.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const DiffMatchPatch = require('googlediff');
// const DiffMatchPatch = require('googlediff');

This comment has been minimized.

Copy link
@honzajavorek

honzajavorek Jul 8, 2019

Contributor

should result in removing the googlediff package from deps

const errors = require('../errors');

const sanitizeSurrogatePairs = (data) => {
return data.replace(/[\uD800-\uDBFF]/g, '').replace(/[\uDC00-\uDFFF]/g, '');
};
// const sanitizeSurrogatePairs = (data) => {
// return data.replace(/[\uD800-\uDBFF]/g, '').replace(/[\uDC00-\uDFFF]/g, '');
// };

This comment has been minimized.

Copy link
@honzajavorek

honzajavorek Jul 8, 2019

Contributor

leftovers


class TextDiff {
constructor(expected, actual) {
Expand All @@ -28,40 +28,21 @@ class TextDiff {
}

validate() {
this.output = null;
const dmp = new DiffMatchPatch();

try {
const patch = dmp.patch_make(this.actual, this.expected);
this.output = dmp.patch_toText(patch);
return this.output;
} catch (error) {
if (error instanceof URIError) {
const patch = dmp.patch_make(
sanitizeSurrogatePairs(this.actual),
sanitizeSurrogatePairs(this.expected)
);
this.output = dmp.patch_toText(patch);
return this.output;
}

throw error;
}
this.valid = this.actual === this.expected;
}

evaluateOutputToResults(data) {
if (!data) {
data = this.output;
}

if (!data) {
evaluateOutputToResults() {
if (this.valid) {
return [];
}

return [
{
// TODO Improve the message to contain real and expected data
message: 'Real and expected data does not match.'
message: 'Actual and expected data do not match.',
values: {
expected: this.expected,
actual: this.actual
}
}
];
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/units/validateBody.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('validateBody', () => {
it('with explanatory message', () => {
expect(result)
.to.have.errorAtIndex(0)
.withMessage('Real and expected data does not match.');
.withMessage('Actual and expected data do not match.');
});
});
});
Expand Down
28 changes: 5 additions & 23 deletions test/unit/validators/text-diff-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ describe('TextDiff', () => {
});

it('should set output property', () => {
assert.isDefined(validator.output);
assert.isDefined(validator.valid);

it('output should be a string', () => {
assert.isString(validator.output);
});

it('output should be empty string', () => {
assert.equal(validator.output, '');
it('output should be marked as valid', () => {
assert.isTrue(validator.vaild);
});
});
});
Expand All @@ -108,22 +104,8 @@ describe('TextDiff', () => {
validationResult = validator.validate();
});

it('output property should be a string', () => {
assert.isString(validator.output);
});

it('output property should not be empty string', () => {
assert.notEqual(validator.output, '');
});

it('output property should contain + and -', () => {
assert.include(validator.output, '-');
assert.include(validator.output, '+');
});

it('output property should be persed by googlediff to an array', () => {
dmp = new DiffMatchPatch();
assert.isArray(dmp.patch_fromText(validator.output));
it('output property should not be marked as valid', () => {
assert.isNotTrue(validator.valid);
});
});
});
Expand Down

0 comments on commit a95de94

Please sign in to comment.