Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: third_party/protobuf/ruby/tests/well_known_types_test.rb

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/ruby
2
3 require 'test/unit'
4 require 'google/protobuf/well_known_types'
5
6 class TestWellKnownTypes < Test::Unit::TestCase
7 def test_timestamp
8 ts = Google::Protobuf::Timestamp.new
9
10 assert_equal Time.at(0), ts.to_time
11
12 ts.seconds = 12345
13 assert_equal Time.at(12345), ts.to_time
14 assert_equal 12345, ts.to_i
15
16 ts.from_time(Time.at(123456, 654321))
17 assert_equal 123456, ts.seconds
18 assert_equal 654321000, ts.nanos
19 assert_equal Time.at(123456.654321), ts.to_time
20 end
21
22 def test_duration
23 duration = Google::Protobuf::Duration.new(seconds: 123, nanos: 456)
24 assert_equal 123.000000456, duration.to_f
25 end
26
27 def test_struct
28 struct = Google::Protobuf::Struct.new
29
30 substruct = {
31 "subkey" => 999,
32 "subkey2" => false
33 }
34
35 sublist = ["abc", 123, {"deepkey" => "deepval"}]
36
37 struct["number"] = 12345
38 struct["boolean-true"] = true
39 struct["boolean-false"] = false
40 struct["null"] = nil
41 struct["string"] = "abcdef"
42 struct["substruct"] = substruct
43 struct["sublist"] = sublist
44
45 assert_equal 12345, struct["number"]
46 assert_equal true, struct["boolean-true"]
47 assert_equal false, struct["boolean-false"]
48 assert_equal nil, struct["null"]
49 assert_equal "abcdef", struct["string"]
50 assert_equal(Google::Protobuf::Struct.from_hash(substruct),
51 struct["substruct"])
52 assert_equal(Google::Protobuf::ListValue.from_a(sublist),
53 struct["sublist"])
54
55 should_equal = {
56 "number" => 12345,
57 "boolean-true" => true,
58 "boolean-false" => false,
59 "null" => nil,
60 "string" => "abcdef",
61 "substruct" => {
62 "subkey" => 999,
63 "subkey2" => false
64 },
65 "sublist" => ["abc", 123, {"deepkey" => "deepval"}]
66 }
67
68 list = struct["sublist"]
69 list.is_a?(Google::Protobuf::ListValue)
70 assert_equal "abc", list[0]
71 assert_equal 123, list[1]
72 assert_equal({"deepkey" => "deepval"}, list[2].to_h)
73
74 # to_h returns a fully-flattened Ruby structure (Hash and Array).
75 assert_equal(should_equal, struct.to_h)
76
77 # Test that we can assign Struct and ListValue directly.
78 struct["substruct"] = Google::Protobuf::Struct.from_hash(substruct)
79 struct["sublist"] = Google::Protobuf::ListValue.from_a(sublist)
80
81 assert_equal(should_equal, struct.to_h)
82
83 struct["sublist"] << nil
84 should_equal["sublist"] << nil
85
86 assert_equal(should_equal, struct.to_h)
87 assert_equal(should_equal["sublist"].length, struct["sublist"].length)
88
89 assert_raise Google::Protobuf::UnexpectedStructType do
90 struct[123] = 5
91 end
92
93 assert_raise Google::Protobuf::UnexpectedStructType do
94 struct[5] = Time.new
95 end
96
97 assert_raise Google::Protobuf::UnexpectedStructType do
98 struct[5] = [Time.new]
99 end
100
101 assert_raise Google::Protobuf::UnexpectedStructType do
102 struct[5] = {123 => 456}
103 end
104
105 assert_raise Google::Protobuf::UnexpectedStructType do
106 struct = Google::Protobuf::Struct.new
107 struct.fields["foo"] = Google::Protobuf::Value.new
108 # Tries to return a Ruby value for a Value class whose type
109 # hasn't been filled in.
110 struct["foo"]
111 end
112 end
113
114 def test_any
115 any = Google::Protobuf::Any.new
116 ts = Google::Protobuf::Timestamp.new(seconds: 12345, nanos: 6789)
117 any.pack(ts)
118
119 assert any.is(Google::Protobuf::Timestamp)
120 assert_equal ts, any.unpack(Google::Protobuf::Timestamp)
121 end
122 end
OLDNEW
« no previous file with comments | « third_party/protobuf/ruby/tests/test_import.proto ('k') | third_party/protobuf/ruby/travis-test.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698