describe("Ext.data.amf.XmlDecoder", function() { var decoder = Ext.create('Ext.data.amf.XmlDecoder'); var val; beforeEach(function() { decoder.clear(); // reset encoder }); var toXml = function(str) { return Ext.data.amf.XmlDecoder.readXml(str).firstChild; }; var decode = function(str) { return decoder.readValue(toXml(str)); }; describe("clear", function() { it("should reset all the reference tables when called", function() { var val = decode("ahello"); expect(decoder.objectReferences).not.toEqual([]); expect(decoder.traitsReferences).not.toEqual([]); expect(decoder.stringReferences).not.toEqual([]); decoder.clear(); expect(decoder.objectReferences).toEqual([]); expect(decoder.traitsReferences).toEqual([]); expect(decoder.stringReferences).toEqual([]); }); }); describe("AMFX", function() { describe("data types", function() { describe("null", function() { it("should decode null", function() { val =decode(""); expect(val).toEqual(null); }); }); describe("false", function() { it("should decode false", function() { val = decode(""); expect(val).toEqual(false); }); }); describe("true", function() { it("should decode true", function() { val = decode(""); expect(val).toEqual(true); }); }); describe("integer", function() { it("should decode 0", function() { val = decode("0"); expect(val).toEqual(0); }); it("should decode 2^29-1 (the largest possible unsigned 29-bit int)", function() { val = decode("536870911"); expect(val).toEqual(Math.pow(2, 29) - 1); }); }); describe("double", function() { it("should decode 10.333", function() { val =decode("10.333"); expect(val).toEqual(10.333); }); it("should decode 1.7976931348623157e+308 (largest positive number)", function() { val =decode("1.7976931348623157e+308"); expect(val).toEqual(Number.MAX_VALUE); }); it("should decode -1.7976931348623157e+308 (largest negative number)", function() { val =decode("-1.7976931348623157e+308"); expect(val).toEqual(-Number.MAX_VALUE); }); it("should decode 5e-324 (smallest positive number)", function() { val =decode("5e-324"); expect(val).toEqual(Number.MIN_VALUE); }); it("should decode -5e-324 (smallest negative number)", function() { val =decode("-5e-324"); expect(val).toEqual(-Number.MIN_VALUE); }); it("should decode subnormal 2.2250738585072014E-308", function() { val =decode("2.2250738585072014e-308"); expect(val).toEqual(2.2250738585072014E-308); }); it("should decode NaN", function() { val =decode("NaN"); expect(isNaN(val)).toEqual(true); }); it("should decode positive infinity", function() { val =decode("Infinity"); expect(val).toEqual(Infinity); }); it("should decode negative infinity", function() { val =decode("-Infinity"); expect(val).toEqual(-Infinity); }); }); describe("string", function() { it("should decode an empty string", function() { val =decode(""); expect(val).toEqual(""); }); // Special thanks to Markus Kuhn's "quickbrown.txt" for the // following awesome pangrams. // http://www.cl.cam.ac.uk/~mgk25/ucs/examples/quickbrown.txt it("should decode Danish", function() { var str = "Quizdeltagerne spiste jordbær med fløde, mens " + "cirkusklovnen Wolther spillede på xylofon"; val =decode("Quizdeltagerne spiste jordbær med fløde, mens cirkusklovnen Wolther spillede på xylofon"); expect(val).toEqual(str); }); it("should decode Hebrew", function() { var str = "דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה"; val =decode("דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה"); expect(val).toEqual(str); }); }); describe("xml document", function() { it("should decode an XMLDocument", function() { var xml = 'foo]]>'; var data = [ // chrome version 'foo', // IE version 'foo', // IE 9 version 'foo', // Opera version 'foo' ]; var encoder=Ext.create('Ext.data.amf.XmlEncoder'); val = encoder.convertXmlToString(decode(xml)); expect(data).toContain(Ext.String.trim(val)); // note reverseal of expect / contain data to use toContain }); }); describe("date", function() { it("should decode 7/24/2012", function() { val =decode("1343164970869"); expect(val).toEqual(new Date(1343164970869)); }); it("should decode 7/24/1912 (100 years before previous test's date)", function() { val =decode("-1812595029131"); expect(val).toEqual(new Date(-1812595029131)); }); it("should decode the UNIX epoch", function() { val =decode("0"); expect(val).toEqual(new Date(0)); }); }); describe("array", function() { it("should decode an empty array", function() { val =decode(''); expect(val).toEqual([]); }); it("should decode an array", function() { val =decode('abc'); expect(val).toEqual(['a','b','c']); }); it("should decode an array with associative data", function() { var arr = ['a']; arr.b = 1; val =decode('a1'); expect(val).toEqual(arr); // And check associtave part manually since it's not checked by the matcher expect(val.b).toEqual(arr.b); }); }); describe("object", function() { it("should decode an empty anonymous object", function() { val =decode(""); expect(val).toEqual({}); }); it("should decode an anonymous object with data items", function() { val =decode('1str1string'); expect(val).toEqual({1: 1, "str":"string"}); }); }); describe("byte-array", function() { it("should decode a byte array", function() { val = decode("090701060361060362060363"); expect(val).toEqual(['a','b','c']); }); }); describe("reference tables", function() { it("should correctly read reference-table objects, arrays and traits", function() { val = decode('\ a\ \ \ \ \ 1\ \ \ \ 2\ \ '); expect(val).toEqual(["a", {a:1}, "a", {a:1}, {a:2}]); }); }); }); describe("AMFX messages", function() { it("should read an AMFX message", function() { val = decoder.readAmfxMessage('timestampheadersbodycorrelationIdmessageIdtimeToLiveclientIddestination1.354577842341E121234500000002-C28A-C38A-984B-6321901916D7FD04F220-6409-515E-8D77-F198A071B85E0.0274FBBCE-2179-FC6D-393A-62A933E67F8B'); expect(val.targetURI).toEqual('/onResult'); expect(val.responseURI).toEqual(""); expect(val.message.headers).toEqual({}); expect(val.message.body).toEqual(12345); expect(val.message.correlationId).toEqual("00000002-C28A-C38A-984B-6321901916D7"); }); }); }); });