Skip to content

Commit

Permalink
Add unit tests for ClassTypeValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
sgilmore10 committed Oct 30, 2023
1 parent 4bbeaab commit 5a955e9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function validateElement(obj, element)
msg = "Expected all cell array elements to be of class type " + ...
obj.ClassName + ", but encountered element of class type " + ...
class(element) + ".";
error(msg, id);
error(id, msg);
end
end

Expand Down
98 changes: 98 additions & 0 deletions matlab/test/arrow/array/list/tClassTypeValidator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
%TCLASSTYPEVALIDATOR Unit tests for arrow.array.internal.list.ClassTypeValidator

% Licensed to the Apache Software Foundation (ASF) under one or more
% contributor license agreements. See the NOTICE file distributed with
% this work for additional information regarding copyright ownership.
% The ASF licenses this file to you 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.

classdef tClassTypeValidator < matlab.unittest.TestCase

methods (Test)
function Smoke(testCase)
import arrow.array.internal.list.ClassTypeValidator
validator = ClassTypeValidator("Sample Data");
testCase.verifyInstanceOf(validator, "arrow.array.internal.list.ClassTypeValidator");
end

function ClassNameGetter(testCase)
% Verify the ClassName getter returns the expected scalar
% string.
import arrow.array.internal.list.ClassTypeValidator

validator = ClassTypeValidator("Sample Data");
testCase.verifyEqual(validator.ClassName, "string");
end

function ClassNameNoSetter(testCase)
% Verify ClassName property is not settable.
import arrow.array.internal.list.ClassTypeValidator

validator = ClassTypeValidator(1);
fcn = @() setfield(validator, "ClassName", "duration");
testCase.verifyError(fcn, "MATLAB:class:SetProhibited");
end

function validateElementNoThrow(testCase) %#ok<MANU>
% Verify validateElement does not throw an exception
% if class type of the input element matches the ClassName
% property value.
import arrow.array.internal.list.ClassTypeValidator

validator = ClassTypeValidator(1);
validator.validateElement(2);
validator.validateElement([1 2 3]);
validator.validateElement([1; 2; 3; 3]);
validator.validateElement([5 6; 7 8]);
validator.validateElement(double.empty(0, 1));
end

function validateElementClassTypeMismatchError(testCase)
% Verify validateElement throws an exception whose identifier
% is "arrow:array:list:ClassTypeMismatch" if the input
% element's class type does not match the ClassName property
% value.
import arrow.array.internal.list.ClassTypeValidator

validator = ClassTypeValidator(1);
errorID = "arrow:array:list:ClassTypeMismatch";
testCase.verifyError(@() validator.validateElement("A"), errorID);
testCase.verifyError(@() validator.validateElement(uint8([1 2])), errorID);
testCase.verifyError(@() validator.validateElement(datetime(2023, 1, 1)), errorID);
end

function getElementLength(testCase)
% Verify getElementLength returns the expected length values
% for the given input arrays.
import arrow.array.internal.list.ClassTypeValidator

validator = ClassTypeValidator(1);
testCase.verifyEqual(validator.getElementLength(2), 1);
testCase.verifyEqual(validator.getElementLength([1 2; 3 4]), 4);
testCase.verifyEqual(validator.getElementLength(double.empty(1, 0)), 0);
end

function reshapeCellElements(testCase)
% Verify reshapeCellElements reshapes all elements in the input
% cell array into column vectors.
import arrow.array.internal.list.ClassTypeValidator

validator = ClassTypeValidator(1);
C = {[1 2 3], [4; 5], [6 7; 8 9], double.empty(1, 0), 10};
act = validator.reshapeCellElements(C);
exp = {[1; 2; 3], [4; 5], [6; 8; 7; 9], double.empty(0, 1), 10};
testCase.verifyEqual(act, exp);
end

end

end

0 comments on commit 5a955e9

Please sign in to comment.