1+ classdef ttb_optparse < RVCTest & matlab .unittest .TestCase
2+ % ttb_optparse Unit tests for tb_optparse function
3+
4+ % Copyright 2025 Peter Corke, Witold Jachimczyk, Remo Pillat
5+
6+ %% Tests for LineSpec parsing and output argument
7+ properties (TestParameter )
8+ % pLineSpecValid - Valid LineSpec definitions
9+ pLineSpecValid = struct(...
10+ " SolidLine" , ' -' , ...
11+ " RedDashedCircles" , ' --or' , ...
12+ " TriangleBlue" , ' b^' ...
13+ )
14+
15+ % pLineSpecInvalid - Invalid LineSpec definitions
16+ pLineSpecInvalid = struct(...
17+ " OtherAlphabetic" , ' fjl' , ...
18+ " OneCharacterWrong" , ' --oQ' , ...
19+ " SpecialChars" , ' &(' , ...
20+ " NotCharArray" , [" --" " o" ], ...
21+ " Cell" , {{' --o' }} ...
22+ )
23+ end
24+
25+ methods (Test )
26+ function validLineSpec(testCase , pLineSpecValid )
27+ % validLineSpec Positive tests for LineSpec parsing
28+ % The actual options structure (first argument to
29+ % tb_optparse) is just an empty structure.
30+
31+ % The LineSpec is the only argument that's passed to tb_optparse.
32+ % Expectation: The LineSpec is returned as third output
33+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {pLineSpecValid });
34+ testCase .assertNotEmpty(actualLineSpec );
35+ testCase .verifyEqual(actualLineSpec{1 }, pLineSpecValid , " Expected LineSpec to be parsed correctly." );
36+
37+ % The LineSpec is passed along with other arguments
38+ % Expectation: The LineSpec argument is returned as third
39+ % output. The other arguments are ignored.
40+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {pLineSpecValid , " LineWidth" , 2 });
41+ testCase .verifyEqual(actualLineSpec{1 }, pLineSpecValid , " Expected LineSpec to be parsed correctly." );
42+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {" ffo" , pLineSpecValid , ' baa' });
43+ testCase .verifyEqual(actualLineSpec{1 }, pLineSpecValid , " Expected LineSpec to be parsed correctly." );
44+ end
45+
46+ function validLineSpecMultiple(testCase , pLineSpecValid )
47+ % validLineSpecMultiple Test passing multiple (valid) LineSpecs
48+
49+ % Expectation: Only the first LineSpec that's matched is
50+ % returned.
51+ flipLineSpec = fliplr(pLineSpecValid );
52+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {pLineSpecValid , flipLineSpec });
53+ testCase .verifyEqual(actualLineSpec{1 }, pLineSpecValid );
54+
55+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {flipLineSpec , pLineSpecValid });
56+ testCase .verifyEqual(actualLineSpec{1 }, flipLineSpec );
57+
58+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {{' ' }, flipLineSpec , pLineSpecValid , struct });
59+ testCase .verifyEqual(actualLineSpec{1 }, flipLineSpec );
60+ end
61+
62+ function validLineSpecString(testCase , pLineSpecValid )
63+ % validLineSpecString String inputs should also work
64+
65+ % Expectation: Result is still returned as character array
66+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {string(pLineSpecValid )});
67+ testCase .verifyEqual(actualLineSpec{1 }, pLineSpecValid , " Expected LineSpec as string to work." );
68+ end
69+
70+ function validLineSpecRepeated(testCase )
71+ % validLineSpecRepeated Test for having repeated tokens
72+
73+ % This test case fails for now, because of a bug that is also
74+ % triggered by chapter 5 code.
75+ testCase .assumeFail(" Waiting for fix" );
76+
77+ repeatedColor = ' :kk' ;
78+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {repeatedColor });
79+ testCase .verifyEqual(actualLineSpec{1 }, repeatedColor );
80+
81+ repeatedMultiChar = ' --:rg-.' ;
82+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {repeatedMultiChar });
83+ testCase .verifyEqual(actualLineSpec{1 }, repeatedMultiChar );
84+ end
85+
86+ function invalidLineSpecNeg(testCase , pLineSpecInvalid )
87+ % invalidLineSpecNeg Negative tests for LineSpec parsing
88+
89+ [~ ,~ ,actualLineSpec ] = tb_optparse(struct , {pLineSpecInvalid });
90+ testCase .verifyEqual(actualLineSpec , {}, " Expected empty cell return for invalid LineSpec." )
91+ end
92+ end
93+
94+ end
0 commit comments