UNPKG

3.14 kBJavaScriptView Raw
1var markdown = require('markdown')
2
3function test_dialect( dialect, features ) {
4 var dialect_test = exports[ "test_" + dialect ] = {};
5
6 for ( var f in features ) {
7 ( function( feature ) {
8 dialect_test[ "test_" + feature ] = function() {
9 var test_path = path + feature + "/";
10
11 // grab all the test files in this feature
12 var tests = fs.list( test_path );
13
14 // filter to only the raw files
15 tests = tests.filter( function( x ) {return x.match( /\.text$/ ) } );
16
17 // remove the extensions
18 tests = tests.map( function( x ) {return x.replace( /\.text$/, "" ) } );
19
20 for ( var t in tests ) {
21 // load the raw text
22 var test_name = tests[ t ].substring( tests[ t ].lastIndexOf( "/" ) + 1 ),
23 text = slurpFile( test_path + tests[ t ] + ".text" );
24
25 // load the target output
26 if ( fs.isFile( test_path + tests[ t ] + ".json" ) ) {
27 try {
28 var json_text = slurpFile( test_path + tests[ t ] + ".json" );
29 var json = JSON.parse( json_text );
30
31 var output = markdown.toHTMLTree( text, dialect );
32 asserts.same( output, json, test_name );
33 }
34 catch( e ) {
35 asserts.ok( 0, "Failed with error on " + test_name + ": " + e );
36 if ( e.stack )
37 asserts.diag( e.stack );
38 }
39 }
40 else {
41 asserts.ok( 0, "No target output for " + test_name );
42 }
43 }
44 }
45 } )( features[ f ] );
46 }
47}
48
49
50// Bootstrap code
51if ( typeof process != "undefined" && process.title == "node" ) {
52 // Setup for node
53 var test = require( 'patr/runner' ),
54 asserts = require( 'assert' ),
55 n_fs = require( 'fs' ),
56 args = process.argv.splice( 1 ),
57 path = __dirname + "/features/";
58
59 test.runner = test.run;
60
61 var slurpFile = function( f ) {
62 return n_fs.readFileSync( f, 'utf8' );
63 }
64
65 var fs = {
66 list: n_fs.readdirSync,
67 rawOpen: n_fs.openSync,
68 isFile: function( f ) {
69 return n_fs.statSync( f ).isFile()
70 },
71 };
72
73 asserts.same = asserts.deepEqual;
74}
75else {
76 // Setup for flusspferd
77 var test = require('test');
78 asserts = test.asserts,
79 fs = require( "fs-base" ),
80 args = require( "system" ).args.splice( 1 ),
81 path = module.resource.resolve( "features" );
82
83 var slurpFile = function ( f ) {
84 var s = fs.rawOpen( f, "r" );
85 var t = s.readWhole();
86 s.close();
87 return t;
88 }
89}
90
91
92
93
94if ( require.main === module ) {
95 var dialects = {};
96 dialects.Gruber = [
97 "blockquotes",
98 "code",
99 "emphasis",
100 "headers",
101 "horizontal_rules",
102 "images",
103 "linebreaks",
104 "links",
105 "lists"
106 ];
107
108 dialects.Maruku = dialects.Gruber.slice( 0 );
109 dialects.Maruku.push( "meta", "definition_lists" );
110
111 // TODO if dialects/features were passed on the command line, filter to them
112 // if ( args.length ) {
113 // features = features.filter( function( x ) args.indexOf( x ) !== -1 );
114 // }
115
116 for ( d in dialects ) {
117 test_dialect( d, dialects[ d ] );
118 }
119
120 test.runner( exports );
121}