Skip to content

Commit 3f9554d

Browse files
Guilhem Codronkris-jusiak
authored andcommitted
Add tests for the non trivial events
non trivial events seems to be incorrectly triggered, add tests to show that.
1 parent 8cb3419 commit 3f9554d

File tree

1 file changed

+106
-16
lines changed

1 file changed

+106
-16
lines changed

test/ft/transitions.cpp

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ test subsequent_anonymous_transitions_composite = [] {
120120
// clang-format off
121121
return make_transition_table(
122122
*idle / [] (V& v) { v+="ss1|"; } = s1
123+
,s1 + sml::on_entry<_> / [] (V& v) { v+="ss1en|"; }
124+
,s1 + sml::on_exit<_> / [] (V& v) { v+="ss1ex|"; }
123125
,s1 / [] (V& v) { v+="ss2|"; } = s2
124126
,s2 / [] (V& v) { v+="ss3|"; } = X
125127
);
@@ -157,7 +159,7 @@ test subsequent_anonymous_transitions_composite = [] {
157159
sml::sm<composite_sm> sm{calls};
158160
expect(sm.is<decltype(state<sub_sm>)>(X));
159161
expect(sm.is(s3));
160-
std::string expected("11|12|s1|s2|s3|ssen|ss1|ss2|ss3|ssex|s4|13|14|");
162+
std::string expected("11|12|s1|s2|s3|ssen|ss1|ss1en|ss1ex|ss2|ss3|ssex|s4|13|14|");
161163
expect(calls == expected);
162164
};
163165

@@ -550,20 +552,59 @@ test initial_nontrivial_entry = [] {
550552
using namespace sml;
551553
// clang-format off
552554
return make_transition_table(
553-
*idle + sml::on_entry<e2> / [] {}
555+
*idle + sml::on_entry<e2> / [this] { calls+="e2|"; }
556+
,idle + sml::on_entry<_> / [this] { calls+="_|"; }
554557
,idle + event<e2> = s1
555-
,s1 + on_entry<_> / [this] { ++entry_calls; }
558+
,s1 + on_entry<_> / [this] { calls+="_|"; }
559+
,s1 + event<e3> = s2
560+
,s2 + on_entry<e3> / [this] { calls+="e3|"; }
561+
,s2 + on_entry<e2> / [this] { calls+="e2|"; }
562+
,s2 + on_entry<e1> / [this] { calls+="e1|"; }
563+
,s2 + on_entry<_> / [this] { calls+="_|"; }
564+
,s2 + event<e3> = s3
565+
,s3 + on_entry<e2> / [this] { calls+="e2|"; }
566+
,s3 + on_entry<e1> / [this] { calls+="e1|"; }
567+
,s3 + on_entry<_> / [this] { calls+="_|"; }
556568
);
557569
// clang-format on
558570
}
559571

560-
int entry_calls = 0;
572+
std::string calls;
561573
};
562574

563-
sml::sm<c> sm{};
564-
const c& c_ = sm;
565-
sm.process_event(e2{});
566-
expect(1 == c_.entry_calls);
575+
struct d {
576+
auto operator()() noexcept {
577+
using namespace sml;
578+
// clang-format off
579+
return make_transition_table(
580+
*idle + event<e2> = state<c>
581+
);
582+
// clang-format on
583+
}
584+
};
585+
{
586+
sml::sm<c> sm{};
587+
const c& c_ = sm;
588+
expect("_|" == c_.calls);
589+
sm.process_event(e2{});
590+
expect("_|_|" == c_.calls);
591+
sm.process_event(e3{});
592+
expect("_|_|e3|" == c_.calls);
593+
sm.process_event(e3{});
594+
expect("_|_|e3|_|" == c_.calls);
595+
}
596+
{
597+
sml::sm<d> sm{};
598+
const c& c_ = sm;
599+
sm.process_event(e2{});
600+
expect("e2|" == c_.calls);
601+
sm.process_event(e2{});
602+
expect("e2|_|" == c_.calls);
603+
sm.process_event(e3{});
604+
expect("e2|_|e3|" == c_.calls);
605+
sm.process_event(e3{});
606+
expect("e2|_|e3|_|" == c_.calls);
607+
}
567608
};
568609

569610
test initial_nontrivial_exit = [] {
@@ -572,20 +613,69 @@ test initial_nontrivial_exit = [] {
572613
using namespace sml;
573614
// clang-format off
574615
return make_transition_table(
575-
*idle + sml::on_exit<_> / [this] { ++entry_calls; }
616+
*idle + sml::on_exit<_> / [](std::string& calls) { calls+="_|"; }
617+
,idle + sml::on_exit<e2> / [](std::string& calls) { calls+="e2|"; }
576618
,idle + event<e1> = s1
577-
,s1 + sml::on_exit<e1> / [] {}
619+
,idle + event<e2> = s1
620+
,s1 + sml::on_exit<e2> / [](std::string& calls) { calls+="e2|"; }
621+
,s1 + sml::on_exit<e1> / [](std::string& calls) { calls+="e1|"; }
622+
,s1 + sml::on_exit<_> / [](std::string& calls) { calls+="_|"; }
623+
,s1 + event<e3> = s2
624+
,s1 + event<e1> = s2
625+
,s2 + sml::on_exit<e4> / [](std::string& calls) { calls+="e4|"; }
626+
,s2 + sml::on_exit<e3> / [](std::string& calls) { calls+="e3|"; }
627+
,s2 + sml::on_exit<e2> / [](std::string& calls) { calls+="e2|"; }
628+
,s2 + sml::on_exit<e1> / [](std::string& calls) { calls+="e1|"; }
629+
,s2 + sml::on_exit<_> / [](std::string& calls) { calls+="_|"; }
630+
,s2 + event<e3> = s3
578631
);
579632
// clang-format on
580633
}
581-
582-
int entry_calls = 0;
583634
};
584635

585-
sml::sm<c> sm{};
586-
const c& c_ = sm;
587-
sm.process_event(e1{});
588-
expect(1 == c_.entry_calls);
636+
struct d {
637+
auto operator()() noexcept {
638+
using namespace sml;
639+
// clang-format off
640+
return make_transition_table(
641+
*state<c> + event<e2> = idle
642+
,state<c> + sml::on_exit<e4> / [](std::string& calls) { calls+="ce4|"; }
643+
);
644+
// clang-format on
645+
}
646+
};
647+
struct e {
648+
auto operator()() noexcept {
649+
using namespace sml;
650+
// clang-format off
651+
return make_transition_table(
652+
*state<d> + event<e4> = idle
653+
);
654+
// clang-format on
655+
}
656+
};
657+
{
658+
// Test with a simple sm
659+
std::string s;
660+
sml::sm<c> sm{s};
661+
sm.process_event(e1{});
662+
expect("_|" == s);
663+
sm.process_event(e3{});
664+
expect("_|_|" == s);
665+
sm.process_event(e3{});
666+
expect("_|_|e3|" == s);
667+
}
668+
{
669+
// Test with a composite sm
670+
std::string s;
671+
sml::sm<e> sm{s};
672+
sm.process_event(e1{});
673+
expect("_|" == s);
674+
sm.process_event(e1{});
675+
expect("_|e1|" == s);
676+
sm.process_event(e4{});
677+
expect("_|e1|e4|ce4|" == s);
678+
}
589679
};
590680

591681
#if !defined(_MSC_VER)

0 commit comments

Comments
 (0)