thrift 0.8.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/{README → README.md} +0 -0
  3. data/ext/binary_protocol_accelerated.c +33 -14
  4. data/ext/bytes.c +36 -0
  5. data/ext/bytes.h +31 -0
  6. data/ext/compact_protocol.c +27 -8
  7. data/ext/constants.h +8 -5
  8. data/ext/extconf.rb +5 -1
  9. data/ext/memory_buffer.c +12 -9
  10. data/ext/protocol.c +0 -185
  11. data/ext/protocol.h +0 -20
  12. data/ext/strlcpy.h +7 -3
  13. data/ext/struct.c +27 -7
  14. data/ext/thrift_native.c +16 -11
  15. data/lib/thrift.rb +10 -4
  16. data/lib/thrift/bytes.rb +131 -0
  17. data/lib/thrift/client.rb +13 -4
  18. data/lib/thrift/exceptions.rb +3 -0
  19. data/lib/thrift/multiplexed_processor.rb +76 -0
  20. data/lib/thrift/processor.rb +24 -6
  21. data/lib/thrift/protocol/base_protocol.rb +109 -12
  22. data/lib/thrift/protocol/binary_protocol.rb +22 -7
  23. data/lib/thrift/protocol/binary_protocol_accelerated.rb +8 -0
  24. data/lib/thrift/protocol/compact_protocol.rb +23 -6
  25. data/lib/thrift/protocol/json_protocol.rb +786 -0
  26. data/lib/thrift/protocol/multiplexed_protocol.rb +44 -0
  27. data/lib/thrift/protocol/protocol_decorator.rb +194 -0
  28. data/lib/thrift/server/base_server.rb +8 -2
  29. data/lib/thrift/server/mongrel_http_server.rb +2 -0
  30. data/lib/thrift/server/simple_server.rb +5 -1
  31. data/lib/thrift/server/thin_http_server.rb +91 -0
  32. data/lib/thrift/server/thread_pool_server.rb +5 -1
  33. data/lib/thrift/server/threaded_server.rb +5 -1
  34. data/lib/thrift/struct.rb +1 -1
  35. data/lib/thrift/struct_union.rb +2 -2
  36. data/lib/thrift/transport/base_server_transport.rb +1 -1
  37. data/lib/thrift/transport/base_transport.rb +30 -20
  38. data/lib/thrift/transport/buffered_transport.rb +25 -11
  39. data/lib/thrift/transport/framed_transport.rb +20 -11
  40. data/lib/thrift/transport/http_client_transport.rb +16 -6
  41. data/lib/thrift/transport/io_stream_transport.rb +5 -2
  42. data/lib/thrift/transport/memory_buffer_transport.rb +10 -6
  43. data/lib/thrift/transport/server_socket.rb +6 -1
  44. data/lib/thrift/transport/socket.rb +23 -17
  45. data/lib/thrift/transport/ssl_server_socket.rb +41 -0
  46. data/lib/thrift/transport/ssl_socket.rb +51 -0
  47. data/lib/thrift/transport/unix_server_socket.rb +5 -1
  48. data/lib/thrift/transport/unix_socket.rb +5 -1
  49. data/lib/thrift/union.rb +3 -6
  50. data/spec/BaseService.thrift +27 -0
  51. data/spec/ExtendedService.thrift +25 -0
  52. data/spec/Referenced.thrift +44 -0
  53. data/spec/ThriftNamespacedSpec.thrift +53 -0
  54. data/spec/ThriftSpec.thrift +52 -1
  55. data/spec/base_protocol_spec.rb +158 -93
  56. data/spec/base_transport_spec.rb +194 -157
  57. data/spec/binary_protocol_accelerated_spec.rb +14 -14
  58. data/spec/binary_protocol_spec.rb +29 -16
  59. data/spec/binary_protocol_spec_shared.rb +148 -65
  60. data/spec/bytes_spec.rb +160 -0
  61. data/spec/client_spec.rb +45 -47
  62. data/spec/compact_protocol_spec.rb +36 -22
  63. data/spec/exception_spec.rb +79 -80
  64. data/spec/flat_spec.rb +62 -0
  65. data/spec/http_client_spec.rb +91 -16
  66. data/spec/json_protocol_spec.rb +552 -0
  67. data/spec/namespaced_spec.rb +67 -0
  68. data/spec/nonblocking_server_spec.rb +26 -28
  69. data/spec/processor_spec.rb +29 -32
  70. data/spec/serializer_spec.rb +31 -33
  71. data/spec/server_socket_spec.rb +32 -28
  72. data/spec/server_spec.rb +112 -84
  73. data/spec/socket_spec.rb +27 -20
  74. data/spec/socket_spec_shared.rb +32 -32
  75. data/spec/spec_helper.rb +17 -11
  76. data/spec/ssl_server_socket_spec.rb +34 -0
  77. data/spec/ssl_socket_spec.rb +78 -0
  78. data/spec/struct_nested_containers_spec.rb +191 -0
  79. data/spec/struct_spec.rb +159 -161
  80. data/spec/thin_http_server_spec.rb +141 -0
  81. data/spec/types_spec.rb +71 -69
  82. data/spec/union_spec.rb +97 -76
  83. data/spec/unix_socket_spec.rb +49 -41
  84. metadata +268 -188
  85. data/CHANGELOG +0 -1
  86. data/benchmark/gen-rb/benchmark_constants.rb +0 -10
  87. data/benchmark/gen-rb/benchmark_service.rb +0 -80
  88. data/benchmark/gen-rb/benchmark_types.rb +0 -9
  89. data/spec/gen-rb/nonblocking_service.rb +0 -272
  90. data/spec/gen-rb/thrift_spec_constants.rb +0 -10
  91. data/spec/gen-rb/thrift_spec_types.rb +0 -345
  92. data/spec/mongrel_http_server_spec.rb +0 -117
  93. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +0 -273
  94. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +0 -760
  95. data/test/debug_proto/gen-rb/empty_service.rb +0 -24
  96. data/test/debug_proto/gen-rb/inherited.rb +0 -79
  97. data/test/debug_proto/gen-rb/reverse_order_service.rb +0 -82
  98. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +0 -81
  99. data/test/debug_proto/gen-rb/srv.rb +0 -330
