File: test

package info (click to toggle)
node-livescript 1.5.0+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 636 kB
  • ctags: 25
  • sloc: makefile: 44; sh: 2
file content (206 lines) | stat: -rwxr-xr-x 5,292 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env nodejs

(function() {
  var fs = require("fs");
  var path = require("path");
  var assert = require("assert");
  var prelude = require("prelude-ls");

  global.LiveScript = require("..");
  global.command = require("../lib/command");

  var bold = "\33[0;1m";
  var red = "\33[0;31m";
  var green = "\33[0;32m";
  var reset = "\33[0m";
  function tint(text, color) {
    if (color == null) {
      color = green;
    }
    return color + text + reset;
  }

  var startTime = Date.now();
  var passedTests = 0;
  var failedTests = 0;

  var key;
  for (key in assert) {
    (function (name, func){
      global[name] = function(){
        func.apply(global, arguments);
        ++passedTests;
      };
    }(key, assert[key]));
  }

  global.eq = strictEqual;

  global.throws = function(msg, fun){
    try {
      fun();
    } catch (e) {
      return eq(e != null ? e.message : void 8, msg);
    }
    ok(false, "should throw: " + msg);
  };

  global.compileThrows = function(msg, lno, code){
    throws(msg + " on line " + lno, function(){
      LiveScript.compile(code);
    });
  };

  var activeCommandTests = 0;
  global.commandEq = function(args, arr, afterTests) {
    activeCommandTests++;
    afterTests = afterTests || prelude.id;
    var i = 0;
    var res = [];
    // switch to mocha in the future, better way to deal with async tests
    var maybeTestResults = function() {
        if (i === arr.length - 1) {
            arr.forEach(function (x, j) {
                if (x instanceof RegExp) {
                    ok(x.test(res[j]), "should pass regex:\n" + x + "\n.test\n" + res[j]);
                } else {
                    eq(x, res[j], "should equal:\n" + x + "\n==\n" + res[j]);
                }
            });
            afterTests();
            activeCommandTests--;
        } else if (i >= arr.length) {
            console.error("more results than expected!\n" + args + "\n" + res[i-1]);
        }
    };
    var log = function(msg) {
        res.push(msg);
        maybeTestResults();
        i++;
    };
    var die = function(msg) {
        res.push(msg);
        maybeTestResults();
        i++;
    };

    try {
        command(args, {say: log, warn: log, die: die, sayWithTimestamp: log});
    } catch (e) {
        ok(false, e.message);
    }
  };

  global.fileRead = function(filename) {
      try {
          return fs.readFileSync(filename, {
              encoding: "utf8"
          });
      } catch (e) {
          return "Error: " + e.message;
      }
  };

  global.fileExists = function(filename) {
      try {
          return fs.statSync(filename).isFile();
      } catch (e) {
          console.error(e.message);
          return false;
      }
  };

  global.fileDelete = function(filename) {
      try {
          fs.unlinkSync(filename);
      } catch (e) {
          console.error(e.message);
      }
  };

  process.on("exit", function(){
      var printResults = function() {
          var time = ((Date.now() - startTime) / 1e3).toFixed(2);
          var message = "passed " + passedTests + " tests in " + time + " seconds";

          console.log(failedTests
            ? tint("failed " + failedTests + " and " + message, red)
            : tint(message));

          if (failedTests) {
            process.exit(1);
          }
      };
      var check = function() {
          if (activeCommandTests > 0) {
              setTimeout(wait, 5);
          } else {
              printResults();
          }
      };
      check();
  });

  var files = fs.readdirSync("test");

  if (process.execArgv.indexOf("--harmony") < 0 && process.execArgv.indexOf("--harmony-generators") < 0) {
    files.splice(files.indexOf("generators.ls"), 1);
  } else {
    console.log("Testing with harmony.");
  }

  files.forEach(function(file){
    var stk, msg, m, ref, num, row, col, that, lines, line;

    if (!/\.ls$/i.test(file)) {
      return;
    }

    var filename = path.join("test", file);
    var code = fs.readFileSync(filename, "utf8");

    try {
      console.log(filename);
      LiveScript.run(code, { filename: filename });
    } catch (e) {
      ++failedTests;

      if (!(stk = e != null ? e.stack : null)) {
        console.error(e);
      }

      msg = e.message || "" + /^[^]+?(?=\n    at )/.exec(stk);

      m = /^(AssertionError:) "(.+)" (===) "(.+)"$/.exec(msg)
      if (m) {
        for (num = 2; num <= 4; num += 2) {
          m[num] = tint(m[num].replace(/\\n/g, "\n"), bold);
        }
        msg = m.slice(1).join("\n");
      }

      if ((ref = RegExp(filename + ":(\\d+):(\\d+)\\)?$", "m").exec(stk)) != null) {
        row = ref[1];
        col = ref[2];
      }

      if (row && col) {
        console.error(tint(msg + "\n" + red + "at " + filename + ":" + row-- + ":" + col--, red));
        code = LiveScript.compile(code, {
          bare: true
        });
      } else if (that = /\bon line (\d+)\b/.exec(msg)) {
        console.error(tint(msg, red));
        row = that[1] - 1;
        col = 0;
      } else {
        console.error(stk);
        return;
      }

      line = (lines = code.split("\n"))[row];
      lines[row] = line.slice(0, col) + tint(line.slice(col), bold);
      console.error(lines.slice((ref = row - 8) > 0 ? ref : 0, row + 9).join("\n"));
    }
  });
}());