Skip to content

Commit 3441c6b

Browse files
committed
Add snapshot tests
Signed-off-by: akhilles <[email protected]>
1 parent 85a4ab3 commit 3441c6b

6 files changed

+391
-0
lines changed

crates/pcb/tests/metadata.rs

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
use pcb_test_utils::assert_snapshot;
2+
use pcb_test_utils::sandbox::Sandbox;
3+
4+
const SIMPLE_METADATA_ZEN: &str = r#"
5+
load("@stdlib:v0.2.10/units.zen", "Voltage", "Current", "Resistance")
6+
7+
# Test basic metadata container operations
8+
voltage_meta = metadata(Voltage)
9+
current_meta = metadata(Current)
10+
resistance_meta = metadata(Resistance)
11+
12+
# Test pushing values
13+
voltage_meta.push(Voltage("5V 5%"))
14+
voltage_meta.push(Voltage("3.3V 1%"))
15+
current_meta.push(Current("100mA 5%"))
16+
resistance_meta.push(Resistance("1kOhm 5%"))
17+
18+
# Test get() - returns most recent
19+
print("Latest voltage:", voltage_meta.get())
20+
print("Latest current:", current_meta.get())
21+
print("Latest resistance:", resistance_meta.get())
22+
23+
# Test list() - returns all in chronological order
24+
print("All voltages:", voltage_meta.list())
25+
print("All currents:", current_meta.list())
26+
print("All resistances:", resistance_meta.list())
27+
28+
# Test empty container
29+
empty_meta = metadata(Voltage)
30+
print("Empty get:", empty_meta.get())
31+
print("Empty list:", empty_meta.list())
32+
"#;
33+
34+
const POWER_MODULE_ZEN: &str = r#"
35+
load("@stdlib:v0.2.10/units.zen", "Voltage", "Current")
36+
37+
PowerSpec = interface(
38+
voltage_meta = metadata(Voltage),
39+
current_meta = metadata(Current),
40+
)
41+
42+
voltage_rating = config("voltage_rating", str, default = "5V")
43+
current_rating = config("current_rating", str, default = "1A")
44+
45+
SPEC = io("SPEC", PowerSpec)
46+
47+
# Store the configured ratings
48+
SPEC.voltage_meta.push(Voltage(voltage_rating))
49+
SPEC.current_meta.push(Current(current_rating))
50+
51+
print("Power module configured with:", SPEC.voltage_meta.get(), "at", SPEC.current_meta.get())
52+
"#;
53+
54+
const CROSS_MODULE_ZEN: &str = r#"
55+
load("@stdlib:v0.2.10/units.zen", "Voltage", "Current")
56+
57+
# Load the power module and access its metadata directly
58+
load("./modules/PowerModule.zen", "SPEC")
59+
60+
# Test accessing metadata across module boundary
61+
print("Loaded voltage metadata:", SPEC.voltage_meta.list())
62+
print("Loaded current metadata:", SPEC.current_meta.list())
63+
64+
# Test that we can add more values to the loaded metadata
65+
SPEC.voltage_meta.push(Voltage("24V"))
66+
SPEC.current_meta.push(Current("2A"))
67+
68+
print("Updated voltage metadata:", SPEC.voltage_meta.list())
69+
print("Updated current metadata:", SPEC.current_meta.list())
70+
"#;
71+
72+
const POWER_INTERFACE_MODULE_ZEN: &str = r#"
73+
load("@stdlib:v0.2.10/units.zen", "Voltage", "Current")
74+
75+
PowerSpec = interface(
76+
voltage_meta = metadata(Voltage),
77+
current_meta = metadata(Current),
78+
)
79+
80+
voltage_rating = config("voltage_rating", str, default = "5V")
81+
current_rating = config("current_rating", str, default = "1A")
82+
83+
POWER = io("POWER", PowerSpec)
84+
85+
# Initialize the interface metadata
86+
POWER.voltage_meta.push(Voltage(voltage_rating))
87+
POWER.current_meta.push(Current(current_rating))
88+
89+
print("Interface power module configured:", POWER.voltage_meta.get(), "at", POWER.current_meta.get())
90+
"#;
91+
92+
const INTERFACE_CROSS_MODULE_ZEN: &str = r#"
93+
load("@stdlib:v0.2.10/units.zen", "Voltage", "Current")
94+
95+
# Load the power interface module and access its interface
96+
load("./modules/PowerInterfaceModule.zen", "POWER")
97+
98+
# Test accessing interface metadata across module boundary
99+
print("Interface voltage metadata:", POWER.voltage_meta.list())
100+
print("Interface current metadata:", POWER.current_meta.list())
101+
102+
# Create another interface instance with same metadata containers (testing shared state)
103+
SharedPowerSpec = interface(
104+
voltage_meta = POWER.voltage_meta,
105+
current_meta = POWER.current_meta,
106+
)
107+
108+
shared_power = SharedPowerSpec()
109+
110+
# Add values via the shared interface
111+
shared_power.voltage_meta.push(Voltage("48V"))
112+
shared_power.current_meta.push(Current("10A"))
113+
114+
print("Updated via shared interface - voltage:", POWER.voltage_meta.list())
115+
print("Updated via shared interface - current:", POWER.current_meta.list())
116+
"#;
117+
118+
const CONDITIONAL_BEHAVIOR_ZEN: &str = r#"
119+
load("@stdlib:v0.2.10/units.zen", "Voltage")
120+
121+
# Create metadata container for tracking all voltages
122+
voltage_tracker = metadata(Voltage)
123+
124+
# Add some voltage requirements
125+
voltage_tracker.push(Voltage("3.3V"))
126+
voltage_tracker.push(Voltage("5V"))
127+
voltage_tracker.push(Voltage("12V"))
128+
129+
print("All voltages:", voltage_tracker.list())
130+
131+
# Conditional behavior based on metadata values
132+
max_voltage = 0.0
133+
high_voltage_present = False
134+
135+
for v in voltage_tracker.list():
136+
voltage_value = v.value # Access numeric value directly
137+
print("Processing voltage value:", voltage_value)
138+
139+
if voltage_value > max_voltage:
140+
max_voltage = voltage_value
141+
142+
if voltage_value > 10.0:
143+
high_voltage_present = True
144+
145+
print("Maximum voltage found:", max_voltage, "V")
146+
147+
# Design decisions based on metadata analysis
148+
if high_voltage_present:
149+
print("HIGH VOLTAGE DESIGN - adding isolation")
150+
voltage_tracker.push(Voltage("1000V")) # Add isolation requirement
151+
print("Added isolation voltage:", voltage_tracker.get())
152+
else:
153+
print("Low voltage design - standard components OK")
154+
155+
# Check total number of power domains
156+
domain_count = len(voltage_tracker.list())
157+
print("Total voltage domains:", domain_count)
158+
159+
if domain_count > 3:
160+
print("COMPLEX POWER DESIGN")
161+
print("Consider power management IC")
162+
else:
163+
print("Simple power design")
164+
165+
print("Final voltage list:", voltage_tracker.list())
166+
"#;
167+
168+
const BASIC_TYPES_METADATA_ZEN: &str = r#"
169+
load("@stdlib:v0.2.10/units.zen", "Voltage")
170+
171+
# Test metadata with basic types
172+
string_meta = metadata(str)
173+
int_meta = metadata(int)
174+
float_meta = metadata(float)
175+
176+
# Test with physical values
177+
voltage_meta = metadata(Voltage)
178+
179+
print("=== Testing Basic Types ===")
180+
181+
# String metadata
182+
string_meta.push("design_v1")
183+
string_meta.push("design_v2")
184+
string_meta.push("final_design")
185+
186+
print("String metadata:", string_meta.list())
187+
print("Latest string:", string_meta.get())
188+
189+
# Integer metadata
190+
int_meta.push(1)
191+
int_meta.push(42)
192+
int_meta.push(100)
193+
194+
print("Integer metadata:", int_meta.list())
195+
print("Latest integer:", int_meta.get())
196+
197+
# Float metadata
198+
float_meta.push(1.5)
199+
float_meta.push(3.14)
200+
float_meta.push(2.718)
201+
202+
print("Float metadata:", float_meta.list())
203+
print("Latest float:", float_meta.get())
204+
205+
# Physical value metadata
206+
voltage_meta.push(Voltage("3.3V"))
207+
voltage_meta.push(Voltage("5V"))
208+
209+
print("Voltage metadata:", voltage_meta.list())
210+
print("Latest voltage:", voltage_meta.get())
211+
212+
print("=== Conditional Logic with Basic Types ===")
213+
214+
# Conditional behavior with different types
215+
total_designs = len(string_meta.list())
216+
max_count = int_meta.get()
217+
precision_factor = float_meta.get()
218+
219+
print("Total designs:", total_designs)
220+
print("Max component count:", max_count)
221+
print("Precision factor:", precision_factor)
222+
223+
if total_designs > 2:
224+
print("Multiple design iterations found")
225+
226+
if max_count > 50:
227+
print("High component count design")
228+
229+
if precision_factor > 3.0:
230+
print("High precision requirements")
231+
232+
# Mixed type analysis
233+
design_complexity = total_designs * max_count * precision_factor
234+
print("Design complexity score:", design_complexity)
235+
236+
if design_complexity > 500:
237+
print("COMPLEX DESIGN - consider modular approach")
238+
else:
239+
print("Simple design - single module OK")
240+
"#;
241+
242+
#[test]
243+
fn test_simple_metadata_operations() {
244+
let output = Sandbox::new()
245+
.seed_stdlib(&["v0.2.10"])
246+
.write("SimpleMetadata.zen", SIMPLE_METADATA_ZEN)
247+
.snapshot_run("pcb", ["build", "SimpleMetadata.zen"]);
248+
assert_snapshot!("simple_metadata_operations", output);
249+
}
250+
251+
#[test]
252+
fn test_cross_module_metadata() {
253+
let output = Sandbox::new()
254+
.seed_stdlib(&["v0.2.10"])
255+
.write("modules/PowerModule.zen", POWER_MODULE_ZEN)
256+
.write("CrossModule.zen", CROSS_MODULE_ZEN)
257+
.snapshot_run("pcb", ["build", "CrossModule.zen"]);
258+
assert_snapshot!("cross_module_metadata", output);
259+
}
260+
261+
#[test]
262+
fn test_interface_cross_module_metadata() {
263+
let output = Sandbox::new()
264+
.seed_stdlib(&["v0.2.10"])
265+
.write(
266+
"modules/PowerInterfaceModule.zen",
267+
POWER_INTERFACE_MODULE_ZEN,
268+
)
269+
.write("InterfaceCrossModule.zen", INTERFACE_CROSS_MODULE_ZEN)
270+
.snapshot_run("pcb", ["build", "InterfaceCrossModule.zen"]);
271+
assert_snapshot!("interface_cross_module_metadata", output);
272+
}
273+
274+
#[test]
275+
fn test_conditional_metadata_behavior() {
276+
let output = Sandbox::new()
277+
.seed_stdlib(&["v0.2.10"])
278+
.write("ConditionalBehavior.zen", CONDITIONAL_BEHAVIOR_ZEN)
279+
.snapshot_run("pcb", ["build", "ConditionalBehavior.zen"]);
280+
assert_snapshot!("conditional_metadata_behavior", output);
281+
}
282+
283+
#[test]
284+
fn test_basic_types_metadata() {
285+
let output = Sandbox::new()
286+
.seed_stdlib(&["v0.2.10"])
287+
.write("BasicTypesMetadata.zen", BASIC_TYPES_METADATA_ZEN)
288+
.snapshot_run("pcb", ["build", "BasicTypesMetadata.zen"]);
289+
assert_snapshot!("basic_types_metadata", output);
290+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
source: crates/pcb/tests/metadata.rs
3+
expression: output
4+
---
5+
Command: pcb build BasicTypesMetadata.zen
6+
Exit Code: 0
7+
8+
--- STDOUT ---
9+
10+
--- STDERR ---
11+
=== Testing Basic Types ===
12+
String metadata: ["design_v1", "design_v2", "final_design"]
13+
Latest string: final_design
14+
Integer metadata: [1, 42, 100]
15+
Latest integer: 100
16+
Float metadata: [1.5, 3.14, 2.718]
17+
Latest float: 2.718
18+
Voltage metadata: [3.3V, 5V]
19+
Latest voltage: 5V
20+
=== Conditional Logic with Basic Types ===
21+
Total designs: 3
22+
Max component count: 100
23+
Precision factor: 2.718
24+
Multiple design iterations found
25+
High component count design
26+
Design complexity score: 815.4
27+
COMPLEX DESIGN - consider modular approach
28+
BasicTypesMetadata.zen (0 components)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/pcb/tests/metadata.rs
3+
expression: output
4+
---
5+
Command: pcb build ConditionalBehavior.zen
6+
Exit Code: 0
7+
8+
--- STDOUT ---
9+
10+
--- STDERR ---
11+
All voltages: [3.3V, 5V, 12V]
12+
Processing voltage value: 3.3
13+
Processing voltage value: 5.0
14+
Processing voltage value: 12.0
15+
Maximum voltage found: 12.0 V
16+
HIGH VOLTAGE DESIGN - adding isolation
17+
Added isolation voltage: 1kV
18+
Total voltage domains: 4
19+
COMPLEX POWER DESIGN
20+
Consider power management IC
21+
Final voltage list: [3.3V, 5V, 12V, 1kV]
22+
ConditionalBehavior.zen (0 components)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: crates/pcb/tests/metadata.rs
3+
expression: output
4+
---
5+
Command: pcb build CrossModule.zen
6+
Exit Code: 0
7+
8+
--- STDOUT ---
9+
10+
--- STDERR ---
11+
Power module configured with: 5V at 1A
12+
Loaded voltage metadata: [5V]
13+
Loaded current metadata: [1A]
14+
Updated voltage metadata: [5V, 24V]
15+
Updated current metadata: [1A, 2A]
16+
CrossModule.zen (0 components)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: crates/pcb/tests/metadata.rs
3+
expression: output
4+
---
5+
Command: pcb build InterfaceCrossModule.zen
6+
Exit Code: 0
7+
8+
--- STDOUT ---
9+
10+
--- STDERR ---
11+
Interface power module configured: 5V at 1A
12+
Interface voltage metadata: [5V]
13+
Interface current metadata: [1A]
14+
Updated via shared interface - voltage: [5V]
15+
Updated via shared interface - current: [1A]
16+
InterfaceCrossModule.zen (0 components)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: crates/pcb/tests/metadata.rs
3+
expression: output
4+
---
5+
Command: pcb build SimpleMetadata.zen
6+
Exit Code: 0
7+
8+
--- STDOUT ---
9+
10+
--- STDERR ---
11+
Latest voltage: 3.3V 1%
12+
Latest current: 100mA 5%
13+
Latest resistance: 1k 5%
14+
All voltages: [5V 5%, 3.3V 1%]
15+
All currents: [100mA 5%]
16+
All resistances: [1k 5%]
17+
Empty get: None
18+
Empty list: []
19+
SimpleMetadata.zen (0 components)

0 commit comments

Comments
 (0)