@@ -0,0 +1,141 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'spec_helper'
21
+ require 'rack/test'
22
+ require 'thrift/server/thin_http_server'
23
+
24
+ describe Thrift::ThinHTTPServer do
25
+
26
+ let(:processor) { double('processor') }
27
+
28
+ describe "#initialize" do
29
+
30
+ context "when using the defaults" do
31
+
32
+ it "binds to port 80, with host 0.0.0.0, a path of '/'" do
33
+ expect(Thin::Server).to receive(:new).with('0.0.0.0', 80, an_instance_of(Rack::Builder))
34
+ Thrift::ThinHTTPServer.new(processor)
35
+ end
36
+
37
+ it 'creates a ThinHTTPServer::RackApplicationContext' do
38
+ expect(Thrift::ThinHTTPServer::RackApplication).to receive(:for).with("/", processor, an_instance_of(Thrift::BinaryProtocolFactory)).and_return(anything)
39
+ Thrift::ThinHTTPServer.new(processor)
40
+ end
41
+
42
+ it "uses the BinaryProtocolFactory" do
43
+ expect(Thrift::BinaryProtocolFactory).to receive(:new)
44
+ Thrift::ThinHTTPServer.new(processor)
45
+ end
46
+
47
+ end
48
+
49
+ context "when using the options" do
50
+
51
+ it 'accepts :ip, :port, :path' do
52
+ ip = "192.168.0.1"
53
+ port = 3000
54
+ path = "/thin"
55
+ expect(Thin::Server).to receive(:new).with(ip, port, an_instance_of(Rack::Builder))
56
+ Thrift::ThinHTTPServer.new(processor,
57
+ :ip => ip,
58
+ :port => port,
59
+ :path => path)
60
+ end
61
+
62
+ it 'creates a ThinHTTPServer::RackApplicationContext with a different protocol factory' do
63
+ expect(Thrift::ThinHTTPServer::RackApplication).to receive(:for).with("/", processor, an_instance_of(Thrift::JsonProtocolFactory)).and_return(anything)
64
+ Thrift::ThinHTTPServer.new(processor,
65
+ :protocol_factory => Thrift::JsonProtocolFactory.new)
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ describe "#serve" do
73
+
74
+ it 'starts the Thin server' do
75
+ underlying_thin_server = double('thin server', :start => true)
76
+ allow(Thin::Server).to receive(:new).and_return(underlying_thin_server)
77
+
78
+ thin_thrift_server = Thrift::ThinHTTPServer.new(processor)
79
+
80
+ expect(underlying_thin_server).to receive(:start)
81
+ thin_thrift_server.serve
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ describe Thrift::ThinHTTPServer::RackApplication do
88
+ include Rack::Test::Methods
89
+
90
+ let(:processor) { double('processor') }
91
+ let(:protocol_factory) { double('protocol factory') }
92
+
93
+ def app
94
+ Thrift::ThinHTTPServer::RackApplication.for("/", processor, protocol_factory)
95
+ end
96
+
97
+ context "404 response" do
98
+
99
+ it 'receives a non-POST' do
100
+ header('Content-Type', "application/x-thrift")
101
+ get "/"
102
+ expect(last_response.status).to be 404
103
+ end
104
+
105
+ it 'receives a header other than application/x-thrift' do
106
+ header('Content-Type', "application/json")
107
+ post "/"
108
+ expect(last_response.status).to be 404
109
+ end
110
+
111
+ end
112
+
113
+ context "200 response" do
114
+
115
+ before do
116
+ allow(protocol_factory).to receive(:get_protocol)
117
+ allow(processor).to receive(:process)
118
+ end
119
+
120
+ it 'creates an IOStreamTransport' do
121
+ header('Content-Type', "application/x-thrift")
122
+ expect(Thrift::IOStreamTransport).to receive(:new).with(an_instance_of(Rack::Lint::InputWrapper), an_instance_of(Rack::Response))
123
+ post "/"
124
+ end
125
+
126
+ it 'fetches the right protocol based on the Transport' do
127
+ header('Content-Type', "application/x-thrift")
128
+ expect(protocol_factory).to receive(:get_protocol).with(an_instance_of(Thrift::IOStreamTransport))
129
+ post "/"
130
+ end
131
+
132
+ it 'status code 200' do
133
+ header('Content-Type', "application/x-thrift")
134
+ post "/"
135
+ expect(last_response.ok?).to be_truthy
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
@@ -17,10 +17,9 @@
17
17
  # under the License.
