1#!/usr/bin/ruby 2 3# basic_test_pb.rb is in the same directory as this test. 4$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) 5 6require 'basic_test_proto2_pb' 7require 'common_tests' 8require 'google/protobuf' 9require 'json' 10require 'test/unit' 11 12# ------------- generated code -------------- 13 14module BasicTestProto2 15 pool = Google::Protobuf::DescriptorPool.new 16 pool.build do 17 add_file "test_proto2.proto", syntax: :proto2 do 18 add_message "BadFieldNames" do 19 optional :dup, :int32, 1 20 optional :class, :int32, 2 21 optional :"a.b", :int32, 3 22 end 23 end 24 end 25 26 BadFieldNames = pool.lookup("BadFieldNames").msgclass 27 28# ------------ test cases --------------- 29 30 class MessageContainerTest < Test::Unit::TestCase 31 # Required by CommonTests module to resolve proto2 proto classes used in tests. 32 def proto_module 33 ::BasicTestProto2 34 end 35 include CommonTests 36 37 def test_has_field 38 m = TestMessage.new 39 assert !m.has_optional_int32? 40 assert !TestMessage.descriptor.lookup('optional_int32').has?(m) 41 assert !m.has_optional_int64? 42 assert !TestMessage.descriptor.lookup('optional_int64').has?(m) 43 assert !m.has_optional_uint32? 44 assert !TestMessage.descriptor.lookup('optional_uint32').has?(m) 45 assert !m.has_optional_uint64? 46 assert !TestMessage.descriptor.lookup('optional_uint64').has?(m) 47 assert !m.has_optional_bool? 48 assert !TestMessage.descriptor.lookup('optional_bool').has?(m) 49 assert !m.has_optional_float? 50 assert !TestMessage.descriptor.lookup('optional_float').has?(m) 51 assert !m.has_optional_double? 52 assert !TestMessage.descriptor.lookup('optional_double').has?(m) 53 assert !m.has_optional_string? 54 assert !TestMessage.descriptor.lookup('optional_string').has?(m) 55 assert !m.has_optional_bytes? 56 assert !TestMessage.descriptor.lookup('optional_bytes').has?(m) 57 assert !m.has_optional_enum? 58 assert !TestMessage.descriptor.lookup('optional_enum').has?(m) 59 60 m = TestMessage.new(:optional_int32 => nil) 61 assert !m.has_optional_int32? 62 63 assert_raise NoMethodError do 64 m.has_repeated_msg? 65 end 66 assert_raise ArgumentError do 67 TestMessage.descriptor.lookup('repeated_msg').has?(m) 68 end 69 70 m.optional_msg = TestMessage2.new 71 assert m.has_optional_msg? 72 assert TestMessage.descriptor.lookup('optional_msg').has?(m) 73 74 m = OneofMessage.new 75 assert !m.has_my_oneof? 76 m.a = "foo" 77 assert m.has_a? 78 assert OneofMessage.descriptor.lookup('a').has?(m) 79 assert_equal "foo", m.a 80 assert m.has_my_oneof? 81 assert !m.has_b? 82 assert !OneofMessage.descriptor.lookup('b').has?(m) 83 assert !m.has_c? 84 assert !OneofMessage.descriptor.lookup('c').has?(m) 85 assert !m.has_d? 86 assert !OneofMessage.descriptor.lookup('d').has?(m) 87 88 m = OneofMessage.new 89 m.b = 100 90 assert m.has_b? 91 assert_equal 100, m.b 92 assert m.has_my_oneof? 93 assert !m.has_a? 94 assert !m.has_c? 95 assert !m.has_d? 96 97 m = OneofMessage.new 98 m.c = TestMessage2.new 99 assert m.has_c? 100 assert_equal TestMessage2.new, m.c 101 assert m.has_my_oneof? 102 assert !m.has_a? 103 assert !m.has_b? 104 assert !m.has_d? 105 106 m = OneofMessage.new 107 m.d = :A 108 assert m.has_d? 109 assert_equal :A, m.d 110 assert m.has_my_oneof? 111 assert !m.has_a? 112 assert !m.has_b? 113 assert !m.has_c? 114 end 115 116 def test_defined_defaults 117 m = TestMessageDefaults.new 118 assert_equal 1, m.optional_int32 119 assert_equal 2, m.optional_int64 120 assert_equal 3, m.optional_uint32 121 assert_equal 4, m.optional_uint64 122 assert_equal true, m.optional_bool 123 assert_equal 6.0, m.optional_float 124 assert_equal 7.0, m.optional_double 125 assert_equal "Default Str", m.optional_string 126 assert_equal "\xCF\xA5s\xBD\xBA\xE6fubar".force_encoding("ASCII-8BIT"), m.optional_bytes 127 assert_equal :B2, m.optional_enum 128 129 assert !m.has_optional_int32? 130 assert !m.has_optional_int64? 131 assert !m.has_optional_uint32? 132 assert !m.has_optional_uint64? 133 assert !m.has_optional_bool? 134 assert !m.has_optional_float? 135 assert !m.has_optional_double? 136 assert !m.has_optional_string? 137 assert !m.has_optional_bytes? 138 assert !m.has_optional_enum? 139 end 140 141 def test_set_clear_defaults 142 m = TestMessageDefaults.new 143 144 m.optional_int32 = -42 145 assert_equal -42, m.optional_int32 146 assert m.has_optional_int32? 147 m.clear_optional_int32 148 assert_equal 1, m.optional_int32 149 assert !m.has_optional_int32? 150 151 m.optional_string = "foo bar" 152 assert_equal "foo bar", m.optional_string 153 assert m.has_optional_string? 154 m.clear_optional_string 155 assert_equal "Default Str", m.optional_string 156 assert !m.has_optional_string? 157 158 m.optional_msg = TestMessage2.new(:foo => 42) 159 assert_equal TestMessage2.new(:foo => 42), m.optional_msg 160 assert m.has_optional_msg? 161 162 m.clear_optional_msg 163 assert_equal nil, m.optional_msg 164 assert !m.has_optional_msg? 165 166 m.optional_msg = TestMessage2.new(:foo => 42) 167 assert_equal TestMessage2.new(:foo => 42), m.optional_msg 168 assert TestMessageDefaults.descriptor.lookup('optional_msg').has?(m) 169 170 TestMessageDefaults.descriptor.lookup('optional_msg').clear(m) 171 assert_equal nil, m.optional_msg 172 assert !TestMessageDefaults.descriptor.lookup('optional_msg').has?(m) 173 174 m = TestMessage.new 175 m.repeated_int32.push(1) 176 assert_equal [1], m.repeated_int32 177 m.clear_repeated_int32 178 assert_equal [], m.repeated_int32 179 180 m = OneofMessage.new 181 m.a = "foo" 182 assert_equal "foo", m.a 183 assert m.has_a? 184 m.clear_a 185 assert !m.has_a? 186 187 m = OneofMessage.new 188 m.a = "foobar" 189 assert m.has_my_oneof? 190 m.clear_my_oneof 191 assert !m.has_my_oneof? 192 193 m = OneofMessage.new 194 m.a = "bar" 195 assert_equal "bar", m.a 196 assert m.has_my_oneof? 197 OneofMessage.descriptor.lookup('a').clear(m) 198 assert !m.has_my_oneof? 199 end 200 201 def test_initialization_map_errors 202 e = assert_raise ArgumentError do 203 TestMessage.new(:hello => "world") 204 end 205 assert_match(/hello/, e.message) 206 207 e = assert_raise ArgumentError do 208 TestMessage.new(:repeated_uint32 => "hello") 209 end 210 assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)." 211 end 212 213 214 def test_to_h 215 m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) 216 expected_result = { 217 :optional_bool=>true, 218 :optional_double=>-10.100001, 219 :optional_string=>"foo", 220 :repeated_string=>["bar1", "bar2"], 221 } 222 assert_equal expected_result, m.to_h 223 224 m = OneofMessage.new(:a => "foo") 225 expected_result = {:a => "foo"} 226 assert_equal expected_result, m.to_h 227 end 228 229 def test_map_keyword_disabled 230 pool = Google::Protobuf::DescriptorPool.new 231 232 e = assert_raise ArgumentError do 233 pool.build do 234 add_file 'test_file.proto', syntax: :proto2 do 235 add_message "MapMessage" do 236 map :map_string_int32, :string, :int32, 1 237 map :map_string_msg, :string, :message, 2, "TestMessage2" 238 end 239 end 240 end 241 end 242 243 assert_match(/Cannot add a native map/, e.message) 244 end 245 246 def test_respond_to 247 # This test fails with JRuby 1.7.23, likely because of an old JRuby bug. 248 return if RUBY_PLATFORM == "java" 249 msg = TestMessage.new 250 assert !msg.respond_to?(:bacon) 251 end 252 253 def test_file_descriptor 254 file_descriptor = TestMessage.descriptor.file_descriptor 255 assert nil != file_descriptor 256 assert_equal "tests/basic_test_proto2.proto", file_descriptor.name 257 assert_equal :proto2, file_descriptor.syntax 258 259 file_descriptor = TestEnum.descriptor.file_descriptor 260 assert nil != file_descriptor 261 assert_equal "tests/basic_test_proto2.proto", file_descriptor.name 262 assert_equal :proto2, file_descriptor.syntax 263 end 264 end 265end 266