Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalparadox committed Nov 13, 2012
1 parent 1407381 commit 5e88625
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
3 changes: 2 additions & 1 deletion test/browser/index.html
Expand Up @@ -14,7 +14,8 @@
<script>
properties = require('tea-properties');
</script>
<!-- <script src="../test.js"></script> -->
<script src="../get.js"></script>
<script src="../set.js"></script>
<script>onload = function () {
if (window.mochaPhantomJS) mochaPhantomJS.run()
else mocha.run();
Expand Down
31 changes: 31 additions & 0 deletions test/get.js
@@ -0,0 +1,31 @@
describe('.get(obj, path)', function () {
it('should get the value for simple nested object', function () {
var obj = { hello: { universe: 'world' }}
, val = properties.get(obj, 'hello.universe');
val.should.equal('world');
});

it('should get the value for simple array', function () {
var obj = { hello: [ 'zero', 'one' ] }
, val = properties.get(obj, 'hello[1]');
val.should.equal('one');
});

it('should get the value of nested array', function () {
var obj = { hello: [ 'zero', [ 'a', 'b' ] ] }
, val = properties.get(obj, 'hello[1][0]');
val.should.equal('a');
});

it('should get the value of array only', function () {
var obj = [ 'zero', 'one' ]
, val = properties.get(obj, '[1]');
val.should.equal('one');
});

it('should get the value of array only nested', function () {
var obj = [ 'zero', [ 'a', 'b' ] ]
, val = properties.get(obj, '[1][1]');
val.should.equal('b');
});
});
51 changes: 51 additions & 0 deletions test/set.js
@@ -0,0 +1,51 @@
describe('.set(obj, path, value)', function () {
it('should allow value to be set in simple object', function () {
var obj = {};
properties.set(obj, 'hello', 'universe');
obj.should.deep.equal({ hello: 'universe' });
});

it('should allow nested object value to be set', function () {
var obj = {};
properties.set(obj, 'hello.universe', 'properties');
obj.should.deep.equal({ hello: { universe: 'properties' }});
});

it('should allow nested array value to be set', function () {
var obj = {};
properties.set(obj, 'hello.universe[1].properties', 'galaxy');
obj.should.deep.equal({ hello: { universe: [ , { properties: 'galaxy' } ] }});
});

it('should allow value to be REset in simple object', function () {
var obj = { hello: 'world' };
properties.set(obj, 'hello', 'universe');
obj.should.deep.equal({ hello: 'universe' });
});

it('should allow value to be set in complex object', function () {
var obj = { hello: { }};
properties.set(obj, 'hello.universe', 42);
obj.should.deep.equal({ hello: { universe: 42 }});
});

it('should allow value to be REset in complex object', function () {
var obj = { hello: { universe: 100 }};
properties.set(obj, 'hello.universe', 42);
obj.should.deep.equal({ hello: { universe: 42 }});
});

it('should allow for value to be set in array', function () {
var obj = { hello: [] };
properties.set(obj, 'hello[0]', 1);
obj.should.deep.equal({ hello: [1] });
properties.set(obj, 'hello[2]', 3);
obj.should.deep.equal({ hello: [1 , , 3] });
});

it('should allow for value to be REset in array', function () {
var obj = { hello: [ 1, 2, 4 ] };
properties.set(obj, 'hello[2]', 3);
obj.should.deep.equal({ hello: [ 1, 2, 3 ] });
});
});

0 comments on commit 5e88625

Please sign in to comment.