iTranslated by AI
Property-based Testing for an Electric Kettle (3): Modeling State Transitions for Boiling and Keeping Warm
Introduction
Following the article Trying out Property-based testing for a Hot Water Pot (2) Dispensing Hot (Cold) Water, I am continuing to explore Property-based testing using fast-check.
As the subject matter, I decided to use the "Hot Water Pot Requirements Specification (GOMA-1015 Type) 7th Edition", which is specified as the test base in the Test Design Contest U-30 Class.
It seems that fast-check also has an implementation for finding failing test cases by random-walking through states. I'm going to start trying out this feature while considering state transitions.
Preparation
I was looking for a good tool to represent state transitions and found a JavaScript library called state-machine-cat. Let's use this to think through the state transitions while visualizing them.
State Transitions
States up to the Previous Article
Source code in state-machine-cat format
- Consider the transition
initialize => offas the behavior of theconstructor().- Set
_water = 0.
- Set
- Power on (
plugIn()) / off (plugOff()) moves between the following states depending on the lid's status:- If the lid is closed:
off <-> idle - If the lid is open:
open/off <-> open/on
- If the lid is closed:
- Similarly, the lid's status moves between the following states depending on power on (
plugIn()) / off (plugOff()):- If power is on:
idle <-> open/on - If power is off:
off <-> open/off
- If power is on:
- Regarding the act of opening the lid (
open()):- Separate the states for when the lid is open while power is on and when it is open while power is off.
- The
idlestate is when the power is on and the lid is closed. - The dispensing function (
dispense()) only activates when the power is on and the lid is closed.- At that time, the water inside decreases at a speed of 10 ml / sec.
- Filling (
fill()) can be done regardless of power status as long as the lid is open (open/offoropen/on).- In this case, an arbitrary amount of water (
water) can be added to the water inside.
- In this case, an arbitrary amount of water (

Version with Boiling/Keep-Warm States Added
Source code in state-machine-cat format
The difference from before is here:

- In the
offstate, initialize the water temperature (_temperature) to room temperature, 25°C. - In the
idlestate, if the water volume reaches 10 ml or more, it automatically transitions to theboil(boiling) state. - If the lid is opened while the power is on (
open/on), the water temperature (_temperature) drops to room temperature, 25°C. - The
boilandkeep(keep-warm) states are collectively expressed asactive; turning the power off (plugOff) transitions back to theoffstate. - Opening the lid while in the
activestate (open/on) drops the water temperature (_temperature) to room temperature, 25°C. - The
activestate consists of two states:boilandkeep.- In the
boilstate,_startis initialized to the current time, and the water temperature (_temperature) increases by 1.25°C every second. - In the
keepstate, the water temperature (_temperature) is always maintained at 100°C. - Pressing the reboil (
reboil()) button in thekeepstate transitions back to theboilstate. - Pressing the dispense (
dispense()) button in thekeepstate dispenses hot water.- The hot water inside decreases at a speed of 10 ml / sec.
- If the hot water volume drops to 10 ml or less while in the
keepstate, it transitions to theidlestate.
- In the

Finally
Next, I would like to align the implementation with these state transitions rather than the tests.
Also, if there are any mistakes in the code, Pull requests are very welcome.
This turned out to be quite long, but thank you very much for reading to the end.
Discussion