| 1 | mod expression_modifier_tests { |
| 2 | use crate::comp::*; |
| 3 | use crate::expr::ExprEngine; |
| 4 | |
| 5 | #[test] |
| 6 | fn evaluates_standalone_expression_modifier_number_and_chained() { |
| 7 | let src = r#"Composition { |
| 8 | Tools = ordered() { |
| 9 | XF1 = Transform { |
| 10 | Inputs = { |
| 11 | Angle = Input { SourceOp = "Clock_MinHandExpr", Source = "NumberResult", }, |
| 12 | Size = Input { SourceOp = "HourHandExpr", Source = "NumberResult", }, |
| 13 | }, |
| 14 | }, |
| 15 | Clock_MinHandExpr = Expression { |
| 16 | Inputs = { |
| 17 | n1 = Input { Value = 1, }, |
| 18 | n2 = Input { SourceOp = "TimeSpline", Source = "Value", }, |
| 19 | NumberExpression = Input { Value = "if(n1>0.5,floor(-n2)*30,-n2*30)", }, |
| 20 | }, |
| 21 | }, |
| 22 | HourHandExpr = Expression { |
| 23 | Inputs = { |
| 24 | n1 = Input { SourceOp = "Clock_MinHandExpr", Source = "NumberResult", }, |
| 25 | NumberExpression = Input { Value = "n1/12", }, |
| 26 | }, |
| 27 | }, |
| 28 | TimeSpline = BezierSpline { |
| 29 | KeyFrames = { |
| 30 | [0] = { 0.0, Flags = { Linear = true } }, |
| 31 | [10] = { 2.5, Flags = { Linear = true } }, |
| 32 | }, |
| 33 | }, |
| 34 | }, |
| 35 | }"#; |
| 36 | |
| 37 | let comp = Comp::from_fusion_str(src).expect("parse comp"); |
| 38 | assert!(comp.expression_modifiers.contains_key("Clock_MinHandExpr")); |
| 39 | assert!(comp.expression_modifiers.contains_key("HourHandExpr")); |
| 40 | // Expression modifiers are modifiers, not flow nodes |
| 41 | assert!(comp.find_node("Clock_MinHandExpr").is_none()); |
| 42 | |
| 43 | let expr = ExprEngine::new(); |
| 44 | let xf = comp.node(comp.find_node("XF1").unwrap()); |
| 45 | // At t=10: n2 = 2.5, floor(-2.5) = -3.0, -3.0 * 30 = -90.0 |
| 46 | let angle = comp.eval_number(xf, "Angle", 10.0, 0.0, &expr); |
| 47 | assert!((angle - (-90.0)).abs() < 1e-6, "got {angle}"); |
| 48 | // Chained HourHandExpr: n1 / 12 = -90 / 12 = -7.5 |
| 49 | let size = comp.eval_number(xf, "Size", 10.0, 1.0, &expr); |
| 50 | assert!((size - (-7.5)).abs() < 1e-6, "got {size}"); |
| 51 | |
| 52 | // Lossless roundtrip preserves Expression modifiers |
| 53 | let saved = comp.to_fusion_string(); |
| 54 | let reloaded = Comp::from_fusion_str(&saved).expect("reload"); |
| 55 | let r_xf = reloaded.node(reloaded.find_node("XF1").unwrap()); |
| 56 | assert!((reloaded.eval_number(r_xf, "Angle", 10.0, 0.0, &expr) - (-90.0)).abs() < 1e-6); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn evaluates_standalone_expression_modifier_point_and_dist_trig() { |
| 61 | let src = r#"Composition { |
| 62 | Tools = ordered() { |
| 63 | XF1 = Transform { |
| 64 | Inputs = { |
| 65 | Center = Input { SourceOp = "PtExpr", Source = "PointResult", }, |
| 66 | Angle = Input { SourceOp = "TrigExpr", Source = "NumberResult", }, |
| 67 | }, |
| 68 | }, |
| 69 | PtExpr = Expression { |
| 70 | Inputs = { |
| 71 | p1 = Input { Value = { 0.8, 0.2 }, }, |
| 72 | n1 = Input { Value = 10, }, |
| 73 | PointExpressionX = Input { Value = "if(p1x>0.5, 0.5 + time*dist(p1x,p1y,0.5,p1y)/n1, 0.5)", }, |
| 74 | PointExpressionY = Input { Value = "-p1y + p1y*0.5 + 0.5", }, |
| 75 | }, |
| 76 | }, |
| 77 | TrigExpr = Expression { |
| 78 | Inputs = { |
| 79 | NumberIn1 = Input { Value = 90, }, |
| 80 | NumberExpression = Input { Value = "sin(n1) + cos(0) + (1/0)", }, |
| 81 | }, |
| 82 | }, |
| 83 | }, |
| 84 | }"#; |
| 85 | |
| 86 | let comp = Comp::from_fusion_str(src).expect("parse comp"); |
| 87 | let expr = ExprEngine::new(); |
| 88 | let xf = comp.node(comp.find_node("XF1").unwrap()); |
| 89 | // At t=5: dist(0.8,0.2,0.5,0.2) = 0.3, x = 0.5 + 5*0.3/10 = 0.65, y = -0.2 + 0.1 + 0.5 = 0.4 |
| 90 | let pt = comp.eval_point(xf, "Center", 5.0, [0.5, 0.5], &expr); |
| 91 | assert!((pt[0] - 0.65).abs() < 1e-6, "got x={}", pt[0]); |
| 92 | assert!((pt[1] - 0.40).abs() < 1e-6, "got y={}", pt[1]); |
| 93 | // sin(90 deg) + cos(0 deg) + safe_div(1,0) = 1 + 1 + 0 = 2.0 |
| 94 | let ang = comp.eval_number(xf, "Angle", 0.0, 0.0, &expr); |
| 95 | assert!((ang - 2.0).abs() < 1e-6, "got {ang}"); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn resolves_instanced_source_op_inheritance_chains_and_keeps_image_wires_isolated() { |
| 100 | let src = r#"Composition { |
| 101 | Tools = ordered() { |
| 102 | Loader1 = Loader {}, |
| 103 | Transform2 = Transform { |
| 104 | Inputs = { |
| 105 | Input = Input { SourceOp = "Loader1", Source = "Output", }, |
| 106 | }, |
| 107 | }, |
| 108 | Ellipse1 = EllipseMask {}, |
| 109 | ColorCorrector1_6Blend = BezierSpline { |
| 110 | KeyFrames = { |
| 111 | [0] = { 0.25, Flags = { Linear = true } }, |
| 112 | [10] = { 0.75, Flags = { Linear = true } }, |
| 113 | }, |
| 114 | }, |
| 115 | ColorCorrector1_6 = ColorCorrector { |
| 116 | Inputs = { |
| 117 | Blend = Input { SourceOp = "ColorCorrector1_6Blend", Source = "Value", }, |
| 118 | WheelTintAngle1 = Input { Value = -0.434283, }, |
| 119 | WheelHue1 = Input { Value = 0.2, }, |
| 120 | PreDividePostMultiply = Input { Value = 1, }, |
| 121 | Input = Input { SourceOp = "Loader1", Source = "Output", }, |
| 122 | EffectMask = Input { SourceOp = "Ellipse1", Source = "Mask", }, |
| 123 | }, |
| 124 | }, |
| 125 | Instance_ColorCorrector1_6 = ColorCorrector { |
| 126 | SourceOp = "ColorCorrector1_6", |
| 127 | Inputs = { |
| 128 | Input = Input { SourceOp = "Transform2", Source = "Output", }, |
| 129 | EffectMask = Input {}, |
| 130 | WheelTintAngle1 = Input {}, |
| 131 | WheelHue1 = Input { Value = 0.9, }, |
| 132 | }, |
| 133 | }, |
| 134 | Instance_Chained = ColorCorrector { |
| 135 | SourceOp = "Instance_ColorCorrector1_6", |
| 136 | Inputs = { |
| 137 | Input = Input { SourceOp = "Loader1", Source = "Output", }, |
| 138 | }, |
| 139 | }, |
| 140 | CycleA = Blur { |
| 141 | SourceOp = "CycleB", |
| 142 | Inputs = { |
| 143 | XBlurSize = Input { Value = 3.0, }, |
| 144 | }, |
| 145 | }, |
| 146 | CycleB = Blur { |
| 147 | SourceOp = "CycleA", |
| 148 | Inputs = { |
| 149 | YBlurSize = Input { Value = 7.0, }, |
| 150 | }, |
| 151 | }, |
| 152 | }, |
| 153 | }"#; |
| 154 | |
| 155 | let comp = Comp::from_fusion_str(src).expect("parse instanced comp"); |
| 156 | let expr = ExprEngine::new(); |
| 157 | |
| 158 | let inst = comp.node(comp.find_node("Instance_ColorCorrector1_6").unwrap()); |
| 159 | // Empty `WheelTintAngle1 = Input {}` is a de-instanced override (falls back to tool default), |
| 160 | // while omitted params (`Blend`, `PreDividePostMultiply`) inherit from master! |
| 161 | let tint = comp.eval_number(inst, "WheelTintAngle1", 0.0, 0.0, &expr); |
| 162 | assert!(tint.abs() < 1e-6, "de-instanced empty Input {{}} must not inherit master value, got {tint}"); |
| 163 | let blend_t5 = comp.eval_number(inst, "Blend", 5.0, 1.0, &expr); |
| 164 | assert!((blend_t5 - 0.5).abs() < 1e-6, "got {blend_t5}"); |
| 165 | let pdpm = comp.eval_number(inst, "PreDividePostMultiply", 0.0, 0.0, &expr); |
| 166 | assert!((pdpm - 1.0).abs() < 1e-6, "got {pdpm}"); |
| 167 | // Keeps its own explicit override (`WheelHue1 = 0.9`) and its own `Input` wire (`Transform2`)! |
| 168 | let hue = comp.eval_number(inst, "WheelHue1", 0.0, 0.0, &expr); |
| 169 | assert!((hue - 0.9).abs() < 1e-6, "got {hue}"); |
| 170 | assert_eq!(inst.connected_node("Input"), Some("Transform2")); |
| 171 | // NEVER inherits parent's `EffectMask` image/mask wire! |
| 172 | assert_eq!(inst.connected_node("EffectMask"), None); |
| 173 | |
| 174 | // Chained inheritance (`Instance_Chained -> Instance_ColorCorrector1_6 -> ColorCorrector1_6`): |
| 175 | // nearest ancestor wins (`WheelHue1 = 0.9`, and `WheelTintAngle1` was de-instanced by parent), |
| 176 | // grandparent fills remaining omitted params (`Blend`, `PreDividePostMultiply`). |
| 177 | let chained = comp.node(comp.find_node("Instance_Chained").unwrap()); |
| 178 | assert!((comp.eval_number(chained, "WheelHue1", 0.0, 0.0, &expr) - 0.9).abs() < 1e-6); |
| 179 | assert!(comp.eval_number(chained, "WheelTintAngle1", 0.0, 0.0, &expr).abs() < 1e-6); |
| 180 | assert!((comp.eval_number(chained, "Blend", 10.0, 0.0, &expr) - 0.75).abs() < 1e-6); |
| 181 | assert!((comp.eval_number(chained, "PreDividePostMultiply", 0.0, 0.0, &expr) - 1.0).abs() < 1e-6); |
| 182 | assert_eq!(chained.connected_node("Input"), Some("Loader1")); |
| 183 | assert_eq!(chained.connected_node("EffectMask"), None); |
| 184 | |
| 185 | // Cycle safety (`CycleA <-> CycleB` terminates cleanly and cross-inherits missing params) |
| 186 | let cyc_a = comp.node(comp.find_node("CycleA").unwrap()); |
| 187 | let cyc_b = comp.node(comp.find_node("CycleB").unwrap()); |
| 188 | assert!((comp.eval_number(cyc_a, "XBlurSize", 0.0, 0.0, &expr) - 3.0).abs() < 1e-6); |
| 189 | assert!((comp.eval_number(cyc_a, "YBlurSize", 0.0, 0.0, &expr) - 7.0).abs() < 1e-6); |
| 190 | assert!((comp.eval_number(cyc_b, "XBlurSize", 0.0, 0.0, &expr) - 3.0).abs() < 1e-6); |
| 191 | assert!((comp.eval_number(cyc_b, "YBlurSize", 0.0, 0.0, &expr) - 7.0).abs() < 1e-6); |
| 192 | |
| 193 | // Roundtrip preserves SourceOp inheritance and de-instanced `Input {}` overrides without duplicating master params |
| 194 | let saved = comp.to_fusion_string(); |
| 195 | let reloaded = Comp::from_fusion_str(&saved).expect("reload"); |
| 196 | let r_inst = reloaded.node(reloaded.find_node("Instance_ColorCorrector1_6").unwrap()); |
| 197 | assert!(reloaded.eval_number(r_inst, "WheelTintAngle1", 0.0, 0.0, &expr).abs() < 1e-6); |
| 198 | assert!((reloaded.eval_number(r_inst, "Blend", 5.0, 1.0, &expr) - 0.5).abs() < 1e-6); |
| 199 | assert_eq!(r_inst.connected_node("EffectMask"), None); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn parses_styled_text_and_text_bezier_spline_keyframes() { |
| 204 | let src = r#"Composition { |
| 205 | Tools = ordered() { |
| 206 | Text_1 = TextPlus { |
| 207 | Inputs = { |
| 208 | StyledText = Input { |
| 209 | SourceOp = "Text_1StyledText", |
| 210 | Source = "Value", |
| 211 | }, |
| 212 | }, |
| 213 | }, |
| 214 | Text_1StyledText = BezierSpline { |
| 215 | KeyFrames = { |
| 216 | [10] = { 0, Value = StyledText { Value = "first line" } }, |
| 217 | [20] = { 1, Value = Text { Value = "second line" } }, |
| 218 | }, |
| 219 | }, |
| 220 | }, |
| 221 | }"#; |
| 222 | let comp = Comp::from_fusion_str(src).expect("parse styled text spline"); |
| 223 | let expr = ExprEngine::new(); |
| 224 | let node = comp.node(comp.find_node("Text_1").unwrap()); |
| 225 | assert_eq!(comp.eval_text(node, "StyledText", 5.0, "", &expr), "first line"); |
| 226 | assert_eq!(comp.eval_text(node, "StyledText", 10.0, "", &expr), "first line"); |
| 227 | assert_eq!(comp.eval_text(node, "StyledText", 25.0, "", &expr), "second line"); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn source_op_inherits_all_color_corrector_params_and_respects_blend_override() { |
| 232 | // Mirrors repro_seed_110: N4 inherits Wheel*/TintMode/*RGBGain/PreDividePostMultiply |
| 233 | // from N2 while keeping its own `Input` wire and `Blend = 0.75` override. |
| 234 | let src = r#"Composition { |
| 235 | Tools = ordered() { |
| 236 | S0 = Loader {}, |
| 237 | S1 = DaySky {}, |
| 238 | N2 = ColorCorrector { |
| 239 | Inputs = { |
| 240 | Input = Input { SourceOp = "S0", Source = "Output", }, |
| 241 | WheelHue1 = Input { Value = -0.235, }, |
| 242 | WheelSaturation1 = Input { Value = 0.65, }, |
| 243 | WheelTintAngle1 = Input { Value = 0.323, }, |
| 244 | WheelTintLength1 = Input { Value = 0.67, }, |
| 245 | TintMode = Input { Value = 0, }, |
| 246 | MasterRGBGain = Input { Value = 0.65, }, |
| 247 | ShadowsRGBGain = Input { Value = 0.84, }, |
| 248 | HighlightsRGBGain = Input { Value = 0.75, }, |
| 249 | PreDividePostMultiply = Input { Value = 1, }, |
| 250 | Blend = Input { Value = 1.0, }, |
| 251 | }, |
| 252 | }, |
| 253 | N4 = ColorCorrector { |
| 254 | SourceOp = "N2", |
| 255 | Inputs = { |
| 256 | Input = Input { SourceOp = "S1", Source = "Output", }, |
| 257 | Blend = Input { Value = 0.75, }, |
| 258 | }, |
| 259 | }, |
| 260 | N5 = ColorCorrector { |
| 261 | SourceOp = "N2", |
| 262 | Inputs = { |
| 263 | Input = Input { SourceOp = "S1", Source = "Output", }, |
| 264 | }, |
| 265 | }, |
| 266 | }, |
| 267 | }"#; |
| 268 | let comp = Comp::from_fusion_str(src).expect("parse seed 110 style SourceOp"); |
| 269 | let expr = ExprEngine::new(); |
| 270 | let n4 = comp.node(comp.find_node("N4").unwrap()); |
| 271 | assert_eq!(n4.connected_node("Input"), Some("S1")); |
| 272 | assert!((comp.eval_number(n4, "Blend", 1.0, 1.0, &expr) - 0.75).abs() < 1e-6); |
| 273 | assert!((comp.eval_number(n4, "WheelHue1", 1.0, 0.0, &expr) - (-0.235)).abs() < 1e-6); |
| 274 | assert!((comp.eval_number(n4, "WheelSaturation1", 1.0, 1.0, &expr) - 0.65).abs() < 1e-6); |
| 275 | assert!((comp.eval_number(n4, "WheelTintAngle1", 1.0, 0.0, &expr) - 0.323).abs() < 1e-6); |
| 276 | assert!((comp.eval_number(n4, "WheelTintLength1", 1.0, 0.0, &expr) - 0.67).abs() < 1e-6); |
| 277 | assert!((comp.eval_number(n4, "TintMode", 1.0, 2.0, &expr) - 0.0).abs() < 1e-6); |
| 278 | assert!((comp.eval_number(n4, "MasterRGBGain", 1.0, 1.0, &expr) - 0.65).abs() < 1e-6); |
| 279 | assert!((comp.eval_number(n4, "ShadowsRGBGain", 1.0, 1.0, &expr) - 0.84).abs() < 1e-6); |
| 280 | assert!((comp.eval_number(n4, "HighlightsRGBGain", 1.0, 1.0, &expr) - 0.75).abs() < 1e-6); |
| 281 | assert!((comp.eval_number(n4, "PreDividePostMultiply", 1.0, 0.0, &expr) - 1.0).abs() < 1e-6); |
| 282 | |
| 283 | let n5 = comp.node(comp.find_node("N5").unwrap()); |
| 284 | assert!((comp.eval_number(n5, "Blend", 1.0, 0.0, &expr) - 1.0).abs() < 1e-6); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn hue_curves_lut_bezier_applies_cyclic_ends_and_roundtrips_cleanly() { |
| 289 | let src = r#"Composition { |
| 290 | Tools = ordered() { |
| 291 | Bg = Background {}, |
| 292 | HC_RSup = LUTBezier { |
| 293 | SmoothEndsTransition = true, |
| 294 | KeyColorSplines = { |
| 295 | [0] = { |
| 296 | [0] = { 0.5 }, |
| 297 | [2] = { 1.0 }, |
| 298 | [6] = { 0.5 }, |
| 299 | } |
| 300 | }, |
| 301 | }, |
| 302 | CC_Red = LUTBezier { |
| 303 | SmoothEndsTransition = true, |
| 304 | KeyColorSplines = { |
| 305 | [0] = { |
| 306 | [0.0] = { 0.0 }, |
| 307 | [0.5] = { 0.8 }, |
| 308 | [1.0] = { 0.2 }, |
| 309 | } |
| 310 | }, |
| 311 | }, |
| 312 | HC1 = HueCurves { |
| 313 | Inputs = { |
| 314 | Input = Input { SourceOp = "Bg", Source = "Output", }, |
| 315 | RedSuppress = Input { SourceOp = "HC_RSup", Source = "Value", }, |
| 316 | }, |
| 317 | }, |
| 318 | CC1 = ColorCurves { |
| 319 | Inputs = { |
| 320 | Input = Input { SourceOp = "Bg", Source = "Output", }, |
| 321 | Red = Input { SourceOp = "CC_Red", Source = "Value", }, |
| 322 | }, |
| 323 | }, |
| 324 | }, |
| 325 | }"#; |
| 326 | let comp = Comp::from_fusion_str(src).expect("parse HueCurves + ColorCurves"); |
| 327 | let rsup = comp.splines.get("HC_RSup").expect("HC_RSup"); |
| 328 | assert!(rsup.keyframes[2].lh.is_some()); |
| 329 | assert!((rsup.eval(5.0) - 0.4140625).abs() < 1e-3); |
| 330 | |
| 331 | // ColorCurves [0..1] must NOT get cyclic HueCurves wrapping even with SmoothEndsTransition = true. |
| 332 | let cc_red = comp.splines.get("CC_Red").expect("CC_Red"); |
| 333 | assert!((cc_red.eval(1.0) - 0.2).abs() < 1e-6); |
| 334 | assert!((cc_red.eval(0.75) - 0.5).abs() < 1e-6); |
| 335 | |
| 336 | // Lossless roundtrip keeps KeyColorSplines untouched when unedited. |
| 337 | let saved = comp.to_fusion_string(); |
| 338 | assert!(!saved.contains("LH =")); |
| 339 | let reloaded = Comp::from_fusion_str(&saved).expect("reload"); |
| 340 | assert!((reloaded.splines["HC_RSup"].eval(5.0) - rsup.eval(5.0)).abs() < 1e-9); |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn comp_eval_number_clamps_loop_spline_before_first_keyframe() { |
| 345 | let src = r#"Composition { |
| 346 | Tools = ordered() { |
| 347 | CG = ColorGain { |
| 348 | Inputs = { |
| 349 | HighHue = Input { SourceOp = "ColorGain1HighHue", Source = "Value", }, |
| 350 | }, |
| 351 | }, |
| 352 | ColorGain1HighHue = BezierSpline { |
| 353 | KeyFrames = { |
| 354 | [4129] = { 0, RH = { 4137.66666666667, 0.333333333333333 }, Flags = { Linear = true, Loop = true } }, |
| 355 | [4155] = { 1, LH = { 4146.33333333333, 0.666666666666667 }, Flags = { Linear = true, Loop = true } }, |
| 356 | }, |
| 357 | }, |
| 358 | }, |
| 359 | }"#; |
| 360 | let comp = Comp::from_fusion_str(src).expect("parse underwater loop spline"); |
| 361 | let expr = ExprEngine::new(); |
| 362 | let cg = comp.node(comp.find_node("CG").unwrap()); |
| 363 | assert!((comp.eval_number(cg, "HighHue", 3933.0, 0.0, &expr) - 0.0).abs() < 1e-9); |
| 364 | assert!((comp.eval_number(cg, "HighHue", 4142.0, 0.0, &expr) - 0.5).abs() < 1e-9); |
| 365 | assert!((comp.eval_number(cg, "HighHue", 4155.0, 0.0, &expr) - 0.0).abs() < 1e-9); |
| 366 | assert!((comp.eval_number(cg, "HighHue", 4168.0, 0.0, &expr) - 0.5).abs() < 1e-9); |
| 367 | } |
| 368 | |
| 369 | #[test] |
| 370 | fn cyclic_expression_references_terminate() { |
| 371 | let src = r#"Composition { |
| 372 | Tools = ordered() { |
| 373 | N = Transform { |
| 374 | Inputs = { Size = Input { SourceOp = "E", Source = "NumberResult", }, }, |
| 375 | }, |
| 376 | E = Expression { |
| 377 | Inputs = { |
| 378 | n1 = Input { Expression = "N.Size", }, |
| 379 | NumberExpression = Input { Value = "n1 + 1", }, |
| 380 | }, |
| 381 | }, |
| 382 | }, |
| 383 | }"#; |
| 384 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 385 | let n = comp.node(comp.find_node("N").unwrap()); |
| 386 | let v = comp.eval_number(n, "Size", 0.0, 7.0, &ExprEngine::new()); |
| 387 | assert!(v.is_finite()); |
| 388 | } |
| 389 | |
| 390 | #[test] |
| 391 | fn clamp_with_inverted_bounds_does_not_panic() { |
| 392 | let src = r#"Composition { |
| 393 | Tools = ordered() { |
| 394 | N = Transform { Inputs = { Size = Input { SourceOp = "E", Source = "NumberResult", }, }, }, |
| 395 | E = Expression { |
| 396 | Inputs = { |
| 397 | n1 = Input { Value = 5, }, |
| 398 | NumberExpression = Input { Value = "clamp(n1, 1, 0)", }, |
| 399 | }, |
| 400 | }, |
| 401 | }, |
| 402 | }"#; |
| 403 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 404 | let n = comp.node(comp.find_node("N").unwrap()); |
| 405 | assert_eq!(comp.eval_number(n, "Size", 0.0, 9.0, &ExprEngine::new()), 0.0); |
| 406 | } |
| 407 | |
| 408 | #[test] |
| 409 | fn explicit_frame_format_is_never_overwritten_by_inferred_size() { |
| 410 | let tools = r#"Tools = ordered() { |
| 411 | BG = Background { Inputs = { Width = Input { Value = 1280, }, Height = Input { Value = 720, }, }, }, |
| 412 | }"#; |
| 413 | let explicit = Comp::from_fusion_str(&format!( |
| 414 | "Composition {{ Prefs = {{ Comp = {{ FrameFormat = {{ Width = 1920, Height = 1080, }}, }}, }}, {tools} }}" |
| 415 | )) |
| 416 | .unwrap(); |
| 417 | assert_eq!((explicit.width, explicit.height), (1920, 1080)); |
| 418 | |
| 419 | let inferred = Comp::from_fusion_str(&format!("Composition {{ {tools} }}")).unwrap(); |
| 420 | assert_eq!((inferred.width, inferred.height), (1280, 720)); |
| 421 | } |
| 422 | |
| 423 | #[test] |
| 424 | fn each_loaded_comp_gets_a_distinct_revision_range() { |
| 425 | let src = "Composition { Tools = ordered() { } }"; |
| 426 | let a = Comp::from_fusion_str(src).unwrap(); |
| 427 | let mut b = Comp::from_fusion_str(src).unwrap(); |
| 428 | for _ in 0..1000 { |
| 429 | b.revision += 1; |
| 430 | } |
| 431 | let c = Comp::from_fusion_str(src).unwrap(); |
| 432 | assert!(a.revision != b.revision && b.revision != c.revision && a.revision != c.revision); |
| 433 | assert!(c.revision > b.revision); |
| 434 | } |
| 435 | |
| 436 | const GROUP_COMP: &str = r#"Composition { |
| 437 | 	Tools = ordered() { |
| 438 | 		BG = Background { }, |
| 439 | 		BG2 = Background { }, |
| 440 | 		Group1 = GroupOperator { |
| 441 | 			Inputs = { |
| 442 | 				MainInput1 = InstanceInput { SourceOp = "Inner1", Source = "Input", }, |
| 443 | 				BlurSize = InstanceInput { SourceOp = "Inner1", Source = "XBlurSize", Default = 3, }, |
| 444 | 			}, |
| 445 | 			Outputs = { |
| 446 | 				MainOutput1 = InstanceOutput { SourceOp = "Inner2", Source = "Output", }, |
| 447 | 			}, |
| 448 | 			Tools = ordered() { |
| 449 | 				Inner1 = Blur { |
| 450 | 					Inputs = { |
| 451 | 						Input = Input { SourceOp = "BG", Source = "Output", }, |
| 452 | 					}, |
| 453 | 				}, |
| 454 | 				Inner2 = Transform { |
| 455 | 					Inputs = { |
| 456 | 						Input = Input { SourceOp = "Inner1", Source = "Output", }, |
| 457 | 						Size = Input { Value = 1, }, |
| 458 | 					}, |
| 459 | 				}, |
| 460 | 				Inner3 = Blur { }, |
| 461 | 			}, |
| 462 | 		}, |
| 463 | 	}, |
| 464 | }"#; |
| 465 | |
| 466 | #[test] |
| 467 | fn unedited_group_roundtrips_byte_for_byte() { |
| 468 | let comp = Comp::from_fusion_str(GROUP_COMP).unwrap(); |
| 469 | let tools = |t: &LuaTable| t.get_table("Tools").unwrap().to_fusion_string(); |
| 470 | assert_eq!(tools(&comp.sync_to_raw_ast()), tools(comp.raw_root.as_ref().unwrap())); |
| 471 | } |
| 472 | |
| 473 | #[test] |
| 474 | fn nested_group_edits_and_wiring_survive_save() { |
| 475 | let mut comp = Comp::from_fusion_str(GROUP_COMP).unwrap(); |
| 476 | let group = comp.find_node("Group1").unwrap(); |
| 477 | assert_eq!(comp.node(group).connected_node("MainInput1"), Some("BG")); |
| 478 | |
| 479 | comp.delete_nodes(&[comp.find_node("Inner3").unwrap()]); |
| 480 | let inner2 = comp.find_node("Inner2").unwrap(); |
| 481 | assert!(comp.set_input_value(inner2, "Size", ParamValue::Number(2.5))); |
| 482 | let (group, bg2) = (comp.find_node("Group1").unwrap(), comp.find_node("BG2").unwrap()); |
| 483 | comp.connect_output(bg2, "Output", group, "MainInput1").unwrap(); |
| 484 | |
| 485 | let saved = Comp::from_fusion_str(&comp.to_fusion_string()).unwrap(); |
| 486 | let inner2 = saved.node(saved.find_node("Inner2").unwrap()); |
| 487 | assert_eq!(inner2.static_number("Size"), Some(2.5)); |
| 488 | assert!(saved.find_node("Inner3").is_none()); |
| 489 | assert_eq!(saved.node(saved.find_node("Inner1").unwrap()).connected_node("Input"), Some("BG2")); |
| 490 | assert_eq!(saved.node(saved.find_node("Group1").unwrap()).connected_node("MainInput1"), Some("BG2")); |
| 491 | // The exposed `Default` stays on the InstanceInput instead of leaking onto Inner1. |
| 492 | let text = comp.to_fusion_string(); |
| 493 | assert_eq!(text.matches("Value = 3").count(), 0, "{text}"); |
| 494 | |
| 495 | let mut comp = saved; |
| 496 | let group = comp.find_node("Group1").unwrap(); |
| 497 | assert!(comp.disconnect(group, "MainInput1")); |
| 498 | let saved = Comp::from_fusion_str(&comp.to_fusion_string()).unwrap(); |
| 499 | assert_eq!(saved.node(saved.find_node("Inner1").unwrap()).connected_node("Input"), None); |
| 500 | } |
| 501 | |
| 502 | const MULTI_INPUT_GROUP: &str = r#"Composition { |
| 503 | 		Tools = ordered() { |
| 504 | 			BG = Background { }, |
| 505 | 			BG2 = Background { }, |
| 506 | 			Group1 = GroupOperator { |
| 507 | 				Inputs = { |
| 508 | 					MainInput1 = InstanceInput { SourceOp = "M", Source = "Background", }, |
| 509 | 					MainInput2 = InstanceInput { SourceOp = "M", Source = "Foreground", }, |
| 510 | 					XBlurSize = InstanceInput { SourceOp = "B", Source = "XBlurSize", }, |
| 511 | 				}, |
| 512 | 				Outputs = { |
| 513 | 					MainOutput1 = InstanceOutput { SourceOp = "B", Source = "Output", }, |
| 514 | 				}, |
| 515 | 				Tools = ordered() { |
| 516 | 					SizeSpline = BezierSpline { |
| 517 | 						KeyFrames = { |
| 518 | 							[0] = { 1, Flags = { Linear = true, }, }, |
| 519 | 							[10] = { 5, Flags = { Linear = true, }, }, |
| 520 | 						}, |
| 521 | 					}, |
| 522 | 					M = Merge { |
| 523 | 						Inputs = { |
| 524 | 							Background = Input { SourceOp = "BG", Source = "Output", }, |
| 525 | 							Foreground = Input { SourceOp = "BG2", Source = "Output", }, |
| 526 | 						}, |
| 527 | 					}, |
| 528 | 					B = Blur { |
| 529 | 						Inputs = { |
| 530 | 							XBlurSize = Input { SourceOp = "SizeSpline", Source = "Value", }, |
| 531 | 							Input = Input { SourceOp = "M", Source = "Output", }, |
| 532 | 						}, |
| 533 | 					}, |
| 534 | 				}, |
| 535 | 			}, |
| 536 | 		}, |
| 537 | 	}"#; |
| 538 | |
| 539 | #[test] |
| 540 | fn multi_input_group_keeps_each_wire_distinct_across_save() { |
| 541 | let mut comp = Comp::from_fusion_str(MULTI_INPUT_GROUP).unwrap(); |
| 542 | let group = comp.find_node("Group1").unwrap(); |
| 543 | assert_eq!(comp.node(group).connected_node("MainInput1"), Some("BG")); |
| 544 | assert_eq!(comp.node(group).connected_node("MainInput2"), Some("BG2")); |
| 545 | let tools = |t: &LuaTable| t.get_table("Tools").unwrap().to_fusion_string(); |
| 546 | assert_eq!(tools(&comp.sync_to_raw_ast()), tools(comp.raw_root.as_ref().unwrap())); |
| 547 | |
| 548 | let (bg, bg2) = (comp.find_node("BG").unwrap(), comp.find_node("BG2").unwrap()); |
| 549 | comp.connect_output(bg2, "Output", group, "MainInput1").unwrap(); |
| 550 | comp.connect_output(bg, "Output", group, "MainInput2").unwrap(); |
| 551 | let saved = Comp::from_fusion_str(&comp.to_fusion_string()).unwrap(); |
| 552 | let merge = saved.node(saved.find_node("M").unwrap()); |
| 553 | assert_eq!(merge.connected_node("Background"), Some("BG2")); |
| 554 | assert_eq!(merge.connected_node("Foreground"), Some("BG")); |
| 555 | } |
| 556 | |
| 557 | #[test] |
| 558 | fn group_wire_edit_keeps_animated_exposed_input() { |
| 559 | let mut comp = Comp::from_fusion_str(MULTI_INPUT_GROUP).unwrap(); |
| 560 | let (group, bg2) = (comp.find_node("Group1").unwrap(), comp.find_node("BG2").unwrap()); |
| 561 | assert!(comp.node(group).inputs.contains_key("XBlurSize")); |
| 562 | comp.connect_output(bg2, "Output", group, "MainInput1").unwrap(); |
| 563 | let saved = Comp::from_fusion_str(&comp.to_fusion_string()).unwrap(); |
| 564 | let blur = saved.node(saved.find_node("B").unwrap()); |
| 565 | assert_eq!(blur.connected_node("XBlurSize"), Some("SizeSpline")); |
| 566 | assert!(saved.splines.contains_key("SizeSpline")); |
| 567 | let merge = saved.node(saved.find_node("M").unwrap()); |
| 568 | assert_eq!(merge.connected_node("Background"), Some("BG2")); |
| 569 | assert_eq!(merge.connected_node("Foreground"), Some("BG2")); |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn fanned_out_expression_modifier_chain_evaluates_linearly() { |
| 574 | const LEN: usize = 12; |
| 575 | let mut src = String::from( |
| 576 | r#"Composition { Tools = ordered() { |
| 577 | N = Transform { Inputs = { Size = Input { SourceOp = "E0", Source = "NumberResult", }, }, },"#, |
| 578 | ); |
| 579 | for i in 0..LEN - 1 { |
| 580 | let next = format!(r#"Input {{ SourceOp = "E{}", Source = "NumberResult", }}"#, i + 1); |
| 581 | src += &format!( |
| 582 | r#"E{i} = Expression {{ Inputs = {{ n1 = {next}, n2 = {next}, n3 = {next}, |
| 583 | NumberExpression = Input {{ Value = "n1 + n2 + n3", }}, }}, }},"# |
| 584 | ); |
| 585 | } |
| 586 | src += &format!( |
| 587 | r#"E{} = Expression {{ Inputs = {{ n1 = Input {{ Value = 1, }}, |
| 588 | NumberExpression = Input {{ Value = "n1", }}, }}, }}, }} }}"#, |
| 589 | LEN - 1 |
| 590 | ); |
| 591 | let comp = Comp::from_fusion_str(&src).unwrap(); |
| 592 | let n = comp.node(comp.find_node("N").unwrap()); |
| 593 | let start = std::time::Instant::now(); |
| 594 | let v = comp.eval_number(n, "Size", 0.0, -1.0, &ExprEngine::new()); |
| 595 | assert_eq!(v, 3f64.powi(LEN as i32 - 1)); |
| 596 | assert!(start.elapsed() < std::time::Duration::from_millis(100), "{:?}", start.elapsed()); |
| 597 | } |
| 598 | } |