using Azimuth; using Prism; using UnitTestSharp; namespace Konis.Testbed.UnitTests { public class TunableSnapshotTests : TestFixture { private static StandardEmitterDef Def( int capacity = 10, IScalarFunction sizeOverLife = null, IScalarFunction alphaOverLife = null, SpriteDef sprite = null, Slab[] slabs = null) { return new StandardEmitterDef { Sprite = sprite, ClipSlabs = slabs, Capacity = capacity, Rate = new Variation(2, 3), SizeOverLife = sizeOverLife, AlphaOverLife = alphaOverLife, TintOverLife = new GradientDef { Gradient = Gradient.Constant(Colors.White) }, }; } public class CtorTests : TestFixture { public void HoldsTheValueOfEveryEditablePropertyAsSource() { var snapshot = new TunableSnapshot(Def(capacity: 10)); CheckEqual("10", snapshot.TextOf("Capacity")); CheckEqual("new Variation(2, 3)", snapshot.TextOf("Rate")); CheckEqual("Axis.Z", snapshot.TextOf("Up")); } public void HoldsSlabsAsSource() { StandardEmitterDef def = Def(slabs: new[] { Slab.Above(Axis.Z, 0) }); CheckEqual("new[] { Slab.Above(Axis.Z, 0) }", new TunableSnapshot(def).TextOf("ClipSlabs")); } public void HoldsNoSlabsAsNull() { CheckEqual("null", new TunableSnapshot(Def()).TextOf("ClipSlabs")); } public void LeavesOutASpriteTheDefLacks() { CheckNull(new TunableSnapshot(Def()).TextOf("Sprite")); } public void HoldsASpriteAsTheStaticFieldThatNamesIt() { StandardEmitterDef def = Def(sprite: TestbedSprites.Star); CheckEqual("TestbedSprites.Star", new TunableSnapshot(def).TextOf("Sprite")); } public void LeavesOutASpriteNoStaticFieldHolds() { StandardEmitterDef def = Def(sprite: Effects.Flicker.Def.Sprite); CheckNull(new TunableSnapshot(def).TextOf("Sprite")); } public void LeavesOutAFunctionTheDefLacks() { CheckNull(new TunableSnapshot(Def()).TextOf("SizeOverLife")); } public void LeavesOutAFunctionMadeOfCode() { StandardEmitterDef def = Def(sizeOverLife: new CallbackFunction(x => 1 - x)); CheckNull(new TunableSnapshot(def).TextOf("SizeOverLife")); } public void HoldsAFunctionAsSource() { StandardEmitterDef def = Def(alphaOverLife: new PiecewiseLinear((0, 1), (1, 0))); CheckEqual("new PiecewiseLinear((0, 1), (1, 0))", new TunableSnapshot(def).TextOf("AlphaOverLife")); } public void HoldsAGradientAsSourceAndKeepsItWhenTheGradientIsReplaced() { StandardEmitterDef def = Def(); var snapshot = new TunableSnapshot(def); string before = snapshot.TextOf("TintOverLife"); def.TintOverLife.Gradient = Gradient.Constant(Colors.Red); CheckTrue(before.StartsWith("Tint(new Gradient(")); CheckTrue(before.Contains("Rgb(255, 255, 255)")); CheckEqual(before, snapshot.TextOf("TintOverLife")); } public void KeepsTheValuesItWasTakenWith() { StandardEmitterDef def = Def(capacity: 10); var snapshot = new TunableSnapshot(def); foreach (TunableProperty property in Tunables.Of(def)) { if (property.Name == "Capacity") { property.Set(def, 25); } } CheckEqual("10", snapshot.TextOf("Capacity")); } } public class TextOfTests : TestFixture { public void APropertyItDoesNotHold_IsNull() { CheckNull(new TunableSnapshot(Def()).TextOf("NoSuchProperty")); } } } }