should = require "should"
Yie = require "../lib/l"

describe "StateMachine", ->
  
  describe "Attributes", ->
    
    describe "Entity", ->
      
      describe "Person", ->
        
        it "should have attributes with default values", (done) ->
          
          class Person extends Yie.Entity
            @attributes ->
              "first name":
                value: "Johnny"
              "last name":
                value: null
                
          person = new Person
          person.read("first name").should.equal "Johnny"
          should.not.exist person.read("last name")
          
          person.set "last name", "Luu"
          person.read("last name").should.equal "Luu"
          
          done()
          
        it "should validate default attribute values", (done) ->
          
          class Person extends Yie.Entity
            @attributes ->
              "first name":
                value: "Johnny"
                validate: -> @value.isAlpha().notNull()
              "last name":
                value: null,
                validate: -> @value.isNull()
                
          done()
          
        it "should validate attribute values on update", (done) ->
          
          class Person extends Yie.Entity
            @attributes ->
              "first name":
                value: "Johnny"
                validate: -> @value.isAlpha().notNull()
              "last name":
                validate: -> @value.isAlpha().notNull()
                
          person = new Person
          person.read("first name").should.equal "Johnny"
          should.not.exist person.read("last name")
          
          person.update "last name", "Luu"
          person.read("last name").should.equal "Luu"
          
          done()
          
    describe "Link", ->
      
      describe "Owns a computer", ->
        
        it "should have attributes just like entities", (done) ->
          
          class OWNS_A_COMPUTER extends Yie.Link
            @attributes ->
              "since":
                value: "2012-12-12"
                
          ownsAComputer = new OWNS_A_COMPUTER
          ownsAComputer.read("since").should.equal "2012-12-12"
          
          done()