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