summaryrefslogtreecommitdiffstats
path: root/src/testutil/process_test.rs
blob: 5c26308c5a81a33d1a5bc578d7b7efff2f8ceeff (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
use std::{path::PathBuf, sync::Arc};

use parking_lot::Mutex;

use crate::{
	display::Size,
	input::Event,
	module::{self, ModuleHandler},
	process::Process,
	runtime::ThreadStatuses,
	test_helpers::{with_event_handler, EventHandlerTestContext},
	testutil::{with_search, SearchTestContext},
	todo_file::testutil::with_todo_file,
	view::testutil::{with_view_state, TestContext as ViewContext},
};

pub(crate) struct TestContext<ModuleProvider: module::ModuleProvider + Send + 'static> {
	pub(crate) event_handler_context: EventHandlerTestContext,
	pub(crate) process: Process<ModuleProvider>,
	pub(crate) search_context: SearchTestContext,
	pub(crate) todo_file_path: PathBuf,
	pub(crate) view_context: ViewContext,
}

pub(crate) fn process_test<C, ModuleProvider: module::ModuleProvider + Send + 'static>(
	module_handler: ModuleHandler<ModuleProvider>,
	callback: C,
) where
	C: FnOnce(TestContext<ModuleProvider>),
{
	with_event_handler(&[Event::from('a')], |event_handler_context| {
		with_view_state(|view_context| {
			with_todo_file(&[], |todo_file_context| {
				with_search(|search_context| {
					let (todo_file_tmp_path, todo_file) = todo_file_context.to_owned();
					let view_state = view_context.state.clone();
					let input_state = event_handler_context.state.clone();
					let todo_file_path = PathBuf::from(todo_file_tmp_path.path());

					callback(TestContext {
						event_handler_context,
						process: Process::new(
							Size::new(300, 120),
							Arc::new(Mutex::new(todo_file)),
							module_handler,
							input_state,
							view_state,
							search_context.state.clone(),
							ThreadStatuses::new(),
						),
						search_context,
						todo_file_path,
						view_context,
					});
				});
			});
		});
	});
}