var esprima = require('esprima');
var esgraph = require('esgraph');
var analyses = require('analyses');
var map = require('es6-map-shim');
var Shade = require("..");
var fs = require('fs');
var should = require('should');
var spaceAnalyzer = require("../src/analyze/space_analyzer.js"),
    SpaceVectorType = spaceAnalyzer.SpaceVectorType;

var Set = analyses.Set;

function createTest(dir, file) {
    if(fs.lstatSync(dir + file).isDirectory())
        return;
    var contents = fs.readFileSync(dir + file, 'utf8');
    var ast = esprima.parse(contents, {comment: true, range: true});
    var comments = ast.comments;
    delete ast.comments;
    var description = comments[0].value.trim() + ' (' + file + ')';
    var attributes = JSON.parse(comments[1].value);
    var expected = JSON.parse(comments[2].value);
    for(var name in expected){
        expected[name] = expected[name].map(function(elem){ return SpaceVectorType[elem]});
    }
    var ctx = {
        "global.shade": attributes
    }
    it(description, function () {
        var aast = Shade.parseAndInferenceExpression(contents, { inject: ctx });
        if (aast.body[0].type === 'FunctionDeclaration')
            aast = aast.body[0].body;
        var actual = spaceAnalyzer.analyze(aast);
        actual.should.eql(expected)
    });
}

describe('Space analysis:', function () {
    var dir = __dirname + '/data/space/';
    //createTest(dir, "dualspace.js");

    var files = fs.readdirSync(dir);
    files.forEach(function (file) {
        createTest(dir, file);
    });
});

@@ -1197,21 +1197,90 @@
 .body%5B0%5D
-.body
+;%0A        else%0A            throw new Error(%22Test Code must be a function%22)
 ;%0A      

Fix in space analysis testing.