
// Full-system tests of Hummingbird parser and compiler.

var fs           = require('fs'),
    vows         = require('vows'),
    expect       = require('expect.js'),
    AST          = require('../lib/ast'),
    types        = require('../lib/types'),
    parseAndWalk = require('./helper').parseAndWalk

var programs = {
  forLoop: fs.readFileSync(__dirname+'/system/for-loop.hb').toString(),
  whileTrue: fs.readFileSync(__dirname+'/system/while-true.hb').toString()
}

function runCompiledCode (tree) {
  // Wrap the compiled code in an immediately-called function
  return eval("(function(){\n"+tree.compile()+"\n})()")
}

describe('System', function () {
  describe('given a for loop program', function () {
    var tree = null
    it('should parse', function () {
      tree = parseAndWalk(programs.forLoop)
      expect(tree).to.be.ok()
    })
    it('should produce the right result when executed', function () {
      var result = runCompiledCode(tree)
      expect(result).to.eql(10)
    })
  })

  describe('given a class', function () {
    describe('with an invalid let-property', function () {
      var source = "var a = func () -> Integer { return 1 }\n"+
                   "class B { var c: () -> Integer = a }\n"
      it('should fail to parse', function () {
        expect(function () {
          parseAndWalk(source)
        }).to.throwException(/non-literal default for property/)
      })
    })

    describe('with a valid definition', function () {
      var source = "class A {\n"+
                   "  var b: Integer = 1\n"+
                   "  init () { this.b = this.b + 1 }\n"+
                   "  func c () -> Integer { return this.b + 1 }\n"+
                   "}\n"+
                   "var a = new A()\n"+
                   "return a.c()\n"
      var tree = null
      it('should parse', function () {
        tree = parseAndWalk(source)
        expect(tree).to.be.ok()
      })
      it('should product the correct result', function () {
        var result = runCompiledCode(tree)
        expect(result).to.eql(3)
      })
    })
  })
  
  xdescribe('given a while-true program', function () {
    var tree = null
    it('should parse', function () {
      tree = parseAndWalk(programs.whileTrue)
      expect(tree).to.be.ok()
    })
    it('should produce the correct result when executed', function () {
      var result = runCompiledCode(tree)
      expect(result).to.eql(5)
    })
  })
})

@@ -1004,16 +1004,355 @@
 )%0A  %7D)%0A%0A
+  describe('given a string literal', function () %7B%0A    var source = %22var a = %5C%22Hello world.%5C%22%5Cnreturn a%22%0A    it('should parse and produce the correct result', function () %7B%0A      var tree = parseAndWalk(source)%0A      expect(tree).to.be.ok()%0A      var result = runCompiledCode(tree)%0A      expect(result).to.eql('Hello world.')%0A    %7D)%0A  %7D)%0A%0A
   descri
@@ -2414,18 +2414,16 @@
 %7D)%0A  %7D)%0A
-  
 %0A  xdesc

Add test for string literals