-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for
mag.graphics.style.Colormap
and `mag.graphics.style.D…
…efault`
- Loading branch information
1 parent
f05b8da
commit 2ea0edc
Showing
7 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |