Skip to content

Commit

Permalink
Merge pull request #1 from syumai/add-test
Browse files Browse the repository at this point in the history
Add test and fix indexing bug
  • Loading branch information
syumai authored Dec 30, 2018
2 parents 30db9ef + 5cd9674 commit 22e5856
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 9 deletions.
15 changes: 6 additions & 9 deletions dpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ async function renderInternal(body: Reader, params: Params): Promise<Reader> {
return {
async read(p: Uint8Array): Promise<ReadResult> {
const len = p.byteLength;
let nread: number;
let nread = 0;

for (nread = 0; nread < len; nread++) {
while (nread < len) {
const { eof } = await src.read(readBuf);
if (eof) {
break;
Expand All @@ -66,23 +66,23 @@ async function renderInternal(body: Reader, params: Params): Promise<Reader> {
break;
case Codes.Comment:
readMode = ReadMode.Comment;
break;
default:
continue;
}
buf.splice(0);
nread -= 3;
continue;
}
if (buf.length > 2) {
p[nread - 2] = buf.shift();
p[nread] = buf.shift();
nread++;
}
continue;
}

// Finish current ReadMode
if (buf[1] === Codes.Percent && buf[2] === Codes.End) {
statement.push(buf.shift());
nread--;
buf.splice(0);
// Don't execute if ReadMode is Comment.
if (readMode !== ReadMode.Comment) {
Expand All @@ -100,17 +100,14 @@ async function renderInternal(body: Reader, params: Params): Promise<Reader> {
}
statement.splice(0);
readMode = ReadMode.Normal;
nread -= 3;
continue;
}
statement.push(buf.shift());
nread--;
}

// Flush buffer
while (nread < len && buf.length > 0) {
p[nread - 2] = buf.shift();
console.log(nread);
p[nread] = buf.shift();
nread++;
}
return { nread, eof: nread === 0 };
Expand Down
115 changes: 115 additions & 0 deletions dpl_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Buffer, copy, cwd, stdout, open } from 'deno';
import {
test,
assertEqual,
assert,
} from 'https://deno.land/x/testing/testing.ts';
import * as dpl from './dpl.ts';
import escape from './escape.ts';

// renderStringTest
(() => {
interface testCase {
name: string;
body: string;
param?: string;
expected: string;
}

const testCases: Array<testCase> = [
{
name: 'Normal',
body: 'normal test',
expected: 'normal test',
},
{
name: 'Escaped',
body: '<%= param %>',
param: '<div>test</div>',
expected: escape('<div>test</div>'),
},
{
name: 'Raw',
body: '<%- param %>',
param: '<div>test</div>',
expected: '<div>test</div>',
},
{
name: 'Comment',
body: '<%# param %>',
param: '<div>test</div>',
expected: '',
},
{
name: 'Escaped without spacing',
body: '<%=param%>',
param: '<div>test</div>',
expected: escape('<div>test</div>'),
},
{
name: 'Raw without spacing',
body: '<%-param%>',
param: '<div>test</div>',
expected: '<div>test</div>',
},
{
name: 'Comment without spacing',
body: '<%#param%>',
param: '<div>test</div>',
expected: '',
},
];

for (const tc of testCases) {
test({
name: tc.name,
fn: async () => {
const buf = new Buffer();
await copy(buf, await dpl.renderString(tc.body, { param: tc.param }));
const actual = buf.toString();
assertEqual(actual, tc.expected);
},
});
}
})();

// renderTest
(() => {
interface testCase {
name: string;
fileName: string;
param?: string;
expected: string;
}

const testCases: Array<testCase> = [
{
name: 'Normal',
fileName: 'normal',
expected: 'normal test',
},
{
name: 'Raw',
fileName: 'raw',
param: '<div>test</div>',
expected: '<div>test</div>',
},
];

for (const tc of testCases) {
test({
name: tc.name,
fn: async () => {
let buf = new Buffer();
await copy(
buf,
await dpl.render(`${cwd()}/testdata/${tc.fileName}.ejs`, {
param: tc.param,
})
);
const actual = buf.toString();
assertEqual(actual, tc.expected);
},
});
}
})();
1 change: 1 addition & 0 deletions testdata/normal.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
normal test
1 change: 1 addition & 0 deletions testdata/raw.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- param %>

0 comments on commit 22e5856

Please sign in to comment.