Tanoda
AssertHelper.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using UnityEngine.Assertions;
11using System;
12using System.Linq;
13using System.Diagnostics;
14using System.Collections.Generic;
15
16public static class AssertHelper {
17
18 [Conditional("UNITY_EDITOR")]
19 public static void AssertRuntimeOnly(string message = null) {
20 message = message ?? "Assert failed because game was not in Play Mode.";
21 Assert.IsTrue(Application.isPlaying, message);
22 }
23
24 [Conditional("UNITY_EDITOR")]
25 public static void AssertEditorOnly(string message = null) {
26 message = message ?? "Assert failed because game was in Play Mode.";
27 Assert.IsFalse(Application.isPlaying, message);
28 }
29
30 [Conditional("UNITY_ASSERTIONS")]
31 public static void Implies(bool condition, bool result, string message = "") {
32 if (condition) {
33 Assert.IsTrue(result, message);
34 }
35 }
36
37 [Conditional("UNITY_ASSERTIONS")]
38 public static void Implies(bool condition, Func<bool> result, string message = "") {
39 if (condition) {
40 Implies(condition, result(), message);
41 }
42 }
43
44 [Conditional("UNITY_ASSERTIONS")]
45 public static void Implies(string conditionName, bool condition, string resultName, bool result) {
46 Implies(condition, result, "When " + conditionName + " is true, " + resultName + " must always be true.");
47 }
48
49 [Conditional("UNITY_ASSERTIONS")]
50 public static void Implies(string conditionName, bool condition, string resultName, Func<bool> result) {
51 if (condition) {
52 Implies(conditionName, condition, resultName, result());
53 }
54 }
55
56 [Conditional("UNITY_ASSERTIONS")]
57 public static void Contains<T>(T value, IEnumerable<T> collection, string message = "") {
58 if (!collection.Contains(value)) {
59 string result = "The value " + value + " was not found in the collection [";
60
61 bool isFirst = true;
62 foreach (T v in collection) {
63 if (!isFirst) {
64 result += ", ";
65 isFirst = false;
66 }
67
68 result += v.ToString();
69 }
70
71 result += "]\n" + message;
72 Assert.IsTrue(false, result);
73 }
74 }
75}