OLD | NEW |
1 # Protocol Buffers - Google's data interchange format | 1 # Copyright 2015, Google Inc. |
2 # Copyright 2008 Google Inc. All rights reserved. | 2 # All rights reserved. |
3 # https://developers.google.com/protocol-buffers/ | |
4 # | 3 # |
5 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
7 # met: | 6 # met: |
8 # | 7 # |
9 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
11 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
12 # copyright notice, this list of conditions and the following disclaimer | 11 # copyright notice, this list of conditions and the following disclaimer |
13 # in the documentation and/or other materials provided with the | 12 # in the documentation and/or other materials provided with the |
14 # distribution. | 13 # distribution. |
15 # * Neither the name of Google Inc. nor the names of its | 14 # * Neither the name of Google Inc. nor the names of its |
16 # contributors may be used to endorse or promote products derived from | 15 # contributors may be used to endorse or promote products derived from |
17 # this software without specific prior written permission. | 16 # this software without specific prior written permission. |
18 # | 17 # |
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 29 |
31 # require mixins before we hook them into the java & c code | 30 require 'grpc' |
32 require 'google/protobuf/message_exts' | |
33 | 31 |
34 # We define these before requiring the platform-specific modules. | 32 TimeConsts = GRPC::Core::TimeConsts |
35 # That way the module init can grab references to these. | 33 |
36 module Google | 34 describe TimeConsts do |
37 module Protobuf | 35 before(:each) do |
38 class Error < StandardError; end | 36 @known_consts = [:ZERO, :INFINITE_FUTURE, :INFINITE_PAST].sort |
39 class ParseError < Error; end | 37 end |
| 38 |
| 39 it 'should have all the known types' do |
| 40 expect(TimeConsts.constants.collect.sort).to eq(@known_consts) |
| 41 end |
| 42 |
| 43 describe '#to_time' do |
| 44 it 'converts each constant to a Time' do |
| 45 m = TimeConsts |
| 46 m.constants.each do |c| |
| 47 expect(m.const_get(c).to_time).to be_a(Time) |
| 48 end |
| 49 end |
40 end | 50 end |
41 end | 51 end |
42 | 52 |
43 if RUBY_PLATFORM == "java" | 53 describe '#from_relative_time' do |
44 require 'json' | 54 it 'cannot handle arbitrary objects' do |
45 require 'google/protobuf_java' | 55 expect { TimeConsts.from_relative_time(Object.new) }.to raise_error |
46 else | 56 end |
47 require 'google/protobuf_c' | |
48 end | |
49 | 57 |
50 require 'google/protobuf/repeated_field' | 58 it 'preserves TimeConsts' do |
| 59 m = TimeConsts |
| 60 m.constants.each do |c| |
| 61 const = m.const_get(c) |
| 62 expect(TimeConsts.from_relative_time(const)).to be(const) |
| 63 end |
| 64 end |
51 | 65 |
52 module Google | 66 it 'converts 0 to TimeConsts::ZERO' do |
53 module Protobuf | 67 expect(TimeConsts.from_relative_time(0)).to eq(TimeConsts::ZERO) |
| 68 end |
54 | 69 |
55 def self.encode(msg) | 70 it 'converts nil to TimeConsts::ZERO' do |
56 msg.to_proto | 71 expect(TimeConsts.from_relative_time(nil)).to eq(TimeConsts::ZERO) |
| 72 end |
| 73 |
| 74 it 'converts negative values to TimeConsts::INFINITE_FUTURE' do |
| 75 [-1, -3.2, -1e6].each do |t| |
| 76 y = TimeConsts.from_relative_time(t) |
| 77 expect(y).to eq(TimeConsts::INFINITE_FUTURE) |
57 end | 78 end |
| 79 end |
58 | 80 |
59 def self.encode_json(msg) | 81 it 'converts a positive value to an absolute time' do |
60 msg.to_json | 82 epsilon = 1 |
| 83 [1, 3.2, 1e6].each do |t| |
| 84 want = Time.now + t |
| 85 abs = TimeConsts.from_relative_time(t) |
| 86 expect(abs.to_f).to be_within(epsilon).of(want.to_f) |
61 end | 87 end |
62 | |
63 def self.decode(klass, proto) | |
64 klass.decode(proto) | |
65 end | |
66 | |
67 def self.decode_json(klass, json) | |
68 klass.decode_json(json) | |
69 end | |
70 | |
71 end | 88 end |
72 end | 89 end |
OLD | NEW |