Skip to content

Commit

Permalink
Initial commit, v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Oct 6, 2019
0 parents commit 478c50b
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/testdata/
/node_modules/
/coverage/
/.nyc_output/
20 changes: 20 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": ["standard", "prettier", "prettier/standard"],
"plugins": ["import", "mocha"],
"env": {
"mocha": true
},
"rules": {
"prefer-template": "error",
"mocha/no-exclusive-tests": "error",
"mocha/no-nested-tests": "error",
"mocha/no-identical-title": "error",
"prefer-const": [
"error",
{
"destructuring": "all",
"ignoreReadBeforeAssign": false
}
]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/
/coverage/
/.nyc_output/
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
save-exact = false
package-lock = false
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/coverage/

# Don't fight npm i --save
package.json
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
167 changes: 167 additions & 0 deletions lib/unexpected-assetgraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
const urlModule = require('url');
const urlTools = require('urltools');

module.exports = {
name: 'unexpected-assetgraph',
installInto(expect) {
expect.addType({
name: 'AssetGraph.asset',
base: 'object',
identify(obj) {
return obj && obj.isAsset && obj.constructor !== Object;
},
equal(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(asset, depth, output) {
return output
.text(`${asset.type}(`)
.text(asset.urlOrDescription)
.text(')');
},
getKeys(value) {
return ['type', 'url', 'isLoaded', 'isInline', 'isRedirect'];
}
});

expect.addType({
name: 'AssetGraph',
base: 'object',
identify(obj) {
return obj && obj.isAssetGraph && obj.constructor !== Object;
},
inspect(assetGraph, depth, output) {
output.text('AssetGraph(').nl();

assetGraph.findAssets({ isInline: false }).forEach(function(asset) {
output
.text(
`${asset.isLoaded ? ' ' : '!'} ${urlTools.buildRelativeUrl(
assetGraph.root,
asset.url
)}`
)
.nl();
});

return output.text(')');
}
});

expect.addType({
name: 'AssetGraph.relation',
base: 'object',
identify(obj) {
return obj && obj.isRelation && obj.constructor !== Object;
},
equal(a, b) {
return a === b;
},
inspect(relation, depth, output) {
return output
.text(`${relation.type}(`)
.text(relation.toString())
.text(')');
},
getKeys(value) {
return ['type', 'hrefType', 'href', 'crossorigin', 'canonical'];
}
});

expect.addAssertion(
[
'<AssetGraph> to contain [no] (asset|assets) <string|object|number?>',
'<AssetGraph> to contain [no] (asset|assets) <string|object|number> <number?>'
],
(expect, subject, value, number) => {
expect.errorMode = 'nested';
if (typeof value === 'string') {
value = { type: value };
} else if (typeof value === 'number') {
number = value;
value = {};
}
if (expect.flags.no) {
return expect(subject.findAssets(value), 'to satisfy', []);
} else if (typeof number === 'undefined') {
number = 1;
}
const foundAssets = subject.findAssets(value);
expect(foundAssets, 'to have length', number);
if (number === 1) {
return foundAssets[0];
} else {
return foundAssets;
}
}
);

expect.addAssertion(
'<AssetGraph> to contain (url|urls) <string|array?>',
function(expect, subject, urls) {
if (!Array.isArray(urls)) {
urls = [urls];
}
urls = urls.map(url => new urlModule.URL(url, subject.root).toString());
expect.errorMode = 'nested';
urls.forEach(function(url) {
expect(subject.findAssets({ url }), 'to have length', 1);
});
}
);

expect.addAssertion(
'<AssetGraph> to contain [no] (relation|relations) <string|object|number?>',
function(expect, subject, queryObj) {
let number;
if (typeof queryObj === 'string') {
queryObj = { type: queryObj };
} else if (typeof queryObj === 'number') {
number = queryObj;
queryObj = {};
}
if (expect.flags.no) {
number = 0;
} else if (typeof number === 'undefined') {
number = 1;
}
expect.errorMode = 'nested';
const foundRelations = subject.findRelations(queryObj);
expect(foundRelations, 'to have length', number);
if (number === 1) {
return foundRelations[0];
} else {
return foundRelations;
}
}
);

expect.addAssertion(
'<AssetGraph> to contain [no] (relation|relations) [including unresolved] <string|object|number> <number?>',
function(expect, subject, queryObj, number) {
if (typeof queryObj === 'string') {
queryObj = { type: queryObj };
} else if (typeof queryObj === 'number') {
number = queryObj;
queryObj = {};
}
if (expect.flags.no) {
number = 0;
} else if (typeof number === 'undefined') {
number = 1;
}
expect.errorMode = 'nested';
expect(
subject.findRelations(queryObj, expect.flags['including unresolved']),
'to have length',
number
);
}
);
}
};
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "unexpected-assetgraph",
"version": "1.0.0",
"description": "Unexpected assertions for assetgraph and plugins",
"main": "lib/unexpected-assetgraph.js",
"directories": {
"test": "test"
},
"dependencies": {
"urltools": "^0.4.1"
},
"devDependencies": {
"eslint": "^6.0.0",
"eslint-config-prettier": "^5.0.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-mocha": "^5.3.0",
"eslint-plugin-node": "^9.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"mocha": "^6.0.0",
"nyc": "^14.0.0",
"prettier": "^1.14.3"
},
"scripts": {
"lint": "eslint . && prettier --check '**/*.js'",
"test": "mocha",
"ci": "npm test && npm run lint && npm run coverage",
"coverage": "NODE_ENV=development nyc --reporter=lcov --reporter=text --all -- npm test && echo google-chrome coverage/lcov-report/index.html"
},
"repository": {
"type": "git",
"url": "git://github.com/assetgraph/assetgraph-traverse.git"
},
"keywords": [
"assetgraph",
"unexpected",
"assertions"
],
"author": "Andreas Lind <[email protected]>",
"license": "BSD-3-Clause",
"nyc": {
"include": [
"lib/**"
]
}
}

0 comments on commit 478c50b

Please sign in to comment.