Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript syntax test cases

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.

like image 821
GAgnew Avatar asked May 25 '11 15:05

GAgnew


2 Answers

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.

like image 119
Matt Avatar answered Sep 27 '22 19:09

Matt


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.

like image 34
Jacob Mattison Avatar answered Sep 27 '22 17:09

Jacob Mattison