Skip to content

Commit

Permalink
Merge pull request #4 from justinwilaby/feature/unit-tests
Browse files Browse the repository at this point in the history
Feature/unit tests
  • Loading branch information
Justin Wilaby committed Aug 11, 2018
2 parents 056de4f + ac65474 commit c88a3bc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "sax-wasm",
"version": "1.0.6",
"version": "1.0.8",
"repository": "https://github.com/justinwilaby/sax-wasm",
"description": "An extremely fast JSX, HTML and XML parser written in Rust compiled to WebAssembly for Node and the Web",
"main": "index.js",
"main": "lib/index.js",
"files": [
"lib/index.js",
"lib/saxWasm.js",
Expand Down
32 changes: 25 additions & 7 deletions src/js/__test__/jsx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {Attribute, SaxEventType, SAXParser} from '../index';
import {SaxEventType, SAXParser, Tag} from '../index';
import * as fs from 'fs';
import * as path from 'path';

const saxWasm = fs.readFileSync(path.resolve(__dirname, '../../../lib/sax-wasm.wasm'));
describe('When parsing JSX, the SaxWasm', () => {
let parser: SAXParser;
let _event: number;
let _data: Attribute[];
let _data: Tag[];
beforeEach(async () => {
parser = new SAXParser(SaxEventType.CloseTag);
_data = [] as Attribute[];
_data = [] as Tag[];
_event = 0;

parser.eventHandler = function (event: SaxEventType, data: Attribute) {
parser.eventHandler = function (event: SaxEventType, data: Tag) {
_event = event as number;
_data.push(data);
};
Expand All @@ -21,13 +21,31 @@ describe('When parsing JSX, the SaxWasm', () => {

it('should recognize child tags within Javascriopt', () => {
parser.write(`
<Component>
{this.authenticated ? <User props={this.userProps}/> : <SignIn props={this.signInProps}/>
</Component>`);
<Component>
{this.authenticated ? <User props={this.userProps}/> : <SignIn props={this.signInProps}/>
</Component>`);

expect(_event).toBe(SaxEventType.CloseTag);
expect(_data[0].name).toBe('User');
expect(_data[1].name).toBe('SignIn');
expect(_data[2].name).toBe('Component');
});

it('should recognize tags within javascript', () => {
parser.write(`
<ul>
{(function (
if (this.index > 1) {
return (<li>{this.getLabel()}</li>)
}
return <li>{this.getDefault()}</li>
))()}
</ul>
`);

expect(_event).toBe(SaxEventType.CloseTag);
expect(_data[0].name).toBe('li');
expect(_data[1].name).toBe('li');
expect(_data[2].name).toBe('ul');
});
});
24 changes: 24 additions & 0 deletions src/js/__test__/procInst.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {SaxEventType, SAXParser} from '../index';
import * as fs from 'fs';
import * as path from 'path';

const saxWasm = fs.readFileSync(path.resolve(__dirname, '../../../lib/sax-wasm.wasm'));
describe('When parsing JSX, the SaxWasm', () => {
let parser: SAXParser;
let _event: number;
let _data: string;
beforeEach(async () => {
parser = new SAXParser(SaxEventType.ProcessingInstruction);
parser.eventHandler = function (event: SaxEventType, data: string) {
_event = event as number;
_data = data;
};
return parser.prepareWasm(saxWasm);
});

it('should recognize Processing Instructions', () => {
parser.write('<?xml version="1.0" encoding="utf-8"?>');
expect(_event).toBe(SaxEventType.ProcessingInstruction);
expect(_data).toBe('version="1.0" encoding="utf-8"');
});
});
7 changes: 2 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ async function runProgram() {
const wasm = fs.readFileSync(path.resolve(__dirname, '../lib/sax-wasm.wasm'));
result = await WebAssembly.instantiate(wasm, imports);
const linearMemory = result.instance.exports.memory;
const document = `
<Component>
{this.authenticated ? <User props={this.userProps}/> : <SignIn props={this.signInProps}/>
</Component>`;
const document = `<?xml version="1.0" encoding="utf-8"?>`;
const docBuff = Buffer.from(document);
const memBuff = new Uint8Array(linearMemory.buffer, 0, docBuff.length);
const s = memBuff.set(docBuff, 0);
result.instance.exports.parser(0b100000000);
result.instance.exports.parser(0b111111111111);
result.instance.exports.write(0, memBuff.length);
}

Expand Down

0 comments on commit c88a3bc

Please sign in to comment.