|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe 'SmartCore::Types::Struct::StrictHash' do |
| 4 | + describe 'runtime-based behavior' do |
| 5 | + specify 'fails on extra attributes' do |
| 6 | + expect do |
| 7 | + SmartCore::Types::Struct::StrictHash({ a: SmartCore::Types::Value::String }, :excessive) |
| 8 | + end.to raise_error(SmartCore::Types::IncorrectRuntimeAttributesError) |
| 9 | + end |
| 10 | + |
| 11 | + specify 'fails when schema attribute is not hash object' do |
| 12 | + expect do |
| 13 | + SmartCore::Types::Struct::StrictHash(%i[bad master]) |
| 14 | + end.to raise_error(SmartCore::Types::IncorrectRuntimeAttributesError) |
| 15 | + end |
| 16 | + |
| 17 | + specify 'fails when schema values are not types' do |
| 18 | + expect do |
| 19 | + SmartCore::Types::Struct::StrictHash({ a: String }) |
| 20 | + end.to raise_error(SmartCore::Types::IncorrectRuntimeAttributesError) |
| 21 | + end |
| 22 | + |
| 23 | + specify 'requires schema (runtime attribute)' do |
| 24 | + expect do |
| 25 | + SmartCore::Types::Struct::StrictHash() |
| 26 | + end.to raise_error(SmartCore::Types::IncorrectRuntimeAttributesError) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe 'logic' do |
| 31 | + let(:correct_value) { Hash[int: 10, str: '20', bool: false] } |
| 32 | + |
| 33 | + let(:type) do |
| 34 | + SmartCore::Types::Struct::StrictHash( |
| 35 | + int: SmartCore::Types::Value::Integer, |
| 36 | + str: SmartCore::Types::Value::String, |
| 37 | + bool: SmartCore::Types::Value::Boolean |
| 38 | + ) |
| 39 | + end |
| 40 | + |
| 41 | + specify 'type-checking' do |
| 42 | + expect(type.valid?(correct_value)).to eq(true) |
| 43 | + |
| 44 | + expect(type.valid?(nil)).to eq(false) |
| 45 | + expect(type.valid?({})).to eq(false) |
| 46 | + expect(type.valid?(correct_value.merge(int: nil))).to eq(false) |
| 47 | + expect(type.valid?(correct_value.merge(int: :test))).to eq(false) |
| 48 | + expect(type.valid?(correct_value.merge(str: []))).to eq(false) |
| 49 | + expect(type.valid?(correct_value.merge(bool: {}))).to eq(false) |
| 50 | + end |
| 51 | + |
| 52 | + specify 'type-validation' do |
| 53 | + expect { type.validate!(correct_value) }.not_to raise_error |
| 54 | + |
| 55 | + expect { type.validate!(nil) }.to raise_error(SmartCore::Types::TypeError) |
| 56 | + expect { type.validate!({}) }.to raise_error(SmartCore::Types::TypeError) |
| 57 | + expect { type.validate!(correct_value.merge(int: nil)) } |
| 58 | + .to raise_error(SmartCore::Types::TypeError) |
| 59 | + expect { type.validate!(correct_value.merge(int: :test)) } |
| 60 | + .to raise_error(SmartCore::Types::TypeError) |
| 61 | + expect { type.validate!(correct_value.merge(str: [])) } |
| 62 | + .to raise_error(SmartCore::Types::TypeError) |
| 63 | + expect { type.validate!(correct_value.merge(bool: {})) } |
| 64 | + .to raise_error(SmartCore::Types::TypeError) |
| 65 | + end |
| 66 | + |
| 67 | + context 'nilable type' do |
| 68 | + let(:type) do |
| 69 | + SmartCore::Types::Struct::StrictHash( |
| 70 | + int: SmartCore::Types::Value::Integer, |
| 71 | + str: SmartCore::Types::Value::String, |
| 72 | + bool: SmartCore::Types::Value::Boolean |
| 73 | + ).nilable |
| 74 | + end |
| 75 | + |
| 76 | + specify 'type-checking' do |
| 77 | + expect(type.valid?(correct_value)).to eq(true) |
| 78 | + expect(type.valid?(nil)).to eq(true) |
| 79 | + |
| 80 | + expect(type.valid?({})).to eq(false) |
| 81 | + expect(type.valid?(correct_value.merge(int: nil))).to eq(false) |
| 82 | + expect(type.valid?(correct_value.merge(int: :test))).to eq(false) |
| 83 | + expect(type.valid?(correct_value.merge(str: []))).to eq(false) |
| 84 | + expect(type.valid?(correct_value.merge(bool: {}))).to eq(false) |
| 85 | + end |
| 86 | + |
| 87 | + specify 'type-validation' do |
| 88 | + expect { type.validate!(correct_value) }.not_to raise_error |
| 89 | + expect { type.validate!(nil) }.not_to raise_error |
| 90 | + |
| 91 | + expect { type.validate!({}) }.to raise_error(SmartCore::Types::TypeError) |
| 92 | + expect { type.validate!(correct_value.merge(int: nil)) } |
| 93 | + .to raise_error(SmartCore::Types::TypeError) |
| 94 | + expect { type.validate!(correct_value.merge(int: :test)) } |
| 95 | + .to raise_error(SmartCore::Types::TypeError) |
| 96 | + expect { type.validate!(correct_value.merge(str: [])) } |
| 97 | + .to raise_error(SmartCore::Types::TypeError) |
| 98 | + expect { type.validate!(correct_value.merge(bool: {})) } |
| 99 | + .to raise_error(SmartCore::Types::TypeError) |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments