Advertisement
Guest User

Untitled

a guest
Dec 29th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Animal
  2.   include TxtRPC::TxtRPCObject
  3.   extend TxtRPC::Serialization
  4.   extend BuildPattern
  5.  
  6.   rpc_property 1, name, String
  7.   rpc_property 2, max_speed, Int64
  8.  
  9.   def initialize
  10.     @name = ""
  11.     @max_speed = 0
  12.   end
  13. end
  14.  
  15. class Person
  16.   include TxtRPC::TxtRPCObject
  17.   extend TxtRPC::Serialization
  18.   extend BuildPattern
  19.  
  20.   rpc_property 5, age, Int64
  21.   rpc_property 1, name, String
  22.   rpc_property 2, left_handed, Bool
  23.   rpc_property 3, emails, Array(String)
  24.   rpc_property 4, nicknames, Set(String)
  25.   rpc_property 6, fav_numbers, Set(Int64)
  26.   rpc_property 7, jobs, Hash(Int64, String)
  27.   # Intentionally skip 8, to simulate a field being deprecated
  28.   rpc_property 9, pets, Hash(Int64, Animal) # key = year adopted
  29.  
  30.   def initialize
  31.     @name = ""
  32.     @age = 0
  33.     @left_handed = false
  34.     @emails = Array(String).new
  35.     @nicknames = Set(String).new
  36.     @fav_numbers = Set(Int64).new
  37.     @jobs = Hash(Int64, String).new
  38.     @pets = Hash(Int64, Animal).new
  39.   end
  40. end
  41.  
  42. it "serializes RPC objects" do
  43.     p = Person.new
  44.     p.name = "Thor"
  45.     p.age = 1500_i64
  46.     p.emails << "thor@asgard.com"
  47.     p.emails << "son_of_odin@asgard.com"
  48.     p.fav_numbers << 7
  49.     p.fav_numbers << 18
  50.     p.fav_numbers << 1
  51.     p.nicknames << "God of Thunder"
  52.  
  53.     p.pets[2000] = Animal.build do |x|
  54.       x.name = "Appa"
  55.       x.max_speed = 200
  56.     end
  57.  
  58.     s = serialize(p)
  59.     s.should start_with("O8")
  60.     s.should contain("i5\ni1500\n")
  61.     s.should contain("i1\ns4\nThor\n")
  62.     s.should contain("i2\nb0\n")
  63.     s.should contain("A2\ns15\nthor@asgard.com\ns22\nson_of_odin@asgard.com\n")
  64.     s.should contain("S3\ni7\ni18\ni1")
  65.     s.should contain("S1\ns14\nGod of Thunder\n")
  66.     s.should contain("M1\ni2000\nO2\ni1\ns4\nAppa\ni2\ni200\n")
  67.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement