astrelis-test-utils 0.2.1

Test utilities for Astrelis projects
Documentation
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! GPU resource wrappers that can be real or mock.
//!
//! These types wrap WGPU resources and allow for both real GPU operations
//! and mock implementations for testing.

use wgpu;

/// Wrapper around GPU buffer that can be real or mock.
///
/// # Design Pattern: Opaque Wrapper
///
/// This type hides whether it contains a real `wgpu::Buffer` or a mock.
/// Users hold owned `GpuBuffer`, which is cheap to clone (Arc inside).
///
/// # Benefits
/// 1. No lifetimes - users own the buffer
/// 2. Can be mock or real without user knowing
/// 3. Clone is cheap (Arc internally for real buffers)
#[derive(Clone, Debug)]
pub struct GpuBuffer {
    inner: GpuBufferInner,
}

#[derive(Clone, Debug)]
enum GpuBufferInner {
    Real(wgpu::Buffer),
    #[cfg(feature = "mock")]
    Mock { id: usize, size: u64 },
}

impl GpuBuffer {
    /// Create from real WGPU buffer
    pub fn from_wgpu(buffer: wgpu::Buffer) -> Self {
        Self {
            inner: GpuBufferInner::Real(buffer),
        }
    }

    /// Create mock buffer (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize, size: u64) -> Self {
        Self {
            inner: GpuBufferInner::Mock { id, size },
        }
    }

    /// Get the underlying wgpu::Buffer (if real)
    ///
    /// # Panics
    /// Panics if this is a mock buffer (test code should never call this)
    pub fn as_wgpu(&self) -> &wgpu::Buffer {
        match &self.inner {
            GpuBufferInner::Real(buffer) => buffer,
            #[cfg(feature = "mock")]
            GpuBufferInner::Mock { .. } => {
                panic!("Attempted to get wgpu::Buffer from mock buffer - this is a test-only buffer")
            }
        }
    }

    /// Check if this is a mock (useful in tests)
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuBufferInner::Mock { .. })
    }

    /// Get mock ID (for test assertions)
    #[cfg(feature = "mock")]
    pub fn mock_id(&self) -> Option<usize> {
        match &self.inner {
            GpuBufferInner::Mock { id, .. } => Some(*id),
            _ => None,
        }
    }
}

/// Wrapper around GPU texture that can be real or mock.
#[derive(Clone, Debug)]
pub struct GpuTexture {
    inner: GpuTextureInner,
}

#[derive(Clone, Debug)]
enum GpuTextureInner {
    Real(wgpu::Texture),
    #[cfg(feature = "mock")]
    Mock {
        id: usize,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
    },
}

impl GpuTexture {
    /// Create from real WGPU texture
    pub fn from_wgpu(texture: wgpu::Texture) -> Self {
        Self {
            inner: GpuTextureInner::Real(texture),
        }
    }

    /// Create mock texture (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize, width: u32, height: u32, format: wgpu::TextureFormat) -> Self {
        Self {
            inner: GpuTextureInner::Mock {
                id,
                width,
                height,
                format,
            },
        }
    }

    /// Get the underlying wgpu::Texture (if real)
    ///
    /// # Panics
    /// Panics if this is a mock texture
    pub fn as_wgpu(&self) -> &wgpu::Texture {
        match &self.inner {
            GpuTextureInner::Real(texture) => texture,
            #[cfg(feature = "mock")]
            GpuTextureInner::Mock { .. } => {
                panic!("Attempted to get wgpu::Texture from mock texture")
            }
        }
    }

    /// Check if this is a mock
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuTextureInner::Mock { .. })
    }

    /// Get mock ID (for test assertions)
    #[cfg(feature = "mock")]
    pub fn mock_id(&self) -> Option<usize> {
        match &self.inner {
            GpuTextureInner::Mock { id, .. } => Some(*id),
            _ => None,
        }
    }
}

