summaryrefslogtreecommitdiff
path: root/js/test.js
blob: c1ea5b6acc38d0f1c427981890fb94f241da861a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env node

var fs = require('fs');
var util = require('util');
var stmd = require('./stmd');
var ansi = require('ansi')
var cursor = ansi(process.stdout);

var writer = new stmd.HtmlRenderer();
var reader = new stmd.DocParser();

var passed = 0;
var failed = 0;

var showSpaces = function(s) {
  var t = s;
  return t.replace(/\t/g,'→')
   .replace(/ /g,'␣');
}

fs.readFile('spec.txt', 'utf8', function(err, data) {
  if (err) {
    return console.log(err);
  }
  var examples = [];
  var current_section = "";
  var example_number = 0;
  tests = data.replace(/^<!-- END TESTS -->(.|[\n])*/m,'');
  tests.replace(/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/gm,
        function(_,x,y,z,w){
          if (z) {
            current_section = z;
          } else {
            example_number++;
            examples.push({markdown: x,
                           html: y,
                           section: current_section,
                           number: example_number});
          }
      });

  current_section = "";

  console.time("Elapsed time");

  for (i = 0; i < examples.length; i++) {
    var example = examples[i];
    if (example.section != current_section) {
      if (current_section !== '') {
        cursor.write('\n');
      }
      current_section = example.section;
      cursor.reset().write(current_section).reset().write('  ');
    }
    var actual = writer.renderBlock(reader.parse(example.markdown.replace(/→/g, '\t')));
    if (actual == example.html) {
      passed++;
      cursor.green().write('✓').reset();
    } else {
      failed++;
      cursor.write('\n');

      cursor.red().write('✘ Example ' + example.number + '\n');
      cursor.cyan();
      cursor.write('=== markdown ===============\n');
      cursor.write(showSpaces(example.markdown));
      cursor.write('=== expected ===============\n');
      cursor.write(showSpaces(example.html));
      cursor.write('=== got ====================\n');
      cursor.write(showSpaces(actual));
      cursor.reset();
    }
  }
  cursor.write('\n' + passed.toString() + ' tests passed, ' +
      failed.toString() + ' failed.\n');

  console.timeEnd("Elapsed time");
});