# HG changeset patch # User hamidouk # Date 1318413266 -7200 # Node ID 6a8cae20f190a5319e7917a5cf769e76c20c6945 # Parent b13359f9ce4801aa5257f5ef304c58bbda3b85bb Added new unit tests and changes to the data classes. diff -r b13359f9ce48 -r 6a8cae20f190 src/js/data.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({ diff -r b13359f9ce48 -r 6a8cae20f190 unittests/index.html --- 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 @@ Unit Tests - + @@ -10,12 +10,15 @@ + + - diff -r b13359f9ce48 -r 6a8cae20f190 unittests/tests/JSONSerializer.js --- /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 diff -r b13359f9ce48 -r 6a8cae20f190 unittests/tests/serializer.js --- 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 diff -r b13359f9ce48 -r 6a8cae20f190 unittests/tests/serializerFactory.js --- /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