]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#233] guid needs to be more unique; +10 unit tests
authorrwldrn <waldron.rick@gmail.com>
Fri, 17 Dec 2010 16:38:38 +0000 (11:38 -0500)
committerrwldrn <waldron.rick@gmail.com>
Fri, 17 Dec 2010 16:40:51 +0000 (11:40 -0500)
popcorn.js
test/popcorn.unit.js

index d5faf6dcb82a9172f22169fc6b93c806e62848a1..dcb30c740d90280d149d9973dca6006cea166236 100644 (file)
     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;
     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
   
     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 );      
index 2063a3626806bdd9d7042ed6bcb665a0e319c607..ad476415d956cd432c04ca44ea89e760363a0ed5 100644 (file)
@@ -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);