A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System.IO;
3using System.Reflection;
4using System.Runtime.CompilerServices;
5using NUnit.Framework;
6using UnityEngine;
7
8namespace UnityEditor.Rendering.Tests
9{
10 // Nested under this namespace otherwise the compiled used the wrong PackageInfo (it used the one found in UnityEditor, despite the using alias).
11 using PackageInfo = UnityEditor.PackageManager.PackageInfo;
12
13 class RenderGraphViewerTests
14 {
15 const string kExpectedCurrentFilePath = "Tests/Editor/RenderGraphViewerTests.cs";
16
17 static IEnumerable<TestCaseData> ScriptPathToAssetPathTestCases()
18 {
19 var packageInfo = PackageInfo.FindForAssembly(Assembly.GetExecutingAssembly());
20 var projectPath = Path.GetDirectoryName(Application.dataPath);
21 var relativePackagePath = Path.GetRelativePath(projectPath, packageInfo.resolvedPath);
22
23 var scenarios = new []
24 {
25 // Script in project
26 ( "Assets/File.cs", "Assets/File.cs" ),
27
28 // Script in package (this will work regardless of where the package is located,
29 // i.e. in Library, embedded in Packages folder, via "file:<path>" dependency or Git URL)
30 ( $"{relativePackagePath}/File.cs".Replace(@"\", "/"), $"{packageInfo.assetPath}/File.cs" ),
31
32 // Unknown path
33 ( "Unknown/Path/File.cs", null )
34 };
35
36 // Relative paths
37 foreach (var (inputPath, expectedResult) in scenarios)
38 {
39 // Relative paths, Unity separators
40 yield return new TestCaseData(inputPath, expectedResult);
41
42 // Relative paths with ./ prefix, as present in MonoScript script path, Unity separators
43 yield return new TestCaseData($"./{inputPath}", expectedResult);
44
45 // Absolute paths, Unity separators
46 yield return new TestCaseData($"{projectPath}/{inputPath}", expectedResult);
47#if PLATFORM_WINDOWS
48 // Relative paths, Windows separators
49 yield return new TestCaseData(inputPath.Replace("/", @"\"), expectedResult);
50
51 // Relative paths with ./ prefix, as present in MonoScript script path, Windows separators
52 yield return new TestCaseData($"./{inputPath}".Replace("/", @"\"), expectedResult);
53
54 // Absolute paths, Windows separators
55 yield return new TestCaseData($"{projectPath}/{inputPath}".Replace("/", @"\"), expectedResult);
56#endif
57 }
58 }
59
60 [Test, TestCaseSource(nameof(ScriptPathToAssetPathTestCases))]
61 public void ScriptPathToAssetPath(string absoluteOrRelativePath, string expectedResult)
62 {
63 // expectedResult == null --> input returned untransformed
64 Assert.AreEqual(expectedResult ?? absoluteOrRelativePath, RenderGraphViewer.ScriptPathToAssetPath(absoluteOrRelativePath));
65 }
66
67 [Test]
68 public void CallerFilePathToRelative()
69 {
70 var absolutePath = GetCallerFilePath();
71 var packageInfo = PackageInfo.FindForPackageName("com.unity.render-pipelines.core");
72 Assert.IsNotNull(packageInfo);
73
74 var expectedPath = $"{packageInfo.assetPath}/{kExpectedCurrentFilePath}";
75 Assert.AreEqual(expectedPath, RenderGraphViewer.ScriptPathToAssetPath(absolutePath));
76 }
77
78 string GetCallerFilePath([CallerFilePath] string filePath = null) => filePath;
79 }
80}