wae_testing/environment/builder.rs
1//! 测试环境构建器模块
2
3use super::{AsyncTestLifecycleHook, TestEnv, TestEnvConfig, TestLifecycleHook, TestServiceConfig};
4
5/// 测试环境构建器
6///
7/// 提供流式 API 构建测试环境,支持配置、生命周期钩子和多服务设置。
8pub struct TestEnvBuilder {
9 config: TestEnvConfig,
10 lifecycle_hooks: Vec<Box<dyn TestLifecycleHook>>,
11 async_lifecycle_hooks: Vec<Box<dyn AsyncTestLifecycleHook>>,
12 services: Vec<TestServiceConfig>,
13}
14
15impl TestEnvBuilder {
16 /// 创建新的构建器
17 ///
18 /// # Examples
19 ///
20 /// ```
21 /// use wae_testing::TestEnvBuilder;
22 ///
23 /// let builder = TestEnvBuilder::new();
24 /// ```
25 pub fn new() -> Self {
26 Self {
27 config: TestEnvConfig::default(),
28 lifecycle_hooks: Vec::new(),
29 async_lifecycle_hooks: Vec::new(),
30 services: Vec::new(),
31 }
32 }
33
34 /// 设置环境名称
35 ///
36 /// # Examples
37 ///
38 /// ```
39 /// use wae_testing::TestEnvBuilder;
40 ///
41 /// let env = TestEnvBuilder::new().name("my_test_env").build();
42 /// ```
43 pub fn name(mut self, name: impl Into<String>) -> Self {
44 self.config.name = name.into();
45 self
46 }
47
48 /// 启用日志
49 ///
50 /// # Examples
51 ///
52 /// ```
53 /// use wae_testing::TestEnvBuilder;
54 ///
55 /// let env = TestEnvBuilder::new().with_logging(false).build();
56 /// ```
57 pub fn with_logging(mut self, enable: bool) -> Self {
58 self.config.enable_logging = enable;
59 self
60 }
61
62 /// 启用追踪
63 ///
64 /// # Examples
65 ///
66 /// ```
67 /// use wae_testing::TestEnvBuilder;
68 ///
69 /// let env = TestEnvBuilder::new().with_tracing(true).build();
70 /// ```
71 pub fn with_tracing(mut self, enable: bool) -> Self {
72 self.config.enable_tracing = enable;
73 self
74 }
75
76 /// 设置超时时间
77 ///
78 /// # Examples
79 ///
80 /// ```
81 /// use std::time::Duration;
82 /// use wae_testing::TestEnvBuilder;
83 ///
84 /// let env = TestEnvBuilder::new().timeout(Duration::from_secs(60)).build();
85 /// ```
86 pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
87 self.config.default_timeout = timeout;
88 self
89 }
90
91 /// 添加自定义配置
92 ///
93 /// # Examples
94 ///
95 /// ```
96 /// use wae_testing::TestEnvBuilder;
97 ///
98 /// let env = TestEnvBuilder::new().custom("key", "value").build();
99 /// ```
100 pub fn custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
101 self.config.custom.insert(key.into(), value.into());
102 self
103 }
104
105 /// 添加同步生命周期钩子
106 ///
107 /// # Examples
108 ///
109 /// ```
110 /// use wae_testing::{TestEnv, TestEnvBuilder, TestLifecycleHook};
111 ///
112 /// struct MyHook;
113 ///
114 /// impl TestLifecycleHook for MyHook {}
115 ///
116 /// let env = TestEnvBuilder::new().with_lifecycle_hook(MyHook).build();
117 /// ```
118 pub fn with_lifecycle_hook<H>(mut self, hook: H) -> Self
119 where
120 H: TestLifecycleHook + 'static,
121 {
122 self.lifecycle_hooks.push(Box::new(hook));
123 self
124 }
125
126 /// 添加异步生命周期钩子
127 pub fn with_async_lifecycle_hook<H>(mut self, hook: H) -> Self
128 where
129 H: AsyncTestLifecycleHook + 'static,
130 {
131 self.async_lifecycle_hooks.push(Box::new(hook));
132 self
133 }
134
135 /// 添加测试服务配置
136 ///
137 /// 向构建器中添加一个服务配置,构建时会自动注册到测试环境。
138 ///
139 /// # Examples
140 ///
141 /// ```
142 /// use wae_testing::{TestEnvBuilder, TestServiceConfig};
143 ///
144 /// let env = TestEnvBuilder::new().with_service(TestServiceConfig::new("database")).build();
145 /// ```
146 pub fn with_service(mut self, service: TestServiceConfig) -> Self {
147 self.services.push(service);
148 self
149 }
150
151 /// 批量添加测试服务配置
152 ///
153 /// 向构建器中添加多个服务配置。
154 pub fn with_services<I>(mut self, services: I) -> Self
155 where
156 I: IntoIterator<Item = TestServiceConfig>,
157 {
158 self.services.extend(services);
159 self
160 }
161
162 /// 构建测试环境
163 ///
164 /// 使用当前配置构建并返回测试环境实例。
165 ///
166 /// # Examples
167 ///
168 /// ```
169 /// use wae_testing::TestEnvBuilder;
170 ///
171 /// let env = TestEnvBuilder::new().build();
172 /// ```
173 pub fn build(self) -> TestEnv {
174 let env = TestEnv::new(self.config);
175
176 for hook in self.lifecycle_hooks {
177 env.lifecycle_hooks.write().push(hook);
178 }
179
180 for hook in self.async_lifecycle_hooks {
181 env.async_lifecycle_hooks.write().push(hook);
182 }
183
184 for service in self.services {
185 env.add_service(service);
186 }
187
188 env
189 }
190}
191
192impl Default for TestEnvBuilder {
193 fn default() -> Self {
194 Self::new()
195 }
196}