C++ / Basic / Lesson 7
Scoped enumerations and state
Model C++ states with scoped enumerations and validated transitions.
On this page
Lesson goals
- Use scoped enumerators without implicit integer conversion.
- Validate input and state transitions.
- Use scoped enumeration names to represent and compare domain states.
- Validate external codes and enforce explicit state-transition rules.
Name the state
A set of related choices is easier to review when each choice has a name instead of an unexplained integer. A scoped enumeration introduces a distinct type and keeps enumerator names within its scope. It is useful for a closed set of states such as a job being queued or complete. The type alone does not validate arbitrary external integers converted into it.
Use scoped enumerators
Declare enum class followed by its enumerators, and refer to a choice with the type name and scope operator. The example initializes a status, compares it to a named value, and produces a deterministic result. Scoped enumerators do not implicitly convert to int. This restriction prevents accidental arithmetic and comparisons with unrelated enumerations, while explicit conversions still require careful validation.
#include <iostream>
enum class Status { queued, complete };
int main() {
Status state = Status::queued;
std::cout << (state == Status::queued) << "\n";
}
1
The comparison is true because the state is queued.
Map states with switch
A switch can map each named state to behavior. Include a deliberate fallback when values can arrive from outside the trusted construction path, and decide whether it reports an error or returns an explicit result. The example handles both declared choices and returns unknown for another representable enum value. Review compiler diagnostics when adding a choice so old mappings do not silently omit new behavior.
#include <iostream>
#include <string_view>
enum class Status { queued, complete };
std::string_view label(Status state) {
switch (state) {
case Status::queued: return "queued";
case Status::complete: return "complete";
}
return "unknown";
}
int main() {
std::cout << label(Status::complete) << "\n";
}
complete
The switch maps complete to its label.
Validate external input
An enum is an internal model, not a complete parser or wire format. At an input boundary, compare the external token to the supported names and reject an unknown token before constructing the domain state. Do not assume the underlying representation or numeric values form a stable file format unless the interface explicitly defines and tests that compatibility contract. Keep user-facing labels separate from identifiers when translation is required.
Test states and transitions
Test each declared choice, an unknown input token, and transitions that the domain forbids. A named state does not itself prevent an invalid transition between two valid states; that rule belongs in the operation that changes state. Keep the mapping and transition tests near the API so adding a state requires an explicit review of both behavior and diagnostics.
Name domain states explicitly
A scoped enumeration gives a distinct type to a set of named values. The enumerators are accessed through the scope, such as Status::queued, which makes a state’s meaning visible and avoids placing those names directly in the enclosing scope. A comparison produces bool; with the default stream formatting true is displayed as 1 unless boolalpha is selected. An enum name alone does not define which transitions are permitted or what an external integer means.
Choose names for the domain rather than scattering unexplained numeric codes through a program. A state machine also needs an initial state, allowed transitions, and behavior for a rejected transition. For example, queued may move to running and running to done, while done cannot return to queued under a particular contract. State these rules independently from the enum declaration and test the exact source and destination for each transition.
Validate external representations first
An external integer must be checked against the accepted cases before it becomes a domain state. An explicit static_cast to an enum is a conversion, not validation that the value corresponds to a named enumerator. Map each accepted code in a switch or another explicit parser and return a failure representation for unknown codes. Keep the external encoding separate from the internal enum values if the protocol may evolve.
A parser can return std::optional<Status> to distinguish a recognized state from an invalid code. Callers must check the optional before accessing its value. Do not silently map an unknown code to a valid state unless the contract explicitly defines that fallback. Test every accepted code, an unknown value, and the effect on existing state when parsing fails.
Make transitions explicit
A transition function should check the current state and requested action before changing state. It can return bool or a more descriptive result type, but the contract must distinguish successful changes from rejection. If rejection promises no mutation, test the prior state after the call as well as the returned status. A switch on the state can make the cases visible; include an explicit policy for unexpected values at a trust boundary.
Sequential calls observe the state left by previous calls. A successful queued-to-running transition can permit a later running-to-done transition, while a direct queued-to-done request can be rejected. Design tests with fresh objects for independent paths and a shared object when testing a sequence. An enum improves naming but does not enforce the transition graph automatically.
Avoid accidental representation assumptions
Scoped enum values do not implicitly convert to int in the same way as unscoped enumerators. An explicit cast can expose an underlying representation when needed, but a wire or file format requires documented codes and validation; it must not depend on an incidental in-memory layout. If an underlying type is specified, its range and the external encoding still need separate reasoning. Avoid using arbitrary casts as a shortcut around domain checks.
A switch can translate named states into stable display labels, while a separate parser maps accepted external codes into states. This separation makes it possible to change internal implementation without silently changing the external contract. Handle a missing or invalid optional before formatting or transitioning. Do not dereference an empty optional merely to demonstrate a failure.
Test state and rejection paths
Use fixed synthetic codes and transitions: accepted queued, running, and done codes; an unknown code; a valid two-step sequence; and a rejected direct transition. Check both the status and the retained prior state on rejection. Verify formatting with boolalpha only when textual Boolean output is intended, since default cout formatting displays true and false as 1 and 0.
The complete examples below use C++20 standard-library features, fixed values, and explicit checks before optional access. They were compiled and run locally with warnings enabled. They illustrate a small synthetic state model and do not establish a protocol encoding or transition policy for a real system.
20 worked examples
1. Compare a named state
Problem: Compare a fixed queued state with its scoped enumerator.
- The initialized state is Status::queued.
- The comparison is true and default cout formatting displays 1.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
int main() {
Status state{Status::queued}; std::cout << (state == Status::queued) << '\n';
return 0;
}
1
Why this result follows: A scoped enumerator names the domain state and comparison yields bool.
2. Display a false comparison
Problem: Compare a running state with done using textual Boolean formatting.
- The two scoped enumerators represent different states.
- The comparison is false and boolalpha displays the word false.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
int main() {
Status state{Status::running}; std::cout << std::boolalpha << (state == Status::done) << '\n';
return 0;
}
false
Why this result follows: Formatting changes the Boolean display rather than the state value.
3. Check a different state
Problem: Test whether a completed synthetic state differs from queued.
- The state is initialized to done.
- It is not queued, so the comparison is true.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
int main() {
Status state{Status::done}; std::cout << (state != Status::queued) << '\n';
return 0;
}
1
Why this result follows: The scoped type keeps both operands within the same named domain.
4. Parse a queued code
Problem: Map accepted external code one to a queued state.
- The parser selects case one and returns queued.
- The optional has a value and its state comparison is true.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
std::optional<Status> parse(int code) { switch (code) { case 1: return Status::queued; case 2: return Status::running; case 3: return Status::done; default: return std::nullopt; } }
int main() {
auto state{parse(1)}; std::cout << (state.has_value() && *state == Status::queued) << '\n';
return 0;
}
1
Why this result follows: The accepted external code is mapped explicitly before state use.
5. Parse a running code
Problem: Map accepted external code two to a running state.
- The parser selects case two and returns running.
- The optional is present and matches Status::running.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
std::optional<Status> parse(int code) { switch (code) { case 1: return Status::queued; case 2: return Status::running; case 3: return Status::done; default: return std::nullopt; } }
int main() {
auto state{parse(2)}; std::cout << (state.has_value() && *state == Status::running) << '\n';
return 0;
}
1
Why this result follows: The caller checks presence before dereferencing the optional.
6. Parse a completed code
Problem: Map accepted external code three to a done state.
- The parser selects case three.
- It returns a present optional containing done.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
std::optional<Status> parse(int code) { switch (code) { case 1: return Status::queued; case 2: return Status::running; case 3: return Status::done; default: return std::nullopt; } }
int main() {
auto state{parse(3)}; std::cout << (state.has_value() && *state == Status::done) << '\n';
return 0;
}
1
Why this result follows: An explicit mapping ties the external code to a named state.
7. Reject an unknown code
Problem: Check a synthetic external code outside the accepted set.
- Code nine matches no listed case.
- The parser returns nullopt, so has_value is false.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
std::optional<Status> parse(int code) { switch (code) { case 1: return Status::queued; case 2: return Status::running; case 3: return Status::done; default: return std::nullopt; } }
int main() {
auto state{parse(9)}; std::cout << state.has_value() << '\n';
return 0;
}
0
Why this result follows: An unknown representation is rejected rather than silently cast into the domain.
8. Advance a queued state
Problem: Apply one valid transition to a synthetic queued state.
- The queued branch changes the state to running.
- The function reports success and the new-state check is true.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
bool advance(Status& state) { if (state == Status::queued) { state = Status::running; return true; } if (state == Status::running) { state = Status::done; return true; } return false; }
int main() {
Status state{Status::queued}; bool ok{advance(state)}; std::cout << ok << ' ' << (state == Status::running) << '\n';
return 0;
}
1 1
Why this result follows: The transition function enforces a named edge in the state graph.
9. Complete a running state
Problem: Apply one valid transition from running to done.
- The running branch selects the done state.
- The function reports success and the final state is done.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
bool advance(Status& state) { if (state == Status::queued) { state = Status::running; return true; } if (state == Status::running) { state = Status::done; return true; } return false; }
int main() {
Status state{Status::running}; bool ok{advance(state)}; std::cout << ok << ' ' << (state == Status::done) << '\n';
return 0;
}
1 1
Why this result follows: The documented transition is applied before success is returned.
10. Reject a terminal transition
Problem: Attempt to advance a done state and inspect both outcomes.
- Neither permitted branch matches the terminal state.
- The function returns false and leaves the state done.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
bool advance(Status& state) { if (state == Status::queued) { state = Status::running; return true; } if (state == Status::running) { state = Status::done; return true; } return false; }
int main() {
Status state{Status::done}; bool ok{advance(state)}; std::cout << ok << ' ' << (state == Status::done) << '\n';
return 0;
}
0 1
Why this result follows: Rejection preserves the previous state under this contract.
11. Trace two transitions
Problem: Advance the same synthetic state twice in sequence.
- The first call changes queued to running.
- The second changes running to done; both calls succeed.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
bool advance(Status& state) { if (state == Status::queued) { state = Status::running; return true; } if (state == Status::running) { state = Status::done; return true; } return false; }
int main() {
Status state{Status::queued}; bool a{advance(state)}; bool b{advance(state)}; std::cout << a << ' ' << b << ' ' << (state == Status::done) << '\n';
return 0;
}
1 1 1
Why this result follows: Sequential calls observe the state produced by the preceding transition.
12. Reject a direct transition
Problem: Attempt to finish a queued state without first running it.
- The precondition requires running, but the state is queued.
- The function returns false and the original state remains queued.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
bool finish(Status& state) { if (state != Status::running) return false; state = Status::done; return true; }
int main() {
Status state{Status::queued}; bool ok{finish(state)}; std::cout << ok << ' ' << (state == Status::queued) << '\n';
return 0;
}
0 1
Why this result follows: Validation before assignment preserves state on this rejected edge.
13. Format a named state
Problem: Translate a fixed running state into a display label.
- The switch selects the running case.
- The function returns the text running.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
const char* label(Status state) { switch (state) { case Status::queued: return "queued"; case Status::running: return "running"; case Status::done: return "done"; } return "unknown"; }
int main() {
std::cout << label(Status::running) << '\n';
return 0;
}
running
Why this result follows: A formatter maps domain states to labels separately from external-code parsing.
14. Format a terminal state
Problem: Translate the done state into its explicit display label.
- The switch selects the done case.
- The function returns the fixed text done for that selected case.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
const char* label(Status state) { switch (state) { case Status::queued: return "queued"; case Status::running: return "running"; case Status::done: return "done"; } return "unknown"; }
int main() {
std::cout << label(Status::done) << '\n';
return 0;
}
done
Why this result follows: Each named state has an explicit display mapping in this fixture.
15. Use a separate enum domain
Problem: Compare a fixed mode within its own scoped type.
- The mode is initialized to Mode::safe.
- The equality check against Mode::safe is true for this initialized object.
#include <iostream>
#include <optional>
enum class Mode { safe = 1, fast = 2 };
int main() {
Mode mode{Mode::safe}; std::cout << (mode == Mode::safe) << '\n';
return 0;
}
1
Why this result follows: A distinct scoped enum gives the mode its own named type.
16. Expose a fixed representation
Problem: Explicitly convert a named enum value to its declared underlying type.
- Code::busy is declared with underlying value two.
- The explicit cast produces unsigned value two.
#include <iostream>
#include <optional>
enum class Code : unsigned { ok = 1, busy = 2 };
int main() {
Code code{Code::busy}; std::cout << static_cast<unsigned>(code) << '\n';
return 0;
}
2
Why this result follows: The cast exposes this declared representation but is not external-input validation.
17. Handle an empty optional
Problem: Report a rejected synthetic code without accessing an absent value.
- The parser returns nullopt for code nine.
- The caller checks the optional and prints invalid.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
std::optional<Status> parse(int code) { switch (code) { case 1: return Status::queued; case 2: return Status::running; case 3: return Status::done; default: return std::nullopt; } }
int main() {
auto state{parse(9)}; if (!state) std::cout << "invalid\n";
return 0;
}
invalid
Why this result follows: The absent result is handled before any optional dereference.
18. Access a validated optional
Problem: Check presence before using an accepted parsed state.
- Code one maps to a present queued state.
- The guarded dereference compares it with queued and prints one.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
std::optional<Status> parse(int code) { switch (code) { case 1: return Status::queued; case 2: return Status::running; case 3: return Status::done; default: return std::nullopt; } }
int main() {
auto state{parse(1)}; if (state) std::cout << (*state == Status::queued) << '\n';
return 0;
}
1
Why this result follows: The caller accesses the optional value only after checking it exists.
19. Copy state by value
Problem: Copy a scoped state and change only the second object.
- b is initialized from a’s queued value.
- Assigning running to b leaves a queued.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
int main() {
Status a{Status::queued}; Status b{a}; b = Status::running; std::cout << (a == Status::queued) << ' ' << (b == Status::running) << '\n';
return 0;
}
1 1
Why this result follows: The two enum objects store independent values.
20. Stop at the terminal state
Problem: Advance a running state twice and inspect the rejected second call.
- The first call moves running to done.
- The second call rejects done and leaves it unchanged.
#include <iostream>
#include <optional>
enum class Status { queued, running, done };
bool advance(Status& state) { if (state == Status::queued) { state = Status::running; return true; } if (state == Status::running) { state = Status::done; return true; } return false; }
int main() {
Status state{Status::running}; bool first{advance(state)}; bool second{advance(state)}; std::cout << first << ' ' << second << ' ' << (state == Status::done) << '\n';
return 0;
}
1 0 1
Why this result follows: The terminal-state rule is checked against the current state on each call.
Common errors and fixes
An unknown external value is accepted
- Why it happens
- A cast was treated as validation.
- How to fix it
- Map only supported tokens to states.
- Prevent it next time
- Test unknown input.
A new state has no behavior
- Why it happens
- An existing switch was not updated.
- How to fix it
- Add the intended case and tests.
- Prevent it next time
- Review diagnostics and state mappings.
A forbidden transition succeeds
- Why it happens
- Both endpoints are valid but the transition is not.
- How to fix it
- Check transitions in the state-changing operation.
- Prevent it next time
- Test the transition table.
20 practice exercises
1. What does the first example print?
Show hint
The comparison produces bool, which cout formats numerically by default.
Show worked solution
It prints 1 because state equals Status::queued and the default stream formatting prints true as 1.
The state equals Status::queued, so the comparison is true. With default stream formatting true is displayed as 1. This is the Boolean comparison result, not automatically an external enum code.
Check your solution
- The state comparison is true
- The default output is 1
2. Why is an explicit cast from an external integer not enough validation?
Show hint
A conversion does not establish that the external code is an accepted domain choice.
Show worked solution
The converted value may not correspond to a declared domain choice. Check the external representation against the accepted cases first.
An explicit cast can produce an enum value without proving it corresponds to a named accepted state. Validate the external representation against documented cases first, and handle unknown values explicitly.
Check your solution
- Accepted external codes are enumerated
- Unknown codes are rejected or handled by a documented policy
3. A workflow has scoped states queued, running and done. Design a function that returns a readable label for each named state and a fallback for an unexpected value; explain how to test the fallback.
Show hint
Use a switch over the scoped values and put the fallback after its cases.
Show worked solution
A switch can return "queued", "running" or "done" for the three named values and return "unknown" after the switch. Test each named state and, for a fixed underlying type, explicitly cast an unrecognized representable value such as 9 to Status and check that it reaches the fallback. Do not mistake that cast for validation of untrusted input.
Check your solution
- All three named states receive their corresponding labels.
- An explicitly constructed unknown representable value reaches "unknown" rather than a named label.
4. Two scoped enum types both define idle. Explain why Mode::idle == Status::idle is rejected and propose an explicit comparison policy.
Show hint
The enumerators belong to distinct types.
Show worked solution
The operands have different enum types, so the comparison is not provided automatically. Define a named function that maps each value to a shared domain concept only if the application specifies that relationship; comparing underlying integers alone can conflate unrelated meanings.
Check your solution
- The answer identifies different enum types.
- The proposed mapping requires an explicit domain rule.
5. An API receives the string "RUNNING" but its documented labels are lowercase. Design an explicit parsing policy and a test for unknown text.
Show hint
Decide whether case folding is part of the contract.
Show worked solution
A strict parser can accept exactly "queued", "running" and "done", returning an empty optional for "RUNNING" or any unknown text. Document case sensitivity and test both "running" and "RUNNING"; if case folding is desired, specify it and test that policy instead.
Check your solution
- The chosen policy handles case explicitly.
- An unknown string returns no state.
6. An enum is stored in a database and later a new enumerator is added. Explain why declaration order should not be treated as a stable wire format and propose a migration-safe approach.
Show hint
Separate internal names from persisted codes.
Show worked solution
Assign and document explicit persistent codes and parse them through a checked mapping, retaining old codes when adding a value. Version the format or migrate stored records when meanings change; relying on implicit ordinal positions can reinterpret existing data after reordering.
Check your solution
- Existing stored codes retain their documented meaning.
- Unknown codes are rejected or handled explicitly.
7. A formatter must handle a newly added paused state. Explain how compiler warnings and a fallback can help detect an incomplete switch.
Show hint
Consider both named values and unexpected runtime values.
Show worked solution
Add a paused case with its intended label and compile with appropriate switch warnings to find omitted named enumerators. Retain a deliberate fallback for unexpected representable values where needed; warnings and fallback serve different checks and depend on compiler options.
Check your solution
- The paused state gets an explicit label.
- The answer distinguishes switch warnings from runtime fallback.
8. A worker may move queued to running only when a slot is available. Describe tests for both available and unavailable slots, including unchanged state on rejection.
Show hint
The transition guard depends on external capacity.
Show worked solution
With a slot available, request running and verify acceptance and state running. With no slot, repeat from a fresh queued state and verify rejection and state queued. Also verify that rejection does not consume a slot; use controlled test capacity.
Check your solution
- Available capacity accepts and moves to running.
- Unavailable capacity rejects and preserves queued and capacity.
9. Two callers read queued and both attempt to start the same job. Explain the race and name a mechanism that makes the transition atomic.
Show hint
Checking and changing state must be one protected operation.
Show worked solution
Both callers can observe queued before either writes running and each may claim success. Protect the check and update with a mutex or an atomic compare-and-exchange when the representation permits it; test that exactly one caller succeeds under contention.
Check your solution
- The race includes two observations of queued.
- The test expects exactly one successful transition.
10. A transition function returns only bool. A caller needs to distinguish an unknown request from a forbidden transition. Propose a result type and two tests.
Show hint
Represent distinct failures explicitly.
Show worked solution
Return a result that distinguishes accepted, unknown request and forbidden transition, such as a scoped result enum. Test an unrecognized external request for unknown and a recognized but disallowed transition for forbidden; in both failures verify that the current state is preserved.
Check your solution
- The result distinguishes the two failure reasons.
- Both failure tests verify unchanged state.
11. A service receives a batch of state requests: running, done, running, starting from queued. Only queued-to-running and running-to-done are allowed. Specify an audit record for each request and the final state.
Show hint
Record the prior state, requested state, acceptance and resulting state before processing the next request.
Show worked solution
The first record is queued → running, accepted, resulting in running. The second is running → done, accepted, resulting in done. The third is done → running, rejected, resulting in done. Recording both the request and result makes the rejection visible without silently changing the terminal state.
Check your solution
- The records contain two acceptances followed by one rejection.
- The third record and the final state both retain done.
12. A workflow must allow retry after failure, but done remains terminal. Propose an additional state and two allowed transitions, then identify one forbidden transition.
Show hint
Model failure as a state, not a hidden Boolean.
Show worked solution
Introduce failed with running-to-failed on error and failed-to-queued on retry. Keep done terminal, so done-to-queued remains forbidden. Document who requests retry and test that a rejected transition preserves its source state.
Check your solution
- The model includes running-to-failed and failed-to-queued.
- A done-to-queued request is rejected without changing done.
13. Explain why an enum declaration alone does not enforce a state machine’s allowed transitions.
Show hint
Named values and transition rules are separate design elements.
Show worked solution
The enum names possible values, but code can still assign one state directly to another unless the interface restricts it. Define an initial state, permitted edges, and rejection behavior in the class or transition function.
Check your solution
- Allowed edges are documented
- Rejected transitions preserve the stated invariant
14. A parser returns an empty optional for code 9. What must the caller do before accessing a value?
Show hint
Check whether the optional contains a state.
Show worked solution
Test has_value() or the optional’s Boolean condition first. For code 9, handle the invalid-code path without dereferencing the empty optional. A successful code should be tested independently.
Check your solution
- The optional is checked
- No absent value is accessed
15. A formatter maps Status::running to "running". Why keep this separate from external-code parsing?
Show hint
Display labels and input encodings have different contracts.
Show worked solution
A formatter translates an internal state to display text, while a parser validates external representations. Separating them makes each mapping explicit and avoids silently coupling a wire code to a label.
Check your solution
- The formatter produces running
- The parser validates external codes separately
16. A configuration uses Mode::fast as a bit flag and tries Mode::fast | Mode::safe. Why might this fail, and what design decision comes first?
Show hint
Scoped enums do not automatically provide bitwise operators.
Show worked solution
A scoped enum does not implicitly convert to its underlying integer for built-in bitwise operations. First decide whether combinations are meaningful; if so define type-safe operators with an appropriate underlying representation and validate unsupported bits. Otherwise represent the choices as mutually exclusive modes.
Check your solution
- The explanation identifies the missing implicit conversion or operator.
- The design explicitly permits or rejects combinations.
17. A function receives Status by value and updates its parameter. The caller expects its own state to change. Explain the observation and propose a clear interface.
Show hint
A by-value parameter is a separate object.
Show worked solution
Changing the parameter changes its local copy, leaving the caller’s object unchanged. Either return the new Status and assign it at the call site, or pass a non-const reference when mutation is intentional; test the chosen contract at the caller.
Check your solution
- The original object is unchanged by the by-value function.
- The proposed interface makes mutation or returned state explicit.
18. A parser accepts code 1 but the existing state must remain unchanged for invalid code 8. Design a test.
Show hint
Parse and validate before assigning the state.
Show worked solution
Initialize the state to queued, parse 8, and expect failure or an empty optional. Assert that the state is still queued. Also test code 1 independently as an accepted mapping.
Check your solution
- Code 8 is rejected
- The existing state remains queued
19. Explain why default cout output 1 for a true enum comparison is not the enum’s external code.
Show hint
The comparison yields a Boolean, not the original enum value.
Show worked solution
state == Status::queued produces bool true, which default stream formatting displays as 1. That displayed Boolean must not be mistaken for a documented numeric encoding of Status::queued.
Check your solution
- The comparison result is bool
- The displayed 1 is not automatically a wire code
20. Choose tests for a three-state workflow with queued, running, and done.
Show hint
Cover accepted and rejected transitions.
Show worked solution
Test queued-to-running and running-to-done success, direct queued-to-done rejection, and an attempted advance from done. Assert both status and resulting state on each path, including preservation after rejection.
Check your solution
- Both valid edges succeed
- Both rejected edges preserve their prior states
Key takeaways
- Scoped enumerations give names and a distinct type to choices.
- Validate external values and transitions explicitly.
- An enum names values but does not automatically enforce a transition graph.
- An explicit cast from external input is not domain validation.
- A rejected transition should preserve state when the contract promises it.