18
18
  #
19
19
 
20
- require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
20
+ require 'spec_helper'
21
21
 
22
- class ThriftTypesSpec < Spec::ExampleGroup
23
- include Thrift
22
+ describe Thrift::Types do
24
23
 
25
24
  before(:each) do
26
25
  Thrift.type_checking = true
@@ -30,87 +29,90 @@ class ThriftTypesSpec < Spec::ExampleGroup
30
29
  Thrift.type_checking = false
31
30
  end
32
31
 
33
- describe "Type checking" do
32
+ context 'type checking' do
34
33
  it "should return the proper name for each type" do
35
- Thrift.type_name(Types::I16).should == "Types::I16"
36
- Thrift.type_name(Types::VOID).should == "Types::VOID"
37
- Thrift.type_name(Types::LIST).should == "Types::LIST"
38
- Thrift.type_name(42).should be_nil
34
+ expect(Thrift.type_name(Thrift::Types::I16)).to eq("Types::I16")
35
+ expect(Thrift.type_name(Thrift::Types::VOID)).to eq("Types::VOID")
36
+ expect(Thrift.type_name(Thrift::Types::LIST)).to eq("Types::LIST")
37
+ expect(Thrift.type_name(42)).to be_nil
39
38
  end
40
39
 
41
40
  it "should check types properly" do
42
- # lambda { Thrift.check_type(nil, Types::STOP) }.should raise_error(TypeError)
43
- lambda { Thrift.check_type(3, {:type => Types::STOP}, :foo) }.should raise_error(TypeError)
44
- lambda { Thrift.check_type(nil, {:type => Types::VOID}, :foo) }.should_not raise_error(TypeError)
45
- lambda { Thrift.check_type(3, {:type => Types::VOID}, :foo) }.should raise_error(TypeError)
46
- lambda { Thrift.check_type(true, {:type => Types::BOOL}, :foo) }.should_not raise_error(TypeError)
47
- lambda { Thrift.check_type(3, {:type => Types::BOOL}, :foo) }.should raise_error(TypeError)
48
- lambda { Thrift.check_type(42, {:type => Types::BYTE}, :foo) }.should_not raise_error(TypeError)
49
- lambda { Thrift.check_type(42, {:type => Types::I16}, :foo) }.should_not raise_error(TypeError)
50
- lambda { Thrift.check_type(42, {:type => Types::I32}, :foo) }.should_not raise_error(TypeError)
51
- lambda { Thrift.check_type(42, {:type => Types::I64}, :foo) }.should_not raise_error(TypeError)
52
- lambda { Thrift.check_type(3.14, {:type => Types::I32}, :foo) }.should raise_error(TypeError)
53
- lambda { Thrift.check_type(3.14, {:type => Types::DOUBLE}, :foo) }.should_not raise_error(TypeError)
54
- lambda { Thrift.check_type(3, {:type => Types::DOUBLE}, :foo) }.should raise_error(TypeError)
55
- lambda { Thrift.check_type("3", {:type => Types::STRING}, :foo) }.should_not raise_error(TypeError)
56
- lambda { Thrift.check_type(3, {:type => Types::STRING}, :foo) }.should raise_error(TypeError)
41
+ # lambda { Thrift.check_type(nil, Thrift::Types::STOP) }.should raise_error(Thrift::TypeError)
42
+ expect { Thrift.check_type(3, {:type => Thrift::Types::STOP}, :foo) }.to raise_error(Thrift::TypeError)
43
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::VOID}, :foo) }.not_to raise_error
44
+ expect { Thrift.check_type(3, {:type => Thrift::Types::VOID}, :foo) }.to raise_error(Thrift::TypeError)
45
+ expect { Thrift.check_type(true, {:type => Thrift::Types::BOOL}, :foo) }.not_to raise_error
46
+ expect { Thrift.check_type(3, {:type => Thrift::Types::BOOL}, :foo) }.to raise_error(Thrift::TypeError)
47
+ expect { Thrift.check_type(42, {:type => Thrift::Types::BYTE}, :foo) }.not_to raise_error
48
+ expect { Thrift.check_type(42, {:type => Thrift::Types::I16}, :foo) }.not_to raise_error
49
+ expect { Thrift.check_type(42, {:type => Thrift::Types::I32}, :foo) }.not_to raise_error
50
+ expect { Thrift.check_type(42, {:type => Thrift::Types::I64}, :foo) }.not_to raise_error
51
+ expect { Thrift.check_type(3.14, {:type => Thrift::Types::I32}, :foo) }.to raise_error(Thrift::TypeError)
52
+ expect { Thrift.check_type(3.14, {:type => Thrift::Types::DOUBLE}, :foo) }.not_to raise_error
53
+ expect { Thrift.check_type(3, {:type => Thrift::Types::DOUBLE}, :foo) }.to raise_error(Thrift::TypeError)
54
+ expect { Thrift.check_type("3", {:type => Thrift::Types::STRING}, :foo) }.not_to raise_error
55
+ expect { Thrift.check_type(3, {:type => Thrift::Types::STRING}, :foo) }.to raise_error(Thrift::TypeError)
57
56
  hello = SpecNamespace::Hello.new
