Skip to content

Commit

Permalink
Fix json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Apr 8, 2018
1 parent 7301f03 commit 7334412
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 28 deletions.
90 changes: 90 additions & 0 deletions inline.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/Mingun/structured-diff/blob/master/inline.schema.json",

"oneOf": [
{ "$ref": "#/definitions/lines" },
{ "$ref": "#/definitions/hunks" }
],

"definitions": {
"lines": {
"type": "array",
"items": { "$ref": "#/definitions/lineChange" }
},
"hunks": {
"type": "array",
"items": { "$ref": "#/definitions/hunk" }
},

"lineChange": {
"description": "Object that describes one line of compared texts",
"oneOf": [
{ "$ref": "#/definitions/changedLineWithInlineChanges" },
{ "$ref": "#/definitions/changedLineWithoutInlineChanges" },
{ "$ref": "#/definitions/unchangedLine" }
]
},
"changedLineWithInlineChanges": {
"type": "object",
"description": "Line that exist in both sides, but different in each other",
"properties": {
"kind": { "enum": ["?"] },
"changes": {
"type": "array",
"items": { "$ref": "#/definitions/change" }
}
},
"required": ["kind", "changes"]
},
"changedLineWithoutInlineChanges": {
"type": "object",
"description": "Line that just added or removed",
"properties": {
"kind": { "enum": ["+", "-"] },
"value": {
"type": "string",
"description": "Line part value that is the same in both sides"
}
},
"required": ["kind", "value"]
},
"unchangedLine": {
"type": "object",
"description": "Line in the diff that is the same in both sides",
"properties": {
"kind": { "enum": [" "] },
"value": {
"type": "string",
"description": "Unchanged line content"
}
},
"required": ["kind", "value"]
},

"change": {
"type": "object",
"description": "Represents inline change of the line",
"properties": {
"kind": { "enum": ["+", "-", " "] },
"value": {
"type": "string",
"description": "Part of line that added, removed or the same in both sides"
}
},
"required": ["kind", "value"]
},

"hunk": {
"type": "object",
"properties": {
"oldStart": { "type": "number", "minimum": 1 },
"oldLines": { "type": "number", "minimum": 0 },
"newStart": { "type": "number", "minimum": 1 },
"newLines": { "type": "number", "minimum": 0 },
"lines": { "$ref": "#/definitions/lines" }
},
"required": ["oldStart", "oldLines", "newStart", "newLines", "lines"]
}
}
}
43 changes: 20 additions & 23 deletions test/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

let Diff = require('../lib/diff');
let generate = require('../lib/generate');
let schema = require('../diff.schema.json');
let schemaUnified = require('../unified.schema.json');
let schemaInline = require('../inline.schema.json');

let chai = require('chai');
let expect = chai.expect;

