using System; using NUnit.Framework; using NUnit.Framework.Constraints; using UnityEngine.Profiling; namespace UnityEngine.TestTools.Constraints { /// /// An NUnit test constraint class to test whether a given block of code makes any GC allocations. /// /// Use this class with NUnit's Assert.That() method to make assertions about the GC behaviour of your code. The constraint executes the delegate you provide, and checks if it has caused any GC memory to be allocated. If any GC memory was allocated, the constraint passes; otherwise, the constraint fails. /// /// Usually you negate this constraint to make sure that your delegate does not allocate any GC memory. This is easy to do using the Is class: /// /// /// /// using NUnit.Framework; /// using UnityEngine.TestTools.Constraints; /// using Is = UnityEngine.TestTools.Constraints.Is; /// /// public class MyTestClass /// { /// [Test] /// public void SettingAVariableDoesNotAllocate() /// { /// Assert.That(() => { /// int a = 0; /// a = 1; /// }, Is.Not.AllocatingGCMemory()); /// } /// } /// /// public class AllocatingGCMemoryConstraint : Constraint { private class AllocatingGCMemoryResult : ConstraintResult { private readonly int diff; public AllocatingGCMemoryResult(IConstraint constraint, object actualValue, int diff) : base(constraint, actualValue, diff > 0) { this.diff = diff; } public override void WriteMessageTo(MessageWriter writer) { if (diff == 0) writer.WriteMessageLine("The provided delegate did not make any GC allocations."); else writer.WriteMessageLine("The provided delegate made {0} GC allocation(s).", diff); } } private ConstraintResult ApplyTo(Action action, object original) { var recorder = Recorder.Get("GC.Alloc"); // The recorder was created enabled, which means it captured the creation of the Recorder object itself, etc. // Disabling it flushes its data, so that we can retrieve the sample block count and have it correctly account // for these initial allocations. recorder.enabled = false; #if !UNITY_WEBGL recorder.FilterToCurrentThread(); #endif recorder.enabled = true; try { action(); } finally { recorder.enabled = false; #if !UNITY_WEBGL recorder.CollectFromAllThreads(); #endif } return new AllocatingGCMemoryResult(this, original, recorder.sampleBlockCount); } /// /// Applies GC memory constraint to the test. /// /// An object to apply the GC constraint to. Should be a . /// A ConstraintResult /// Throws a if the provided object is null. /// Throws a if the provided object is not a . public override ConstraintResult ApplyTo(object obj) { if (obj == null) throw new ArgumentNullException(); TestDelegate d = obj as TestDelegate; if (d == null) throw new ArgumentException(string.Format("The actual value must be a TestDelegate but was {0}", obj.GetType())); return ApplyTo(() => d.Invoke(), obj); } /// /// Test whether the constraint is satisfied by a given reference. /// The default implementation simply dereferences the value but /// derived classes may override it to provide for delayed processing. /// /// The type of the actual value delegate to be tested. /// A reference to the value delegate to be tested /// A ConstraintResult /// Throws a if the provided delegate is null. public override ConstraintResult ApplyTo(ActualValueDelegate del) { if (del == null) throw new ArgumentNullException(); return ApplyTo(() => del.Invoke(), del); } /// /// The Description of what this constraint tests, for to use in messages and in the ConstraintResult. /// public override string Description { get { return "allocates GC memory"; } } } }