【Unity】アサートまとめ【Assertions】
2020年9月29日
混乱するのでまとめ
よく使うのは
IsNotNull オブジェクトをしっかり握っているか?等
IsTrue ファイルが有るか?等
AreNotApproximatelyEqual floatのゼロ除算防止
ぶっちゃけ IsTrue あれば事足りる。
Assert.AreApproximatelyEqual()
ほぼ等しければ通過。0.00001f.以上差があればアサート
public static void AreApproximatelyEqual( float expected, float actual);
public static void AreApproximatelyEqual( float expected, float actual, string message);
public static void AreApproximatelyEqual( float expected, float actual, float tolerance);
public static void AreApproximatelyEqual( float expected, float actual, float tolerance, string message);
こんな感じ?
float distance = 何かしらの距離
UnityEngine.Assertions.Assert.AreApproximatelyEqual( 0.0f, distance, "接触してないよ");
// 接触している想定で処理
Assert.AreEqual
等しければ通過。違えばアサート。
public static void AreEqual (Object expected, Object actual, string message);
public static void AreEqual (sbyte expected, sbyte actual, string message);
public static void AreEqual (byte expected, byte actual, string message);
public static void AreEqual (char expected, char actual, string message);
public static void AreEqual (short expected, short actual, string message);
public static void AreEqual (ushort expected, ushort actual);
public static void AreEqual (sbyte expected, sbyte actual);
public static void AreEqual (byte expected, byte actual);
public static void AreEqual (char expected, char actual);
public static void AreEqual (short expected, short actual);
public static void AreEqual (ushort expected, ushort actual, string message);
public static void AreEqual (int expected, int actual, string message);
public static void AreEqual (uint expected, uint actual);
public static void AreEqual (int expected, int actual);
public static void AreEqual (uint expected, uint actual, string message);
public static void AreEqual (long expected, long actual);
public static void AreEqual (long expected, long actual, string message);
public static void AreEqual (ulong expected, ulong actual);
public static void AreEqual (ulong expected, ulong actual, string message);
public static void AreEqual (T expected, T actual);
public static void AreEqual (T expected, T actual, string message);
public static void AreEqual (T expected, T actual, string message, IEqualityComparer<T> comparer);
こんな感じ?
var type = Type();
UnityEngine.Assertions.Assert.AreEqual( type, typeof( HogeBehaviour), "HogeBehaviourでないよ");
Assert.AreNotApproximatelyEqual
ほぼ等しくなければ通過。差が0.00001f 以内ならアサート
public static void AreNotApproximatelyEqual (float expected, float actual);
public static void AreNotApproximatelyEqual (float expected, float actual, string message);
public static void AreNotApproximatelyEqual (float expected, float actual, float tolerance);
public static void AreNotApproximatelyEqual (float expected, float actual, float tolerance, string message);
こんな感じ?
float delta = Time.deltaTime;
UnityEngine.Assertions.Assert.AreNotApproximatelyEqual( 0.0f, delta), "ゼロ除算しちゃうよ");
var a = 10.0f / delta;
Assert.IsFalse
条件が false のとき通過。trueのときアサート発生
public static void IsFalse (bool condition);
public static void IsFalse (bool condition, string message);
こんな感じ?
static bool isInitialized = false;
void Awake()
{
UnityEngine.Assertions.Assert.IsFalse( isInitialized, "多重初期化してない?");
isInitialized = true;
}
Assert.IsNotNull
値が null 以外なら通過。 nullならアサート
public static void IsNotNull (Object value, string message);
public static void IsNotNull (T value);
public static void IsNotNull (T value, string message);
こんな感じ?
var rigidbody = GetComponent<Rigidbody>();
UnityEngine.Assertions.Assert.IsNotNull( rigidbody, "剛体ないやん");
Assert.IsNull
値が null なら通過。null以外ならアサート。
public static void IsNull (Object value, string message);
public static void IsNull (T value);
public static void IsNull (T value, string message);
こんな感じ?
UnityEngine.Assertions.Assert.IsNull( array[index], "すでに入ってない?");
array[index] = hoge;
Assert.IsTrue
値が true なら通過。false ならアサート。
public static void IsTrue (bool condition);
public static void IsTrue (bool condition, string message);
こんな感じ?
Dictionary<A,B> m_Map;
UnityEngine.Assertions.Assert.IsTrue( m_Map.ContainsKey( key), "辞書の中にないよ");
var value = m_Map[key];