| 1 |
import { StreamWriter, Quad, NamedNode, termFromId } from '../src/'; |
| 2 |
import { Readable, Writable } from 'readable-stream'; |
| 3 |
|
| 4 |
describe('StreamWriter', () => { |
| 5 |
describe('The StreamWriter export', () => { |
| 6 |
it('should be a function', () => { |
| 7 |
StreamWriter.should.be.a('function'); |
| 8 |
}); |
| 9 |
|
| 10 |
it('should be a StreamWriter constructor', () => { |
| 11 |
new StreamWriter().should.be.an.instanceof(StreamWriter); |
| 12 |
}); |
| 13 |
}); |
| 14 |
|
| 15 |
describe('A StreamWriter instance', () => { |
| 16 |
it('should serialize 0 triples', |
| 17 |
shouldSerialize('')); |
| 18 |
|
| 19 |
it('should serialize 1 triple', |
| 20 |
shouldSerialize(['abc', 'def', 'ghi'], |
| 21 |
'<abc> <def> <ghi>.\n')); |
| 22 |
|
| 23 |
it('should serialize 2 triples', |
| 24 |
shouldSerialize(['abc', 'def', 'ghi'], |
| 25 |
['jkl', 'mno', 'pqr'], |
| 26 |
'<abc> <def> <ghi>.\n' + |
| 27 |
'<jkl> <mno> <pqr>.\n')); |
| 28 |
|
| 29 |
it('should serialize 3 triples', |
| 30 |
shouldSerialize(['abc', 'def', 'ghi'], |
| 31 |
['jkl', 'mno', 'pqr'], |
| 32 |
['stu', 'vwx', 'yz'], |
| 33 |
'<abc> <def> <ghi>.\n' + |
| 34 |
'<jkl> <mno> <pqr>.\n' + |
| 35 |
'<stu> <vwx> <yz>.\n')); |
| 36 |
|
| 37 |
it('should use prefixes when possible', |
| 38 |
shouldSerialize({ prefixes: { a: 'http://a.org/', b: new NamedNode('http://a.org/b#'), c: 'http://a.org/b' } }, |
| 39 |
['http://a.org/bc', 'http://a.org/b#ef', 'http://a.org/bhi'], |
| 40 |
['http://a.org/bc/de', 'http://a.org/b#e#f', 'http://a.org/b#x/t'], |
| 41 |
['http://a.org/3a', 'http://a.org/b#3a', 'http://a.org/b#a3'], |
| 42 |
'@prefix a: <http://a.org/>.\n' + |
| 43 |
'@prefix b: <http://a.org/b#>.\n' + |
| 44 |
'@prefix c: <http://a.org/b>.\n\n' + |
| 45 |
'a:bc b:ef a:bhi.\n' + |
| 46 |
'<http://a.org/bc/de> <http://a.org/b#e#f> <http://a.org/b#x/t>.\n' + |
| 47 |
'<http://a.org/3a> <http://a.org/b#3a> b:a3.\n')); |
| 48 |
|
| 49 |
it('should take over prefixes from the input stream', done => { |
| 50 |
const inputStream = new Readable(), |
| 51 |
writer = new StreamWriter(), |
| 52 |
outputStream = new StringWriter(); |
| 53 |
writer.import(inputStream); |
| 54 |
writer.pipe(outputStream); |
| 55 |
|
| 56 |
// emit prefixes and close |
| 57 |
inputStream.emit('prefix', 'a', new NamedNode('http://a.org/')); |
| 58 |
inputStream.emit('prefix', 'b', new NamedNode('http://b.org/')); |
| 59 |
inputStream.push(null); |
| 60 |
|
| 61 |
writer.on('error', done); |
| 62 |
writer.on('end', () => { |
| 63 |
outputStream.result.should.equal('@prefix a: <http://a.org/>.\n\n' + |
| 64 |
'@prefix b: <http://b.org/>.\n\n'); |
| 65 |
done(); |
| 66 |
}); |
| 67 |
}); |
| 68 |
}); |
| 69 |
|
| 70 |
it('passes an error', () => { |
| 71 |
const input = new Readable(), writer = new StreamWriter(); |
| 72 |
let error = null; |
| 73 |
input._read = function () {}; |
| 74 |
writer.on('error', e => { error = e; }); |
| 75 |
writer.import(input); |
| 76 |
input.emit('error', new Error()); |
| 77 |
expect(error).to.be.an.instanceof(Error); |
| 78 |
}); |
| 79 |
}); |
| 80 |
|
| 81 |
|
| 82 |
function shouldSerialize(/* options?, tripleArrays..., expectedResult */) { |
| 83 |
let tripleArrays = Array.prototype.slice.call(arguments); |
| 84 |
const expectedResult = tripleArrays.pop(), |
| 85 |
options = tripleArrays[0] instanceof Array ? null : tripleArrays.shift(); |
| 86 |
|
| 87 |
tripleArrays = tripleArrays.map(i => { |
| 88 |
return new Quad(termFromId(i[0]), termFromId(i[1]), termFromId(i[2])); |
| 89 |
}); |
| 90 |
|
| 91 |
return function (done) { |
| 92 |
const inputStream = new ArrayReader(tripleArrays), |
| 93 |
writer = new StreamWriter(options), |
| 94 |
outputStream = new StringWriter(); |
| 95 |
writer.import(inputStream).should.equal(writer); |
| 96 |
writer.pipe(outputStream); |
| 97 |
writer.on('error', done); |
| 98 |
writer.on('end', () => { |
| 99 |
outputStream.result.should.equal(expectedResult); |
| 100 |
done(); |
| 101 |
}); |
| 102 |
}; |
| 103 |
} |
| 104 |
|
| 105 |
function ArrayReader(items) { |
| 106 |
const reader = new Readable({ objectMode: true }); |
| 107 |
reader._read = function () { this.push(items.shift() || null); }; |
| 108 |
return reader; |
| 109 |
} |
| 110 |
|
| 111 |
function StringWriter() { |
| 112 |
const writer = new Writable({ encoding: 'utf-8', decodeStrings: false }); |
| 113 |
writer.result = ''; |
| 114 |
writer._write = function (chunk, encoding, done) { this.result += chunk; done(); }; |
| 115 |
return writer; |
| 116 |
} |