Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsonld

Package Overview
Dependencies
Maintainers
2
Versions
214
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonld - npm Package Compare versions

Comparing version 0.0.44 to 0.0.45

coverage.html

1

js/request.js

@@ -1,2 +0,1 @@

#!/usr/bin/env node
/**

@@ -3,0 +2,0 @@ * JSON-LD extension to load JSON-LD from stdin, a URL, or a file.

{
"name": "jsonld",
"version": "0.0.44",
"version": "0.0.45",
"description": "A JSON-LD Processor and API implementation in JavaScript.",

@@ -32,2 +32,6 @@ "homepage": "http://github.com/digitalbazaar/jsonld.js",

},
"devDependencies": {
"mocha": "~1.9.0",
"visionmedia-jscoverage": "~1.0.0"
},
"engines": {

@@ -45,3 +49,6 @@ "node": "*"

"jsonld": "./bin/jsonld"
},
"scripts": {
"test": "make test"
}
}

@@ -82,2 +82,5 @@ Introduction

Tests
-----
This library includes a sample testing utility which may be used to verify

@@ -91,9 +94,27 @@ that changes to the processor maintain the correct output.

Then run the nodejs-jsonld.tests.js application and point it at the directory
containing the tests.
If the json-ld.org directory is a sibling of the jsonld.js directory:
node tests/nodejs-jsonld.tests.js {PATH_TO_JSON_LD_ORG/test-suite/tests}
make test
If you installed the test suite elsewhere:
make test JSONLD_TEST_SUITE={PATH_TO_YOUR_JSON_LD_ORG}/test-suite}
Code coverage output can be generated in `coverage.html`:
make test-cov
The Mocha output reporter can be changed to min, dot, list, nyan, etc:
make test REPORTER=dot
Remote context tests are also available:
# run the context server in the background or another terminal
node tests/remote-context-server.js
make test JSONLD_TEST_SUITE=./tests
[JSON-LD]: http://json-ld.org/
[json-ld.org]: https://github.com/json-ld/json-ld.org

@@ -8,56 +8,15 @@ /**

*/
'use strict';
var assert = require('assert');
var async = require('async');
var fs = require('fs');
var path = require('path');
var util = require('util');
var jsonld = require('../js/jsonld')();
var _jsdir = process.env.JSDIR || 'js';
var jsonld = require('../' + _jsdir + '/jsonld')();
function TestRunner() {
// set up groups, add root group
this.groups = [];
this.group('');
var JSONLD_TEST_SUITE = '../json-ld.org/test-suite';
this.passed = 0;
this.failed = 0;
this.total = 0;
};
TestRunner.prototype.group = function(name) {
this.groups.push( {
name: name,
tests: [],
count: 1
});
};
TestRunner.prototype.ungroup = function() {
this.groups.pop();
};
TestRunner.prototype.test = function(name) {
this.groups[this.groups.length - 1].tests.push(name);
this.total += 1;
var line = '';
for(var i in this.groups) {
var g = this.groups[i];
line += (line === '') ? g.name : ('/' + g.name);
}
var g = this.groups[this.groups.length - 1];
if(g.name !== '') {
var count = '' + g.count;
var end = 4 - count.length;
for(var i = 0; i < end; ++i) {
count = '0' + count;
}
line += ' ' + count;
g.count += 1;
}
line += '/' + g.tests.pop() + '... ';
process.stdout.write(line);
};
TestRunner.prototype.expandedEqual = function(x, y, isList) {
function expandedEqual(x, y, isList) {
if(x === y) {

@@ -77,3 +36,3 @@ return true;

for(var i = 0; rval && i < x.length; ++i) {
rval = this.expandedEqual(x, y, false);
rval = expandedEqual(x, y, false);
}

@@ -88,3 +47,3 @@ }

if(!(j in iso)) {
if(this.expandedEqual(x[i], y[j], false)) {
if(expandedEqual(x[i], y[j], false)) {
iso[j] = i;

@@ -111,3 +70,3 @@ rval = true;

}
if(!this.expandedEqual(x[key], y[key], key === '@list')) {
if(!expandedEqual(x[key], y[key], key === '@list')) {
return false;

@@ -120,123 +79,143 @@ }

return false;
};
}
TestRunner.prototype.check = function(test, expect, result, type) {
var line = '';
var pass = false;
function check(test, expect, result, done) {
var type = test['@type'];
var expanded = (
type.indexOf('jld:ExpandTest') !== -1 ||
type.indexOf('jld:FlattenTest') !== -1);
try {
assert.deepEqual(expect, result);
assert.equal(expect.constructor, result.constructor);
pass = true;
}
catch(ex) {
var expanded = (
type.indexOf('jld:ExpandTest') !== -1 ||
type.indexOf('jld:FlattenTest') !== -1);
if(expanded) {
pass = this.expandedEqual(expect, result);
assert(expandedEqual(expect, result));
}
assert.deepEqual(expect, result);
done();
}
finally {
if(pass) {
line += 'PASS';
this.passed += 1;
}
else {
line += 'FAIL';
this.failed += 1;
}
catch(ex) {
var expectedJson = JSON.stringify(expect, null, 2);
var resultJson = JSON.stringify(result, null, 2);
ex.expected = expectedJson;
ex.actual = resultJson;
//console.log('Expect:');
//console.log(expectedJson);
//console.log('Result:');
//console.log(actualJson);
done(ex);
}
}
console.log(line);
if(!pass) {
console.log('Expect:');
console.log(JSON.stringify(expect, null, 2));
console.log('Result:');
console.log(JSON.stringify(result, null, 2));
}
};
TestRunner.prototype.load = function(filepath) {
function load(path_) {
var manifests = [];
var stat = fs.statSync(path_);
// get full path
filepath = fs.realpathSync(filepath);
util.log('Reading test files from: "' + filepath + '"');
// read each test file from the directory
var files = fs.readdirSync(filepath);
for(var i in files) {
// TODO: read manifests as JSON-LD, process cleanly, this is hacked
var file = path.join(filepath, files[i]);
if(file.indexOf('manifest') !== -1 && path.extname(file) == '.jsonld') {
util.log('Reading manifest file: "' + file + '"');
try {
var manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
if(stat.isFile()) {
//util.log('Reading manifest from: "' + path_ + '"');
var manifest;
try {
manifest = JSON.parse(fs.readFileSync(path_, 'utf8'));
}
catch(e) {
//util.log('Exception while parsing manifest: ' + path_);
throw e;
}
manifest._path = path_;
manifests.push(manifest);
}
else if(stat.isDirectory()) {
//util.log('Reading manifests from: "' + path_ + '"');
// read each test file from the directory
var files = fs.readdirSync(path_);
for(var i in files) {
// TODO: read manifests as JSON-LD, process cleanly, this is hacked
//var file = path.join(filepath, files[i]);
var file = files[i];
if(file.indexOf('manifest') !== -1 && path.extname(file) == '.jsonld') {
//util.log('Reading manifest: "' + file + '"');
var subpath = path.join(path_, file);
manifests.push.apply(manifests, load(subpath));
}
catch(e) {
util.log('Exception while parsing manifest file: ' + file);
throw e;
}
manifest.filepath = filepath;
manifests.push(manifest);
}
}
else {
throw new Error('Unhandled path type: ' + path);
}
util.log(manifests.length + ' manifest file(s) read');
//util.log(manifests.length + ' manifests read: ' + path_);
return manifests;
};
}
/**
* Reads test JSON files.
* Reads a file.
*
* @param file the file to read.
* @param filepath the test filepath.
* @param dir the test dir (optional)
* @param path_ the file to read.
*
* @return the read JSON.
*/
var _readTestJson = function(file, filepath) {
var rval;
var _readTestData = function(dir, path_) {
try {
file = path.join(filepath, file);
rval = JSON.parse(fs.readFileSync(file, 'utf8'));
if(!path_) {
path_ = dir;
dir = null;
}
if(dir) {
path_ = path.join(dir, path_);
}
return fs.readFileSync(path_, 'utf8');
}
catch(e) {
util.log('Exception while parsing test file: ' + file);
util.log('Exception reading test test file: ' + path_);
throw e;
}
return rval;
};
/**
* Reads test N-Quads files.
* Reads test JSON files.
*
* @param file the file to read.
* @param filepath the test filepath.
* @param dir the test dir (optional)
* @param path the file to read.
*
* @return the read N-Quads.
* @return the read JSON.
*/
var _readTestNQuads = function(file, filepath) {
var rval;
var _readTestJson = function(dir, path) {
try {
file = path.join(filepath, file);
rval = fs.readFileSync(file, 'utf8');
return JSON.parse(_readTestData(dir, path));
}
catch(e) {
util.log('Exception while parsing test file: ' + file);
util.log('Exception while parsing JSON test file: ' + path);
throw e;
}
};
return rval;
/**
* Reads test N-Quads files.
*
* @param dir the test dir (optional)
* @param path the file to read.
*
* @return the read N-Quads.
*/
var _readTestNQuads = function(dir, path) {
return _readTestData(dir, path);
};
TestRunner.prototype.run = function(manifests, callback) {
/* Manifest format: {
name: <optional manifest name>,
sequence: [{
/**
* Run a manifest test file, array, or object.
*
* @param manifest the manifest path, array, or object.
* @param src the source manifest (optional)
*/
function run(manifest, src) {
/* Manifest format:
{
name: <optional manifest name>,
sequence: [test | string]
}
string: another manifest to load and run.
test format:
{
'name': <test name>,

@@ -248,142 +227,120 @@ '@type': ["test:TestCase", "jld:<type of test>"],

'expect': <expected result file>,
}]
}
*/
var self = this;
async.forEachSeries(manifests, function(manifest, callback) {
var filepath = manifest.filepath;
if('name' in manifest) {
self.group(manifest.name);
}
*/
if(typeof manifest === 'string') {
if(src) {
manifest = path.join(path.dirname(src._path), manifest);
}
async.forEachSeries(manifest.sequence, function(test, callback) {
try {
// read test input files
var type = test['@type'];
var options = {
base: 'http://json-ld.org/test-suite/tests/' + test.input
};
// check results
var checkResult = function(err, result) {
// skip error, go onto next test
if(err) {
console.log('EXCEPTION');
self.failed += 1;
outputError(err);
return callback();
}
self.check(test, test.expect, result, type);
callback();
};
if(type.indexOf('jld:ApiErrorTest') !== -1) {
util.log('Skipping test "' + test.name + '" of type: ' +
JSON.stringify(type));
return callback();
}
else if(type.indexOf('jld:NormalizeTest') !== -1) {
self.test(test.name);
input = _readTestJson(test.input, filepath);
test.expect = _readTestNQuads(test.expect, filepath);
options.format = 'application/nquads';
jsonld.normalize(input, options, checkResult);
}
else if(type.indexOf('jld:ExpandTest') !== -1) {
self.test(test.name);
input = _readTestJson(test.input, filepath);
test.expect = _readTestJson(test.expect, filepath);
jsonld.expand(input, options, checkResult);
}
else if(type.indexOf('jld:CompactTest') !== -1) {
self.test(test.name);
input = _readTestJson(test.input, filepath);
test.context = _readTestJson(test.context, filepath);
test.expect = _readTestJson(test.expect, filepath);
options.optimize = test.optimize || false;
jsonld.compact(input, test.context, options, checkResult);
}
else if(type.indexOf('jld:FlattenTest') !== -1) {
self.test(test.name);
input = _readTestJson(test.input, filepath);
test.expect = _readTestJson(test.expect, filepath);
jsonld.flatten(input, null, options, checkResult);
}
else if(type.indexOf('jld:FrameTest') !== -1) {
self.test(test.name);
input = _readTestJson(test.input, filepath);
test.frame = _readTestJson(test.frame, filepath);
test.expect = _readTestJson(test.expect, filepath);
jsonld.frame(input, test.frame, options, checkResult);
}
else if(type.indexOf('jld:FromRDFTest') !== -1) {
self.test(test.name);
input = _readTestNQuads(test.input, filepath);
test.expect = _readTestJson(test.expect, filepath);
jsonld.fromRDF(input, options, checkResult);
}
else if(type.indexOf('jld:ToRDFTest') !== -1) {
self.test(test.name);
input = _readTestJson(test.input, filepath);
test.expect = _readTestNQuads(test.expect, filepath);
options.format = 'application/nquads';
options.collate = true;
jsonld.toRDF(input, options, checkResult);
}
else {
util.log('Skipping test "' + test.name + '" of type: ' +
JSON.stringify(type));
return callback();
}
}
catch(ex) {
callback(ex);
}
}, function(err) {
if(err) {
return callback(err);
}
if('name' in manifest) {
self.ungroup();
}
callback();
run(load(manifest), src);
}
else if(Array.isArray(manifest)) {
manifest.forEach(function(el) {
run(el, src);
});
}, function(err) {
callback(err);
});
};
function outputError(err) {
console.log();
if(err.stack !== undefined) {
console.log(err.stack);
}
else if(typeof manifest === 'object') {
if(!manifest.name) {
throw new Error('Unnamed test found: ' +
JSON.stringify(manifest, null, 2));
}
if(manifest['@type'].indexOf('mf:Manifest') !== -1) {
describe(manifest.name, function() {
manifest.sequence.forEach(function(el) {
run(el, manifest);
});
});
}
else {
_run(manifest, src);
}
}
else {
console.log(err);
throw new Error('Unknown manifest type: ' + typeof manifest);
}
if('details' in err) {
console.log(util.inspect(err, false, 10));
}
return;
}
// load and run tests
try {
var tr = new TestRunner();
tr.group('JSON-LD');
// FIXME: use commander module
if(process.argv.length < 2) {
throw 'Usage: node nodejs-jsonld-tests <test directory>';
/**
* Run a test.
*
* @param test the test specification object.
* @param src the source manifest (optional)
*/
function _run(test, src) {
// read test input files
var type = test['@type'];
var options = {};
if(src) {
options.base = src.baseIri + test.input;
}
var dir = process.argv[2];
tr.run(tr.load(dir), function(err) {
if(err) {
throw err;
var dir = src ? path.dirname(src._path) : null;
// check results
var checkResult = function(expect, done) {
return function(err, result) {
if(err) {
return done(err);
}
check(test, expect, result, done);
};
};
it(test.name, function(done) {
if(type.indexOf('jld:ApiErrorTest') !== -1) {
util.log('Skipping test "' + test.name + '" of type: ' +
JSON.stringify(type));
}
tr.ungroup();
console.log(util.format('Done. Total:%s Passed:%s Failed:%s',
tr.total, tr.passed, tr.failed));
else if(type.indexOf('jld:NormalizeTest') !== -1) {
var input = _readTestJson(dir, test.input);
var expect = _readTestNQuads(dir, test.expect);
options.format = 'application/nquads';
jsonld.normalize(input, options, checkResult(expect, done));
}
else if(type.indexOf('jld:ExpandTest') !== -1) {
var input = _readTestJson(dir, test.input);
var expect = _readTestJson(dir, test.expect);
jsonld.expand(input, options, checkResult(expect, done));
}
else if(type.indexOf('jld:CompactTest') !== -1) {
var input = _readTestJson(dir, test.input);
var context = _readTestJson(dir, test.context);
var expect = _readTestJson(dir, test.expect);
options.optimize = test.optimize || false;
jsonld.compact(input, context, options, checkResult(expect, done));
}
else if(type.indexOf('jld:FlattenTest') !== -1) {
var input = _readTestJson(dir, test.input);
var expect = _readTestJson(dir, test.expect);
jsonld.flatten(input, null, options, checkResult(expect, done));
}
else if(type.indexOf('jld:FrameTest') !== -1) {
var input = _readTestJson(dir, test.input);
var frame = _readTestJson(dir, test.frame);
var expect = _readTestJson(dir, test.expect);
jsonld.frame(input, frame, options, checkResult(expect, done));
}
else if(type.indexOf('jld:FromRDFTest') !== -1) {
var input = _readTestNQuads(dir, test.input);
var expect = _readTestJson(dir, test.expect);
jsonld.fromRDF(input, options, checkResult(expect, done));
}
else if(type.indexOf('jld:ToRDFTest') !== -1) {
var input = _readTestJson(dir, test.input);
var expect = _readTestNQuads(dir, test.expect);
options.format = 'application/nquads';
options.collate = true;
jsonld.toRDF(input, options, checkResult(expect, done));
}
else {
util.log('Skipping test "' + test.name + '" of type: ' +
JSON.stringify(type));
}
});
}
catch(ex) {
outputError(ex);
}
describe('JSON-LD', function() {
var path =
process.env.JSONLD_TEST_SUITE ||
JSONLD_TEST_SUITE;
run(path);
});

@@ -9,3 +9,3 @@ var express = require('express');

server.use(CORS);
server.use(express.static(path.resolve('./contexts')));
server.use(express.static(path.resolve(path.join(__dirname, 'contexts'))));
});

@@ -12,0 +12,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc