JavaScript 语法测试用例
我正在创建一个文本编辑器,并且刚刚编写完突出显示算法,以使每个语法以不同的颜色显示,并使用正确的解析树在正确的位置呈现。
我想知道是否有人可以向我提供测试或一系列测试用例的位置,以确保不会出现任何问题。测试用例应涵盖 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是迄今为止我能想到的最好的测试用例。
编辑:添加了正则表达式,并抛出。这种情况在语法上是有效的,应该涵盖 JS 的所有情况。如果您发现缺少任何内容,请直接给我留言,以便我可以在此处添加。
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.
一个好的开始方法是通过 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.
有趣的问题。我认为我最初的方法是从相当主要的库中获取一堆 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.
一种可能的方法:有各种应用程序会从语言的 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.