Skip to content

Commit

Permalink
Add tests for mag.graphics.style.Colormap and `mag.graphics.style.D…
Browse files Browse the repository at this point in the history
…efault`
  • Loading branch information
mfacchinelli committed Feb 29, 2024
1 parent f05b8da commit 2ea0edc
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
- Allow adding input arguments to conversion function in `mag.graphics.operation.Convert`
- Fix issue with setting I-ALiRT mode when loading
- Fix issues with exporting I-ALiRT data
- Add tests for `mag.graphics.style.Colormap` and `mag.graphics.style.Default`
36 changes: 36 additions & 0 deletions tests/unit/visualize/style/GridSupportTestCase.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
classdef (Abstract) GridSupportTestCase < MAGStyleTestCase
% GRIDSUPPORTTESTCASE Base class for all styles that support grids.

properties (TestParameter)
GridProperties = {struct(Name = "Grid", Value = true, VerifiableName = "XGrid", VerifiableValue = matlab.lang.OnOffSwitchState.on), ...
struct(Name = "Grid", Value = false, VerifiableName = "XGrid", VerifiableValue = matlab.lang.OnOffSwitchState.off)}
end

methods (Test)

function setGridProperty(testCase, GridProperties)

% Set up.
[tl, ax] = mag.test.GraphicsTestUtilities.createFigure(testCase);

args = testCase.getExtraArguments();

% Exercise.
style = feval(testCase.ClassName, ...
args{:}, ...
GridProperties.Name, GridProperties.Value);

axes = style.assemble(tl, ax, []);

% Verify.
[verifiableName, verifiableValue] = mag.test.GraphicsTestUtilities.getVerifiables(GridProperties);
actualValue = axes.(verifiableName);

if isa(actualValue, "matlab.graphics.primitive.Text")
actualValue = actualValue.String;
end

