diff --git a/inline.schema.json b/inline.schema.json new file mode 100644 index 0000000..6b2477b --- /dev/null +++ b/inline.schema.json @@ -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"] + } + } +} \ No newline at end of file diff --git a/test/diff.js b/test/diff.js index 0ab7745..3111354 100644 --- a/test/diff.js +++ b/test/diff.js @@ -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); }); @@ -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 = [ @@ -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); }); } }); @@ -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 ); }); @@ -226,11 +223,11 @@ 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 ); }); @@ -238,11 +235,11 @@ describe('structured-diff', 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); }); }); } @@ -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: '}' }, @@ -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: [ diff --git a/diff.schema.json b/unified.schema.json similarity index 65% rename from diff.schema.json rename to unified.schema.json index 26c95b4..72da3fe 100644 --- a/diff.schema.json +++ b/unified.schema.json @@ -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": [ @@ -21,7 +32,7 @@ "kind": { "enum": ["+", "-"] }, "changes": { "type": "array", - "item": { "$ref": "#/definitions/change" } + "items": { "$ref": "#/definitions/change" } } }, "required": ["kind", "changes"] @@ -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"] } } } \ No newline at end of file