How to write a composite contraint or comparator to be usable in unittest framework
Show older comments
I'm struggeling with a supposedly simple task:
For a given example class
classdef SimpleClass
properties
some_string (1,1) string = "some_string"
some_vague_number (1,1) double = 0
some_precise_number (1,1) double = pi()
end
end
I want to write a constraint that checks "similarity" of two instances of the class. The strings should be equal and I want to parametrize the tolerances for each property separately. Clearly i could verify the conditions one after another but I would have to repeat that for each qualification, which is not good, when the class changes.
In short, I would like to have a class that combines a sequence of Comparators (pluggable into IsEqualTo(...'Using'...) ) or a constraint that uses the tolerances for checking and the diagnostic reporting thereof.
I have already made some attempts that don't work:
- Derive a class SimpleClassComparator from IsEqualTo and pass an array of PublicPropertyComparator, each just looking at one field, to the base class constructor. But the the tests always pass.
- Derive a class from contraint and implement the abstract methods as a sequence of my nested constraints for each field. That had some other problems and cannot be used for containers.
I can provide code for my approaches but firstly I would like to know:
What is the right way to implement a custom Comparator for the unittest framework?
6 Comments
Jan Tusch
on 11 Dec 2019
Edited: per isakson
on 15 Dec 2019
per isakson
on 15 Dec 2019
Edited: per isakson
on 15 Dec 2019
BUMP
I tried to understand what's going on by debug stepping through the code. The size of dbstack was 36 when I found a real "low-level" comparison. I found comparisons of property names, but not propery values.
Proposal: replace
some_string (1,1) string = "some_string"
by
some_string (1,1) string = "factory_string_value"
Have you found the answer?
Jan Tusch
on 16 Dec 2019
per isakson
on 17 Dec 2019
I'm amazed of the sheer size of the code of the Matlab unit testing framework. The Myth tells that the first unit testing framework was designed and implemented during the flight back from Switzerland to the US.
I tried to step through the code to find the cause of your problem. At the dbstack depth of 36 it was hard to stay concentrated and keep track of a variable, the name and value of which are identical. Thus, I proposed you to change the value.
David Hruska
on 18 Dec 2019
Hi Jan and others,
I was going to suggest writing a custom comparator subclass, but it seems you've already tried this and run into issues. Could you expand on what those issues were? Also, could you clarify how you'd like your constraint to work with containers? Are you saying that you'd like to compare two SimpleClass instances stored inside cell arrays, for instance?
As you've found, comparing values can be trickier than you might expect. For this reason, the Comparator interface isn't documented for extension (it may change in future releases as the framework evolves). We'd definitely want to address these challenges in writing a custom comparator before documenting the interface.
Accepted Answer
More Answers (0)
Categories
Find more on Use Prebuilt MATLAB Interface to C/C++ Library in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!