Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3d23ad95 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "init_flags small fixup"

parents 514191e1 1ac7a676
Loading
Loading
Loading
Loading
+36 −3
Original line number Diff line number Diff line
@@ -35,6 +35,20 @@ macro_rules! test_value {
    };
}

#[cfg(test)]
macro_rules! call_getter_fn {
    ($flag:ident) => {
        paste! {
            [<$flag _is_enabled>]()
        }
    };
    ($flag:ident $type:ty) => {
        paste! {
            [<get_ $flag>]()
        }
    };
}

macro_rules! create_getter_fn {
    ($flag:ident) => {
        paste! {
@@ -96,7 +110,9 @@ macro_rules! init_flags {

                    match values[0] {
                        $(concat!("INIT_", stringify!($flag)) =>
                            init_flags.$flag = values[1].parse().unwrap_or(default_value!($($type)? $(= $default)?)),)*
                            init_flags.$flag = values[1].parse().unwrap_or_else(|e| {
                                error!("Parse failure on '{}': {}", flag, e);
                                default_value!($($type)? $(= $default)?)}),)*
                        $($extra_flag => $extra_flag_fn(&mut init_flags, values $(, $extra_args)*),)*
                        _ => error!("Unsaved flag: {} = {}", values[0], values[1])
                    }
@@ -135,6 +151,23 @@ macro_rules! init_flags {
        }

        $(create_getter_fn!($flag $($type)?);)*

        #[cfg(test)]
        mod tests_autogenerated {
            use super::*;
            $(paste! {
                #[test]
                pub fn [<test_get_ $flag>]() {
                    let _guard = tests::ASYNC_LOCK.lock().unwrap();
                    tests::test_load(vec![
                        &*format!(concat!(concat!("INIT_", stringify!($flag)), "={}"), test_value!($($type)?))
                    ]);
                    let get_value = call_getter_fn!($flag $($type)?);
                    drop(_guard); // Prevent poisonning other tests if a panic occurs
                    assert_eq!(get_value, test_value!($($type)?));
                }
            })*
        }
    }
}

@@ -229,10 +262,10 @@ mod tests {
    lazy_static! {
        /// do not run concurrent tests as they all use the same global init_flag struct and
        /// accessor
        static ref ASYNC_LOCK: Mutex<bool> = Mutex::new(false);
        pub(super) static ref ASYNC_LOCK: Mutex<bool> = Mutex::new(false);
    }

    fn test_load(raw_flags: Vec<&str>) {
    pub(super) fn test_load(raw_flags: Vec<&str>) {
        let raw_flags = raw_flags.into_iter().map(|x| x.to_string()).collect();
        load(raw_flags);
    }