Skip to content

Commit

Permalink
Document and test options to createPatch (#345)
Browse files Browse the repository at this point in the history
* Document and test ignoreWhiteSpace for createPatch

* Document and test newLineIsToken for createPatch.

I feel like this option is probably not that good since I supsect the patches it outputs doesn't really make any sense.
  • Loading branch information
oBusk authored May 6, 2022
1 parent 92c9d90 commit fffd8a7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ npm install diff --save
* `newStr` : New string value
* `oldHeader` : Additional information to include in the old file header
* `newHeader` : Additional information to include in the new file header
* `options` : An object with options. Currently, only `context` is supported and describes how many lines of context should be included.
* `options` : An object with options.
- `context` describes how many lines of context should be included.
- `ignoreWhitespace`: `true` to ignore leading and trailing whitespace.
- `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output.

* `Diff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.

Expand Down
72 changes: 72 additions & 0 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,78 @@ describe('patch/create', function() {
const diffResult = createTwoFilesPatch('foo', 'bar', '', '');
expect(diffResult).to.equal(expectedResult);
});

describe('ignoreWhitespace', function() {
it('ignoreWhitespace: false', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,2 +1,2 @@\n'
+ '-line \n'
+ '- line\n'
+ '\\ No newline at end of file\n'
+ '+line\n'
+ '+line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: false});
expect(diffResult).to.equal(expectedResult);
});

it('ignoreWhitespace: true', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n';

const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: true});
expect(diffResult).to.equal(expectedResult);
});
});

describe('newlineIsToken', function() {
it('newlineIsToken: false', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,2 +1,2 @@\n'

// Diff is shown as entire row, eventhough text is unchanged
+ '-line\n'
+ '+line\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: false});
expect(diffResult).to.equal(expectedResult);
});

it('newlineIsToken: true', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,3 +1,3 @@\n'
+ ' line\n'

// Newline change is shown as a single diff
+ '-\n'
+ '+\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: true});
expect(diffResult).to.equal(expectedResult);
});
});
});

describe('#structuredPatch', function() {
Expand Down

0 comments on commit fffd8a7

Please sign in to comment.