niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use proptest::prelude::*; |
| 2 | use proptest_derive::Arbitrary; |
| 3 | |
| 4 | use super::*; |
| 5 | |
| 6 | fn create_thumbnail() -> Thumbnail { |
| 7 | Thumbnail { |
| 8 | id: MappedId::next(), |
| 9 | timestamp: None, |
| 10 | on_current_output: false, |
| 11 | on_current_workspace: false, |
| 12 | app_id: None, |
| 13 | size: Size::new(100, 100), |
| 14 | clock: Clock::with_time(Duration::ZERO), |
| 15 | config: niri_config::MruPreviews::default(), |
| 16 | open_animation: None, |
| 17 | move_animation: None, |
| 18 | title_texture: Default::default(), |
| 19 | background: RefCell::new(FocusRing::new(Default::default())), |
| 20 | border: RefCell::new(FocusRing::new(Default::default())), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn remove_last_window_out_of_two() { |
| 26 | let ops = [Op::Backward, Op::Remove(1)]; |
| 27 | |
| 28 | let thumbnails = vec![create_thumbnail(), create_thumbnail()]; |
| 29 | let current_id = thumbnails.first().map(|t| t.id); |
| 30 | let mut mru = WindowMru { |
| 31 | thumbnails, |
| 32 | current_id, |
| 33 | scope: MruScope::All, |
| 34 | app_id_filter: None, |
| 35 | }; |
| 36 | |
| 37 | check_ops(&mut mru, &ops); |
| 38 | } |
| 39 | |
| 40 | fn arbitrary_scope() -> impl Strategy<Value = MruScope> { |
| 41 | prop_oneof![ |
| 42 | Just(MruScope::All), |
| 43 | Just(MruScope::Output), |
| 44 | Just(MruScope::Workspace), |
| 45 | ] |
| 46 | } |
| 47 | |
| 48 | fn arbitrary_filter() -> impl Strategy<Value = MruFilter> { |
| 49 | prop_oneof![Just(MruFilter::All), Just(MruFilter::AppId)] |
| 50 | } |
| 51 | |
| 52 | fn arbitrary_app_id() -> impl Strategy<Value = Option<String>> { |
| 53 | prop_oneof![Just(None), Just(Some(1)), Just(Some(2))] |
| 54 | .prop_map(|id| id.map(|id| format!("app-{id}"))) |
| 55 | } |
| 56 | |
| 57 | prop_compose! { |
| 58 | fn arbitrary_thumbnail()( |
| 59 | timestamp: Option<Duration>, |
| 60 | on_current_output: bool, |
| 61 | on_current_workspace: bool, |
| 62 | app_id in arbitrary_app_id(), |
| 63 | ) -> Thumbnail { |
| 64 | let mut thumbnail = create_thumbnail(); |
| 65 | thumbnail.timestamp = timestamp; |
| 66 | thumbnail.on_current_workspace = on_current_workspace; |
| 67 | thumbnail.on_current_output = on_current_output; |
| 68 | thumbnail.app_id = app_id; |
| 69 | thumbnail |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | prop_compose! { |
| 74 | fn arbitrary_mru()( |
| 75 | thumbnails in proptest::collection::vec(arbitrary_thumbnail(), 1..10), |
| 76 | ) -> WindowMru { |
| 77 | let current_id = thumbnails.first().map(|t| t.id); |
| 78 | WindowMru { |
| 79 | thumbnails, |
| 80 | current_id, |
| 81 | scope: MruScope::All, |
| 82 | app_id_filter: None, |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[derive(Debug, Clone, Arbitrary)] |
| 88 | enum Op { |
| 89 | Forward, |
| 90 | Backward, |
| 91 | First, |
| 92 | Last, |
| 93 | SetScope(#[proptest(strategy = "arbitrary_scope()")] MruScope), |
| 94 | SetFilter(#[proptest(strategy = "arbitrary_filter()")] MruFilter), |
| 95 | Remove(#[proptest(strategy = "1..10usize")] usize), |
| 96 | } |
| 97 | |
| 98 | impl Op { |
| 99 | fn apply(&self, mru: &mut WindowMru) { |
| 100 | match self { |
| 101 | Op::Forward => mru.forward(), |
| 102 | Op::Backward => mru.backward(), |
| 103 | Op::First => mru.first(), |
| 104 | Op::Last => mru.last(), |
| 105 | Op::SetScope(scope) => { |
| 106 | mru.set_scope(*scope); |
| 107 | } |
| 108 | Op::SetFilter(filter) => { |
| 109 | mru.set_filter(*filter); |
| 110 | } |
| 111 | Op::Remove(idx) => { |
| 112 | if *idx < mru.thumbnails.len() { |
| 113 | mru.remove_by_idx(*idx); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn check_ops(mru: &mut WindowMru, ops: &[Op]) { |
| 121 | for op in ops { |
| 122 | op.apply(mru); |
| 123 | mru.verify_invariants(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | proptest! { |
| 128 | #[test] |
| 129 | fn random_operations_dont_panic( |
| 130 | mut mru in arbitrary_mru(), |
| 131 | ops: Vec<Op>, |
| 132 | ) { |
| 133 | check_ops(&mut mru, &ops); |
| 134 | } |
| 135 | } |