JavaScript 语法测试用例

发布于 2024-11-09 16:08:57 字数 394 浏览 4 评论 0原文

我正在创建一个文本编辑器,并且刚刚编写完突出显示算法,以使每个语法以不同的颜色显示,并使用正确的解析树在正确的位置呈现。

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

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

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

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

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

I'm creating a text editor and I've just finished writing the highlighting algorithms to have each of the syntax appear in a different color, and render in the right position using the proper parse trees.

I was wondering if anyone could provide me with, or the location of a test or series of test cases to make sure nothing will break. The test case(s) should cover all of JavaScript syntax as it is used on the web including edge cases (i.e., including syntax like throw although it is rarely used), DOM creation and manipulation etc.

I have added the following static test case. It should cover all the syntax.

There are a few things to note: since the code is being parse recursively on a grammar level, only basic cases are required. For example, to the editor:

a[1]; and a[1][2][3][4][5]; would be the same syntax. Since the second line, is just recursively more subs then the the first line.

The test case I have created has been moved to an answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋意浓 2024-11-16 16:08:58

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

编辑:添加了正则表达式,并抛出。这种情况在语法上是有效的,应该涵盖 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 */

This is so far the best test case I was able to come up with.

EDIT: Added regexp, and throw. This case is syntactically valid and should cover all cases of JS. Please message me directly if you find anything missing so that I can add it here.

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 */
烟花易冷人易散 2024-11-16 16:08:58

一个好的开始方法是通过 JSLint 运行它来查看您的 JavaScript 是否有效。这是我所知道的最好的检查工具,但我不确定它在检查代码是否损坏方面效果如何。 :(

希望有帮助。

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.

北方。的韩爷 2024-11-16 16:08:57

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

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

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

最后,我会考虑通过 JSLint 运行其中一些库,然后手动浏览并修改源以明确遵守 JSLint 制定的一些“规则”。

编辑:通过“命中”,我的意思是确保您涵盖每个规则提供的两种场景,而不仅仅是“干净”版本。

Interesting question. I think my initial approach, barring any other interesting suggestions here, would be to grab a bunch of JavaScript from fairly major libraries. I'm thinking jQuery, Mootools, Prototype, etc.

Then, once you've done a few major libs, do some smaller ones. I'd checkout Github. Maybe look at Underscore, HeadJS, and maybe some others at https://github.com/languages/JavaScript.

I would also take a couple minified libraries, run them through JSBeautifier. Not sure if beautified JS may have slightly altered syntax from the original.

Lastly, I would consider running some of these libraries through JSLint, and then manually go through and modify the sources to explicitly hit some of the 'rules' that JSLint has laid out.

EDIT: And by "hit", I mean make sure you cover both scenarios offered by each rule, not just the 'clean' version.

月亮坠入山谷 2024-11-16 16:08:57

一种可能的方法:有各种应用程序会从语言的 BNF 语法开始生成随机代码段(例如 这个)并且有语法文件< /a>对于可用的 JavaScript。

这不会为您提供一个静态测试用例,您可以用已知的预期结果编写测试脚本,但可能是针对意外(但合法)字符串测试解析器并确保它不会中断的好方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文