Able
Askowl Base Library Enabler
PlayModeTests.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 using System;
4 using System.Collections;
5 using System.IO;
6 using System.Text.RegularExpressions;
7 using UnityEditor;
8 using UnityEngine;
9 using UnityEngine.Assertions;
10 using UnityEngine.UI;
11 using Object = UnityEngine.Object;
12 
13 namespace Askowl {
14  /// <a href="http://bit.ly/2NZZYKj">PlayModeController wrapper with asserts</a> <inheritdoc />
16  /*
17  * Built-In Helpers
18  * https://docs.unity3d.com/Manual/PlaymodeTestFramework.html
19  * LogAssert.Expect(LogType, string);
20  * LogAssert.Expect(LogType, Regex);
21  */
22 
23  /// <a href="http://bit.ly/2NZZYKj">Load scene. Assert on failure</a> <inheritdoc />
24  protected override IEnumerator LoadScene(string sceneName) {
25  yield return base.LoadScene(sceneName);
26  CheckPattern(new Regex($"^({Scene.name}|.*/{Scene.name})"), sceneName);
27  }
28 
29  /// <a href="http://bit.ly/2NTezHo">Components.Find, assert on failure</a>
30  protected static T Component<T>(string path) where T : Component {
31  var component = Components.Find<T>(path);
32  Assert.IsNotNull(value: component, message: $"'{path}' not found");
33  return component;
34  }
35 
36  /// <a href="http://bit.ly/2Rj0WQ6">FindObject{GameObject} shortcut</a>
37  protected static GameObject FindGameObject(string name) => FindObject<GameObject>(name);
38 
39  /// <a href="http://bit.ly/2Rj0WQ6">Objects.Find, assert if none found</a>
40  protected static T FindObject<T>(string name) where T : Object {
41  var objects = Objects.FindAll<T>(name);
42  Assert.AreNotEqual(0, objects?.Length);
43  return objects?.Length > 0 ? objects[0] : null;
44  }
45 
46  /// <a href="http://bit.ly/2NZZYKj">IsDisplayingInUI, assert if not visible/invisible as expected after nn frames</a>
47  public IEnumerator IsDisplayingInUi(string path, bool visible = true, int repeats = 300) {
48  for (var count = 0; count < repeats; count++) {
49  if (IsDisplayingInUi(Components.Find<RectTransform>(path)) == visible) yield break;
50 
51  yield return null;
52  }
53 
54  Assert.IsFalse(true, $"IsDisplayingInUI '{path}' failed to act as expected");
55  }
56 
57  /// <a href="http://bit.ly/2Oq9xBM">Push button, assert if it can't be found</a>
58  protected IEnumerator PushButton(string path) {
59  yield return PushButton(Component<Button>(path)); // so it uses test version with assert
60  }
61 
62  /// <a href="http://bit.ly/2OrQoPH">Check string against regex, assert if no match</a>
63  protected static void CheckPattern(Regex regex, string against) {
64  MatchCollection matches = regex.Matches(against);
65  Assert.AreEqual(matches.Count, 1, against);
66  }
67 
68  /// <a href=""></a> //#TBD#//
69  protected static void Fail(string message) => throw new Exception(message);
70 
71  #region Unity Editor Only Methods
72  #if UNITY_EDITOR
73  /// <a href=""></a> //#TBD#//
74  protected static void SelectMenuItem(string path) =>
75  Assert.IsTrue(EditorApplication.ExecuteMenuItem(path));
76  #endif
77  #endregion
78  }
79 }
Definition: Clock.cs:3
static T FindObject< T >(string name)
Objects.Find, assert if none found
static void Fail(string message)
//#TBD#//
override IEnumerator LoadScene(string sceneName)
Load scene. Assert on failure Load a scene by name (must be in build)
static GameObject FindGameObject(string name)
FindObject{GameObject} shortcut
IEnumerator IsDisplayingInUi(string path, bool visible=true, int repeats=300)
IsDisplayingInUI, assert if not visible/invisible as expected after nn frames
PlayModeController wrapper with asserts
IEnumerator PushButton(string path)
Push button, assert if it can&#39;t be found
static T Component< T >(string path)
Components.Find, assert on failure
static void CheckPattern(Regex regex, string against)
Check string against regex, assert if no match
Control a running scene (skeleton implementation)