mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
unit tests for parser generator
This commit is contained in:
parent
0f9d93b3b7
commit
d3b893223b
4 changed files with 100 additions and 6 deletions
4
Makefile
4
Makefile
|
|
@ -19,6 +19,10 @@ build: node_modules
|
||||||
test: node_modules
|
test: node_modules
|
||||||
node node_modules/.bin/mocha --opts mocha.opts
|
node node_modules/.bin/mocha --opts mocha.opts
|
||||||
|
|
||||||
|
.PHONY: unit
|
||||||
|
unit: node_modules
|
||||||
|
node node_modules/.bin/mocha --opts unit.mocha.opts
|
||||||
|
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
lint: node_modules
|
lint: node_modules
|
||||||
node_modules/.bin/tslint -p tsconfig.json
|
node_modules/.bin/tslint -p tsconfig.json
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import {Packet} from '../../Data/Packet';
|
import {Packet} from '../../Data/Packet';
|
||||||
import {PacketHandler, Parser} from './Parser';
|
import {PacketHandler, voidEncoder} from './Parser';
|
||||||
|
|
||||||
export function make(name: string, definition: string): PacketHandler {
|
export function make(name: string, definition: string): PacketHandler {
|
||||||
const parts = definition.split('}');
|
const parts = definition.split('}');
|
||||||
|
|
@ -23,26 +23,24 @@ export function make(name: string, definition: string): PacketHandler {
|
||||||
}
|
}
|
||||||
return result as Packet;
|
return result as Packet;
|
||||||
},
|
},
|
||||||
encoder: (packet, match, stream) => {
|
encoder: voidEncoder
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function readItem(stream, description, data) {
|
function readItem(stream, description, data) {
|
||||||
let length;
|
|
||||||
if (description[0] === 'b') {
|
if (description[0] === 'b') {
|
||||||
return stream.readBoolean();
|
return stream.readBoolean();
|
||||||
} else if (description[0] === 's') {
|
} else if (description[0] === 's') {
|
||||||
if (description.length === 1) {
|
if (description.length === 1) {
|
||||||
return stream.readUTF8String();
|
return stream.readUTF8String();
|
||||||
} else {
|
} else {
|
||||||
length = parseInt(description.substr(1), 10);
|
const length = parseInt(description.substr(1), 10);
|
||||||
return stream.readASCIIString(length);
|
return stream.readASCIIString(length);
|
||||||
}
|
}
|
||||||
} else if (description === 'f32') {
|
} else if (description === 'f32') {
|
||||||
return stream.readFloat32();
|
return stream.readFloat32();
|
||||||
} else if (description[0] === 'u') {
|
} else if (description[0] === 'u') {
|
||||||
length = parseInt(description.substr(1), 10);
|
const length = parseInt(description.substr(1), 10);
|
||||||
return stream.readBits(length);
|
return stream.readBits(length);
|
||||||
} else if (description[0] === '$') {
|
} else if (description[0] === '$') {
|
||||||
const variable = description.substr(1);
|
const variable = description.substr(1);
|
||||||
|
|
|
||||||
86
src/tests/unit/Parser/Packet/ParserGeneratorTest.ts
Normal file
86
src/tests/unit/Parser/Packet/ParserGeneratorTest.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
import * as assert from 'assert';
|
||||||
|
import {make} from '../../../../Parser/Packet/ParserGenerator';
|
||||||
|
import {BitStream} from 'bit-buffer';
|
||||||
|
|
||||||
|
function getStream(data: string) {
|
||||||
|
const buffer = new Buffer(data);
|
||||||
|
return new BitStream(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertParser(definition: string, stream: BitStream, expected: any, length: number) {
|
||||||
|
const {parser} = make('packetName', definition);
|
||||||
|
expected.packetType = 'packetName';
|
||||||
|
const start = stream.index;
|
||||||
|
assert.deepEqual(expected, parser(stream));
|
||||||
|
assert.equal(length, stream.index - start, 'Unexpected number of bits consumed from stream');
|
||||||
|
}
|
||||||
|
|
||||||
|
suite('Parser generator', () => {
|
||||||
|
test('Empty parser', () => {
|
||||||
|
assertParser('', getStream('dummy'), {}, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Fixed string', () => {
|
||||||
|
assertParser('foo{s3}', getStream('dummy'), {foo: 'dum'}, 3 * 8);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Null terminated string', () => {
|
||||||
|
assertParser('foo{s}', getStream('dummy'), {foo: 'dummy'}, 5 * 8);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Boolean', () => {
|
||||||
|
const stream = new BitStream(new ArrayBuffer(64));
|
||||||
|
stream.writeBoolean(1);
|
||||||
|
stream.writeBoolean(0);
|
||||||
|
stream.writeASCIIString('remaining');
|
||||||
|
stream.index = 0;
|
||||||
|
|
||||||
|
assertParser('foo{b}', stream, {foo: true}, 1);
|
||||||
|
assertParser('foo{b}', stream, {foo: false}, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Unsigned Int', () => {
|
||||||
|
const stream = new BitStream(new ArrayBuffer(64));
|
||||||
|
stream.writeUint8(0b11111111);
|
||||||
|
stream.writeUint8(0b00001100);
|
||||||
|
stream.writeUint8(0b11111111);
|
||||||
|
stream.writeASCIIString('remaining');
|
||||||
|
stream.index = 0;
|
||||||
|
|
||||||
|
assertParser('foo{u2}', stream, {foo: 3}, 2);
|
||||||
|
assertParser('foo{u8}', stream, {foo: 0b00111111}, 8);
|
||||||
|
assertParser('foo{u12}', stream, {foo: 0b0000111111000011}, 12);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Signed Int', () => {
|
||||||
|
const stream = new BitStream(new ArrayBuffer(64));
|
||||||
|
stream.writeUint8(0b11111111);
|
||||||
|
stream.writeUint8(0b00001100);
|
||||||
|
stream.writeUint8(0b11111111);
|
||||||
|
stream.writeASCIIString('remaining');
|
||||||
|
stream.index = 0;
|
||||||
|
|
||||||
|
assertParser('foo{2}', stream, {foo: -1}, 2);
|
||||||
|
assertParser('foo{8}', stream, {foo: 63}, 8);
|
||||||
|
assertParser('foo{12}', stream, {foo: -61}, 12);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Variable length', () => {
|
||||||
|
const stream = new BitStream(new ArrayBuffer(64));
|
||||||
|
stream.writeUint8(0b11111111);
|
||||||
|
stream.writeUint8(0b00001100);
|
||||||
|
stream.writeASCIIString('remaining');
|
||||||
|
stream.index = 0;
|
||||||
|
|
||||||
|
assertParser('length{u2}foo{$length}', stream, {length: 3, foo: 7}, 5);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Float32', () => {
|
||||||
|
const stream = new BitStream(new ArrayBuffer(64));
|
||||||
|
stream.writeFloat32(12.234233856201172);
|
||||||
|
stream.writeASCIIString('remaining');
|
||||||
|
stream.index = 0;
|
||||||
|
|
||||||
|
assertParser('foo{f32}', stream, {foo: 12.234233856201172}, 32);
|
||||||
|
});
|
||||||
|
});
|
||||||
6
unit.mocha.opts
Normal file
6
unit.mocha.opts
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
--require ts-node/register
|
||||||
|
--watch-extensions ts
|
||||||
|
--ui tdd
|
||||||
|
--timeout 20000
|
||||||
|
--watch
|
||||||
|
src/tests/unit/**/*.ts
|
||||||
Loading…
Add table
Add a link
Reference in a new issue