Search code examples
javascriptunit-testingsyntax-highlightingparse-tree

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.


Solution

  • 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 */