58
- lambda { Thrift.check_type(hello, {:type => Types::STRUCT, :class => SpecNamespace::Hello}, :foo) }.should_not raise_error(TypeError)
59
- lambda { Thrift.check_type("foo", {:type => Types::STRUCT}, :foo) }.should raise_error(TypeError)
60
- lambda { Thrift.check_type({:foo => 1}, {:type => Types::MAP}, :foo) }.should_not raise_error(TypeError)
61
- lambda { Thrift.check_type([1], {:type => Types::MAP}, :foo) }.should raise_error(TypeError)
62
- lambda { Thrift.check_type([1], {:type => Types::LIST}, :foo) }.should_not raise_error(TypeError)
63
- lambda { Thrift.check_type({:foo => 1}, {:type => Types::LIST}, :foo) }.should raise_error(TypeError)
64
- lambda { Thrift.check_type(Set.new([1,2]), {:type => Types::SET}, :foo) }.should_not raise_error(TypeError)
65
- lambda { Thrift.check_type([1,2], {:type => Types::SET}, :foo) }.should raise_error(TypeError)
66
- lambda { Thrift.check_type({:foo => true}, {:type => Types::SET}, :foo) }.should raise_error(TypeError)
57
+ expect { Thrift.check_type(hello, {:type => Thrift::Types::STRUCT, :class => SpecNamespace::Hello}, :foo) }.not_to raise_error
58
+ expect { Thrift.check_type("foo", {:type => Thrift::Types::STRUCT}, :foo) }.to raise_error(Thrift::TypeError)
59
+ field = {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRING}}
60
+ expect { Thrift.check_type({1 => "one"}, field, :foo) }.not_to raise_error
61
+ expect { Thrift.check_type([1], field, :foo) }.to raise_error(Thrift::TypeError)
62
+ field = {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::I32}}
63
+ expect { Thrift.check_type([1], field, :foo) }.not_to raise_error
64
+ expect { Thrift.check_type({:foo => 1}, field, :foo) }.to raise_error(Thrift::TypeError)
65
+ field = {:type => Thrift::Types::SET, :element => {:type => Thrift::Types::I32}}
66
+ expect { Thrift.check_type(Set.new([1,2]), field, :foo) }.not_to raise_error
67
+ expect { Thrift.check_type([1,2], field, :foo) }.to raise_error(Thrift::TypeError)
68
+ expect { Thrift.check_type({:foo => true}, field, :foo) }.to raise_error(Thrift::TypeError)
67
69
  end
68
70
 
69
71
  it "should error out if nil is passed and skip_types is false" do
