Skip to content

Commit c538cda

Browse files
add view free memory action (#33)
1 parent 00f9f84 commit c538cda

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/ui_state/view_menu/view_free_memory.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@
22
The file to hold the View Free Memory class
33
"""
44

5+
from src.devices.library import Keypad
56
from src.ui_state.ui_state import UIState
67

78

89
class ViewFreeMemory(UIState):
910
"""
1011
This is a class for the ViewFreeMemory state of the Tank Controller
1112
"""
13+
14+
def __init__(self, titrator, previous_state=None):
15+
"""
16+
The constructor for the ViewFreeMemory class
17+
"""
18+
super().__init__(titrator)
19+
self.previous_state = previous_state
20+
21+
def loop(self):
22+
"""
23+
The loop function for the ViewFreeMemory class
24+
"""
25+
free_memory = "3519"
26+
self.titrator.lcd.print("Free Memory:", line=1)
27+
self.titrator.lcd.print(f"{free_memory} bytes", line=2)
28+
29+
def handle_key(self, key):
30+
"""
31+
The handle_key function for the ViewFreeMemory class
32+
"""
33+
if key in [Keypad.KEY_4, Keypad.KEY_D]:
34+
self._set_next_state(self.previous_state, True)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
the file to test the View Free Memory class
3+
"""
4+
5+
from unittest import mock
6+
7+
from src.devices.library import LiquidCrystal
8+
from src.titrator import Titrator
9+
from src.ui_state.main_menu import MainMenu
10+
from src.ui_state.ui_state import UIState
11+
from src.ui_state.view_menu.view_free_memory import ViewFreeMemory
12+
13+
14+
class MockPreviousState(UIState):
15+
"""
16+
A mock previous state for testing purposes
17+
"""
18+
19+
def __init__(self, titrator):
20+
super().__init__(titrator)
21+
22+
23+
@mock.patch.object(LiquidCrystal, "print")
24+
def test_view_free_memory(print_mock):
25+
"""
26+
The function to test ViewFreeMemory's loop function
27+
"""
28+
state = ViewFreeMemory(Titrator(), MainMenu(Titrator()))
29+
30+
state.loop()
31+
32+
print_mock.assert_any_call("Free Memory:", line=1)
33+
print_mock.assert_any_call("3519 bytes", line=2)
34+
35+
36+
def test_handle_key_4():
37+
"""
38+
The function to test the back handle key
39+
"""
40+
titrator = Titrator()
41+
42+
titrator.state = ViewFreeMemory(titrator, MockPreviousState(titrator))
43+
44+
titrator.state.handle_key("4")
45+
assert isinstance(titrator.state, MockPreviousState)
46+
47+
48+
def test_handle_key_d():
49+
"""
50+
The function to test the reset handle keys
51+
"""
52+
titrator = Titrator()
53+
54+
titrator.state = ViewFreeMemory(titrator, MockPreviousState(titrator))
55+
56+
titrator.state.handle_key("D")
57+
assert isinstance(titrator.state, MockPreviousState)

0 commit comments

Comments
 (0)