/// Wrapper around GPU shader module that can be real or mock.
#[derive(Clone, Debug)]
pub struct GpuShaderModule {
    inner: GpuShaderModuleInner,
}

#[derive(Clone, Debug)]
enum GpuShaderModuleInner {
    Real(wgpu::ShaderModule),
    #[cfg(feature = "mock")]
    Mock { id: usize },
}

impl GpuShaderModule {
    /// Create from real WGPU shader module
    pub fn from_wgpu(module: wgpu::ShaderModule) -> Self {
        Self {
            inner: GpuShaderModuleInner::Real(module),
        }
    }

    /// Create mock shader module (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize) -> Self {
        Self {
            inner: GpuShaderModuleInner::Mock { id },
        }
    }

    /// Get the underlying wgpu::ShaderModule (if real)
    pub fn as_wgpu(&self) -> &wgpu::ShaderModule {
        match &self.inner {
            GpuShaderModuleInner::Real(module) => module,
            #[cfg(feature = "mock")]
            GpuShaderModuleInner::Mock { .. } => {
                panic!("Attempted to get wgpu::ShaderModule from mock")
            }
        }
    }

    /// Check if this is a mock
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuShaderModuleInner::Mock { .. })
    }
}

/// Wrapper around GPU render pipeline that can be real or mock.
#[derive(Clone, Debug)]
pub struct GpuRenderPipeline {
    inner: GpuRenderPipelineInner,
}

#[derive(Clone, Debug)]
enum GpuRenderPipelineInner {
    Real(wgpu::RenderPipeline),
    #[cfg(feature = "mock")]
    Mock { id: usize },
}

impl GpuRenderPipeline {
    /// Create from real WGPU render pipeline
    pub fn from_wgpu(pipeline: wgpu::RenderPipeline) -> Self {
        Self {
            inner: GpuRenderPipelineInner::Real(pipeline),
        }
    }

    /// Create mock render pipeline (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize) -> Self {
        Self {
            inner: GpuRenderPipelineInner::Mock { id },
        }
    }

    /// Get the underlying wgpu::RenderPipeline (if real)
    pub fn as_wgpu(&self) -> &wgpu::RenderPipeline {
        match &self.inner {
            GpuRenderPipelineInner::Real(pipeline) => pipeline,
            #[cfg(feature = "mock")]
            GpuRenderPipelineInner::Mock { .. } => {
                panic!("Attempted to get wgpu::RenderPipeline from mock")
            }
        }
    }

    /// Check if this is a mock
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuRenderPipelineInner::Mock { .. })
    }
}

/// Wrapper around GPU compute pipeline that can be real or mock.
#[derive(Clone, Debug)]
pub struct GpuComputePipeline {
    inner: GpuComputePipelineInner,
}

#[derive(Clone, Debug)]
enum GpuComputePipelineInner {
    Real(wgpu::ComputePipeline),
    #[cfg(feature = "mock")]
    Mock { id: usize },
}

impl GpuComputePipeline {
    /// Create from real WGPU compute pipeline
    pub fn from_wgpu(pipeline: wgpu::ComputePipeline) -> Self {
        Self {
            inner: GpuComputePipelineInner::Real(pipeline),
        }
    }

    /// Create mock compute pipeline (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize) -> Self {
        Self {
            inner: GpuComputePipelineInner::Mock { id },
        }
    }

    /// Get the underlying wgpu::ComputePipeline (if real)
    pub fn as_wgpu(&self) -> &wgpu::ComputePipeline {
        match &self.inner {
            GpuComputePipelineInner::Real(pipeline) => pipeline,
            #[cfg(feature = "mock")]
            GpuComputePipelineInner::Mock { .. } => {
                panic!("Attempted to get wgpu::ComputePipeline from mock")
            }
        }
    }

    /// Check if this is a mock
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuComputePipelineInner::Mock { .. })
    }
}

