-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a72055e
commit 0f59e14
Showing
2 changed files
with
5 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,3 @@ | ||
var URL = require('url'), | ||
urlTools = require('urltools'), | ||
AssetGraph = require('assetgraph'), | ||
uglifyJs = AssetGraph.JavaScript.uglifyJs, | ||
expect = module.exports = require('unexpected') | ||
.clone(); | ||
|
||
expect.addAssertion('to contain [no] (asset|assets)', function (expect, subject, queryObj, number) { | ||
this.errorMode = 'nested'; | ||
if (typeof queryObj === 'string') { | ||
queryObj = {type: queryObj}; | ||
} else if (typeof queryObj === 'number') { | ||
number = queryObj; | ||
queryObj = {}; | ||
} | ||
if (this.flags.no) { | ||
number = 0; | ||
} else if (typeof number === 'undefined') { | ||
number = 1; | ||
} | ||
expect(subject.findAssets(queryObj).length, 'to equal', number); | ||
}); | ||
|
||
expect.addAssertion('to contain (url|urls)', function (expect, subject, urls) { | ||
if (!Array.isArray(urls)) { | ||
urls = [urls]; | ||
} | ||
urls = urls.map(function (url) { | ||
return URL.resolve(this.obj.root, url); | ||
}, this); | ||
this.errorMode = 'nested'; | ||
urls.forEach(function (url) { | ||
expect(subject.findAssets({url: url}).length, 'to equal', 1); | ||
}); | ||
}); | ||
|
||
expect.addAssertion('to contain [no] (relation|relations) [including unresolved]', function (expect, subject, queryObj, number) { | ||
if (typeof queryObj === 'string') { | ||
queryObj = {type: queryObj}; | ||
} else if (typeof queryObj === 'number') { | ||
number = queryObj; | ||
queryObj = {}; | ||
} | ||
if (this.flags.no) { | ||
number = 0; | ||
} else if (typeof number === 'undefined') { | ||
number = 1; | ||
} | ||
this.errorMode = 'nested'; | ||
expect(subject.findRelations(queryObj, this.flags['including unresolved']).length, 'to equal', number); | ||
}); | ||
|
||
function toAst(assetOrFunctionOrAst) { | ||
if (assetOrFunctionOrAst.isAsset) { | ||
return assetOrFunctionOrAst.parseTree; | ||
} else if (typeof assetOrFunctionOrAst === 'function') { | ||
return uglifyJs.parse(assetOrFunctionOrAst.toString().replace(/^function[^\(]*?\(\)\s*\{|\}$/g, '')); | ||
} else { | ||
return assetOrFunctionOrAst; | ||
} | ||
} | ||
|
||
expect.addAssertion('to have the same AST as', function (expect, subject, value) { | ||
expect(toAst(subject).print_to_string(), 'to equal', toAst(value).print_to_string()); | ||
}); | ||
|
||
expect.addType({ | ||
identify: function (obj) { | ||
return obj && obj.isAsset; | ||
}, | ||
equal: function (a, b) { | ||
return ( | ||
a.type === b.type && | ||
a.urlOrDescription === b.urlOrDescription && | ||
(a.isText ? a.text : a.rawSrc) === (b.isText ? b.text : b.rawSrc) | ||
// && same outgoing relations | ||
); | ||
}, | ||
inspect: function (asset) { | ||
return asset.urlOrDescription; | ||
}, | ||
toJSON: function (asset) { | ||
return { | ||
$Asset: { | ||
type: asset.type, | ||
urlOrDescription: asset.urlOrDescription, | ||
outgoingRelations: asset.outgoingRelations | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
expect.addType({ | ||
identify: function (obj) { | ||
return obj && obj.isAssetGraph; | ||
}, | ||
inspect: function (assetGraph) { | ||
return ['AssetGraph'].concat(assetGraph.findAssets({isInline: false}).map(function (asset) { | ||
return ' ' + (asset.isLoaded ? ' ' : '!') + ' ' + urlTools.buildRelativeUrl(assetGraph.root, asset.url); | ||
}, this)).join('\n '); | ||
}, | ||
toJSON: function (assetGraph) { | ||
return { | ||
$AssetGraph: { | ||
assets: assetGraph.findAssets({isInline: false}), | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
expect.addType({ | ||
identify: function (obj) { | ||
return obj && obj.isRelation; | ||
}, | ||
equal: function (a, b) { | ||
return a === b; | ||
}, | ||
inspect: function (relation) { | ||
return relation.toString(); | ||
}, | ||
toJSON: function (relation) { | ||
return { | ||
$Relation: { | ||
id: relation.id, | ||
type: relation.type, | ||
from: relation.from.urlOrDescription, | ||
to: relation.to.urlOrDescription | ||
} | ||
}; | ||
} | ||
}); | ||
module.exports = require('unexpected') | ||
.clone() | ||
.installPlugin(require('assetgraph/test/unexpectedAssetGraph')); |