Skip to content

Commit

Permalink
Migrate mocha -> ava (and use nyc instead of istanbul)
Browse files Browse the repository at this point in the history
  • Loading branch information
chbrown committed May 7, 2018
1 parent 07a98ed commit 29f919e
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 552 deletions.
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@
"unorm": "^1.4.1"
},
"devDependencies": {
"@types/mocha": "^2.2.43",
"@types/node": "^9.6.12",
"coveralls": "2.11.8",
"istanbul": "0.4.2",
"mocha": "2.4.5",
"mocha-lcov-reporter": "1.2.0",
"ava": "1.0.0-beta.4",
"coveralls": "^3.0.1",
"nyc": "^11.7.1",
"pdfi-dev": "0.0.3",
"typescript": "2.8.3"
},
"scripts": {
"prepare": "tsc -d",
"pretest": "tsc",
"test": "istanbul cover _mocha -- test/ -R spec",
"posttest": "coveralls < coverage/lcov.info || true",
"test": "nyc ava",
"posttest": "nyc report --reporter=text-lcov | coveralls || true",
"clean": "tsc -d --listEmittedFiles | sed 's/^TSFILE: //' | xargs rm -v; rm -rfv coverage"
}
}
161 changes: 79 additions & 82 deletions test/cmap.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {deepEqual} from 'assert';
import test, {ExecutionContext} from 'ava';

import {parseCMap} from '../parsers/index';