70
- lambda { Thrift.check_type(nil, {:type => Types::BOOL}, :foo, false) }.should raise_error(TypeError)
71
- lambda { Thrift.check_type(nil, {:type => Types::BYTE}, :foo, false) }.should raise_error(TypeError)
72
- lambda { Thrift.check_type(nil, {:type => Types::I16}, :foo, false) }.should raise_error(TypeError)
73
- lambda { Thrift.check_type(nil, {:type => Types::I32}, :foo, false) }.should raise_error(TypeError)
74
- lambda { Thrift.check_type(nil, {:type => Types::I64}, :foo, false) }.should raise_error(TypeError)
75
- lambda { Thrift.check_type(nil, {:type => Types::DOUBLE}, :foo, false) }.should raise_error(TypeError)
76
- lambda { Thrift.check_type(nil, {:type => Types::STRING}, :foo, false) }.should raise_error(TypeError)
77
- lambda { Thrift.check_type(nil, {:type => Types::STRUCT}, :foo, false) }.should raise_error(TypeError)
78
- lambda { Thrift.check_type(nil, {:type => Types::LIST}, :foo, false) }.should raise_error(TypeError)
79
- lambda { Thrift.check_type(nil, {:type => Types::SET}, :foo, false) }.should raise_error(TypeError)
80
- lambda { Thrift.check_type(nil, {:type => Types::MAP}, :foo, false) }.should raise_error(TypeError)
72
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::BOOL}, :foo, false) }.to raise_error(Thrift::TypeError)
73
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::BYTE}, :foo, false) }.to raise_error(Thrift::TypeError)
74
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::I16}, :foo, false) }.to raise_error(Thrift::TypeError)
75
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::I32}, :foo, false) }.to raise_error(Thrift::TypeError)
76
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::I64}, :foo, false) }.to raise_error(Thrift::TypeError)
77
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::DOUBLE}, :foo, false) }.to raise_error(Thrift::TypeError)
78
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::STRING}, :foo, false) }.to raise_error(Thrift::TypeError)
79
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::STRUCT}, :foo, false) }.to raise_error(Thrift::TypeError)
80
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::LIST}, :foo, false) }.to raise_error(Thrift::TypeError)
81
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::SET}, :foo, false) }.to raise_error(Thrift::TypeError)
82
+ expect { Thrift.check_type(nil, {:type => Thrift::Types::MAP}, :foo, false) }.to raise_error(Thrift::TypeError)
81
83
  end
82
84
 
83
85
  it "should check element types on containers" do
84
- field = {:type => Types::LIST, :element => {:type => Types::I32}}
85
- lambda { Thrift.check_type([1, 2], field, :foo) }.should_not raise_error(TypeError)
86
- lambda { Thrift.check_type([1, nil, 2], field, :foo) }.should raise_error(TypeError)
87
- field = {:type => Types::MAP, :key => {:type => Types::I32}, :value => {:type => Types::STRING}}
88
- lambda { Thrift.check_type({1 => "one", 2 => "two"}, field, :foo) }.should_not raise_error(TypeError)
89
- lambda { Thrift.check_type({1 => "one", nil => "nil"}, field, :foo) }.should raise_error(TypeError)
90
- lambda { Thrift.check_type({1 => nil, 2 => "two"}, field, :foo) }.should raise_error(TypeError)
91
- field = {:type => Types::SET, :element => {:type => Types::I32}}
92
- lambda { Thrift.check_type(Set.new([1, 2]), field, :foo) }.should_not raise_error(TypeError)
93
- lambda { Thrift.check_type(Set.new([1, nil, 2]), field, :foo) }.should raise_error(TypeError)
94
- lambda { Thrift.check_type(Set.new([1, 2.3, 2]), field, :foo) }.should raise_error(TypeError)
95
-
96
- field = {:type => Types::STRUCT, :class => SpecNamespace::Hello}
97
- lambda { Thrift.check_type(SpecNamespace::BoolStruct, field, :foo) }.should raise_error(TypeError)
86
+ field = {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::I32}}
87
+ expect { Thrift.check_type([1, 2], field, :foo) }.not_to raise_error
88
+ expect { Thrift.check_type([1, nil, 2], field, :foo) }.to raise_error(Thrift::TypeError)
89
+ field = {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRING}}
90
+ expect { Thrift.check_type({1 => "one", 2 => "two"}, field, :foo) }.not_to raise_error
91
+ expect { Thrift.check_type({1 => "one", nil => "nil"}, field, :foo) }.to raise_error(Thrift::TypeError)
92
+ expect { Thrift.check_type({1 => nil, 2 => "two"}, field, :foo) }.to raise_error(Thrift::TypeError)
93
+ field = {:type => Thrift::Types::SET, :element => {:type => Thrift::Types::I32}}
94
+ expect { Thrift.check_type(Set.new([1, 2]), field, :foo) }.not_to raise_error
95
+ expect { Thrift.check_type(Set.new([1, nil, 2]), field, :foo) }.to raise_error(Thrift::TypeError)
96
+ expect { Thrift.check_type(Set.new([1, 2.3, 2]), field, :foo) }.to raise_error(Thrift::TypeError)
97
+
98
+ field = {:type => Thrift::Types::STRUCT, :class => SpecNamespace::Hello}
99
+ expect { Thrift.check_type(SpecNamespace::BoolStruct, field, :foo) }.to raise_error(Thrift::TypeError)
98
100
  end
