8

我正在创建一个文本编辑器,并且刚刚完成了高亮算法的编写,以使每种语法都以不同的颜色显示,并使用正确的解析树在正确的位置进行渲染。

我想知道是否有人可以向我提供测试或一系列测试用例的位置,以确保不会出现任何问题。测试用例应涵盖 Web 上使用的所有 JavaScript 语法,包括边缘用例(即,包括throw虽然很少使用的语法)、DOM 创建和操作等。

我添加了以下静态测试用例。它应该涵盖所有语法。

有几点需要注意:由于代码是在语法级别上递归解析的,因此只需要基本情况​​。例如,对编辑:

一个[1];和 [1][2][3][4][5];将是相同的语法。由于第二行,只是递归地比第一行更多。

我创建的测试用例已移至下面的答案。

4

4 回答 4

2

有趣的问题。我认为我最初的方法,除非这里有任何其他有趣的建议,否则会从相当主要的库中获取一堆 JavaScript。我在想 jQuery、Mootools、Prototype 等。

然后,一旦你完成了一些主要的库,再做一些较小的库。我会检查Github。也许看看UnderscoreHeadJS,也许还有其他一些https://github.com/languages/JavaScript

我还会使用几个缩小的库,通过JSBeautifier运行它们。不确定美化的 JS 是否可能与原始的语法略有不同。

最后,我会考虑通过JSLint运行其中的一些库,然后手动检查并修改源代码以明确符合 JSLint 制定的一些“规则”。

编辑:“命中”是指确保涵盖每条规则提供的两种情况,而不仅仅是“干净”版本。

于 2011-05-25T15:21:08.053 回答
1

这是迄今为止我能想到的最好的测试用例。

编辑:添加了正则表达式,然后抛出。这种情况在语法上是有效的,应该涵盖 JS 的所有情况。如果您发现缺少任何内容,请直接给我发消息,以便我可以在此处添加。

a = 1;
b = { 'a' : a };
c = 'a';
d = this;
var patt1=/w3ghouls/i;
throw "Err3";
function e(a,b,c){
    d += a + b + c++;
    return d;
}
this.xy.z = function(a, b){
    var x = null;
}
var f = function(a,b){
    if(a == b || (b === a && a)){
        var f = [a,b];
        try{
            f = f.slice(0);
        }catch(e){
            console.log(e * e + '');
        }
    }else if(a){
        a = null;
        a = undefined;
        b = typeof a;
        b = true;
        b = false;
    }else{
        switch(c){
           case 'c':
             break;
           default:
             null;
             break;
        }
    }
}
for(var i =0; i <= a.length; i++){
    do{
       continue;
       null;
      }while(a != b);
}
if(a == b)
  (a) ? null : null;
/* This is a finished 
   test case */
于 2011-05-25T19:42:24.397 回答
1

One possible approach: there are various applications that will generate random pieces of code starting from a BNF grammar of a language (such as this one) and there are grammar files for javascript available.

That won't get you a static test case that you can script tests against with known expected results, necessarily, but might be a good way to test your parser against unexpected (but legal) strings and make sure it doesn't break.

于 2011-05-25T15:20:54.040 回答
0

A good way to start would be to run this through JSLint to see if your JavaScript is valid. It is the best checking tool I know of, but I'm not sure how well it will do to check if code broken. :(

Hope that helps.

于 2011-05-25T15:20:46.777 回答