Skip to content

Commit 3e4a74a

Browse files
committed
Add missing script validators
1 parent 2aa997b commit 3e4a74a

File tree

9 files changed

+125
-15
lines changed

9 files changed

+125
-15
lines changed
File renamed without changes.

Assets/BasicExample/Tests/Editor/AssetValidators/LevelValidator.cs renamed to Assets/BasicExample/Tests/Editor/Validators/LevelValidator.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,45 @@
22
// This software is released under the MIT License.
33

44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using BasicExample.Level;
78
using NUnit.Framework;
89
using UnityEditor;
910
using UnityEditor.SceneManagement;
1011
using UnityEngine;
1112

12-
namespace BasicExample.Editor.AssetValidators
13+
namespace BasicExample.Editor.Validators
1314
{
1415
/// <summary>
15-
/// Scenes/Levels/下のすべてのSceneに対して、次のコンポーネントが設置されていることを検証する
16-
/// - <see cref="BasicExample.Level.SpawnPoint"/>
17-
/// - <see cref="BasicExample.Level.ExitPoint"/>
16+
/// Scenes/Levels/ 下のすべての Scene に対して、次のコンポーネントが設置されていることを検証する
17+
/// <list type="bullet">
18+
/// <item><see cref="BasicExample.Level.SpawnPoint"/></item>
19+
/// <item><see cref="BasicExample.Level.ExitPoint"/></item>
20+
/// </list>
1821
/// </summary>
1922
/// <remarks>
20-
/// <see cref="ValueSourceAttribute"/>の応用例
23+
/// <see cref="TestCaseSourceAttribute"/> の使用例
2124
/// </remarks>
25+
[TestFixture]
2226
public class LevelValidator
2327
{
24-
private static IEnumerable<string> Levels => AssetDatabase
28+
private static IEnumerable<TestCaseData> Levels => AssetDatabase
2529
.FindAssets("t:SceneAsset", new[] { "Assets/BasicExample/Scenes/Levels" })
26-
.Select(AssetDatabase.GUIDToAssetPath);
30+
.Select(AssetDatabase.GUIDToAssetPath)
31+
.Select(path => new TestCaseData(path).SetName(Path.GetFileName(path)));
2732

28-
[Test]
29-
public void Levels下のSceneにSpawnPointが1つ設置されていること([ValueSource(nameof(Levels))] string path)
33+
[TestCaseSource(nameof(Levels))]
34+
public void Levels下のSceneにSpawnPointが1つ設置されていること(string path)
3035
{
3136
EditorSceneManager.OpenScene(path);
3237
var spawnPoints = Object.FindObjectsOfType<SpawnPoint>();
3338

3439
Assert.That(spawnPoints, Has.Length.EqualTo(1));
3540
}
3641

37-
[Test]
38-
public void Levels下のSceneにExitPointが設置されていること([ValueSource(nameof(Levels))] string path)
42+
[TestCaseSource(nameof(Levels))]
43+
public void Levels下のSceneにExitPointが設置されていること(string path)
3944
{
4045
EditorSceneManager.OpenScene(path);
4146
var exitPoints = Object.FindObjectsOfType<ExitPoint>();

Assets/BasicExample/Tests/Editor/AssetValidators/LevelValidator.cs.meta renamed to Assets/BasicExample/Tests/Editor/Validators/LevelValidator.cs.meta

File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using NUnit.Framework;
8+
using UnityEditor;
9+
using UnityEngine;
10+
11+
namespace BasicExample.Editor.Validators
12+
{
13+
/// <summary>
14+
/// Assets/ 下のすべての Prefab に対して、missing script がないことを検証する.
15+
/// </summary>
16+
/// <remarks>
17+
/// <see cref="TestCaseSourceAttribute"/> の使用例
18+
/// </remarks>
19+
[TestFixture]
20+
public class PrefabValidator
21+
{
22+
private static IEnumerable<TestCaseData> Prefabs => AssetDatabase
23+
.FindAssets("t:Prefab", new string[] { "Assets/" })
24+
.Select(AssetDatabase.GUIDToAssetPath)
25+
.Select(path => new TestCaseData(path).SetName(Path.GetFileName(path)));
26+
27+
private static void AssertMissingScriptRecursive(GameObject gameObject)
28+
{
29+
var components = gameObject.GetComponents<Component>();
30+
foreach (var component in components)
31+
{
32+
Assert.That(component, Is.Not.Null, $"Component in {gameObject.name}");
33+
}
34+
35+
foreach (Transform child in gameObject.transform)
36+
{
37+
AssertMissingScriptRecursive(child.gameObject);
38+
}
39+
}
40+
41+
[TestCaseSource(nameof(Prefabs))]
42+
public void MissingScriptがないこと(string path)
43+
{
44+
var root = AssetDatabase.LoadAssetAtPath<GameObject>(path);
45+
AssertMissingScriptRecursive(root);
46+
}
47+
}
48+
}

Assets/BasicExample/Tests/Editor/Validators/PrefabValidator.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/BasicExample/Tests/Editor/AssetValidators/RaceValidator.cs renamed to Assets/BasicExample/Tests/Editor/Validators/RaceValidator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
using UnityEditor;
1212
using Object = UnityEngine.Object;
1313

14-
namespace BasicExample.Editor.AssetValidators
14+
namespace BasicExample.Editor.Validators
1515
{
1616
/// <summary>
17-
/// ScriptableObjects/Races/下のすべてのSOに対して、フィールドの設定漏れがないことを検証する
17+
/// ScriptableObjects/Races/ 下のすべての ScriptableObject に対して、フィールドの設定漏れがないことを検証する.
1818
/// </summary>
1919
/// <remarks>
20-
/// <see cref="ValueSourceAttribute"/>の応用例として書いたものの、フィールドは素直にRace型を使うほうがよさそう
20+
/// <see cref="ValueSourceAttribute"/> の応用例として書いたものの、フィールドは素直にRace型を使うほうがよさそう
2121
/// </remarks>
2222
public class RaceValidator
2323
{
2424
// Race型のScriptableObjectを列挙
2525
private static IEnumerable<string> RacePaths => AssetDatabase
26-
.FindAssets("t:Race", new [] { "Assets/BasicExample/ScriptableObjects/Races" })
26+
.FindAssets("t:Race", new[] { "Assets/BasicExample/ScriptableObjects/Races" })
2727
.Select(AssetDatabase.GUIDToAssetPath);
2828

2929
// Race型のフィールドを列挙

Assets/BasicExample/Tests/Editor/AssetValidators/RaceValidator.cs.meta renamed to Assets/BasicExample/Tests/Editor/Validators/RaceValidator.cs.meta

File renamed without changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using NUnit.Framework;
8+
using UnityEditor;
9+
using UnityEditor.SceneManagement;
10+
using UnityEngine;
11+
12+
namespace BasicExample.Editor.Validators
13+
{
14+
/// <summary>
15+
/// Assets/ 下のすべての Scene に対して、missing script がないことを検証する.
16+
/// </summary>
17+
/// <remarks>
18+
/// <see cref="TestCaseSourceAttribute"/> の使用例
19+
/// </remarks>
20+
[TestFixture]
21+
public class SceneValidator
22+
{
23+
private static IEnumerable<TestCaseData> Scenes => AssetDatabase
24+
.FindAssets("t:SceneAsset", new string[] { "Assets/" })
25+
.Select(AssetDatabase.GUIDToAssetPath)
26+
.Select(path => new TestCaseData(path).SetName(Path.GetFileName(path)));
27+
28+
private static void AssertMissingScriptRecursive(GameObject gameObject)
29+
{
30+
var components = gameObject.GetComponents<Component>();
31+
foreach (var component in components)
32+
{
33+
Assert.That(component, Is.Not.Null, $"Component in {gameObject.name}");
34+
}
35+
36+
foreach (Transform child in gameObject.transform)
37+
{
38+
AssertMissingScriptRecursive(child.gameObject);
39+
}
40+
}
41+
42+
[TestCaseSource(nameof(Scenes))]
43+
public void MissingScriptがないこと(string path)
44+
{
45+
var scene = EditorSceneManager.OpenScene(path);
46+
foreach (var root in scene.GetRootGameObjects())
47+
{
48+
AssertMissingScriptRecursive(root);
49+
}
50+
}
51+
}
52+
}

Assets/BasicExample/Tests/Editor/Validators/SceneValidator.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)