99
101
 
100
- it "should give the TypeError a readable message" do
101
- msg = "Expected Types::STRING, received Fixnum for field foo"
102
- lambda { Thrift.check_type(3, {:type => Types::STRING}, :foo) }.should raise_error(TypeError, msg)
103
- msg = "Expected Types::STRING, received Fixnum for field foo.element"
104
- field = {:type => Types::LIST, :element => {:type => Types::STRING}}
105
- lambda { Thrift.check_type([3], field, :foo) }.should raise_error(TypeError, msg)
102
+ it "should give the Thrift::TypeError a readable message" do
103
+ msg = /Expected Types::STRING, received (Integer|Fixnum) for field foo/
104
+ expect { Thrift.check_type(3, {:type => Thrift::Types::STRING}, :foo) }.to raise_error(Thrift::TypeError, msg)
105
+ msg = /Expected Types::STRING, received (Integer|Fixnum) for field foo.element/
106
+ field = {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::STRING}}
107
+ expect { Thrift.check_type([3], field, :foo) }.to raise_error(Thrift::TypeError, msg)
106
108
  msg = "Expected Types::I32, received NilClass for field foo.element.key"
107
- field = {:type => Types::LIST,
108
- :element => {:type => Types::MAP,
109
- :key => {:type => Types::I32},
110
- :value => {:type => Types::I32}}}
111
- lambda { Thrift.check_type([{nil => 3}], field, :foo) }.should raise_error(TypeError, msg)
109
+ field = {:type => Thrift::Types::LIST,
110
+ :element => {:type => Thrift::Types::MAP,
111
+ :key => {:type => Thrift::Types::I32},
112
+ :value => {:type => Thrift::Types::I32}}}
113
+ expect { Thrift.check_type([{nil => 3}], field, :foo) }.to raise_error(Thrift::TypeError, msg)
112
114
  msg = "Expected Types::I32, received NilClass for field foo.element.value"
113
- lambda { Thrift.check_type([{1 => nil}], field, :foo) }.should raise_error(TypeError, msg)
115
+ expect { Thrift.check_type([{1 => nil}], field, :foo) }.to raise_error(Thrift::TypeError, msg)
114
116
  end
115
117
  end
116
118
  end
@@ -17,156 +17,177 @@
17
17
  # under the License.
18
18
  #
19
19
 
20
- require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
20
+ require 'spec_helper'
21
21
 
22
- class ThriftUnionSpec < Spec::ExampleGroup
23
- include Thrift
24
- include SpecNamespace
22
+ describe 'Union' do
25
23
 
26
- describe Union do
24
+ describe Thrift::Union do
27
25
  it "should return nil value in unset union" do
28
- union = My_union.new
29
- union.get_set_field.should == nil
30
- union.get_value.should == nil
26
+ union = SpecNamespace::My_union.new
27
+ expect(union.get_set_field).to eq(nil)
28
+ expect(union.get_value).to eq(nil)
31
29
  end
32
30
 
33
31
  it "should set a field and be accessible through get_value and the named field accessor" do
34
- union = My_union.new
32
+ union = SpecNamespace::My_union.new
35
33
  union.integer32 = 25
36
- union.get_set_field.should == :integer32
37
- union.get_value.should == 25
38
- union.integer32.should == 25
34
+ expect(union.get_set_field).to eq(:integer32)
35
+ expect(union.get_value).to eq(25)
36
+ expect(union.integer32).to eq(25)
39
37
  end
40
38
 
41
39
  it "should work correctly when instantiated with static field constructors" do
42
- union = My_union.integer32(5)
43
- union.get_set_field.should == :integer32
44
- union.integer32.should == 5
40
+ union = SpecNamespace::My_union.integer32(5)
41
+ expect(union.get_set_field).to eq(:integer32)
42
+ expect(union.integer32).to eq(5)
45
43
  end
46
44
 
47
45
  it "should raise for wrong set field" do
48
- union = My_union.new
46
+ union = SpecNamespace::My_union.new
49
47
  union.integer32 = 25
50
- lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
48
+ expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
51
49
  end
52
-
50
+
51
+ it "should raise for wrong set field when hash initialized and type checking is off" do
52
+ Thrift.type_checking = false
53
+ union = SpecNamespace::My_union.new({incorrect_field: :incorrect})
54
+ expect { Thrift::Serializer.new.serialize(union) }.to raise_error(RuntimeError, "set_field is not valid for this union!")
55
+ end
56
+
53
57
  it "should not be equal to nil" do
54
- union = My_union.new
55
- union.should_not == nil
58
+ union = SpecNamespace::My_union.new
59
+ expect(union).not_to eq(nil)
56
60
  end
