spec/amf/serializer_spec.rb in RocketAMF-0.0.7 vs spec/amf/serializer_spec.rb in RocketAMF-0.1.0

- old
+ new

@@ -1,5 +1,7 @@ +# encoding: UTF-8 + require File.dirname(__FILE__) + '/../spec_helper.rb' require 'rexml/document' describe "when serializing" do @@ -32,13 +34,10 @@ output = RocketAMF.serialize(['a', 'b', 'c', 'd'], 0) output.should == object_fixture('amf0-strict-array.bin') end it "should serialize references" do - class OtherClass - attr_accessor :foo, :bar - end obj = OtherClass.new obj.foo = "baz" obj.bar = 3.14 output = RocketAMF.serialize({'0' => obj, '1' => obj}, 0) @@ -49,36 +48,39 @@ output = RocketAMF.serialize(Time.utc(2003, 2, 13, 5), 0) output.should == object_fixture('amf0-date.bin') end it "should serialize hashes" do - output = RocketAMF.serialize({:a => 'b', :c => 'd'}, 0) + output = RocketAMF.serialize({:a => 'b', 'c' => 'd'}, 0) output.should == object_fixture('amf0-hash.bin') end it "should serialize unmapped objects" do - class RubyClass - attr_accessor :foo, :baz - end obj = RubyClass.new obj.foo = "bar" output = RocketAMF.serialize(obj, 0) output.should == object_fixture('amf0-untyped-object.bin') end it "should serialize mapped objects" do - class RubyClass - attr_accessor :foo, :baz - end obj = RubyClass.new obj.foo = "bar" RocketAMF::ClassMapper.define {|m| m.map :as => 'org.rocketAMF.ASClass', :ruby => 'RubyClass'} output = RocketAMF.serialize(obj, 0) output.should == object_fixture('amf0-typed-object.bin') end + + if "".respond_to?(:force_encoding) + it "should support multiple encodings" do + shift_str = "\x53\x68\x69\x66\x74\x20\x83\x65\x83\x58\x83\x67".force_encoding("Shift_JIS") # "Shift テスト" + utf_str = "\x55\x54\x46\x20\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88".force_encoding("UTF-8") # "UTF テスト" + output = RocketAMF.serialize({"0" => 5, "1" => shift_str, "2" => utf_str, "3" => 5}, 0) + output.should == object_fixture("amf0-complexEncodedStringArray.bin") + end + end end describe "AMF3" do describe "simple messages" do it "should serialize a null" do @@ -151,13 +153,10 @@ expected = object_fixture("amf3-date.bin") input = Time.utc 1970, 1, 1, 0 output = RocketAMF.serialize(input, 3) output.should == expected end - - #BAH! Who sends XML over AMF? - it "should serialize XML" end describe "objects" do it "should serialize an unmapped object as a dynamic anonymous object" do class NonMappedObject @@ -186,11 +185,11 @@ end it "should serialize a hash as a dynamic anonymous object" do hash = {} hash[:answer] = 42 - hash[:foo] = "bar" + hash['foo'] = "bar" expected = object_fixture("amf3-hash.bin") input = hash output = RocketAMF.serialize(input, 3) output.should == expected @@ -276,10 +275,32 @@ input = [[obj1, obj2], "bar", [obj1, obj2]] output = RocketAMF.serialize(input, 3) output.should == expected end + it "should keep reference of duplicate object traits" do + obj1 = RubyClass.new + obj1.foo = "foo" + def obj1.to_amf serializer + stream = "" + serializer.write_object(self, stream, {:class_name => 'org.rocketAMF.ASClass', :dynamic => false, :externalizable => false, :members => ["baz", "foo"]}) + stream + end + obj2 = RubyClass.new + obj2.foo = "bar" + def obj2.to_amf serializer + stream = "" + serializer.write_object(self, stream, {:class_name => 'org.rocketAMF.ASClass', :dynamic => false, :externalizable => false, :members => ["baz", "foo"]}) + stream + end + input = [obj1, obj2] + + expected = object_fixture("amf3-traitRef.bin") + output = RocketAMF.serialize(input, 3) + output.should == expected + end + it "should keep references of duplicate arrays" do a = [1,2,3] b = %w{ a b c } expected = object_fixture("amf3-arrayRef.bin") @@ -298,12 +319,10 @@ input = [a,b,a,b] output = RocketAMF.serialize(input, 3) output.should == expected end - it "should keep references of duplicate XML and XMLDocuments" - it "should keep references of duplicate byte arrays" do b = StringIO.new "ASDF" expected = object_fixture("amf3-byteArrayRef.bin") input = [b, b] @@ -311,12 +330,12 @@ output.should == expected end it "should serialize a deep object graph with circular references" do class GraphMember - attr_accessor :parent attr_accessor :children + attr_accessor :parent def initialize self.children = [] end @@ -333,9 +352,30 @@ expected = object_fixture("amf3-graphMember.bin") input = parent output = RocketAMF.serialize(input, 3) output.should == expected + end + end + + if "".respond_to?(:force_encoding) + describe "and handling encodings" do + it "should support multiple encodings" do + shift_str = "\x53\x68\x69\x66\x74\x20\x83\x65\x83\x58\x83\x67".force_encoding("Shift_JIS") # "Shift テスト" + utf_str = "\x55\x54\x46\x20\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88".force_encoding("UTF-8") # "UTF テスト" + output = RocketAMF.serialize([5, shift_str, utf_str, 5], 3) + output.should == object_fixture("amf3-complexEncodedStringArray.bin") + end + + it "should keep references of duplicate strings with different encodings" do + # String is "this is a テスト" + shift_str = "\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\x83\x65\x83\x58\x83\x67".force_encoding("Shift_JIS") + utf_str = "\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88".force_encoding("UTF-8") + + expected = object_fixture("amf3-encodedStringRef.bin") + output = RocketAMF.serialize([shift_str, utf_str], 3) + output.should == expected + end end end end end \ No newline at end of file