Kouhei Sutou
null+****@clear*****
Mon Feb 6 12:57:17 JST 2017
Kouhei Sutou 2017-02-06 12:57:17 +0900 (Mon, 06 Feb 2017) New Revision: 9141448a69a201ec1509906c2c6a3252e3549a65 https://github.com/ranguba/groonga-client-model/commit/9141448a69a201ec1509906c2c6a3252e3549a65 Message: Support all Int family validation Modified files: lib/groonga_client_model/locale/en.yml lib/groonga_client_model/validations/type_validator.rb test/unit/test_record.rb Modified: lib/groonga_client_model/locale/en.yml (+10 -0) =================================================================== --- lib/groonga_client_model/locale/en.yml 2017-02-06 12:49:21 +0900 (ad78066) +++ lib/groonga_client_model/locale/en.yml 2017-02-06 12:57:17 +0900 (91cf410) @@ -11,3 +11,13 @@ en: "must be less than 2 ** 32: %{inspected_value}" uint64: "must be less than 2 ** 64: %{inspected_value}" + int: + "must be integer: %{inspected_value}" + int8: + "must be between -(2 ** 7) and (2 ** 7) - 1: %{inspected_value}" + int16: + "must be between -(2 ** 15) and (2 ** 15) - 1: %{inspected_value}" + int32: + "must be between -(2 ** 31) and (2 ** 31) - 1: %{inspected_value}" + int64: + "must be between -(2 ** 63) and (2 ** 63) - 1: %{inspected_value}" Modified: lib/groonga_client_model/validations/type_validator.rb (+33 -0) =================================================================== --- lib/groonga_client_model/validations/type_validator.rb 2017-02-06 12:49:21 +0900 (d45b47b) +++ lib/groonga_client_model/validations/type_validator.rb 2017-02-06 12:57:17 +0900 (01c922f) @@ -32,6 +32,14 @@ module GroongaClientModel validate_uint(record, attribute, value, 32) when "UInt64" validate_uint(record, attribute, value, 64) + when "Int8" + validate_int(record, attribute, value, 8) + when "Int16" + validate_int(record, attribute, value, 16) + when "Int32" + validate_int(record, attribute, value, 32) + when "Int64" + validate_int(record, attribute, value, 64) end end @@ -64,6 +72,31 @@ module GroongaClientModel options.merge(inspected_value: value.inspect)) end end + + def validate_int(record, attribute, value, n_bits) + if value.is_a?(String) + begin + value = Integer(value) + rescue ArgumentError + end + end + + case value + when Numeric + min = -(2 ** (n_bits - 1)) + max = ((2 ** (n_bits - 1)) - 1) + if value < min or value > max + record.errors.add(attribute, + :"int#{n_bits}", + options.merge(inspected_value: value.inspect)) + return + end + else + record.errors.add(attribute, + :int, + options.merge(inspected_value: value.inspect)) + end + end end end end Modified: test/unit/test_record.rb (+88 -0) =================================================================== --- test/unit/test_record.rb 2017-02-06 12:49:21 +0900 (78bfa77) +++ test/unit/test_record.rb 2017-02-06 12:57:17 +0900 (0349ffc) @@ -125,6 +125,38 @@ class TestRecord < Test::Unit::TestCase end end + class Int8Key < Key + class << self + def key_type + "Int8" + end + end + end + + class Int16Key < Key + class << self + def key_type + "Int16" + end + end + end + + class Int32Key < Key + class << self + def key_type + "Int32" + end + end + end + + class Int64Key < Key + class << self + def key_type + "Int64" + end + end + end + sub_test_case("presence") do test "no key" do record = NoKey.new @@ -223,6 +255,62 @@ class TestRecord < Test::Unit::TestCase assert_invalid(UInt64Key, 2 ** 64, :uint64) end end + + sub_test_case("Int8") do + test("invalid") do + assert_invalid(Int8Key, "String", :int) + end + + test("too small") do + assert_invalid(Int8Key, -(2 ** 7) - 1, :int8) + end + + test("too large") do + assert_invalid(Int8Key, 2 ** 7, :int8) + end + end + + sub_test_case("Int16") do + test("invalid") do + assert_invalid(Int16Key, "String", :int) + end + + test("too small") do + assert_invalid(Int16Key, -(2 ** 15) - 1, :int16) + end + + test("too large") do + assert_invalid(Int16Key, 2 ** 15, :int16) + end + end + + sub_test_case("Int32") do + test("invalid") do + assert_invalid(Int32Key, "String", :int) + end + + test("too small") do + assert_invalid(Int32Key, -(2 ** 31) - 1, :int32) + end + + test("too large") do + assert_invalid(Int32Key, 2 ** 31, :int32) + end + end + + sub_test_case("Int64") do + test("invalid") do + assert_invalid(Int64Key, "String", :int) + end + + test("too small") do + assert_invalid(Int64Key, -(2 ** 63) - 1, :int64) + end + + test("too large") do + assert_invalid(Int64Key, 2 ** 63, :int64) + end + end end end end -------------- next part -------------- HTML����������������������������...Download