57
-
61
+
62
+ it "should not be equal with an empty String" do
63
+ union = SpecNamespace::My_union.new
64
+ expect(union).not_to eq('')
65
+ end
66
+
58
67
  it "should not equate two different unions, i32 vs. string" do
59
- union = My_union.new(:integer32, 25)
60
- other_union = My_union.new(:some_characters, "blah!")
61
- union.should_not == other_union
68
+ union = SpecNamespace::My_union.new(:integer32, 25)
69
+ other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
70
+ expect(union).not_to eq(other_union)
62
71
  end
63
72
 
64
73
  it "should properly reset setfield and setvalue" do
65
- union = My_union.new(:integer32, 25)
66
- union.get_set_field.should == :integer32
74
+ union = SpecNamespace::My_union.new(:integer32, 25)
75
+ expect(union.get_set_field).to eq(:integer32)
67
76
  union.some_characters = "blah!"
68
- union.get_set_field.should == :some_characters
69
- union.get_value.should == "blah!"
70
- lambda { union.integer32 }.should raise_error(RuntimeError, "integer32 is not union's set field.")
77
+ expect(union.get_set_field).to eq(:some_characters)
78
+ expect(union.get_value).to eq("blah!")
79
+ expect { union.integer32 }.to raise_error(RuntimeError, "integer32 is not union's set field.")
71
80
  end
72
81
 
73
82
  it "should not equate two different unions with different values" do
74
- union = My_union.new(:integer32, 25)
75
- other_union = My_union.new(:integer32, 400)
76
- union.should_not == other_union
83
+ union = SpecNamespace::My_union.new(:integer32, 25)
84
+ other_union = SpecNamespace::My_union.new(:integer32, 400)
85
+ expect(union).not_to eq(other_union)
77
86
  end
78
87
 
79
88
  it "should not equate two different unions with different fields" do
80
- union = My_union.new(:integer32, 25)
81
- other_union = My_union.new(:other_i32, 25)
82
- union.should_not == other_union
89
+ union = SpecNamespace::My_union.new(:integer32, 25)
90
+ other_union = SpecNamespace::My_union.new(:other_i32, 25)
91
+ expect(union).not_to eq(other_union)
83
92
  end
84
93
 
85
94
  it "should inspect properly" do
86
- union = My_union.new(:integer32, 25)
87
- union.inspect.should == "<SpecNamespace::My_union integer32: 25>"
95
+ union = SpecNamespace::My_union.new(:integer32, 25)
96
+ expect(union.inspect).to eq("<SpecNamespace::My_union integer32: 25>")
88
97
  end
89
98
 
90
99
  it "should not allow setting with instance_variable_set" do
91
- union = My_union.new(:integer32, 27)
100
+ union = SpecNamespace::My_union.new(:integer32, 27)
92
101
  union.instance_variable_set(:@some_characters, "hallo!")
93
- union.get_set_field.should == :integer32
94
- union.get_value.should == 27
95
- lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
102
+ expect(union.get_set_field).to eq(:integer32)
103
+ expect(union.get_value).to eq(27)
104
+ expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
96
105
  end
97
106
 
98
- it "should serialize correctly" do
107
+ it "should serialize to binary correctly" do
99
108
  trans = Thrift::MemoryBufferTransport.new
100
109
  proto = Thrift::BinaryProtocol.new(trans)
101
110
 
102
- union = My_union.new(:integer32, 25)
111
+ union = SpecNamespace::My_union.new(:integer32, 25)
112
+ union.write(proto)
113
+
114
+ other_union = SpecNamespace::My_union.new(:integer32, 25)
115
+ other_union.read(proto)
116
+ expect(other_union).to eq(union)
117
+ end
118
+
119
+ it "should serialize to json correctly" do
120
+ trans = Thrift::MemoryBufferTransport.new
121
+ proto = Thrift::JsonProtocol.new(trans)
122
+
123
+ union = SpecNamespace::My_union.new(:integer32, 25)
103
124
  union.write(proto)
104
125
 
105
- other_union = My_union.new(:integer32, 25)
126
+ other_union = SpecNamespace::My_union.new(:integer32, 25)
106
127
  other_union.read(proto)
107
- other_union.should == union
128
+ expect(other_union).to eq(union)
108
129
  end
109
130
 
110
131
  it "should raise when validating unset union" do
111
- union = My_union.new
112
- lambda { union.validate }.should raise_error(StandardError, "Union fields are not set.")
132
+ union = SpecNamespace::My_union.new
133
+ expect { union.validate }.to raise_error(StandardError, "Union fields are not set.")
113
134
 
