Home | History | Annotate | Download | only in spec
      1 # Copyright 2015 gRPC authors.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 require 'spec_helper'
     16 
     17 StatusCodes = GRPC::Core::StatusCodes
     18 
     19 describe StatusCodes do
     20   # convert upper snake-case to camel case.
     21   # e.g., DEADLINE_EXCEEDED -> DeadlineExceeded
     22   def upper_snake_to_camel(name)
     23     name.to_s.split('_').map(&:downcase).map(&:capitalize).join('')
     24   end
     25 
     26   StatusCodes.constants.each do |status_name|
     27     it 'there is a subclass of BadStatus corresponding to StatusCode: ' \
     28       "#{status_name} that has code: #{StatusCodes.const_get(status_name)}" do
     29       camel_case = upper_snake_to_camel(status_name)
     30       error_class = GRPC.const_get(camel_case)
     31       # expect the error class to be a subclass of BadStatus
     32       expect(error_class < GRPC::BadStatus)
     33 
     34       error_object = error_class.new
     35       # check that the code matches the int value of the error's constant
     36       status_code = StatusCodes.const_get(status_name)
     37       expect(error_object.code).to eq(status_code)
     38 
     39       # check default parameters
     40       expect(error_object.details).to eq('unknown cause')
     41       expect(error_object.metadata).to eq({})
     42 
     43       # check that the BadStatus factory for creates the correct
     44       # exception too
     45       from_factory = GRPC::BadStatus.new_status_exception(status_code)
     46       expect(from_factory.is_a?(error_class)).to be(true)
     47     end
     48   end
     49 end
     50