iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌟

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 => off as the behavior of the constructor().
    • Set _water = 0.
  • 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
  • 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
  • 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 idle state 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/off or open/on).
    • In this case, an arbitrary amount of water (water) can be added to the water inside.

image.png

Version with Boiling/Keep-Warm States Added

Source code in state-machine-cat format
The difference from before is here:
image.png

  • In the off state, initialize the water temperature (_temperature) to room temperature, 25°C.
  • In the idle state, if the water volume reaches 10 ml or more, it automatically transitions to the boil (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 boil and keep (keep-warm) states are collectively expressed as active; turning the power off (plugOff) transitions back to the off state.
  • Opening the lid while in the active state (open/on) drops the water temperature (_temperature) to room temperature, 25°C.
  • The active state consists of two states: boil and keep.
    • In the boil state, _start is initialized to the current time, and the water temperature (_temperature) increases by 1.25°C every second.
    • In the keep state, the water temperature (_temperature) is always maintained at 100°C.
    • Pressing the reboil (reboil()) button in the keep state transitions back to the boil state.
    • Pressing the dispense (dispense()) button in the keep state 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 keep state, it transitions to the idle state.

image.png

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