-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add marked.test.ts for deno test
- Loading branch information
Showing
110 changed files
with
4,641 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!doctype html> | ||
<title>marked tests</title> | ||
<p>testing...</p> | ||
<script src="marked.js"></script> | ||
<script src="test.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var fs = require('fs'); | ||
|
||
var test = require('../') | ||
, runTests = test.runTests | ||
, load = test.load; | ||
|
||
var express = require('express') | ||
, app = express(); | ||
|
||
app.use(function(req, res, next) { | ||
var setHeader = res.setHeader; | ||
res.setHeader = function(name) { | ||
switch (name) { | ||
case 'Cache-Control': | ||
case 'Last-Modified': | ||
case 'ETag': | ||
return; | ||
} | ||
return setHeader.apply(res, arguments); | ||
}; | ||
next(); | ||
}); | ||
|
||
var dir = __dirname + '/../tests' | ||
, files = {}; | ||
|
||
app.get('/test.js', function(req, res, next) { | ||
var test = fs.readFileSync(__dirname + '/test.js', 'utf8') | ||
, files = load(); | ||
|
||
test = test.replace('__TESTS__', JSON.stringify(files)); | ||
test = test.replace('__MAIN__', runTests + ''); | ||
|
||
res.contentType('.js'); | ||
res.send(test); | ||
}); | ||
|
||
app.use(express.static(__dirname + '/../../lib')); | ||
app.use(express.static(__dirname)); | ||
|
||
app.listen(8080); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
;(function() { | ||
|
||
var console = {} | ||
, files = __TESTS__; | ||
|
||
console.log = function(text) { | ||
var args = Array.prototype.slice.call(arguments, 1) | ||
, i = 0; | ||
|
||
text = text.replace(/%\w/g, function() { | ||
return args[i++] || ''; | ||
}); | ||
|
||
if (window.console) window.console.log(text); | ||
document.body.innerHTML += '<pre>' + escape(text) + '</pre>'; | ||
}; | ||
|
||
if (!Object.keys) { | ||
Object.keys = function(obj) { | ||
var out = [] | ||
, key; | ||
|
||
for (key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
out.push(key); | ||
} | ||
} | ||
|
||
return out; | ||
}; | ||
} | ||
|
||
if (!Array.prototype.forEach) { | ||
Array.prototype.forEach = function(callback, context) { | ||
for (var i = 0; i < this.length; i++) { | ||
callback.call(context || null, this[i], i, obj); | ||
} | ||
}; | ||
} | ||
|
||
if (!String.prototype.trim) { | ||
String.prototype.trim = function() { | ||
return this.replace(/^\s+|\s+$/g, ''); | ||
}; | ||
} | ||
|
||
function load() { | ||
return files; | ||
} | ||
|
||
function escape(html, encode) { | ||
return html | ||
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
.replace(/'/g, '''); | ||
} | ||
|
||
(__MAIN__)(); | ||
|
||
}).call(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
/** | ||
* @license | ||
* | ||
* marked tests | ||
* Copyright (c) 2011-2013, Christopher Jeffrey. (MIT Licensed) | ||
* https://github.com/chjj/marked | ||
* | ||
* @ts-stack/markdown tests | ||
* Copyright (c) 2018, Третяк Костя. (MIT Licensed) | ||
* https://github.com/ts-stack/markdown | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
interface RunBenchOptions { | ||
single?: number; | ||
times?: number; | ||
length?: number; | ||
} | ||
|
||
const widthTable = 100; | ||
|
||
runBench(); | ||
|
||
/** | ||
* @param benchStrLen Length in kilobytes. Default 300 KB. | ||
*/ | ||
function initBench(benchStrLen: number = 300, times: number = 1): string { | ||
benchStrLen = benchStrLen * 1024; | ||
const files = load(); | ||
const countFiles = files.length; | ||
let accumulatedMarkdown = ''; | ||
|
||
while (benchStrLen > accumulatedMarkdown.length) { | ||
for (let i = 0; i < countFiles && benchStrLen > accumulatedMarkdown.length; i++) { | ||
accumulatedMarkdown += '\n\n' + files[i]; | ||
} | ||
} | ||
|
||
const lenAcumulatedFile = Math.round(accumulatedMarkdown.length / 1024); | ||
console.log('~'.repeat(widthTable)); | ||
console.log(`Benchmark run ${times} times for one file ${lenAcumulatedFile} KB with accumulated Markdown tests:`); | ||
console.log('='.repeat(widthTable)); | ||
|
||
const marginFromName = ' '.repeat(16); | ||
console.log(`Lib ${marginFromName} | Lib load, ms | Lib init, ms | Bench work, ms | Total, ms | Memory usage, KB`); | ||
|
||
console.log('='.repeat(widthTable)); | ||
return accumulatedMarkdown; | ||
} | ||
|
||
/** | ||
* @param name Name of engine. | ||
* @param parseAndCompile Function to be used for testing. | ||
*/ | ||
function bench( | ||
name: string, | ||
accumulatedMarkdown: string, | ||
parseAndCompile: (...args: any[]) => any, | ||
times: number = 1, | ||
loadTime: number = 0, | ||
initTime: number = 0 | ||
): void { | ||
// Forcing Garbage Collection (for memory usage purpose). | ||
global.gc(); | ||
const startBench = Date.now(); | ||
|
||
while (times--) { | ||
parseAndCompile(accumulatedMarkdown); | ||
} | ||
|
||
const heapUsed = Math.round(process.memoryUsage().heapUsed / 1024); | ||
const benchTime = Date.now() - startBench; | ||
const total = loadTime + initTime + benchTime; | ||
const marginFromName = ' '.repeat(20 - name.length); | ||
const marginFromLoad = ' '.repeat(12 - loadTime.toString().length); | ||
const marginFromInit = ' '.repeat(12 - initTime.toString().length); | ||
const marginFromBench = ' '.repeat(14 - benchTime.toString().length); | ||
const marginFromTotal = ' '.repeat(9 - total.toString().length); | ||
|
||
const output = | ||
name + | ||
marginFromName + | ||
' | ' + | ||
(loadTime || '-') + | ||
marginFromLoad + | ||
' | ' + | ||
initTime + | ||
marginFromInit + | ||
' | ' + | ||
benchTime + | ||
marginFromBench + | ||
' | ' + | ||
total + | ||
marginFromTotal + | ||
' | ' + | ||
heapUsed; | ||
|
||
console.log(output); | ||
console.log('-'.repeat(widthTable)); | ||
} | ||
|
||
/** | ||
* Benchmark all engines | ||
*/ | ||
function runBench() { | ||
let options: RunBenchOptions = parseArg(); | ||
|
||
interface Lib { | ||
name: string; | ||
parserClass?: string; | ||
compilerClass?: string; | ||
parserAndCompilerMethod?: string; | ||
parserMethod?: string; | ||
compilerMethod?: string; | ||
isParserStatic?: boolean; | ||
} | ||
|
||
let libs: Lib[] = [ | ||
{ name: '@ts-stack/markdown', parserClass: 'Marked', parserAndCompilerMethod: 'parse', isParserStatic: true }, | ||
{ name: 'marked', parserAndCompilerMethod: 'parse', isParserStatic: true }, | ||
{ name: 'markdown', parserAndCompilerMethod: 'parse', isParserStatic: true }, | ||
{ name: 'remarkable', parserClass: 'Remarkable', parserAndCompilerMethod: 'render' }, | ||
{ | ||
name: 'commonmark', | ||
parserClass: 'Parser', | ||
parserMethod: 'parse', | ||
compilerClass: 'HtmlRenderer', | ||
compilerMethod: 'render' | ||
}, | ||
{ name: 'markdown-it', parserAndCompilerMethod: 'render' }, | ||
{ name: 'showdown', parserClass: 'Converter', parserAndCompilerMethod: 'makeHtml' } | ||
]; | ||
|
||
options = options || {}; | ||
const times = options.times; | ||
const length = options.length; | ||
const accumulatedMarkdown = initBench(length, times); | ||
if (options.single !== -1) { | ||
if (!libs[options.single]) { | ||
return console.warn(`libraries with this index ${options.single} do not exist.\n`); | ||
} | ||
|
||
libs = [libs[options.single]]; | ||
} | ||
|
||
libs.forEach(lib => { | ||
let loadFrom: string = lib.name; | ||
|
||
if (lib.name == '@ts-stack/markdown') { | ||
loadFrom = '../'; | ||
} | ||
|
||
try { | ||
const startLoadTime = Date.now(); | ||
const fullLib = require(loadFrom); | ||
const loadTime = Date.now() - startLoadTime; | ||
|
||
const ParserClass = lib.parserClass ? fullLib[lib.parserClass] : fullLib; | ||
const CompilerClass = lib.compilerClass ? fullLib[lib.compilerClass] : null; | ||
const parserInstance = lib.isParserStatic ? ParserClass : new ParserClass(); | ||
const compilerInstance = CompilerClass ? new CompilerClass() : null; | ||
let parseAndCompile: (md: string) => string; | ||
|
||
if (lib.parserAndCompilerMethod) { | ||
parseAndCompile = parserInstance[lib.parserAndCompilerMethod].bind(parserInstance); | ||
} else { | ||
const parse = parserInstance[lib.parserMethod].bind(parserInstance); | ||
const compile = compilerInstance[lib.compilerMethod].bind(compilerInstance); | ||
parseAndCompile = function(md: string): string { | ||
return compile(parse(md)); | ||
}; | ||
} | ||
|
||
const startInit = Date.now(); | ||
parseAndCompile('1'); | ||
const initTime = Date.now() - startInit; | ||
|
||
bench(lib.name, accumulatedMarkdown, parseAndCompile, times, loadTime, initTime); | ||
} catch (e) { | ||
console.log(`Could not bench '${lib.name}'.`); | ||
console.log(e.stack); | ||
} | ||
}); | ||
} | ||
|
||
function parseArg(): RunBenchOptions { | ||
const argv = process.argv.slice(2); | ||
const options: RunBenchOptions = { single: -1 }; | ||
|
||
for (let i = 0; i < argv.length; i++) { | ||
let [key, value] = argv[i].split('='); | ||
|
||
// In `argv` we have next parameter or value of current parameter. | ||
if (!value && argv[i + 1]) { | ||
value = argv[i + 1].split('-')[0]; | ||
|
||
// Skip next parameter. | ||
if (value) { | ||
i++; | ||
} | ||
} | ||
|
||
switch (key) { | ||
case '-l': | ||
case '--length': | ||
options.length = +value; | ||
break; | ||
case '-s': | ||
case '--single': | ||
options.single = +value || 0; | ||
break; | ||
case '-t': | ||
case '--times': | ||
options.times = +value; | ||
break; | ||
} | ||
} | ||
|
||
return options; | ||
} | ||
|
||
function load(): string[] { | ||
const dir = path.normalize(__dirname + '/../test/tests'); | ||
const files: string[] = []; | ||
|
||
const list = fs.readdirSync(dir).filter(file => path.extname(file) == '.md'); | ||
|
||
list.forEach(path => { | ||
files.push(fs.readFileSync(dir + '/' + path, 'utf8')); | ||
}); | ||
|
||
return files; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "https://deno.land/std@0.100.0/testing/asserts.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Marked, MarkedOptions } from "../mod.ts"; | ||
import { assertEquals } from "./deps.ts"; | ||
|
||
const HTMLDIR = "./tests/html"; | ||
const MDDIR = "./tests/md"; | ||
|
||
const tests = Deno.readDirSync(HTMLDIR); | ||
|
||
for (const t of tests) { | ||
Deno.test({ | ||
name: t.name, | ||
fn(): void { | ||
const md = Deno.readTextFileSync(MDDIR + "/" + t.name.slice(0, t.name.lastIndexOf(".")) + ".md"); | ||
const html = Deno.readTextFileSync(HTMLDIR + "/" + t.name); | ||
|
||
const flags = t.name.split('.').slice(1); | ||
const options = new MarkedOptions(); | ||
if (flags.length) { | ||
for (let flag of flags) { | ||
let val = true; | ||
|
||
if (flag.indexOf('no') === 0) { | ||
flag = flag.substring(2); | ||
val = false; | ||
} | ||
|
||
if (options.hasOwnProperty(flag)) { | ||
(options as any)[flag] = val; | ||
} | ||
} | ||
} | ||
Marked.setOptions(options); | ||
const actual = Marked.parse(md).content; | ||
assertEquals(actual, html); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<p>AT&T has an ampersand in their name.</p> | ||
<p>AT&T is another way to write it.</p> | ||
<p>This & that.</p> | ||
<p>4 < 5.</p> | ||
<p>6 > 5.</p> | ||
<p>Here's a <a href="http://example.com/?foo=1&bar=2">link</a> with an ampersand in the URL.</p> | ||
<p>Here's a link with an amersand in the link text: <a href="http://att.com/" title="AT&T">AT&T</a>.</p> | ||
<p>Here's an inline <a href="/script?foo=1&bar=2">link</a>.</p> | ||
<p>Here's an inline <a href="/script?foo=1&bar=2">link</a>.</p> |
Oops, something went wrong.