unexpected-htmllike
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -313,2 +313,26 @@ 'use strict'; | ||
var onlyExact = true; | ||
var exactDiffResult = tryDiffChildren(actualAdapter, expectedAdapter, actualChildren, expectedChildren, equal, options, onlyExact); | ||
// If it wasn't a perfect match, and there were both inserts and removals, we can try allowing the children that | ||
// don't match to be "similar". | ||
if (exactDiffResult.weight.real !== 0 && exactDiffResult.insertCount && exactDiffResult.removeCount) { | ||
onlyExact = false; | ||
var changesDiffResult = tryDiffChildren(actualAdapter, expectedAdapter, actualChildren, expectedChildren, equal, options, onlyExact); | ||
if (changesDiffResult.weight.real < exactDiffResult.weight.real) { | ||
return { | ||
diff: changesDiffResult.diff, | ||
weight: changesDiffResult.weight | ||
}; | ||
} | ||
} | ||
return { | ||
diff: exactDiffResult.diff, | ||
weight: exactDiffResult.weight | ||
}; | ||
} | ||
function tryDiffChildren(actualAdapter, expectedAdapter, actualChildren, expectedChildren, equal, options, onlyExactMatches) { | ||
var diffWeights = new _Weights2['default'](); | ||
@@ -321,9 +345,28 @@ var diffResult = []; | ||
}, function (a, b) { | ||
// Any element that is not identical, is not similar. | ||
// We could call diffElementOrWrapper again, and compare the weight to some arbitrary amount, | ||
// But the amount is dependant on the other items. What is right for one case, will almost certainly be wrong for another | ||
return false; | ||
if (onlyExactMatches) { | ||
return false; | ||
} | ||
var aIsNativeType = (0, _isNativeType2['default'])(a); | ||
var bIsNativeType = (0, _isNativeType2['default'])(b); | ||
// If they're native types, assume they're similar | ||
if (aIsNativeType && bIsNativeType) { | ||
return true; | ||
} | ||
// If one is an element, then don't count them as "similar" | ||
if (aIsNativeType !== bIsNativeType) { | ||
return false; | ||
} | ||
// Here we could diff and get a weight, but the weight as to what is similar is dependant on | ||
// what the other "similar" elements got, so we'll just take a simplistic view - | ||
// elements with the same name are similar, otherwise they're not | ||
return actualAdapter.getName(a) === expectedAdapter.getName(b); | ||
}); | ||
var insertCount = 0; | ||
var removeCount = 0; | ||
var changeCount = 0; | ||
changes.forEach(function (diffItem) { | ||
@@ -335,2 +378,3 @@ | ||
case 'insert': | ||
insertCount++; | ||
itemResult = (0, _convertToDiff2['default'])(expectedAdapter, diffItem.value); | ||
@@ -347,2 +391,3 @@ if (options.diffMissingChildren) { | ||
case 'remove': | ||
removeCount++; | ||
itemResult = (0, _convertToDiff2['default'])(actualAdapter, diffItem.value); | ||
@@ -360,2 +405,6 @@ | ||
case 'similar': | ||
changeCount++; | ||
// fallthrough | ||
case 'equal': | ||
@@ -378,3 +427,6 @@ default: | ||
weight: diffWeights, | ||
diff: diffResult | ||
diff: diffResult, | ||
insertCount: insertCount, | ||
removeCount: removeCount, | ||
changeCount: changeCount | ||
}; | ||
@@ -381,0 +433,0 @@ } |
@@ -145,4 +145,10 @@ 'use strict'; | ||
}); | ||
} else if (typeof description.value === typeof description.diff.expectedValue) { | ||
this.append(diffFn(description.value, description.diff.expectedValue).diff); | ||
} else { | ||
this.append(diffFn(description.value, description.diff.expectedValue).diff); | ||
this.block(function () { | ||
this.append(diffFn('' + description.value, '' + description.diff.expectedValue).diff); | ||
}).sp().annotationBlock(function () { | ||
this.error('and mismatched type').sp().block(diffFn(typeof description.value, typeof description.diff.expectedValue).diff); | ||
}); | ||
} | ||
@@ -149,0 +155,0 @@ }); |
@@ -299,2 +299,40 @@ 'use strict'; | ||
it('diffs a changed middle child', function () { | ||
var result = getDiff({ name: 'span', attribs: {}, children: [{ name: 'child', attribs: {}, children: ['child1'] }, { name: 'child', attribs: {}, children: ['child2 changed'] }, { name: 'child', attribs: {}, children: ['child3'] }] }, { name: 'span', attribs: {}, children: [{ name: 'child', attribs: {}, children: ['child1'] }, { name: 'child', attribs: {}, children: ['child2'] }, { name: 'child', attribs: {}, children: ['child3'] }] }); | ||
expect(result, 'to satisfy', { | ||
diff: { | ||
children: [{ | ||
type: 'ELEMENT', | ||
name: 'child', | ||
children: [{ | ||
type: 'CONTENT', | ||
value: 'child1' | ||
}] | ||
}, { | ||
type: 'ELEMENT', | ||
name: 'child', | ||
children: [{ | ||
type: 'CONTENT', | ||
value: 'child2 changed', | ||
diff: { | ||
type: 'changed', | ||
expectedValue: 'child2' | ||
} | ||
}] | ||
}, { | ||
type: 'ELEMENT', | ||
name: 'child', | ||
diff: undefined, | ||
children: [{ | ||
type: 'CONTENT', | ||
value: 'child3' | ||
}] | ||
}] | ||
}, | ||
weight: _diff2['default'].DefaultWeights.STRING_CONTENT_MISMATCH | ||
}); | ||
}); | ||
it('diffs a missing content entry', function () { | ||
@@ -301,0 +339,0 @@ var result = getDiff({ name: 'span', attribs: {}, children: ['child1', 'child3'] }, { name: 'span', attribs: {}, children: ['child1', 'child2', 'child3'] }); |
@@ -49,2 +49,3 @@ | ||
}; | ||
expect.output.preferredWidth = 80; | ||
@@ -51,0 +52,0 @@ expect.addType({ |
@@ -18,2 +18,3 @@ 'use strict'; | ||
var expect = _unexpected2['default'].clone(); | ||
expect.output.preferredWidth = 80; | ||
@@ -20,0 +21,0 @@ describe('magicpen-linebreaker', function () { |
@@ -19,2 +19,4 @@ 'use strict'; | ||
expect.output.preferredWidth = 80; | ||
function duplicate(object, count) { | ||
@@ -101,2 +103,13 @@ var result = []; | ||
it('outputs a different attribute type and value', function () { | ||
(0, _painter2['default'])(pen, { | ||
type: 'ELEMENT', | ||
name: 'div', | ||
attributes: [{ name: 'id', value: '123', diff: { type: 'changed', expectedValue: 1234 } }] | ||
}, expect.inspect); | ||
expect(pen.toString(), 'to equal', '<div id="123" // should be id={1234}\n' + '/>'); | ||
}); | ||
it('outputs a different boolean attribute', function () { | ||
@@ -343,2 +356,20 @@ | ||
it('outputs a changed type and value content', function () { | ||
(0, _painter2['default'])(pen, { | ||
type: 'ELEMENT', | ||
name: 'div', | ||
children: [{ | ||
type: 'CONTENT', | ||
value: '1234', | ||
diff: { | ||
type: 'changed', | ||
expectedValue: 123 | ||
} | ||
}] | ||
}, expect.inspect, expect.diff); | ||
expect(pen.toString(), 'to equal', '<div>\n' + ' -1234 // and mismatched type -string\n' + ' +123 // +number\n' + '</div>'); | ||
}); | ||
it('outputs a missing child', function () { | ||
@@ -345,0 +376,0 @@ |
{ | ||
"name": "unexpected-htmllike", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Helper library for unexpected plugins that perform assertions on XML like structures", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -301,2 +301,28 @@ import ArrayChanges from 'array-changes'; | ||
let onlyExact = true; | ||
const exactDiffResult = tryDiffChildren(actualAdapter, expectedAdapter, actualChildren, expectedChildren, equal, options, onlyExact); | ||
// If it wasn't a perfect match, and there were both inserts and removals, we can try allowing the children that | ||
// don't match to be "similar". | ||
if (exactDiffResult.weight.real !== 0 && exactDiffResult.insertCount && exactDiffResult.removeCount) { | ||
onlyExact = false; | ||
const changesDiffResult = tryDiffChildren(actualAdapter, expectedAdapter, actualChildren, expectedChildren, equal, options, onlyExact); | ||
if (changesDiffResult.weight.real < exactDiffResult.weight.real) { | ||
return { | ||
diff: changesDiffResult.diff, | ||
weight: changesDiffResult.weight | ||
}; | ||
} | ||
} | ||
return { | ||
diff: exactDiffResult.diff, | ||
weight: exactDiffResult.weight | ||
}; | ||
} | ||
function tryDiffChildren(actualAdapter, expectedAdapter, actualChildren, expectedChildren, equal, options, onlyExactMatches) { | ||
let diffWeights = new Weights(); | ||
@@ -312,9 +338,28 @@ const diffResult = []; | ||
function (a, b) { | ||
// Any element that is not identical, is not similar. | ||
// We could call diffElementOrWrapper again, and compare the weight to some arbitrary amount, | ||
// But the amount is dependant on the other items. What is right for one case, will almost certainly be wrong for another | ||
return false; | ||
if (onlyExactMatches) { | ||
return false; | ||
} | ||
var aIsNativeType = isNativeType(a); | ||
var bIsNativeType = isNativeType(b); | ||
// If they're native types, assume they're similar | ||
if (aIsNativeType && bIsNativeType) { | ||
return true; | ||
} | ||
// If one is an element, then don't count them as "similar" | ||
if (aIsNativeType !== bIsNativeType) { | ||
return false; | ||
} | ||
// Here we could diff and get a weight, but the weight as to what is similar is dependant on | ||
// what the other "similar" elements got, so we'll just take a simplistic view - | ||
// elements with the same name are similar, otherwise they're not | ||
return (actualAdapter.getName(a) === expectedAdapter.getName(b)); | ||
} ); | ||
let insertCount = 0; | ||
let removeCount = 0; | ||
let changeCount = 0; | ||
changes.forEach(diffItem => { | ||
@@ -326,2 +371,3 @@ | ||
case 'insert': | ||
insertCount++; | ||
itemResult = convertToDiff(expectedAdapter, diffItem.value); | ||
@@ -338,2 +384,3 @@ if (options.diffMissingChildren) { | ||
case 'remove': | ||
removeCount++; | ||
itemResult = convertToDiff(actualAdapter, diffItem.value); | ||
@@ -351,2 +398,6 @@ | ||
case 'similar': | ||
changeCount++; | ||
// fallthrough | ||
case 'equal': | ||
@@ -370,3 +421,6 @@ default: | ||
weight: diffWeights, | ||
diff: diffResult | ||
diff: diffResult, | ||
insertCount, | ||
removeCount, | ||
changeCount | ||
}; | ||
@@ -373,0 +427,0 @@ } |
@@ -147,4 +147,10 @@ | ||
}); | ||
} else if (typeof description.value === typeof description.diff.expectedValue) { | ||
this.append(diffFn(description.value, description.diff.expectedValue).diff); | ||
} else { | ||
this.append(diffFn(description.value, description.diff.expectedValue).diff); | ||
this.block(function () { | ||
this.append(diffFn('' + description.value, '' + description.diff.expectedValue).diff) | ||
}).sp().annotationBlock(function () { | ||
this.error('and mismatched type').sp().block(diffFn(typeof description.value, typeof description.diff.expectedValue).diff); | ||
}) | ||
} | ||
@@ -151,0 +157,0 @@ }); |
@@ -355,2 +355,53 @@ import Unexpected from 'unexpected'; | ||
it('diffs a changed middle child', () => { | ||
const result = getDiff( { name: 'span', attribs: {}, children: [ | ||
{ name: 'child', attribs: {}, children: ['child1'] }, | ||
{ name: 'child', attribs: {}, children: ['child2 changed'] }, | ||
{ name: 'child', attribs: {}, children: ['child3'] } | ||
] }, | ||
{ name: 'span', attribs: {}, children: [ | ||
{ name: 'child', attribs: {}, children: ['child1'] }, | ||
{ name: 'child', attribs: {}, children: ['child2'] }, | ||
{ name: 'child', attribs: {}, children: ['child3'] } | ||
] }); | ||
expect(result, 'to satisfy', { | ||
diff: { | ||
children: [ | ||
{ | ||
type: 'ELEMENT', | ||
name: 'child', | ||
children: [{ | ||
type: 'CONTENT', | ||
value: 'child1' | ||
}] | ||
}, | ||
{ | ||
type: 'ELEMENT', | ||
name: 'child', | ||
children: [{ | ||
type: 'CONTENT', | ||
value: 'child2 changed', | ||
diff: { | ||
type: 'changed', | ||
expectedValue: 'child2' | ||
} | ||
}] | ||
}, | ||
{ | ||
type: 'ELEMENT', | ||
name: 'child', | ||
diff: undefined, | ||
children: [{ | ||
type: 'CONTENT', | ||
value: 'child3' | ||
}] | ||
} | ||
] | ||
}, | ||
weight: Diff.DefaultWeights.STRING_CONTENT_MISMATCH | ||
}); | ||
}); | ||
it('diffs a missing content entry', () => { | ||
@@ -357,0 +408,0 @@ const result = getDiff( { name: 'span', attribs: {}, children: [ |
@@ -24,2 +24,3 @@ | ||
}; | ||
expect.output.preferredWidth = 80; | ||
@@ -26,0 +27,0 @@ expect.addType({ |
@@ -7,2 +7,3 @@ | ||
const expect = Unexpected.clone(); | ||
expect.output.preferredWidth = 80; | ||
@@ -9,0 +10,0 @@ describe('magicpen-linebreaker', () => { |
@@ -11,2 +11,3 @@ | ||
expect.output.preferredWidth = 80; | ||
@@ -122,3 +123,18 @@ function duplicate(object, count) { | ||
it('outputs a different attribute type and value', () => { | ||
Painter(pen, { | ||
type: 'ELEMENT', | ||
name: 'div', | ||
attributes: [ | ||
{ name: 'id', value: '123', diff: { type: 'changed', expectedValue: 1234 } } | ||
] | ||
}, expect.inspect); | ||
expect(pen.toString(), 'to equal', | ||
'<div id="123" // should be id={1234}\n' + | ||
'/>'); | ||
}); | ||
it('outputs a different boolean attribute', () => { | ||
@@ -482,2 +498,27 @@ | ||
it('outputs a changed type and value content', () => { | ||
Painter(pen, { | ||
type: 'ELEMENT', | ||
name: 'div', | ||
children: [ | ||
{ | ||
type: 'CONTENT', | ||
value: '1234', | ||
diff: { | ||
type: 'changed', | ||
expectedValue: 123 | ||
} | ||
} | ||
] | ||
}, expect.inspect, expect.diff); | ||
expect(pen.toString(), 'to equal', | ||
'<div>\n' + | ||
' -1234 // and mismatched type -string\n' + | ||
' +123 // +number\n' + | ||
'</div>'); | ||
}); | ||
it('outputs a missing child', () => { | ||
@@ -484,0 +525,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
903110
63
8287