From: rwldrn Date: Fri, 17 Dec 2010 16:38:38 +0000 (-0500) Subject: [#233] guid needs to be more unique; +10 unit tests X-Git-Tag: v0.2~37 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=1868b6e7a4539a6f564c0470573b2740a65cfbe5;p=popcorn-js.git [#233] guid needs to be more unique; +10 unit tests --- diff --git a/popcorn.js b/popcorn.js index d5faf6d..dcb30c7 100644 --- a/popcorn.js +++ b/popcorn.js @@ -170,8 +170,9 @@ error: function( msg ) { throw msg; }, - guid: function() { - return +new Date() + Math.floor(Math.random()*11); + guid: function( prefix ) { + Popcorn.guid.counter++; + return ( prefix ? prefix : '' ) + ( +new Date() + Popcorn.guid.counter ); }, sizeOf: function ( obj ) { var size = 0; @@ -187,6 +188,9 @@ nop: function () {} }); + // Memoization property + Popcorn.guid.counter = 1; + // Simple Factory pattern to implement getters, setters and controllers // as methods of the returned Popcorn instance. The immediately invoked function // creates and returns an object of methods @@ -437,7 +441,7 @@ if ( track.natives ) { // supports user defined track event id - track._id = !track.id ? track.natives.type + Popcorn.guid() : track.id; + track._id = !track.id ? Popcorn.guid( track.natives.type ) : track.id; // Push track event ids into the history obj.data.history.push( track._id ); diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 2063a36..ad47641 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -56,6 +56,45 @@ test("Utility", function () { }); +test("guid", function () { + + expect(10); + + var count = 3, + guids = [], + temp; + + for ( var i = 0; i < count; i++ ) { + + temp = Popcorn.guid(); + + ok( temp, "Popcorn.guid() returns value" ); + + if ( i > 0 ) { + notEqual( temp, guids[ guids.length - 1 ], "Current guid does not equal last guid" ); + } + + guids.push(temp); + } + + guids = []; + + for ( var i = 0; i < count; i++ ) { + + temp = Popcorn.guid( "pre" ); + + ok( temp, "Popcorn.guid( 'pre' ) returns value" ); + + if ( i > 0 ) { + notEqual( temp, guids[ guids.length - 1 ], "Current guid does not equal last guid" ); + } + + guids.push(temp); + } + +}); + + test("Protected", function () { expect(1);