yash_env/test_helper/function.rs
1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2025 WATANABE Yuki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17//! Test helpers for the [`function`](crate::function) module.
18
19use crate::Env;
20use crate::function::{FunctionBody, FunctionBodyObject};
21use std::rc::Rc;
22
23/// A stub implementation of [`FunctionBody`] that panics on use.
24#[derive(Clone, Debug, Default)]
25pub struct FunctionBodyStub;
26
27impl FunctionBodyStub {
28 /// Creates a new [`FunctionBodyStub`].
29 #[inline(always)]
30 #[must_use]
31 pub fn new() -> Self {
32 Self
33 }
34
35 /// Creates a new [`FunctionBodyStub`] contained in an [`Rc`].
36 ///
37 /// Suitable for passing to [`crate::function::Function::new`].
38 #[inline]
39 #[must_use]
40 pub fn rc_dyn<S>() -> Rc<dyn FunctionBodyObject<S>> {
41 Rc::new(Self::new())
42 }
43}
44
45/// Always panics when formatted.
46impl std::fmt::Display for FunctionBodyStub {
47 fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 unreachable!("unexpected call to FunctionBodyStub::fmt")
49 }
50}
51
52/// Always panics when executed.
53impl<S> FunctionBody<S> for FunctionBodyStub {
54 async fn execute(&self, _: &mut Env<S>) -> crate::semantics::Result {
55 unreachable!("unexpected call to FunctionBodyStub::execute")
56 }
57}