diff --git a/lib/bson/binary.rb b/lib/bson/binary.rb index f4f4d2ed4..e3fc78c27 100644 --- a/lib/bson/binary.rb +++ b/lib/bson/binary.rb @@ -455,7 +455,7 @@ def self.extract_args_for_vector(vector, dtype, padding) # Validate the arguments for a binary vector. # @param [ Array ] data The vector data. # @param [ ::Symbol ] dtype The vector data type. - # @param [ Integer | nil ] padding The padding. Must be 0 if vector is a BSON::Vector. + # @param [ Integer ] padding The padding. Must be 0 if vector is a BSON::Vector. # @raise [ ArgumentError ] If the arguments are invalid. def self.validate_args_for_vector!(data, dtype, padding) raise ArgumentError, "Unknown dtype #{dtype}" unless VECTOR_DATA_TYPES.key?(dtype) diff --git a/lib/bson/vector.rb b/lib/bson/vector.rb index bdec582eb..5d442a023 100644 --- a/lib/bson/vector.rb +++ b/lib/bson/vector.rb @@ -35,7 +35,7 @@ def data # @param [ Integer ] padding The number of bits in the final byte that are to # be ignored when a vector element's size is less than a byte # and the length of the vector is not a multiple of 8. - def initialize(data, dtype, padding) + def initialize(data, dtype, padding = 0) @dtype = dtype @padding = padding super(data.dup) diff --git a/spec/bson/vector_spec.rb b/spec/bson/vector_spec.rb new file mode 100644 index 000000000..cfb43b517 --- /dev/null +++ b/spec/bson/vector_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +# Copyright (C) 2025-present MongoDB Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'spec_helper' + +describe BSON::Vector do + it 'behaves like an Array' do + expect(described_class.new([ 1, 2, 3 ], :int8)).to be_a(Array) + end + + describe '#initialize' do + context 'when padding is not provided' do + let(:vector) { described_class.new([ 1, 2, 3 ], :int8) } + + it 'sets the padding to 0' do + expect(vector.padding).to eq(0) + end + end + end +end