Skip to content

Commit

Permalink
Fix PrefixMap#list, add tests for PrefixMap
Browse files Browse the repository at this point in the history
  • Loading branch information
awwright committed Apr 24, 2019
1 parent 5f7036c commit 049b0ae
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ api.PrefixMap.prototype.set = function(prefix, iri){
}
api.PrefixMap.prototype.list = function(){
var list = [];
return this.prefixMap.forEach(function(expansion, prefix){
this.prefixMap.forEach(function(expansion, prefix){
list.push(prefix);
});
return list;
Expand Down Expand Up @@ -127,7 +127,7 @@ api.TermMap.prototype.set = function(term, iri){
}
api.TermMap.prototype.list = function(){
var list = [];
return this.prefixMap.forEach(function(definition, term){
this.prefixMap.forEach(function(definition, term){
list.push(term);
});
return list;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"license": "Unlicense",
"devDependencies": {
"mocha": "^5.2.0",
"nyc": "^13.1.0"
"nyc": "^14.0.0"
}
}
127 changes: 127 additions & 0 deletions test/PrefixMap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

var assert = require('assert');
var rdf = require('..');

/*
[NoInterfaceObject]
interface PrefixMap {
omittable getter DOMString get (DOMString prefix);
omittable setter void set (DOMString prefix, DOMString iri);
omittable deleter void remove (DOMString prefix);
DOMString resolve (DOMString curie);
DOMString shrink (DOMString iri);
void setDefault (DOMString iri);
PrefixMap addAll (PrefixMap prefixes, optional boolean override);
};
*/

describe('PrefixMap', function(){
it('PrefixMap#get(string, string)', function(){
var map = new rdf.PrefixMap;
assert.equal(typeof map.get, 'function');
});
it('PrefixMap#set(string, string)', function(){
var map = new rdf.PrefixMap;
assert.equal(typeof map.set, 'function');
assert.equal(typeof map.set('ex', 'http://example.com/'), 'undefined');
assert.equal(map.resolve('ex:type'), 'http://example.com/type');
});
it('PrefixMap#setDefault(string)', function(){
var map = new rdf.PrefixMap;
assert.equal(typeof map.set, 'function');
assert.equal(typeof map.setDefault('http://example.com/'), 'undefined');
assert.equal(map.resolve(':type'), 'http://example.com/type');
});
it("PrefixMap#set negative tests", function(){
var map = new rdf.PrefixMap;
assert.equal(typeof map.set, 'function');
assert.throws(function(){
map.set('0', 'http://example.com/');
});
assert.throws(function(){
map.set('::', 'http://example.com/');
});
assert.throws(function(){
map.set(' ', 'http://example.com/');
});
assert.throws(function(){
map.set('.', 'http://example.com/');
});
});
it("PrefixMap#remove(string)", function(){
var map = new rdf.PrefixMap;
map.set('prefix', 'http://example.com/vocab/foo/');
map.remove('prefix');
assert.strictEqual(map.resolve('prefix:foo'), null);
});
it("PrefixMap#resolve(string)", function(){
var map = new rdf.PrefixMap;
map.set('ex', 'http://example.com/');
assert.equal(typeof map.resolve, 'function');
assert.equal(typeof map.resolve('ex:type'), 'string');
assert.equal(map.resolve('undefinedTerm'), null);
});
it("PrefixMap#shrink(string)", function(){
var map = new rdf.PrefixMap;
map.set('ex2', 'http://example.com/vocab/foo/');
map.set('ex', 'http://example.com/');
map.set('exv', 'http://example.com/vocab/');
map.set('🐉', 'http://example.com/vocab/dragon/');

assert.equal(map.shrink('http://example.com/vocab/a'), 'exv:a');
assert.equal(map.shrink('http://example.com/vocab/foo/b'), 'ex2:b');
assert.equal(map.shrink('http://example.com/c'), 'ex:c');
// File is UTF-8 (probably), but escape sequence UTF-16 surrogate pairs
// idk I didn't invent it, man
assert.equal(map.shrink('http://example.com/vocab/dragon/🐲'), '\uD83D\uDC09:\uD83D\uDC32');
assert.equal(map.shrink('http://example.com/vocab/dragon/🐲🐧'), '\uD83D\uDC09:\uD83D\uDC32\uD83D\uDC27');
});
it("PrefixMap#addAll", function(){
var map = new rdf.PrefixMap;
assert.equal(typeof map.addAll, 'function');
map.set('ex', 'http://example.com/');

var other = new rdf.PrefixMap;
other.set('ex', 'http://example.org/vocab/');
other.set('fx', 'http://example.org/vocab/');

map.addAll(other);
assert.equal(map.resolve('ex:a'), 'http://example.com/a');
assert.equal(map.resolve('fx:a'), 'http://example.org/vocab/a');
assert.strictEqual(map.resolve('c:foo'), null);
});
it("PrefixMap#addAll (overwrite=false)", function(){
var map = new rdf.PrefixMap;
map.set('ex', 'http://example.com/');

var other = new rdf.PrefixMap;
other.set('ex', 'http://example.org/vocab/');
other.set('fx', 'http://example.org/vocab/');

map.addAll(other, false);
assert.equal(map.resolve('ex:a'), 'http://example.com/a');
assert.equal(map.resolve('fx:a'), 'http://example.org/vocab/a');
assert.strictEqual(map.resolve('c:foo'), null);
});
it("PrefixMap#addAll (overwrite=true)", function(){
var map = new rdf.PrefixMap;
map.set('ex', 'http://example.com/');

var other = new rdf.PrefixMap;
other.set('ex', 'http://example.org/vocab/');
other.set('fx', 'http://example.org/vocab/');

map.addAll(other, true);
assert.equal(map.resolve('ex:a'), 'http://example.org/vocab/a');
assert.equal(map.resolve('fx:a'), 'http://example.org/vocab/a');
assert.strictEqual(map.resolve('c:foo'), null);
});
it("PrefixMap#list", function(){
var map = new rdf.PrefixMap;
assert.equal(map.list().length, 0);
map.set('ex', 'http://example.com/');
var list = map.list();
assert.equal(list.length, 1);
assert.equal(list[0], 'ex');
});
});

0 comments on commit 049b0ae

Please sign in to comment.