"use strict";

var PEG = require("pegjs"),
    fs = require("fs"),
    nodeunit = require('nodeunit'),

    ks_parser_fn = __dirname + '/../lib/kumascript/parser.pegjs',
    ks_parser_src = fs.readFileSync(ks_parser_fn, 'utf8'),
    ks_parser = PEG.buildParser(ks_parser_src);

module.exports = nodeunit.testCase({
  "JSON values are parsed correctly": function (test) {
    var tokens = ks_parser.parse('{{ f({ "a": "x", "b": -1e2, "c": 0.5, "d": [1,2] }) }}');
    test.deepEqual(tokens,
                   [{type: "MACRO",
                     name: "f",
                     args: [{a: "x", b: -1e2, c: 0.5, d: [1, 2]}],
                     offset: 0}],
                   "The macro is parsed correctly");
    test.done();
  },

  "Invalid JSON should cause a syntax error": function (test) {
    test.throws(function() {
      ks_parser.parse('{{ f({ x: 1 }) }}');
    }, PEG.parser.SyntaxError, "Quotes around property names are required");

    test.throws(function() {
      ks_parser.parse('{{ f({ "x": 01 }) }}');
    }, PEG.parser.SyntaxError, "Octal literals are not allowed");

    test.throws(function() {
      ks_parser.parse('{{ f({ "x": [1,] }) }}');
    }, PEG.parser.SyntaxError, "Trailing commas are not allowed");

    test.done();
  },

  "JSON strings should be able to contain ')'": function (test) {
    var tokens = ks_parser.parse('{{ f({ "a": "f)" }) }}');
    test.deepEqual(tokens,
                   [{type: "MACRO", name: "f", args: [{a: "f)"}], offset: 0}],
                   "The macro is parsed correctly");
    test.done();
  }
});

Add unit tests for JSON parsing
Add unit tests for JSON parsing
