error_sanity_spec.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. require 'grpc'
  15. StatusCodes = GRPC::Core::StatusCodes
  16. describe StatusCodes do
  17. # convert upper snake-case to camel case.
  18. # e.g., DEADLINE_EXCEEDED -> DeadlineExceeded
  19. def upper_snake_to_camel(name)
  20. name.to_s.split('_').map(&:downcase).map(&:capitalize).join('')
  21. end
  22. StatusCodes.constants.each do |status_name|
  23. it 'there is a subclass of BadStatus corresponding to StatusCode: ' \
  24. "#{status_name} that has code: #{StatusCodes.const_get(status_name)}" do
  25. camel_case = upper_snake_to_camel(status_name)
  26. error_class = GRPC.const_get(camel_case)
  27. # expect the error class to be a subclass of BadStatus
  28. expect(error_class < GRPC::BadStatus)
  29. error_object = error_class.new
  30. # check that the code matches the int value of the error's constant
  31. status_code = StatusCodes.const_get(status_name)
  32. expect(error_object.code).to eq(status_code)
  33. # check default parameters
  34. expect(error_object.details).to eq('unknown cause')
  35. expect(error_object.metadata).to eq({})
  36. # check that the BadStatus factory for creates the correct
  37. # exception too
  38. from_factory = GRPC::BadStatus.new_status_exception(status_code)
  39. expect(from_factory.is_a?(error_class)).to be(true)
  40. end
  41. end
  42. end