chai.use(require('chai-json-schema'));
chai.use(function() {
chai.Assertion.addMethod('diff', function(diff) {
return new chai.Assertion(this._obj)
chai.Assertion.addMethod('diff', function(diff, inline, context) {
let result = inline ? this._obj.inline(context) : this._obj.unified(context);
let schema = inline ? schemaInline : schemaUnified;

return new chai.Assertion(result)
.to.be.jsonSchema(schema)
.and.deep.equals(diff);
});
Expand All @@ -25,13 +29,6 @@ function simpleClone(obj) {

describe('structured-diff', function() {
for (let inline of [false, true]) {
/* eslint-disable no-inner-declarations */
function diff(expected, actual, context) {
let d = generate(expected, actual);
return inline ? d.inline(context) : d.unified(context);
}
/* eslint-enable no-inner-declarations */

describe('in ' + (inline ? 'inline' : 'unified') + ' mode', function() {
describe('returns object without changes for the same objects', function() {
const SAME_OBJECTS = [
Expand Down Expand Up @@ -90,7 +87,7 @@ describe('structured-diff', function() {
];
for (let desc of SAME_OBJECTS) {
it(desc.name, function() {
expect(diff(desc.value, simpleClone(desc.value))).to.be.diff(desc.diff);
expect(generate(desc.value, simpleClone(desc.value))).to.be.diff(desc.diff, inline);
});
}
});
Expand Down Expand Up @@ -149,11 +146,11 @@ describe('structured-diff', function() {
let obj = [];
obj.push(obj);

expect(diff(obj, 'some boring string')).to.be.diff(
inline ? makeInlineDiff(false) : makeDiff(false)
expect(generate(obj, 'some boring string')).to.be.diff(
inline ? makeInlineDiff(false) : makeDiff(false), inline
);
expect(diff('some boring string', obj)).to.be.diff(
inline ? makeInlineDiff(true ) : makeDiff(true )
expect(generate('some boring string', obj)).to.be.diff(
inline ? makeInlineDiff(true ) : makeDiff(true ), inline
);
});

Expand Down Expand Up @@ -226,23 +223,23 @@ describe('structured-diff', function() {
let obj = {};
obj.self = obj;

expect(diff(obj, 'some boring string')).to.be.diff(
inline ? makeInlineDiff(false) : makeDiff(false)
expect(generate(obj, 'some boring string')).to.be.diff(
inline ? makeInlineDiff(false) : makeDiff(false), inline
);
expect(diff('some boring string', obj)).to.be.diff(
inline ? makeInlineDiff(true ) : makeDiff(true )
expect(generate('some boring string', obj)).to.be.diff(
inline ? makeInlineDiff(true ) : makeDiff(true ), inline
);
});

it('use context', function() {
let expected = 'foo bar baz'.split(' ').join('\n');
let actual = 'foo baz'.split(' ').join('\n');

expect(diff(expected, actual, 0)).to.be.diff([{
expect(generate(expected, actual)).to.be.diff([{
oldStart: 2, oldLines: 1,
newStart: 2, newLines: 0,
lines: [{ kind: '-', value: 'bar' }]
}]);
}], inline, 0);
});
});
}
Expand All @@ -258,7 +255,7 @@ describe('structured-diff', function() {
expect(new Diff(
'{\nstring\n}',
'string'
).unified()).to.be.diff([
)).to.be.diff([
{ kind: '-', value: '{' },
{ kind: '-', value: 'string' },
{ kind: '-', value: '}' },
Expand All @@ -270,7 +267,7 @@ describe('structured-diff', function() {
expect(new Diff(
'some foo string',
'some bar string'
).unified()).to.be.diff([
)).to.be.diff([
{
kind: '-',
changes: [
Expand Down
33 changes: 28 additions & 5 deletions diff.schema.json → unified.schema.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://github.com/Mingun/structured-diff/blob/master/diff.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/Mingun/structured-diff/blob/master/unified.schema.json",

"type": "array",
"item": { "$ref": "#/definitions/lineChange" },
"oneOf": [
{ "$ref": "#/definitions/lines" },
{ "$ref": "#/definitions/hunks" }
],

"definitions": {
"lines": {
"type": "array",
"items": { "$ref": "#/definitions/lineChange" }
},
"hunks": {
"type": "array",
"items": { "$ref": "#/definitions/hunk" }
},

"lineChange": {
"description": "Object that describes one line of compared texts",
"oneOf": [
Expand All @@ -21,7 +32,7 @@
"kind": { "enum": ["+", "-"] },
"changes": {
"type": "array",
"item": { "$ref": "#/definitions/change" }
"items": { "$ref": "#/definitions/change" }
}
},
"required": ["kind", "changes"]
Expand Down Expand Up @@ -62,6 +73,18 @@
}
},
"required": ["kind", "value"]
},

"hunk": {
"type": "object",
"properties": {
"oldStart": { "type": "number", "minimum": 1 },
"oldLines": { "type": "number", "minimum": 0 },
"newStart": { "type": "number", "minimum": 1 },
"newLines": { "type": "number", "minimum": 0 },
"lines": { "$ref": "#/definitions/lines" }
},
"required": ["oldStart", "oldLines", "newStart", "newLines", "lines"]
}
}
}

0 comments on commit 7334412

Please sign in to comment.