import fs from 'fs';
import { expect } from 'chai';
import { CodeElements } from './config';

const peg = require('pegjs'); // import { peg } from 'pegjs';

// code element constants
describe('test-block', () => {
  let parser;
  let stmts;

  // generate choux-parser
  before(() => (parser = peg.generate(fs.readFileSync('./grammar/choux.pegjs').toString())));

  describe('#parser-success', () => {
    it('type should be BLOCK_BEGIN or BLOCK_END', () => {
      stmts = parser.parse('[a] {}');
      expect(stmts[1].type).to.equal(CodeElements.BLOCK_BEGIN);
      expect(stmts[2].type).to.equal(CodeElements.BLOCK_END);

      stmts = parser.parse('[a] {[b]}');
      expect(stmts[1].type).to.equal(CodeElements.BLOCK_BEGIN);
      expect(stmts[3].type).to.equal(CodeElements.BLOCK_END);
    });

    it('nested block', () => {
      expect(() => (stmts = parser.parse('[a]{[b]{}}'))).not.throw(Error);
      expect(stmts[1].type).to.equal(CodeElements.BLOCK_BEGIN);
      expect(stmts[3].type).to.equal(CodeElements.BLOCK_BEGIN);
      expect(stmts[4].type).to.equal(CodeElements.BLOCK_END);
      expect(stmts[5].type).to.equal(CodeElements.BLOCK_END);
    });
  });

  describe('#parser-failure', () => {
    it('illegal nesting', () => {
      expect(() => (parser.parse('[a]{{}'))).throw(Error);
      expect(() => (parser.parse('[a]{}}'))).throw(Error);
      expect(() => (parser.parse('[a]{}{'))).throw(Error);
      expect(() => (parser.parse('[a]{{}}'))).throw(Error);
    });
  });
});
