mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
228 lines
8.1 KiB
JavaScript
228 lines
8.1 KiB
JavaScript
describe('Gun', function(){
|
|
var Gun = require('../gun')
|
|
, t = {};
|
|
describe('Utility', function(){
|
|
describe('Type Check', function(){
|
|
it('binary', function(){
|
|
expect(Gun.bi.is(false)).to.be(true);
|
|
expect(Gun.bi.is(true)).to.be(true);
|
|
expect(Gun.bi.is('')).to.be(false);
|
|
expect(Gun.bi.is('a')).to.be(false);
|
|
expect(Gun.bi.is(0)).to.be(false);
|
|
expect(Gun.bi.is(1)).to.be(false);
|
|
expect(Gun.bi.is([])).to.be(false);
|
|
expect(Gun.bi.is([1])).to.be(false);
|
|
expect(Gun.bi.is({})).to.be(false);
|
|
expect(Gun.bi.is({a:1})).to.be(false);
|
|
expect(Gun.bi.is(function(){})).to.be(false);
|
|
});
|
|
it('number',function(){
|
|
expect(Gun.num.is(0)).to.be(true);
|
|
expect(Gun.num.is(1)).to.be(true);
|
|
expect(Gun.num.is(Infinity)).to.be(true);
|
|
expect(Gun.num.is(NaN)).to.be(false);
|
|
expect(Gun.num.is('')).to.be(false);
|
|
expect(Gun.num.is('a')).to.be(false);
|
|
expect(Gun.num.is([])).to.be(false);
|
|
expect(Gun.num.is([1])).to.be(false);
|
|
expect(Gun.num.is({})).to.be(false);
|
|
expect(Gun.num.is({a:1})).to.be(false);
|
|
expect(Gun.num.is(false)).to.be(false);
|
|
expect(Gun.num.is(true)).to.be(false);
|
|
expect(Gun.num.is(function(){})).to.be(false);
|
|
});
|
|
it('text',function(){
|
|
expect(Gun.text.is('')).to.be(true);
|
|
expect(Gun.text.is('a')).to.be(true);
|
|
expect(Gun.text.is(false)).to.be(false);
|
|
expect(Gun.text.is(true)).to.be(false);
|
|
expect(Gun.text.is(0)).to.be(false);
|
|
expect(Gun.text.is(1)).to.be(false);
|
|
expect(Gun.text.is([])).to.be(false);
|
|
expect(Gun.text.is([1])).to.be(false);
|
|
expect(Gun.text.is({})).to.be(false);
|
|
expect(Gun.text.is({a:1})).to.be(false);
|
|
expect(Gun.text.is(function(){})).to.be(false);
|
|
});
|
|
it('list',function(){
|
|
expect(Gun.list.is([])).to.be(true);
|
|
expect(Gun.list.is([1])).to.be(true);
|
|
expect(Gun.list.is(0)).to.be(false);
|
|
expect(Gun.list.is(1)).to.be(false);
|
|
expect(Gun.list.is('')).to.be(false);
|
|
expect(Gun.list.is('a')).to.be(false);
|
|
expect(Gun.list.is({})).to.be(false);
|
|
expect(Gun.list.is({a:1})).to.be(false);
|
|
expect(Gun.list.is(false)).to.be(false);
|
|
expect(Gun.list.is(true)).to.be(false);
|
|
expect(Gun.list.is(function(){})).to.be(false);
|
|
});
|
|
it('obj',function(){
|
|
expect(Gun.obj.is({})).to.be(true);
|
|
expect(Gun.obj.is({a:1})).to.be(true);
|
|
expect(Gun.obj.is(0)).to.be(false);
|
|
expect(Gun.obj.is(1)).to.be(false);
|
|
expect(Gun.obj.is('')).to.be(false);
|
|
expect(Gun.obj.is('a')).to.be(false);
|
|
expect(Gun.obj.is([])).to.be(false);
|
|
expect(Gun.obj.is([1])).to.be(false);
|
|
expect(Gun.obj.is(false)).to.be(false);
|
|
expect(Gun.obj.is(true)).to.be(false);
|
|
expect(Gun.obj.is(function(){})).to.be(false);
|
|
});
|
|
it('fns',function(){
|
|
expect(Gun.fns.is(function(){})).to.be(true);
|
|
expect(Gun.fns.is('')).to.be(false);
|
|
expect(Gun.fns.is('a')).to.be(false);
|
|
expect(Gun.fns.is(0)).to.be(false);
|
|
expect(Gun.fns.is(1)).to.be(false);
|
|
expect(Gun.fns.is([])).to.be(false);
|
|
expect(Gun.fns.is([1])).to.be(false);
|
|
expect(Gun.fns.is({})).to.be(false);
|
|
expect(Gun.fns.is({a:1})).to.be(false);
|
|
expect(Gun.fns.is(false)).to.be(false);
|
|
expect(Gun.fns.is(true)).to.be(false);
|
|
});
|
|
it('time',function(){
|
|
t.ts = Gun.time.is();
|
|
expect(13 <= t.ts.toString().length).to.be.ok();
|
|
expect(Gun.num.is(t.ts)).to.be.ok();
|
|
expect(Gun.time.is(new Date())).to.be.ok();
|
|
});
|
|
});
|
|
describe('Text', function(){
|
|
it('ify',function(){
|
|
expect(Gun.text.ify(0)).to.be('0');
|
|
expect(Gun.text.ify(22)).to.be('22');
|
|
expect(Gun.text.ify([true,33,'yay'])).to.be('[true,33,"yay"]');
|
|
expect(Gun.text.ify({a:0,b:'1',c:[0,'1'],d:{e:'f'}})).to.be('{"a":0,"b":"1","c":[0,"1"],"d":{"e":"f"}}');
|
|
expect(Gun.text.ify(false)).to.be('false');
|
|
expect(Gun.text.ify(true)).to.be('true');
|
|
});
|
|
it('random',function(){
|
|
expect(Gun.text.random().length).to.be(24);
|
|
expect(Gun.text.random(11).length).to.be(11);
|
|
expect(Gun.text.random(4).length).to.be(4);
|
|
t.tr = Gun.text.random(2,'as'); expect((t.tr=='as'||t.tr=='aa'||t.tr=='sa'||t.tr=='ss')).to.be.ok();
|
|
});
|
|
});
|
|
describe('List', function(){
|
|
it('slit',function(){
|
|
(function(){
|
|
expect(Gun.list.slit.call(arguments, 0)).to.eql([1,2,3,'a','b','c']);
|
|
}(1,2,3,'a','b','c'));
|
|
});
|
|
it('sort',function(){
|
|
expect([{i:9},{i:4},{i:1},{i:-3},{i:0}].sort(Gun.list.sort('i'))).to.eql([{i:-3},{i:0},{i:1},{i:4},{i:9}]);
|
|
});
|
|
it('map',function(){
|
|
expect(Gun.list.map([1,2,3,4,5],function(v,i,t){ t(v+=this.d); this.d=v; },{d:0})).to.eql([1,3,6,10,15]);
|
|
expect(Gun.list.map([2,3,0,4],function(v,i,t){ if(!v){ return } t(v*=this.d); this.d=v; },{d:1})).to.eql([2,6,24]);
|
|
expect(Gun.list.map([true,false,NaN,Infinity,'',9],function(v,i,t){ if(i===3){ return 0 }})).to.be(0);
|
|
});
|
|
});
|
|
describe('Object', function(){
|
|
it('del',function(){
|
|
var obj = {a:1,b:2};
|
|
Gun.obj.del(obj,'a');
|
|
expect(obj).to.eql({b:2});
|
|
});
|
|
it('has',function(){
|
|
var obj = {a:1,b:2};
|
|
expect(Gun.obj.has(obj,'a')).to.be.ok();
|
|
});
|
|
it('copy',function(){
|
|
var obj = {"a":false,"b":1,"c":"d","e":[0,1],"f":{"g":"h"}};
|
|
var copy = Gun.obj.copy(obj);
|
|
expect(copy).to.eql(obj);
|
|
expect(copy).to.not.be(obj);
|
|
});
|
|
it('ify',function(){
|
|
expect(Gun.obj.ify('[0,1]')).to.eql([0,1]);
|
|
expect(Gun.obj.ify('{"a":false,"b":1,"c":"d","e":[0,1],"f":{"g":"h"}}')).to.eql({"a":false,"b":1,"c":"d","e":[0,1],"f":{"g":"h"}});
|
|
});
|
|
it('map',function(){
|
|
expect(Gun.obj.map({a:'z',b:'y',c:'x'},function(v,i,t){ t(v,i) })).to.eql({x:'c',y:'b',z:'a'});
|
|
expect(Gun.obj.map({a:'z',b:false,c:'x'},function(v,i,t){ if(!v){ return } t(i,v) })).to.eql({a:'z',c:'x'});
|
|
expect(Gun.obj.map({a:'z',b:3,c:'x'},function(v,i,t){ if(v===3){ return 0 }})).to.be(0);
|
|
});
|
|
});
|
|
});
|
|
|
|
it('ify', function(){
|
|
var data, test;
|
|
|
|
data = {a: false, b: true, c: 0, d: 1, e: '', f: 'g', h: null};
|
|
test = Gun.ify(data);
|
|
expect(test.err).to.not.be.ok();
|
|
|
|
data = {};
|
|
data.a = {x: 1, y: 2, z: 3}
|
|
data.b = {m: 'n', o: 'p', q: 'r', s: 't'};
|
|
data.a.kid = data.b;
|
|
data.b.parent = data.a;
|
|
data.loop = [data.b, data.a.kid, data];
|
|
test = Gun.ify(data);
|
|
expect(test.err).to.not.be.ok();
|
|
|
|
data = {_: {'#': 'shhh', meta: {yay: 1}}, sneak: true};
|
|
test = Gun.ify(data);
|
|
expect(test.err).to.not.be.ok(); // metadata needs to be stored, but it can't be used for data.
|
|
|
|
data = {};
|
|
data.sneak = false;
|
|
data.both = {inside: 'meta data'};
|
|
data._ = {'#': 'shhh', data: {yay: 1}, spin: data.both};
|
|
test = Gun.ify(data);
|
|
expect(test.err.meta).to.be.ok(); // TODO: Fail: this passes, somehow? Fix ify code!
|
|
|
|
data = {one: {two: [9, 8, 7, 6, 5]}};
|
|
test = Gun.ify(data);
|
|
expect(test.err.array).to.be.ok();
|
|
|
|
data = {z: undefined, x: 'bye'};
|
|
test = Gun.ify(data);
|
|
expect(test.err.invalid).to.be.ok();
|
|
|
|
data = {a: NaN, b: 2};
|
|
test = Gun.ify(data);
|
|
expect(test.err.invalid).to.be.ok();
|
|
|
|
data = {a: 1, b: Infinity};
|
|
test = Gun.ify(data);
|
|
expect(test.err.invalid).to.be.ok();
|
|
|
|
data = {c: function(){}, d: 'hi'};
|
|
test = Gun.ify(data);
|
|
expect(test.err.invalid).to.be.ok();
|
|
|
|
console.log(test.nodes);
|
|
});
|
|
|
|
it('union', function(){
|
|
var graph, prime;
|
|
|
|
graph = Gun.ify({a: false, b: true, c: 0, d: 1, e: '', f: 'g', h: null}).nodes;
|
|
prime = Gun.ify({h: 9, i: 'foo', j: 'k', l: 'bar', m: 'Mark', n: 'Nadal'}).nodes;
|
|
|
|
Gun.union(graph, prime);
|
|
});
|
|
|
|
it('path', function(done){
|
|
console.log("fix path!");
|
|
return done(); // TODO: FIX! This is broken because of API changes, fix it!
|
|
|
|
this.timeout(9000);
|
|
var gun = require('gun')({
|
|
s3: require('./shotgun') // replace this with your own keys!
|
|
});
|
|
gun.load('d1ed426098eae2bba8c60605e1e4552f66281770', null, {id: true}) // get Rodney Morris
|
|
.path('parent.parent.first') // Rodney's parent is Juan Colon, whose parent is Francis Peters
|
|
.get(function(val){
|
|
console.log("val!", val);
|
|
done();
|
|
});
|
|
console.log("________________________");
|
|
});
|
|
|
|
}); |