Jared K. Smith 5dda8de
'use strict';
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
require('mocha');
Jared K. Smith 5dda8de
var assert = require('assert');
Jared K. Smith 5dda8de
var getter = require('./');
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
describe('set-getter', function() {
Jared K. Smith 5dda8de
  it('should export a function', function() {
Jared K. Smith 5dda8de
    assert.equal(typeof getter, 'function');
Jared K. Smith 5dda8de
  });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
  it('should set a getter on an object', function() {
Jared K. Smith 5dda8de
    var obj = {};
Jared K. Smith 5dda8de
    getter(obj, 'foo', function() {
Jared K. Smith 5dda8de
      return 'bar';
Jared K. Smith 5dda8de
    });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert('foo' in obj);
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo, 'string');
Jared K. Smith 5dda8de
    assert.equal(obj.foo, 'bar');
Jared K. Smith 5dda8de
  });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
  it('should only call the getter once', function() {
Jared K. Smith 5dda8de
    var calls = 0;
Jared K. Smith 5dda8de
    var cache = {}, obj = {};
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    getter(obj, 'foo', function() {
Jared K. Smith 5dda8de
      if (cache.foo) return cache.foo;
Jared K. Smith 5dda8de
      calls++;
Jared K. Smith 5dda8de
      return (cache.foo = 'bar');
Jared K. Smith 5dda8de
    });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert.equal(calls, 0);
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert('foo' in obj);
Jared K. Smith 5dda8de
    assert.equal(calls, 0);
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo, 'string');
Jared K. Smith 5dda8de
    assert.equal(calls, 1);
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert.equal(obj.foo, 'bar');
Jared K. Smith 5dda8de
    assert.equal(calls, 1);
Jared K. Smith 5dda8de
  });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
  it('should expose the object as "this" in the getter', function() {
Jared K. Smith 5dda8de
    var obj = {abc: 'xyz'};
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    getter(obj, 'foo', function() {
Jared K. Smith 5dda8de
      assert.equal(this.abc, 'xyz');
Jared K. Smith 5dda8de
      return 'bar';
Jared K. Smith 5dda8de
    });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert('foo' in obj);
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo, 'string');
Jared K. Smith 5dda8de
    assert.equal(obj.foo, 'bar');
Jared K. Smith 5dda8de
  });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
  it('should add getter to an object hierarchy using dot notation', function() {
Jared K. Smith 5dda8de
    var obj = {};
Jared K. Smith 5dda8de
    getter(obj, 'foo.bar', function() {
Jared K. Smith 5dda8de
      return 'beep';
Jared K. Smith 5dda8de
    });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    getter(obj, 'foo.baz', function() {
Jared K. Smith 5dda8de
      return 'boop';
Jared K. Smith 5dda8de
    });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert('foo' in obj);
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo, 'object');
Jared K. Smith 5dda8de
    assert('bar' in obj.foo);
Jared K. Smith 5dda8de
    assert('baz' in obj.foo);
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo.bar, 'string');
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo.baz, 'string');
Jared K. Smith 5dda8de
    assert.equal(obj.foo.bar, 'beep');
Jared K. Smith 5dda8de
    assert.equal(obj.foo.baz, 'boop');
Jared K. Smith 5dda8de
  });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
  it('should add getter to an object hierarchy using array notation', function() {
Jared K. Smith 5dda8de
    var obj = {};
Jared K. Smith 5dda8de
    getter(obj, ['foo', 'bar'], function() {
Jared K. Smith 5dda8de
      return 'beep';
Jared K. Smith 5dda8de
    });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    getter(obj, ['foo', 'baz'], function() {
Jared K. Smith 5dda8de
      return 'boop';
Jared K. Smith 5dda8de
    });
Jared K. Smith 5dda8de
Jared K. Smith 5dda8de
    assert('foo' in obj);
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo, 'object');
Jared K. Smith 5dda8de
    assert('bar' in obj.foo);
Jared K. Smith 5dda8de
    assert('baz' in obj.foo);
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo.bar, 'string');
Jared K. Smith 5dda8de
    assert.equal(typeof obj.foo.baz, 'string');
Jared K. Smith 5dda8de
    assert.equal(obj.foo.bar, 'beep');
Jared K. Smith 5dda8de
    assert.equal(obj.foo.baz, 'boop');
Jared K. Smith 5dda8de
  });
Jared K. Smith 5dda8de
});