/// Wrapper around GPU bind group layout that can be real or mock.
#[derive(Clone, Debug)]
pub struct GpuBindGroupLayout {
    inner: GpuBindGroupLayoutInner,
}

#[derive(Clone, Debug)]
enum GpuBindGroupLayoutInner {
    Real(wgpu::BindGroupLayout),
    #[cfg(feature = "mock")]
    Mock { id: usize },
}

impl GpuBindGroupLayout {
    /// Create from real WGPU bind group layout
    pub fn from_wgpu(layout: wgpu::BindGroupLayout) -> Self {
        Self {
            inner: GpuBindGroupLayoutInner::Real(layout),
        }
    }

    /// Create mock bind group layout (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize) -> Self {
        Self {
            inner: GpuBindGroupLayoutInner::Mock { id },
        }
    }

    /// Get the underlying wgpu::BindGroupLayout (if real)
    pub fn as_wgpu(&self) -> &wgpu::BindGroupLayout {
        match &self.inner {
            GpuBindGroupLayoutInner::Real(layout) => layout,
            #[cfg(feature = "mock")]
            GpuBindGroupLayoutInner::Mock { .. } => {
                panic!("Attempted to get wgpu::BindGroupLayout from mock")
            }
        }
    }

    /// Check if this is a mock
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuBindGroupLayoutInner::Mock { .. })
    }
}

/// Wrapper around GPU bind group that can be real or mock.
#[derive(Clone, Debug)]
pub struct GpuBindGroup {
    inner: GpuBindGroupInner,
}

#[derive(Clone, Debug)]
enum GpuBindGroupInner {
    Real(wgpu::BindGroup),
    #[cfg(feature = "mock")]
    Mock { id: usize },
}

impl GpuBindGroup {
    /// Create from real WGPU bind group
    pub fn from_wgpu(bind_group: wgpu::BindGroup) -> Self {
        Self {
            inner: GpuBindGroupInner::Real(bind_group),
        }
    }

    /// Create mock bind group (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize) -> Self {
        Self {
            inner: GpuBindGroupInner::Mock { id },
        }
    }

    /// Get the underlying wgpu::BindGroup (if real)
    pub fn as_wgpu(&self) -> &wgpu::BindGroup {
        match &self.inner {
            GpuBindGroupInner::Real(bind_group) => bind_group,
            #[cfg(feature = "mock")]
            GpuBindGroupInner::Mock { .. } => {
                panic!("Attempted to get wgpu::BindGroup from mock")
            }
        }
    }

    /// Check if this is a mock
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuBindGroupInner::Mock { .. })
    }
}

/// Wrapper around GPU sampler that can be real or mock.
#[derive(Clone, Debug)]
pub struct GpuSampler {
    inner: GpuSamplerInner,
}

#[derive(Clone, Debug)]
enum GpuSamplerInner {
    Real(wgpu::Sampler),
    #[cfg(feature = "mock")]
    Mock { id: usize },
}

impl GpuSampler {
    /// Create from real WGPU sampler
    pub fn from_wgpu(sampler: wgpu::Sampler) -> Self {
        Self {
            inner: GpuSamplerInner::Real(sampler),
        }
    }

    /// Create mock sampler (for testing)
    #[cfg(feature = "mock")]
    pub fn mock(id: usize) -> Self {
        Self {
            inner: GpuSamplerInner::Mock { id },
        }
    }

    /// Get the underlying wgpu::Sampler (if real)
    pub fn as_wgpu(&self) -> &wgpu::Sampler {
        match &self.inner {
            GpuSamplerInner::Real(sampler) => sampler,
            #[cfg(feature = "mock")]
            GpuSamplerInner::Mock { .. } => {
                panic!("Attempted to get wgpu::Sampler from mock")
            }
        }
    }

    /// Check if this is a mock
    #[cfg(feature = "mock")]
    pub fn is_mock(&self) -> bool {
        matches!(self.inner, GpuSamplerInner::Mock { .. })
    }
}