25 class mitkMessageTestTestClass
32 Package(
int content = 43) : m_Content(content) {}
33 void Clear() { m_Content = 0; }
34 bool operator==(
const Package &other) {
return m_Content == other.m_Content; }
39 class MessageSenderClass
49 Message1<const std::string &> Say;
52 Message1<double> WalkMeters;
55 Message1<const Package &> GivePackage;
58 Message2<int, float> ShoutAgeAndFootSize;
72 void DoSay(
const std::string &words) { Say(words); }
73 void DoWalk(
double meters) { WalkMeters(meters); }
74 void DoGivePackage(
const Package &package) { GivePackage.Send(package); }
75 void DoShoutAgeAndFootSize(
int age,
float size) { ShoutAgeAndFootSize(age, size); }
80 class MessageReceiverClass
83 MessageReceiverClass() { Amnesia(); }
84 void OnWaveHand() { m_HandWaved =
true; }
85 bool HandWaved() {
return m_HandWaved; }
86 void OnSay(
const std::string &words) { m_WordsSaid = words; }
87 const std::string WordsSaid() {
return m_WordsSaid; }
88 void OnWalk(
double meters) { m_MetersWalked = meters; }
89 double MetersWalked() {
return m_MetersWalked; }
90 void OnGivePackage(
const Package &p) { m_PackageReceived = p; }
91 Package PackageReceived() {
return m_PackageReceived; }
92 void OnShoutAgeAndFootSize(
int age,
float footSize)
95 m_FootSize = footSize;
98 int Age() {
return m_Age; }
99 float FootSize() {
return m_FootSize; }
104 m_MetersWalked = 0.0;
105 m_PackageReceived.Clear();
110 void RegisterObservers(MessageSenderClass &sender)
112 sender.WaveHand += MessageDelegate<MessageReceiverClass>(
this, &MessageReceiverClass::OnWaveHand);
113 sender.ShowFinger += MessageDelegate<MessageReceiverClass>(
114 this, &MessageReceiverClass::OnWaveHand);
116 sender.Say += MessageDelegate1<MessageReceiverClass, const std::string &>(
this, &MessageReceiverClass::OnSay);
117 sender.WalkMeters += MessageDelegate1<MessageReceiverClass, double>(
this, &MessageReceiverClass::OnWalk);
118 sender.GivePackage +=
119 MessageDelegate1<MessageReceiverClass, const Package &>(
this, &MessageReceiverClass::OnGivePackage);
120 sender.ShoutAgeAndFootSize +=
121 MessageDelegate2<MessageReceiverClass, int, float>(
this, &MessageReceiverClass::OnShoutAgeAndFootSize);
126 std::string m_WordsSaid;
127 double m_MetersWalked;
128 Package m_PackageReceived;
137 std::string m_Description;
140 Law(
const std::string &law) : m_Description(law) {}
141 std::string GetDescription()
const {
return m_Description; }
156 m_AnalysisStartedMessage();
159 Law massLaw(
"Unit tests are mandatory!");
160 m_LawDiscoveredMessage(massLaw);
167 m_AnalysisStoppedMessage(
true);
181 NewtonMachine *m_Machine;
184 Observer(NewtonMachine *machine)
185 : m_Machine(machine), m_MachineStarted(false), m_MachineStopped(false), m_Error(false), m_Law(
"NONE")
189 m_Machine->AddAnalysisStoppedListener(
191 m_Machine->AddLawDiscoveredListener(
201 m_Machine->RemoveAnalysisStoppedListener(
203 m_Machine->RemoveLawDiscoveredListener(
209 void MachineStarted() { m_MachineStarted =
true; }
210 void MachineStopped(
bool error)
212 m_MachineStopped =
true;
216 void LawDiscovered(
const Law &law) { m_Law = law; }
219 m_PatentReviewed =
true;
222 bool m_MachineStarted;
223 bool m_MachineStopped;
226 bool m_PatentReviewed;
237 mitk::mitkMessageTestTestClass::MessageSenderClass sender;
238 mitk::mitkMessageTestTestClass::MessageReceiverClass receiver;
242 receiver.RegisterObservers(sender);
247 receiver.HandWaved()),
248 "Message without parameters");
255 "Message with std::string parameter");
259 "Message with double parameter");
262 mitk::mitkMessageTestTestClass::Package package(8);
264 "Message with class parameter");
268 (sender.DoShoutAgeAndFootSize(46, 30.5), (receiver.Age() == 46 && (receiver.FootSize() - 30.5 < 0.0001))),
269 "Message with int AND float parameter");
272 mitk::mitkMessageTestTestClass::NewtonMachine newtonMachine;
273 mitk::mitkMessageTestTestClass::Observer observer1(&newtonMachine);
274 mitk::mitkMessageTestTestClass::Observer observer2(&newtonMachine);
277 newtonMachine.StartAnalysis();
278 MITK_TEST_CONDITION(observer1.m_MachineStarted ==
true,
"Message from Message Macro send to receiver 1");
279 MITK_TEST_CONDITION(observer2.m_MachineStarted ==
true,
"Message from Message Macro send to receiver 2");
281 MITK_TEST_CONDITION(observer1.m_Law.GetDescription() == std::string(
"Unit tests are mandatory!"),
282 "Message1 from Message Macro send to receiver 1");
283 MITK_TEST_CONDITION(observer2.m_Law.GetDescription() == std::string(
"Unit tests are mandatory!"),
284 "Message1 from Message Macro send to receiver 2");
287 newtonMachine.StopAnalysis();
288 MITK_TEST_CONDITION(observer1.m_MachineStopped ==
true,
"Message1 from Message Macro send to receiver 1");
289 MITK_TEST_CONDITION(observer1.m_Error ==
true,
"Message1 parameter from Message Macro send to receiver 1");
291 MITK_TEST_CONDITION(observer2.m_MachineStopped ==
true,
"Message1 from Message Macro send to receiver 2");
292 MITK_TEST_CONDITION(observer2.m_Error ==
true,
"Message1 parameter from Message Macro send to receiver 2");
#define mitkNewMessage1Macro(msgHandleObject, type1)
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
DataCollection - Class to facilitate loading/accessing structured data.
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
#define mitkNewMessageMacro(msgHandleObject)
#define MITK_TEST_CONDITION(COND, MSG)
int mitkMessageTest(int, char *[])