Added new unit tests and changes to the data classes. popcorn-port
authorhamidouk
Wed, 12 Oct 2011 11:54:26 +0200
branchpopcorn-port
changeset 65 6a8cae20f190
parent 64 b13359f9ce48
child 66 13013b9452af
Added new unit tests and changes to the data classes.
src/js/data.js
unittests/index.html
unittests/tests/JSONSerializer.js
unittests/tests/serializer.js
unittests/tests/serializerFactory.js
--- a/src/js/data.js	Tue Oct 11 17:15:37 2011 +0200
+++ b/src/js/data.js	Wed Oct 12 11:54:26 2011 +0200
@@ -19,10 +19,46 @@
   }
 }
 
-IriSP.Serializer = function(DataLoader) {
-  this.DataLoader = DataLoader;
+/* the base abstract "class" */
+IriSP.Serializer = function(DataLoader, url) {
+  this._DataLoader = DataLoader;
+  this._url = url;
+};
+
+IriSP.Serializer.prototype.serialize = function(data) { };
+IriSP.Serializer.prototype.deserialize = function(data) {};
+
+IriSP.JSONSerializer = function(DataLoader, url) {
+  IriSP.Serializer.call(this, DataLoader, url);
+}
+
+IriSP.JSONSerializer.prototype = IriSP.Serializer;
+
+IriSP.JSONSerializer.prototype.serialize = function(data) {
+  return JSON.stringify(data);
 };
 
+IriSP.JSONSerializer.prototype.deserialize = function(data) {
+  return JSON.parse(data);
+};
+
+IriSP.SerializerFactory = function(DataLoader) {
+  this._dataloader = DataLoader;
+};
+
+IriSP.SerializerFactory.prototype.getSerializer = function(config) {
+  /* This function returns serializer set-up with the correct
+     configuration
+  */
+  switch(config.metadata.load) {
+    case "json":
+      return new IriSP.JSONSerializer(this._dataloader, config.metadata.src);
+    default:
+      return undefined;
+  }
+};
+
+
 IriSP.getMetadata = function() {
 	
 	IriSP.jQuery.ajax({
--- a/unittests/index.html	Tue Oct 11 17:15:37 2011 +0200
+++ b/unittests/index.html	Wed Oct 12 11:54:26 2011 +0200
@@ -1,7 +1,7 @@
 <html>
 <head>
 	<title>Unit Tests</title>
-	<script src="http://code.jquery.com/jquery-latest.js"></script>
+	<script src="../res/js/jquery.min.js"></script>
 	<script src="../build/LdtPlayer-release.js"></script>
 	<script src="qunit.js" type="text/javascript"></script>
 	<script src="sinon.js" type="text/javascript"></script>
@@ -10,12 +10,15 @@
 	  <link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />
 	<script src="tests/dataloader.js" type="text/javascript"></script>
 	<script src="tests/serializer.js" type="text/javascript"></script>
+	<script src="tests/JSONSerializer.js" type="text/javascript"></script>
+	<script src="tests/serializerFactory.js" type="text/javascript"></script>
 </head>
-<body>
 <script>
  $(document).ready(function(){
 		test_dataloader();
 		test_serializer();
+		test_JSONSerializer();
+		test_serializerFactory()
 });
 </script>	
 <body>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unittests/tests/JSONSerializer.js	Wed Oct 12 11:54:26 2011 +0200
@@ -0,0 +1,16 @@
+function test_JSONSerializer() {
+  module("JSON Serializer tests", 
+    { setup: function() {
+      this.dt = new IriSP.DataLoader();
+      }
+    }
+    );    
+    
+    test("should return the correct JSON", function() {
+      var arr = ["ab", {"de" : "fg"}, "lp"];
+      var serializer = new IriSP.JSONSerializer(this.dt);
+
+      equal(serializer.serialize(arr), JSON.stringify(arr), "assert that the outputted json is correct");
+    });
+
+};
\ No newline at end of file
--- a/unittests/tests/serializer.js	Tue Oct 11 17:15:37 2011 +0200
+++ b/unittests/tests/serializer.js	Wed Oct 12 11:54:26 2011 +0200
@@ -1,10 +1,18 @@
 function test_serializer() {
   module("Serializer basic tests");
   
-  test("a basic test example", function() {
-      ok( true, "this test is fine" );
-      var value = "hello";
-      equal( value, "hello", "We expect value to be hello" );
+  test("init the serializer with a DataLoader and an url", function() {
+      var dt = new IriSP.DataLoader();
+      var ser = new IriSP.Serializer(dt, "http://google.com");
+      equal( ser._DataLoader, dt, "The dataloader reference is copied to the object." );
+      equal( ser._url, "http://google.com", "The url has been copied as well." );
+  });
+  
+  test("check that the serialize and deserialize abstract functions are defined", function() {
+      var dt = new IriSP.DataLoader();
+      var ser = new IriSP.Serializer(dt);
+      equal(ser.serialize(), undefined, ".serialize is defined");
+      equal(ser.deserialize(), undefined, ".deserialize is defined");
   });
 
 };
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unittests/tests/serializerFactory.js	Wed Oct 12 11:54:26 2011 +0200
@@ -0,0 +1,23 @@
+/* tests for the serializer factory */
+function test_serializerFactory() {
+  module("SerializerFactory tests", 
+    { setup: function() {
+              this.dt = new IriSP.DataLoader();
+  }}); 
+  
+  test("test instantiation of a json serializer", function() {
+    var factory = new IriSP.SerializerFactory(this.dt);    
+    var config = {metadata : { load: "json", src : "/url" } };
+    var ser = factory.getSerializer(config);
+    
+    ok(ser instanceof IriSP.JSONSerializer, "returned object is instance of json serializer");    
+  });
+  
+  test("test instantiation of a garbage serializer", function() {
+    var factory = new IriSP.SerializerFactory(this.dt);    
+    var config = {metadata : { load: "garbage", src : "/url" } };
+    var ser = factory.getSerializer(config);
+    
+    equal(ser, undefined, "returned object is undefined");    
+  });
+};
\ No newline at end of file