A game about forced loneliness, made by TACStudios
1using System; 2using System.Text.RegularExpressions; 3using UnityEngine.TestTools.Logging; 4 5namespace UnityEngine.TestTools 6{ 7 /// <summary> 8 /// `LogAssert` lets you expect Unity log messages that would otherwise cause the test to fail. A test fails if Unity logs a message other than a regular log or warning message. Use `LogAssert` to check for an expected message in the log so that the test does not fail when Unity logs the message. 9 /// 10 /// Use `LogAssert.Expect` before running the code under test, as the check for expected logs runs at the end of each frame. 11 /// 12 /// A test also reports a failure, if an expected message does not appear, or if Unity does not log any regular log or warning messages. 13 /// </summary> 14 public static class LogAssert 15 { 16 /// <summary> 17 /// Verifies that a log message of a specified type appears in the log. A test won't fail from an expected error, assertion, or exception log message. It does fail if an expected message does not appear in the log. 18 /// If multiple LogAssert.Expect are used to expect multiple messages, they are expected to be logged in that order. 19 /// </summary> 20 /// <param name="type">A type of log to expect. It can take one of the [LogType enum](https://docs.unity3d.com/ScriptReference/LogType.html) values.</param> 21 /// <param name="message">A string value that should equate to the expected message.</param> 22 /// <example> 23 /// <code> 24 /// [Test] 25 /// public void LogAssertExample() 26 /// { 27 /// // Expect a regular log message 28 /// LogAssert.Expect(LogType.Log, "Log message"); 29 /// 30 /// // The test fails without the following expected log message 31 /// Debug.Log("Log message"); 32 /// 33 /// // An error log 34 /// Debug.LogError("Error message"); 35 /// 36 /// // Without expecting an error log, the test would fail 37 /// LogAssert.Expect(LogType.Error, "Error message"); 38 /// } 39 /// 40 /// </code> 41 /// </example> 42 public static void Expect(LogType type, string message) 43 { 44 LogScope.Current.ExpectedLogs.Enqueue(new LogMatch { LogType = type, Message = message }); 45 } 46 47 /// <summary> 48 /// Verifies that a log message of a specified type appears in the log. A test won't fail from an expected error, assertion, or exception log message. It does fail if an expected message does not appear in the log. 49 /// </summary> 50 /// <param name="type">A type of log to expect. It can take one of the [LogType enum](https://docs.unity3d.com/ScriptReference/LogType.html) values.</param> 51 /// <param name="message">A regular expression pattern to match the expected message.</param> 52 public static void Expect(LogType type, Regex message) 53 { 54 LogScope.Current.ExpectedLogs.Enqueue(new LogMatch { LogType = type, MessageRegex = message }); 55 } 56 57 /// <summary> 58 /// Verifies that a log message of any type appears in the log. A test won't fail from an expected error, assertion, or exception log message. It does fail if an expected message does not appear in the log. 59 /// If multiple LogAssert.Expect are used to expect multiple messages, they are expected to be logged in that order. 60 /// </summary> 61 /// <param name="message">A string value that should equate to the expected message.</param> 62 /// <example> 63 /// <code> 64 /// [Test] 65 /// public void LogAssertExample() 66 /// { 67 /// // Expect a log entry of any kind with this message 68 /// LogAssert.Expect("Log message"); 69 /// 70 /// // Logging the message does not cause the test to fail 71 /// Debug.LogError("Log message"); 72 /// } 73 /// 74 /// </code> 75 /// </example> 76 public static void Expect(string message) 77 { 78 LogScope.Current.ExpectedLogs.Enqueue(new LogMatch { LogType = null, Message = message }); 79 } 80 81 /// <summary> 82 /// Verifies that a log message of any type appears in the log. A test won't fail from an expected error, assertion, or exception log message. It does fail if an expected message does not appear in the log. 83 /// </summary> 84 /// <param name="message">A regular expression pattern to match the expected message.</param> 85 public static void Expect(Regex message) 86 { 87 LogScope.Current.ExpectedLogs.Enqueue(new LogMatch { LogType = null, MessageRegex = message }); 88 } 89 90 /// <summary> 91 /// Triggers an assertion when receiving any log messages and fails the test if some are unexpected messages. If multiple tests need to check for no received unexpected logs, consider using the <see cref="TestMustExpectAllLogsAttribute"/> attribute instead. 92 /// </summary> 93 public static void NoUnexpectedReceived() 94 { 95 LogScope.Current.NoUnexpectedReceived(); 96 } 97 98 /// <summary>Set this property to `true` to prevent unexpected error log messages from triggering an assertion. By default, it is `false`.</summary> 99 /// <returns>The value of the ignoreFailingMessages boolean property.</returns> 100 public static bool ignoreFailingMessages 101 { 102 get 103 { 104 return LogScope.Current.IgnoreFailingMessages; 105 } 106 set 107 { 108 if (value != LogScope.Current.IgnoreFailingMessages) 109 { 110 Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "\nIgnoreFailingMessages:" + (value ? "true" : "false")); 111 } 112 LogScope.Current.IgnoreFailingMessages = value; 113 } 114 } 115 } 116}