114
- other_union = My_union.new(:integer32, 1)
115
- lambda { other_union.validate }.should_not raise_error(StandardError, "Union fields are not set.")
135
+ other_union = SpecNamespace::My_union.new(:integer32, 1)
136
+ expect { other_union.validate }.not_to raise_error
116
137
  end
117
138
 
118
139
  it "should validate an enum field properly" do
119
- union = TestUnion.new(:enum_field, 3)
120
- union.get_set_field.should == :enum_field
121
- lambda { union.validate }.should raise_error(ProtocolException, "Invalid value of field enum_field!")
140
+ union = SpecNamespace::TestUnion.new(:enum_field, 3)
141
+ expect(union.get_set_field).to eq(:enum_field)
142
+ expect { union.validate }.to raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
122
143
 
123
- other_union = TestUnion.new(:enum_field, 1)
124
- lambda { other_union.validate }.should_not raise_error(ProtocolException, "Invalid value of field enum_field!")
144
+ other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
145
+ expect { other_union.validate }.not_to raise_error
125
146
  end
126
147
 
127
148
  it "should properly serialize and match structs with a union" do
128
- union = My_union.new(:integer32, 26)
129
- swu = Struct_with_union.new(:fun_union => union)
149
+ union = SpecNamespace::My_union.new(:integer32, 26)
150
+ swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
130
151
 
131
152
  trans = Thrift::MemoryBufferTransport.new
132
153
  proto = Thrift::CompactProtocol.new(trans)
133
154
 
134
155
  swu.write(proto)
135
156
 
136
- other_union = My_union.new(:some_characters, "hello there")
137
- swu2 = Struct_with_union.new(:fun_union => other_union)
157
+ other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
158
+ swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
138
159
 
139
- swu2.should_not == swu
160
+ expect(swu2).not_to eq(swu)
140
161
 
141
162
  swu2.read(proto)
142
- swu2.should == swu
163
+ expect(swu2).to eq(swu)
143
164
  end
144
165
 
145
166
  it "should support old style constructor" do
146
- union = My_union.new(:integer32 => 26)
147
- union.get_set_field.should == :integer32
148
- union.get_value.should == 26
167
+ union = SpecNamespace::My_union.new(:integer32 => 26)
168
+ expect(union.get_set_field).to eq(:integer32)
169
+ expect(union.get_value).to eq(26)
149
170
  end
150
171
 
151
172
  it "should not throw an error when inspected and unset" do
152
- lambda{TestUnion.new().inspect}.should_not raise_error
173
+ expect{SpecNamespace::TestUnion.new().inspect}.not_to raise_error
153
174
  end
154
175
 
155
176
  it "should print enum value name when inspected" do
156
- My_union.new(:some_enum => SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
177
+ expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect).to eq("<SpecNamespace::My_union some_enum: ONE (0)>")
157
178
 
158
- My_union.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>"
179
+ expect(SpecNamespace::My_union.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect).to eq("<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>")
159
180
  end
160
181
 
161
182
  it "should offer field? methods" do
162
- My_union.new.some_enum?.should be_false
163
- My_union.new(:some_enum => SomeEnum::ONE).some_enum?.should be_true
164
- My_union.new(:im_true => false).im_true?.should be_true
165
- My_union.new(:im_true => true).im_true?.should be_true
183
+ expect(SpecNamespace::My_union.new.some_enum?).to be_falsey
184
+ expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?).to be_truthy
185
+ expect(SpecNamespace::My_union.new(:im_true => false).im_true?).to be_truthy
186
+ expect(SpecNamespace::My_union.new(:im_true => true).im_true?).to be_truthy
166
187
  end
167
188
 
168
189
  it "should pretty print binary fields" do
169
- TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
190
+ expect(SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect).to eq("<SpecNamespace::TestUnion binary_field: 010203>")
170
191
  end
171
192
 
172
193
  it "should be comparable" do
@@ -177,15 +198,15 @@ class ThriftUnionSpec < Spec::ExampleGroup
177
198
  [1, 1, 1, 0]]
178
199
 
179
200
  objs = [
180
- TestUnion.new(:string_field, "blah"),
181
- TestUnion.new(:string_field, "blahblah"),
182
- TestUnion.new(:i32_field, 1),
183
- TestUnion.new()]
201
+ SpecNamespace::TestUnion.new(:string_field, "blah"),
202
+ SpecNamespace::TestUnion.new(:string_field, "blahblah"),
203
+ SpecNamespace::TestUnion.new(:i32_field, 1),
204
+ SpecNamespace::TestUnion.new()]
184
205
 
185
206
  for y in 0..3
186
207
  for x in 0..3
187
208
  # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
188
- (objs[y] <=> objs[x]).should == relationships[y][x]
209
+ expect(objs[y] <=> objs[x]).to eq(relationships[y][x])
189
210
  end
190
211
  end
191
212
  end