• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

veeso / tui-realm / 20410740724

21 Dec 2025 01:45PM UTC coverage: 92.022% (-0.08%) from 92.106%
20410740724

Pull #133

github

web-flow
Merge d2b106970 into 36f357c61
Pull Request #133: Change `Component::on` parameter `Event` to be a reference

9 of 10 new or added lines in 3 files covered. (90.0%)

5 existing lines in 2 files now uncovered.

5479 of 5954 relevant lines covered (92.02%)

6.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.79
/src/mock/components.rs
1
//! # Components
2
//!
3
//! mock components
4

5
use ratatui::Frame;
6

7
use super::{MockEvent, MockMsg};
8
use crate::command::{Cmd, CmdResult, Direction};
9
use crate::event::{Event, Key, KeyEvent, KeyModifiers};
10
use crate::{AttrValue, Attribute, Component, MockComponent, Props, State, StateValue};
11

12
/// Mocked component implementing `MockComponent`
13
pub struct MockInput {
14
    props: Props,
15
    states: MockInputStates,
16
}
17

18
impl Default for MockInput {
19
    fn default() -> Self {
69✔
20
        Self {
69✔
21
            props: Props::default(),
69✔
22
            states: MockInputStates::default(),
69✔
23
        }
69✔
24
    }
69✔
25
}
26

27
impl MockComponent for MockInput {
28
    fn view(&mut self, _: &mut Frame, _: crate::ratatui::layout::Rect) {}
×
29

30
    fn query(&self, attr: Attribute) -> Option<AttrValue> {
39✔
31
        self.props.get(attr)
39✔
32
    }
39✔
33

34
    fn attr(&mut self, query: Attribute, attr: AttrValue) {
58✔
35
        self.props.set(query, attr);
58✔
36
    }
58✔
37

38
    fn state(&self) -> State {
18✔
39
        State::One(StateValue::String(self.states.text.clone()))
18✔
40
    }
18✔
41

42
    fn perform(&mut self, cmd: Cmd) -> CmdResult {
16✔
43
        match cmd {
×
44
            Cmd::Move(Direction::Left) => {
45
                self.states.left();
×
46
                CmdResult::Changed(State::One(StateValue::Usize(self.states.cursor)))
×
47
            }
48
            Cmd::Move(Direction::Right) => {
49
                self.states.right();
×
50
                CmdResult::Changed(State::One(StateValue::Usize(self.states.cursor)))
×
51
            }
52
            Cmd::Type(ch) => {
4✔
53
                self.states.input(ch);
4✔
54
                CmdResult::Changed(self.state())
4✔
55
            }
56
            _ => CmdResult::None,
12✔
57
        }
58
    }
16✔
59
}
60

61
// -- component states
62

63
struct MockInputStates {
64
    text: String,
65
    cursor: usize,
66
}
67

68
impl MockInputStates {
69
    fn default() -> Self {
69✔
70
        Self {
69✔
71
            text: String::default(),
69✔
72
            cursor: 0,
69✔
73
        }
69✔
74
    }
69✔
75
}
76

77
impl MockInputStates {
78
    fn input(&mut self, c: char) {
4✔
79
        self.text.push(c);
4✔
80
    }
4✔
81

82
    fn left(&mut self) {
×
83
        if self.cursor > 0 {
×
84
            self.cursor -= 1;
×
85
        }
×
86
    }
×
87

88
    fn right(&mut self) {
×
89
        self.cursor += 1;
×
90
    }
×
91
}
92

93
// -- component impl
94

95
#[derive(MockComponent, Default)]
96
pub struct MockFooInput {
97
    component: MockInput,
98
}
99

100
impl Component<MockMsg, MockEvent> for MockFooInput {
101
    fn on(&mut self, ev: &Event<MockEvent>) -> Option<MockMsg> {
25✔
102
        let cmd = match ev {
13✔
103
            Event::Keyboard(KeyEvent {
104
                code: Key::Left,
105
                modifiers: _,
106
            }) => Cmd::Move(Direction::Left),
×
107
            Event::Keyboard(KeyEvent {
108
                code: Key::Right,
109
                modifiers: _,
110
            }) => Cmd::Move(Direction::Right),
×
111
            Event::Keyboard(KeyEvent {
112
                code: Key::Char(ch),
1✔
113
                modifiers: KeyModifiers::NONE,
114
            }) => Cmd::Type(*ch),
1✔
115
            Event::Keyboard(KeyEvent {
116
                code: Key::Enter,
117
                modifiers: KeyModifiers::NONE,
118
            }) => return Some(MockMsg::FooSubmit(self.component.states.text.clone())),
12✔
119
            _ => Cmd::None,
12✔
120
        };
121
        match self.component.perform(cmd) {
13✔
122
            CmdResult::Changed(State::One(StateValue::String(s))) => {
1✔
123
                Some(MockMsg::FooInputChanged(s))
1✔
124
            }
125
            _ => None,
12✔
126
        }
127
    }
25✔
128
}
129

130
#[derive(MockComponent, Default)]
131
pub struct MockBarInput {
132
    component: MockInput,
133
}
134

135
impl Component<MockMsg, MockEvent> for MockBarInput {
136
    fn on(&mut self, ev: &Event<MockEvent>) -> Option<MockMsg> {
12✔
137
        let cmd = match ev {
4✔
138
            Event::Keyboard(KeyEvent {
139
                code: Key::Left,
140
                modifiers: _,
141
            }) => Cmd::Move(Direction::Left),
×
142
            Event::Keyboard(KeyEvent {
143
                code: Key::Right,
144
                modifiers: _,
145
            }) => Cmd::Move(Direction::Right),
×
146
            Event::Keyboard(KeyEvent {
147
                code: Key::Char(ch),
×
148
                modifiers: KeyModifiers::NONE,
NEW
149
            }) => Cmd::Type(*ch),
×
150
            Event::Keyboard(KeyEvent {
151
                code: Key::Enter,
152
                modifiers: KeyModifiers::NONE,
153
            }) => return Some(MockMsg::BarSubmit(self.component.states.text.clone())),
4✔
154
            Event::Tick => return Some(MockMsg::BarTick),
8✔
155
            _ => Cmd::None,
×
156
        };
157
        match self.component.perform(cmd) {
×
158
            CmdResult::Changed(State::One(StateValue::String(s))) => {
×
159
                Some(MockMsg::BarInputChanged(s))
×
160
            }
161
            _ => None,
×
162
        }
163
    }
12✔
164
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc