summaryrefslogtreecommitdiffstats
path: root/src/modules/confirm_abort.rs
blob: 32c1b405df2582f4dc0c05c4062327a4aba038bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::sync::Arc;

use parking_lot::Mutex;

use crate::{
	components::confirm::{Confirm, Confirmed, INPUT_OPTIONS},
	input::{Event, InputOptions, KeyBindings},
	module::{ExitStatus, Module, State},
	process::Results,
	todo_file::TodoFile,
	view::{RenderContext, ViewData},
};

pub(crate) struct ConfirmAbort {
	dialog: Confirm,
	todo_file: Arc<Mutex<TodoFile>>,
}

impl Module for ConfirmAbort {
	fn build_view_data(&mut self, _: &RenderContext) -> &ViewData {
		self.dialog.get_view_data()
	}

	fn input_options(&self) -> &InputOptions {
		&INPUT_OPTIONS
	}

	fn read_event(&self, event: Event, key_bindings: &KeyBindings) -> Event {
		Confirm::read_event(event, key_bindings)
	}

	fn handle_event(&mut self, event: Event, _: &crate::view::State) -> Results {
		let confirmed = self.dialog.handle_event(event);
		let mut results = Results::new();
		match confirmed {
			Confirmed::Yes => {
				self.todo_file.lock().set_lines(vec![]);
				results.exit_status(ExitStatus::Good);
			},
			Confirmed::No => {
				results.state(State::List);
			},
			Confirmed::Other => {},
		}
		results
	}
}

impl ConfirmAbort {
	pub(crate) fn new(confirm_yes: &[String], confirm_no: &[String], todo_file: Arc<Mutex<TodoFile>>) -> Self {
		Self {
			dialog: Confirm::new("Are you sure you want to abort", confirm_yes, confirm_no),
			todo_file,
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{
		assert_rendered_output,
		assert_results,
		input::{KeyCode, StandardEvent},
		process::Artifact,
		testutil::module_test,
		view::testutil::AssertRenderOptions,
	};

	fn create_confirm_abort(todo_file: TodoFile) -> ConfirmAbort {
		ConfirmAbort::new(
			&[String::from("y")],
			&[String::from("n")],
			Arc::new(Mutex::new(todo_file)),
		)
	}

	#[test]
	fn build_view_data() {
		module_test(&["pick aaa comment"], &[], |mut test_context| {
			let mut module = create_confirm_abort(test_context.take_todo_file());
			let view_data = test_context.build_view_data(&mut module);
			assert_rendered_output!(
				Options AssertRenderOptions::INCLUDE_TRAILING_WHITESPACE | AssertRenderOptions::INCLUDE_STYLE,
				view_data,
				"{TITLE}",
				"{BODY}",
				"{Normal}Are you sure you want to abort (y/n)? "
			);
		});
	}
	#[test]
	fn handle_event_yes() {
		module_test(
			&["pick aaa comment"],
			&[Event::from(StandardEvent::Yes)],
			|mut test_context| {
				let mut module = create_confirm_abort(test_context.take_todo_file());
				assert_results!(
					test_context.handle_event(&mut module),
					Artifact::Event(Event::from(StandardEvent::Yes)),
					Artifact::ExitStatus(ExitStatus::Good)
				);
				assert!(module.todo_file.lock().is_empty());
			},
		);
	}

	#[test]
	fn handle_event_no() {
		module_test(
			&["pick aaa comment"],
			&[Event::from(StandardEvent::No)],
			|mut test_context| {
				let mut module = create_confirm_abort(test_context.take_todo_file());
				assert_results!(
					test_context.handle_event(&mut module),
					Artifact::Event(Event::from(StandardEvent::No)),
					Artifact::ChangeState(State::List)
				);
			},
		);
	}

	#[test]
	fn handle_event_confirmed_other() {
		module_test(
			&["pick aaa comment"],
			&[Event::from(KeyCode::Null)],
			|mut test_context| {
				let mut module = create_confirm_abort(test_context.take_todo_file());
				assert_results!(
					test_context.handle_event(&mut module),
					Artifact::Event(Event::from(KeyCode::Null))
				);
			},
		);
	}
}