const chai = require("chai");
const sinon = require("sinon");
const expect = chai.expect;
chai.use(require("sinon-chai"));

var tag = require('../src/tag.js')();
var Scope = require('../src/scope.js');

describe('tag', function() {
    var scope;
    before(function() {
        scope = Scope.factory({
            foo: 'bar',
            arr: [2, 1]
        });
        tag.clear();
    });

    it('should throw when not registered', function() {
        expect(function() {
            tag.construct({
                type: 'tag',
                value: 'foo',
                name: 'foo'
            }, []);
        }).to.throw(/tag foo not found/);
    });

    it('should register simple tag', function() {
        expect(
            function() {
                tag.register('foo', {
                    render: x => 'bar'
                });
            }).not.throw();
    });

    it('should call tag.render', function() {
        var spy = sinon.spy(),
            tokens = [];
        tag.register('foo', {
            render: spy
        });
        return tag
            .construct({
                type: 'tag',
                value: 'foo',
                name: 'foo'
            }, [])
            .render(scope, {})
            .then(() => expect(spy).to.have.been.called);
    });

    it('should call tag.render with resolved hash', function() {
        var spy = sinon.spy(),
            tokens = [];
        tag.register('foo', {
            render: spy
        });
        var token = {
            type: 'tag',
            value: 'foo aa:foo bb: arr[0] cc: 2.3',
            name: 'foo',
            args: 'aa:foo bb: arr[0] cc: 2.3'
        };
        return tag.construct(token, []).render(scope, {})
            .then(() => expect(spy).to.have.been.calledWithMatch(scope, {
                aa: 'bar',
                bb: 2,
                cc: 2.3
            }));
    });
});

@@ -343,16 +343,77 @@
 : %5B2, 1%5D
+,%0A            bar: %7B%0A                coo: 'uoo'%0A            %7D
 %0A       
@@ -1021,33 +1021,8 @@
 py()
-,%0A            tokens = %5B%5D
 ;%0A  
@@ -1341,49 +1341,18 @@
     
-it('should call tag.render with resolved 
+describe('
 hash
@@ -1356,33 +1356,32 @@
 ash', function()
- 
 %7B%0A        var sp
@@ -1385,23 +1385,47 @@
  spy
- = sinon.spy(),
+, token;%0A        beforeEach(function()%7B
 %0A   
@@ -1437,21 +1437,31 @@
     
-tokens = %5B%5D;%0A
+spy = sinon.spy();%0A    
     
@@ -1490,32 +1490,36 @@
 , %7B%0A            
+    
 render: spy%0A    
@@ -1506,32 +1506,36 @@
     render: spy%0A
+    
         %7D);%0A    
@@ -1538,19 +1538,19 @@
         
-var
+   
  token =
@@ -1552,16 +1552,20 @@
 ken = %7B%0A
+    
         
@@ -1585,32 +1585,36 @@
 g',%0A            
+    
 value: 'foo aa:f
@@ -1634,19 +1634,34 @@
  cc: 2.3
-',%0A
+ dd:bar.coo',%0A    
         
@@ -1689,16 +1689,20 @@
         
+    
 args: 'a
@@ -1725,18 +1725,33 @@
  cc: 2.3
-'%0A
+ dd:bar.coo'%0A    
         
@@ -1753,16 +1753,93 @@
      %7D;%0A
+        %7D);%0A        it('should call tag.render with scope', function() %7B%0A    
         
@@ -1880,32 +1880,36 @@
 nder(scope, %7B%7D)%0A
+    
             .the
@@ -1966,100 +1966,1068 @@
 cope
-, %7B%0A                aa: 'bar',%0A                bb: 2,%0A                cc: 2.3%0A            %7D)
+));%0A        %7D);%0A        it('should resolve identifier hash', function() %7B%0A            return tag.construct(token, %5B%5D).render(scope, %7B%7D)%0A                .then(() =%3E expect(spy).to.have.been.calledWithMatch(%7B%7D, %7B%0A                    aa: 'bar'%0A                %7D));%0A        %7D);%0A        it('should accept space between key/value', function() %7B%0A            return tag.construct(token, %5B%5D).render(scope, %7B%7D)%0A                .then(() =%3E expect(spy).to.have.been.calledWithMatch(%7B%7D, %7B%0A                    bb: 2,%0A                %7D));%0A        %7D);%0A        it('should resolve number value hash', function() %7B%0A            return tag.construct(token, %5B%5D).render(scope, %7B%7D)%0A                .then(() =%3E expect(spy).to.have.been.calledWithMatch(scope, %7B%0A                    cc: 2.3%0A                %7D));%0A        %7D);%0A        it('should resolve property access hash', function() %7B%0A            return tag.construct(token, %5B%5D).render(scope, %7B%7D)%0A                .then(() =%3E expect(spy).to.have.been.calledWithMatch(scope, %7B%0A                    dd: 'uoo'%0A                %7D));%0A        %7D
 );%0A 

update tag test