function check(input: string, expected: any) {
function check(input: string, expected: any, t: ExecutionContext) {
var output = parseCMap(Buffer.from(input));
var message = `parse result does not match expected output.
parse("${input}") => ${JSON.stringify(output)}
but should == ${JSON.stringify(expected)}`;
deepEqual(output, expected, message);
t.deepEqual(output, expected, message);
}

describe('CMap parsing:', () => {
it('should parse simple byteLength=1 bfrange', () => {
var input = `/CIDInit /ProcSet findresource begin
test('CMap parser should parse simple byteLength=1 bfrange', t => {
var input = `/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CMapType 2 def
Expand All @@ -29,22 +28,22 @@ endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
end end`;
var expected = {
"codeSpaceRanges": [{"low":0, "high":255}],
"mappings": [
{"src":0, "dst":"−", "byteLength":1},
{"src":1, "dst":"·", "byteLength":1},
{"src":2, "dst":"×", "byteLength":1},
{"src":20, "dst":"≤", "byteLength":1},
{"src":21, "dst":"≥", "byteLength":1},
],
"byteLength": 1,
};
check(input, expected);
});
var expected = {
"codeSpaceRanges": [{"low":0, "high":255}],
"mappings": [
{"src":0, "dst":"−", "byteLength":1},
{"src":1, "dst":"·", "byteLength":1},
{"src":2, "dst":"×", "byteLength":1},
{"src":20, "dst":"≤", "byteLength":1},
{"src":21, "dst":"≥", "byteLength":1},
],
"byteLength": 1,
};
check(input, expected, t);
});

it('should parse simple byteLength=2 bfchar', () => {
var input = `/CIDInit /ProcSet findresource begin
test('CMap parser should parse simple byteLength=2 bfchar', t => {
var input = `/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
Expand All @@ -64,21 +63,21 @@ endcmap
CMapName currentdict /CMap defineresource pop
end
end`;
var expected = {
"codeSpaceRanges": [{"low":0, "high":65535}],
"mappings": [
{"src":271, "dst":"b", "byteLength":2},
{"src":1004, "dst":"0", "byteLength":2},
{"src":1005, "dst":"1", "byteLength":2},
{"src":1006, "dst":"2", "byteLength":2},
],
"byteLength": 2,
};
check(input, expected);
});
var expected = {
"codeSpaceRanges": [{"low":0, "high":65535}],
"mappings": [
{"src":271, "dst":"b", "byteLength":2},
{"src":1004, "dst":"0", "byteLength":2},
{"src":1005, "dst":"1", "byteLength":2},
{"src":1006, "dst":"2", "byteLength":2},
],
"byteLength": 2,
};
check(input, expected, t);
});

it('should parse single bfchar', () => {
var input = `/CIDInit /ProcSet findresource begin
test('CMap parser should parse single bfchar', t => {
var input = `/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
Expand All @@ -93,24 +92,24 @@ endcodespacerange
<0078> <2022>
endbfchar
endcmap CMapName currentdict /CMap defineresource pop end end`;
var expected = {
"codeSpaceRanges": [{"low":0, "high":65535}],
"mappings": [
{"src":120, "dst":"•", "byteLength":2},
],
"byteLength": 2,
};
check(input, expected);
});
var expected = {
"codeSpaceRanges": [{"low":0, "high":65535}],
"mappings": [
{"src":120, "dst":"•", "byteLength":2},
],
"byteLength": 2,
};
check(input, expected, t);
});

it('should parse multiple bfchars and bfrange', () => {
var input = `/CIDInit /ProcSet findresource begin
test('CMap parser should parse multiple bfchars and bfrange', t => {
var input = `/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo <<
/Registry (Adobe)
/Ordering (UCS)
/Supplement 0
/Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
Expand All @@ -135,27 +134,27 @@ endcmap
CMapName currentdict /CMap defineresource pop
end
end`;
var expected = {
"codeSpaceRanges": [
{"low": 0, "high": 255},
],
"mappings": [
{"src": 44, "dst": "\t\r  ", "byteLength": 1},
{"src": 67, "dst": "-­‐", "byteLength": 1},
{"src": 33, "dst": "1", "byteLength": 1},
{"src": 34, "dst": ".", "byteLength": 1},
{"src": 35, "dst": "I", "byteLength": 1},
{"src": 36, "dst": "n", "byteLength": 1},
{"src": 37, "dst": "t", "byteLength": 1},
{"src": 38, "dst": "r", "byteLength": 1},
],
"byteLength": 1,
};
check(input, expected);
});
var expected = {
"codeSpaceRanges": [
{"low": 0, "high": 255},
],
"mappings": [
{"src": 44, "dst": "\t\r  ", "byteLength": 1},
{"src": 67, "dst": "-­‐", "byteLength": 1},
{"src": 33, "dst": "1", "byteLength": 1},
{"src": 34, "dst": ".", "byteLength": 1},
{"src": 35, "dst": "I", "byteLength": 1},
{"src": 36, "dst": "n", "byteLength": 1},
{"src": 37, "dst": "t", "byteLength": 1},
{"src": 38, "dst": "r", "byteLength": 1},
],
"byteLength": 1,
};
check(input, expected, t);
});

it('should parse multiple code space ranges', () => {
var input = `%!PS-Adobe-3.0 Resource-CMap
test('CMap parser should parse multiple code space ranges', t => {
var input = `%!PS-Adobe-3.0 Resource-CMap
%%DocumentNeededResources: ProcSet (CIDInit)
%%IncludeResource: ProcSet (CIDInit)
%%BeginResource: CMap (90ms-RKSJ-H)
Expand Down Expand Up @@ -197,19 +196,17 @@ CMapName currentdict /CMap defineresource pop end
end
%%EndResource
%%EOF`;
var expected = {
"codeSpaceRanges": [
{"low": 0, "high": 128},
{"low": 33088, "high": 40956},
{"low": 160, "high": 223},
{"low": 57408, "high": 64764},
],
"mappings": [],
"byteLength": 1,
};
check(input, expected);
});

var expected = {
"codeSpaceRanges": [
{"low": 0, "high": 128},
{"low": 33088, "high": 40956},
{"low": 160, "high": 223},
{"low": 57408, "high": 64764},
],
"mappings": [],
"byteLength": 1,
};
check(input, expected, t);
});

// /CIDInit /ProcSet findresource begin
Expand Down
54 changes: 24 additions & 30 deletions test/consumers.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
import {strictEqual} from 'assert';
import test from 'ava';

import {BufferIterator} from 'lexing';
import {consumeString} from '../parsers/consumers';

function assertBuffersEqual(actual, expected, message?: string) {
return strictEqual(actual.toString('hex'), expected.toString('hex'), message);
}

describe('consumers', () => {
it('should consume a typical PDF string', () => {
const iterable = BufferIterator.fromString('Hello world) >>\n2 0 obj');
const expected = Buffer.from('Hello world', 'ascii');
const actual = consumeString(iterable);
assertBuffersEqual(actual, expected);
});
test('should consume a typical PDF string', t => {
const iterable = BufferIterator.fromString('Hello world) >>\n2 0 obj');
const expected = Buffer.from('Hello world', 'ascii');
const actual = consumeString(iterable);
t.deepEqual(actual, expected);
});

it('should consume a nested PDF string', () => {
const iterable = BufferIterator.fromString('Hello (world)!) >>');
const expected = Buffer.from('Hello (world)!', 'ascii');
const actual = consumeString(iterable);
assertBuffersEqual(actual, expected);
});
test('should consume a nested PDF string', t => {
const iterable = BufferIterator.fromString('Hello (world)!) >>');
const expected = Buffer.from('Hello (world)!', 'ascii');
const actual = consumeString(iterable);
t.deepEqual(actual, expected);
});

it('should consume a double-nested PDF string', () => {
const iterable = BufferIterator.fromString('Hello (world (hi))!) >>');
const expected = Buffer.from('Hello (world (hi))!', 'ascii');
const actual = consumeString(iterable);
assertBuffersEqual(actual, expected);
});
test('should consume a double-nested PDF string', t => {
const iterable = BufferIterator.fromString('Hello (world (hi))!) >>');
const expected = Buffer.from('Hello (world (hi))!', 'ascii');
const actual = consumeString(iterable);
t.deepEqual(actual, expected);
});

it('should consume a PDF string with unusual characters', () => {
const iterable = BufferIterator.fromString('Hello \(world\)!) >>');
const expected = Buffer.from('Hello (world)!', 'ascii');
const actual = consumeString(iterable);
assertBuffersEqual(actual, expected);
});
test('should consume a PDF string with unusual characters', t => {
const iterable = BufferIterator.fromString('Hello \(world\)!) >>');
const expected = Buffer.from('Hello (world)!', 'ascii');
const actual = consumeString(iterable);
t.deepEqual(actual, expected);
});
22 changes: 10 additions & 12 deletions test/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {strictEqual} from 'assert';
import test from 'ava';

import {swapEndian} from '../util';

describe('util', () => {
it('should swap every two bytes in a Buffer', () => {
const littleEndianWithBOM = Buffer.from('fffe680065006c006c006f00', 'hex');
const swapped = swapEndian(littleEndianWithBOM);
strictEqual(swapped.toString('hex'), 'feff00680065006c006c006f');
});
test('util should swap every two bytes in a Buffer', t => {
const littleEndianWithBOM = Buffer.from('fffe680065006c006c006f00', 'hex');
const swapped = swapEndian(littleEndianWithBOM);
t.deepEqual(swapped.toString('hex'), 'feff00680065006c006c006f');
});

it('should swap every two bytes in a Buffer except the last if there are an odd number of bytes', () => {
const brokenLittleEndianWithBOM = Buffer.from('fffe680065006c006c006f', 'hex');
const swapped = swapEndian(brokenLittleEndianWithBOM);
strictEqual(swapped.toString('hex'), 'feff00680065006c006c6f');
});
test('util should swap every two bytes in a Buffer except the last if there are an odd number of bytes', t => {
const brokenLittleEndianWithBOM = Buffer.from('fffe680065006c006c006f', 'hex');
const swapped = swapEndian(brokenLittleEndianWithBOM);
t.deepEqual(swapped.toString('hex'), 'feff00680065006c006c6f');
});
Loading

0 comments on commit 29f919e

Please sign in to comment.