File: type_errors.rb

package info (click to toggle)
protobuf 3.25.4-4
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 45,992 kB
  • sloc: cpp: 204,199; java: 87,622; ansic: 81,204; objc: 58,434; cs: 27,303; python: 22,799; php: 11,340; ruby: 8,637; pascal: 3,324; xml: 2,333; sh: 1,331; makefile: 538; lisp: 86; awk: 17
file content (173 lines) | stat: -rwxr-xr-x 7,050 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/ruby

# generated_code.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))

require 'test/unit'
require 'google/protobuf/well_known_types'
require 'generated_code_pb'

class TestTypeErrors < Test::Unit::TestCase
  def test_bad_string
    check_error Google::Protobuf::TypeError,
                "Invalid argument for string field 'optional_string' (given Integer)." do
      A::B::C::TestMessage.new(optional_string: 4)
    end
    check_error Google::Protobuf::TypeError,
                "Invalid argument for string field 'oneof_string' (given Integer)." do
      A::B::C::TestMessage.new(oneof_string: 4)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_string' (given String)." do
      A::B::C::TestMessage.new(repeated_string: '4')
    end
  end

  def test_bad_float
    check_error Google::Protobuf::TypeError,
                "Expected number type for float field 'optional_float' (given TrueClass)." do
      A::B::C::TestMessage.new(optional_float: true)
    end
    check_error Google::Protobuf::TypeError,
                "Expected number type for float field 'oneof_float' (given TrueClass)." do
      A::B::C::TestMessage.new(oneof_float: true)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_float' (given String)." do
      A::B::C::TestMessage.new(repeated_float: 'true')
    end
  end

  def test_bad_double
    check_error Google::Protobuf::TypeError,
                "Expected number type for double field 'optional_double' (given Symbol)." do
      A::B::C::TestMessage.new(optional_double: :double)
    end
    check_error Google::Protobuf::TypeError,
                "Expected number type for double field 'oneof_double' (given Symbol)." do
      A::B::C::TestMessage.new(oneof_double: :double)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_double' (given FalseClass)." do
      A::B::C::TestMessage.new(repeated_double: false)
    end
  end

  def test_bad_bool
    check_error Google::Protobuf::TypeError,
                "Invalid argument for boolean field 'optional_bool' (given Float)." do
      A::B::C::TestMessage.new(optional_bool: 4.4)
    end
    check_error Google::Protobuf::TypeError,
                "Invalid argument for boolean field 'oneof_bool' (given Float)." do
      A::B::C::TestMessage.new(oneof_bool: 4.4)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_bool' (given String)." do
      A::B::C::TestMessage.new(repeated_bool: 'hi')
    end
  end

  def test_bad_int
    check_error Google::Protobuf::TypeError,
                "Expected number type for integral field 'optional_int32' (given String)." do
      A::B::C::TestMessage.new(optional_int32: 'hi')
    end
    check_error RangeError,
                "Non-integral floating point value assigned to integer field 'optional_int64' (given Float)." do
      A::B::C::TestMessage.new(optional_int64: 2.4)
    end
    check_error Google::Protobuf::TypeError,
                "Expected number type for integral field 'optional_uint32' (given Symbol)." do
      A::B::C::TestMessage.new(optional_uint32: :thing)
    end
    check_error Google::Protobuf::TypeError,
                "Expected number type for integral field 'optional_uint64' (given FalseClass)." do
      A::B::C::TestMessage.new(optional_uint64: false)
    end
    check_error Google::Protobuf::TypeError,
                "Expected number type for integral field 'oneof_int32' (given Symbol)." do
      A::B::C::TestMessage.new(oneof_int32: :hi)
    end
    check_error RangeError,
                "Non-integral floating point value assigned to integer field 'oneof_int64' (given Float)." do
      A::B::C::TestMessage.new(oneof_int64: 2.4)
    end
    check_error Google::Protobuf::TypeError,
                "Expected number type for integral field 'oneof_uint32' (given String)." do
      A::B::C::TestMessage.new(oneof_uint32: 'x')
    end
    check_error RangeError,
                "Non-integral floating point value assigned to integer field 'oneof_uint64' (given Float)." do
      A::B::C::TestMessage.new(oneof_uint64: 1.1)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_int32' (given Symbol)." do
      A::B::C::TestMessage.new(repeated_int32: :hi)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_int64' (given Float)." do
      A::B::C::TestMessage.new(repeated_int64: 2.4)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_uint32' (given String)." do
      A::B::C::TestMessage.new(repeated_uint32: 'x')
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_uint64' (given Float)." do
      A::B::C::TestMessage.new(repeated_uint64: 1.1)
    end
  end

  def test_bad_enum
    check_error RangeError,
                "Unknown symbol value for enum field 'optional_enum'." do
      A::B::C::TestMessage.new(optional_enum: 'enum')
    end
    check_error RangeError,
                "Unknown symbol value for enum field 'oneof_enum'." do
      A::B::C::TestMessage.new(oneof_enum: '')
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_enum' (given String)." do
      A::B::C::TestMessage.new(repeated_enum: '')
    end
  end

  def test_bad_bytes
    check_error Google::Protobuf::TypeError,
                "Invalid argument for bytes field 'optional_bytes' (given Float)." do
      A::B::C::TestMessage.new(optional_bytes: 22.22)
    end
    check_error Google::Protobuf::TypeError,
                "Invalid argument for bytes field 'oneof_bytes' (given Symbol)." do
      A::B::C::TestMessage.new(oneof_bytes: :T22)
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_bytes' (given Symbol)." do
      A::B::C::TestMessage.new(repeated_bytes: :T22)
    end
  end

  def test_bad_msg
    check_error Google::Protobuf::TypeError,
                "Invalid type Integer to assign to submessage field 'optional_msg'." do
      A::B::C::TestMessage.new(optional_msg: 2)
    end
    check_error Google::Protobuf::TypeError,
                "Invalid type String to assign to submessage field 'oneof_msg'." do
      A::B::C::TestMessage.new(oneof_msg: '2')
    end
    check_error ArgumentError,
                "Expected array as initializer value for repeated field 'repeated_msg' (given String)." do
      A::B::C::TestMessage.new(repeated_msg: '2')
    end
  end

  def check_error(type, message)
    err = assert_raises type do
      yield
    end
    assert_equal message, err.message
  end
end