| 
 | 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 | +}  | 
0 commit comments