testCase.verifyEqual(actualValue, verifiableValue, compose("""%s"" property value should match.", GridProperties.Name));
end
end
end
49 changes: 49 additions & 0 deletions tests/unit/visualize/style/LegendSupportTestCase.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
classdef (Abstract) LegendSupportTestCase < MAGStyleTestCase
% LEGENDSUPPORTTESTCASE Base class for all styles that support legends.

methods (Test)

% Test that legend labels can be set.
function setLegendLabel(testCase)

% Set up.
[tl, ax] = mag.test.GraphicsTestUtilities.createFigure(testCase);
args = testCase.getExtraArguments();

labels = ["a", "b"];

% Exercise.
style = feval(testCase.ClassName, ...
args{:}, ...
Legend = labels);

plot(ax, 0:9, 1:1:10, 0:9, 10:-1:1);
axes = style.assemble(tl, ax, []);

% Verify.
testCase.verifyEqual(axes.Legend.String, cellstr(labels), """Legend"" property value should match.");
end

% Test that legend location can be set.
function setLegendLocation(testCase)

% Set up.
[tl, ax] = mag.test.GraphicsTestUtilities.createFigure(testCase);
args = testCase.getExtraArguments();

location = 'southwest';

% Exercise.
style = feval(testCase.ClassName, ...
args{:}, ...
Legend = ["a", "b"], ...
LegendLocation = location);

plot(ax, 0:9, 1:1:10, 0:9, 10:-1:1);
axes = style.assemble(tl, ax, []);

% Verify.
testCase.verifyEqual(axes.Legend.Location, location, """LegendLocation"" property value should match.");
end
end
end
19 changes: 19 additions & 0 deletions tests/unit/visualize/style/MAGStyleTestCase.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
classdef (Abstract) MAGStyleTestCase < matlab.unittest.TestCase
% MAGSTYLETESTCASE Base class for all axes styles that support extra
% properties.

properties (Abstract, Constant)
% CLASSNAME Fully qualified name of class under test.
ClassName
end

methods (Access = protected)

function args = getExtraArguments(~)
% GETEXTRAARGUMENTS Retrieve extra arguments needed to construct a
% chart. Can be overridden by subclasses for customization.

args = {};
end
end
end
36 changes: 36 additions & 0 deletions tests/unit/visualize/style/PropertiesTestCase.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
classdef (Abstract) PropertiesTestCase < MAGStyleTestCase
% PROPERTIESTESTCASE Base class for all charts that support extra
% properties.

properties (Abstract, TestParameter)
Properties (1, :) cell
end

methods (Test)

function setSimpleProperty(testCase, Properties)

% Set up.
[tl, ax] = mag.test.GraphicsTestUtilities.createFigure(testCase);

args = testCase.getExtraArguments();

% Exercise.
style = feval(testCase.ClassName, ...
args{:}, ...
Properties.Name, Properties.Value);

axes = style.assemble(tl, ax, []);

% Verify.
[verifiableName, verifiableValue] = mag.test.GraphicsTestUtilities.getVerifiables(Properties);
actualValue = axes.(verifiableName);

if isa(actualValue, "matlab.graphics.primitive.Text")
actualValue = actualValue.String;
end

testCase.verifyEqual(actualValue, verifiableValue, compose("""%s"" property value should match.", Properties.Name));
end
end
end
83 changes: 83 additions & 0 deletions tests/unit/visualize/style/tColormap.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
classdef tColormap < MAGStyleTestCase & GridSupportTestCase & LegendSupportTestCase
% TCOLORMAP Unit tests for "mag.graphics.style.Colormap" class.

properties (Constant)
ClassName = "mag.graphics.style.Colormap"
end

properties (TestParameter)
AllowedClassName = {'mag.graphics.chart.Spectrogram'}
ForbiddenClassName = tColormap.getForbiddenClassNames()
end

methods (Test)

% Test that only spectrogram can be used as chart.
function allowedChart(~, AllowedClassName)
mag.graphics.style.Colormap(Charts = feval(AllowedClassName));
end

% Test that error is thrown for forbidden charts.
function forbiddenChart(testCase, ForbiddenClassName)

testCase.verifyError(@() mag.graphics.style.Colormap(Charts = feval(ForbiddenClassName)), "MATLAB:validation:UnableToConvert", ...
"Only ""mag.graphics.chart.Spectrogram"" is allowed as chart.");
end

% Test that label of colorbar can be set.
function colorbar(testCase)

% Set up.
[tl, ax] = mag.test.GraphicsTestUtilities.createFigure(testCase);

label = 'c label';

% Exercise.
style = mag.graphics.style.Colormap(CLabel = label);

axes = style.assemble(tl, ax, []);

% Verify.
testCase.assertNotEmpty(axes, "Axes object should still exist.");
testCase.verifySameHandle(axes, ax, "Axes should not be overwritten.");

colorbar = findobj(tl, Type = "Colorbar");
testCase.assertNotEmpty(colorbar, "Colorbar should exist.");

testCase.verifyEqual(colorbar.Label.String, label, """CLabel"" property value should match.");
end

% Test that colormap value can be set.
function colormap(testCase)

% Set up.
[tl, ax] = mag.test.GraphicsTestUtilities.createFigure(testCase);

map = 'cool';

% Exercise.
style = mag.graphics.style.Colormap(Map = map);

axes = style.assemble(tl, ax, []);

% Verify.
testCase.assertNotEmpty(axes, "Axes object should still exist.");
testCase.verifySameHandle(axes, ax, "Axes should not be overwritten.");

testCase.verifyEqual(colormap(axes), feval(map), """Colormap"" value should match."); %#ok<FVAL>
end
end

methods (Static, Access = private)

function classNames = getForbiddenClassNames()

metaPackage = meta.package.fromName("mag.graphics.chart");

metaClass = metaPackage.ClassList;
metaClass = metaClass((metaClass < ?mag.graphics.chart.Chart) & (metaClass ~= ?mag.graphics.chart.Spectrogram));

classNames = {metaClass.Name};
end
end
end
20 changes: 20 additions & 0 deletions tests/unit/visualize/style/tDefault.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
classdef tDefault < PropertiesTestCase & GridSupportTestCase & LegendSupportTestCase
% TDEFAULT Unit tests for "mag.graphics.style.Default" class.

properties (Constant)
ClassName = "mag.graphics.style.Default"
end

properties (TestParameter)
Properties = {struct(Name = "XLabel", Value = 'x value'), ...
struct(Name = "XLimits", Value = "tickaligned", VerifiableName = "XLim", VerifiableValue = [0, 1]), ...
struct(Name = "XLimits", Value = [-1, pi], VerifiableName = "XLim"), ...
struct(Name = "YLimits", Value = "tickaligned", VerifiableName = "YLim", VerifiableValue = [0, 1]), ...
struct(Name = "YLimits", Value = [-pi, 1], VerifiableName = "YLim"), ...
struct(Name = "Title", Value = 'the title'), ...
struct(Name = "YLabel", Value = 'y value'), ...
struct(Name = "XScale", Value = 'log'), ...
struct(Name = "YScale", Value = 'log'), ...
struct(Name = "YAxisLocation", Value = 'right')}
end
end

0 comments on commit 2ea0edc

Please sign in to comment.