THRIFT-553. rb: thrift structs should be comparable (<=>)
This patch adds the spaceship operator to the struct and union base classes, enabling object comparisons between objects without regenerating code.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@911644 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb
index c1271b9..c937023 100644
--- a/lib/rb/spec/struct_spec.rb
+++ b/lib/rb/spec/struct_spec.rb
@@ -79,6 +79,16 @@
       Foo.new(:my_bool => true).my_bool?.should be_true
     end
 
+    it "should be comparable" do
+      s1 = StructWithSomeEnum.new(:some_enum => SomeEnum::ONE)
+      s2 = StructWithSomeEnum.new(:some_enum => SomeEnum::TWO)
+
+      (s1 <=> s2).should == -1
+      (s2 <=> s1).should == 1
+      (s1 <=> s1).should == 0
+      (s1 <=> StructWithSomeEnum.new()).should == -1
+    end
+
     it "should read itself off the wire" do
       struct = Foo.new
       prot = BaseProtocol.new(mock("transport"))
diff --git a/lib/rb/spec/union_spec.rb b/lib/rb/spec/union_spec.rb
index 33df251..702e920 100644
--- a/lib/rb/spec/union_spec.rb
+++ b/lib/rb/spec/union_spec.rb
@@ -141,28 +141,53 @@
       swu2.read(proto)
       swu2.should == swu
     end
-    
+
     it "should support old style constructor" do
       union = My_union.new(:integer32 => 26)
       union.get_set_field.should == :integer32
       union.get_value.should == 26
     end
-    
+
+    it "should not throw an error when inspected and unset" do
+      lambda{TestUnion.new().inspect}.should_not raise_error
+    end
+
     it "should print enum value name when inspected" do
       My_union.new(:some_enum => SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
-      
+
       My_union.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>" 
     end
-    
+
     it "should offer field? methods" do
       My_union.new.some_enum?.should be_false
       My_union.new(:some_enum => SomeEnum::ONE).some_enum?.should be_true
       My_union.new(:im_true => false).im_true?.should be_true
       My_union.new(:im_true => true).im_true?.should be_true
     end
-    
+
     it "should pretty print binary fields" do
       TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
     end
+
+    it "should be comparable" do
+      relationships = [
+        [0,   -1, -1, -1],
+        [1,   0,  -1, -1],
+        [1,   1,  0,  -1],
+        [1,   1,  1,  0]]
+
+      objs = [
+        TestUnion.new(:string_field, "blah"), 
+        TestUnion.new(:string_field, "blahblah"),
+        TestUnion.new(:i32_field, 1),
+        TestUnion.new()]
+
+      for y in 0..3
+        for x in 0..3
+          # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
+          (objs[y] <=> objs[x]).should == relationships[y][x]
+        end
+      end
+    end
   end
 end