Permalink
Browse files

Add unserialization specs

  • Loading branch information...
1 parent 3474196 commit 61053d4f16773eababf1d25f3d02b51025c33603 @divoxx committed Oct 2, 2009
Showing with 56 additions and 1 deletion.
  1. +1 −0 Rakefile
  2. +0 −1 spec/php_serialization/parser.rb
  3. +3 −0 spec/spec.opts
  4. +52 −0 spec/unserialization_spec.rb
View
@@ -25,6 +25,7 @@ require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
+ spec.spec_opts = ["--options spec/spec.opts"]
end
Spec::Rake::SpecTask.new(:rcov) do |spec|
@@ -1 +0,0 @@
-require File.dirname(__FILE__) + "/../spec_helper"
View
@@ -0,0 +1,3 @@
+--diff u
+--color
+--format profile
@@ -0,0 +1,52 @@
+require File.dirname(__FILE__) + "/spec_helper"
+
+describe "Unserialization" do
+ it "should unserialize a integer" do
+ PhpSerialization.load("i:10;").should == 10
+ end
+
+ it "should unserialize a string" do
+ PhpSerialization.load('s:4:"Name";').should == "Name"
+ end
+
+ it "should unserialize true" do
+ PhpSerialization.load('b:1;').should == true
+ end
+
+ it "should unserialize false" do
+ PhpSerialization.load('b:0;').should == false
+ end
+
+ it "should unserialize nil" do
+ PhpSerialization.load('N;').should == nil
+ end
+
+ it "should unzerialize an array" do
+ PhpSerialization.load('a:2:{i:0;b:1;i:1;s:3:"foo";};').should == [true, "foo"]
+ end
+
+ it "should unserialize a hash" do
+ PhpSerialization.load('a:2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};').should == {"name" => "Rodrigo", "age" => 23}
+ end
+
+ it "should unserialize object with class existant" do
+ class Person
+ attr_accessor :name, :age
+ end
+
+ person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
+ person.should be_instance_of(Person)
+ person.name.should == "Rodrigo"
+ person.age.should == 23
+
+
+ Object.send(:remove_const, :Person)
+ end
+
+ it "should unserialize object without class as a struct" do
+ person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
+ person.should be_instance_of(Struct::Person)
+ person.name.should == "Rodrigo"
+ person.age.should == 23
+ end
+end

0 comments on commit 61053d4

Please sign in to comment.