Skip to content

Commit 3652a59

Browse files
author
Benoit Hudson
committed
UNI-22052: add unit tests
Oh shoot, they fail.
1 parent 46bcc5b commit 3652a59

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ private static string GetRelativePath(string fromDir, string toDir) {
183183

184184
private static string NormalizeRelativePath(string relativePath)
185185
{
186+
// The empty path is the current directory.
187+
if (string.IsNullOrEmpty(relativePath)) {
188+
relativePath = ".";
189+
}
190+
186191
// Normalize to the platform path separator.
187192
relativePath = relativePath.Replace(
188193
Path.AltDirectorySeparatorChar,
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using System.IO;
4+
using System.Collections.Generic;
5+
using FbxExporters.EditorTools;
6+
using NUnit.Framework;
7+
8+
namespace FbxExporters.UnitTests
9+
{
10+
public class FbxExportSettingsTest {
11+
ExportSettings m_originalSettings;
12+
13+
static System.Reflection.FieldInfo s_InstanceField; // static
14+
static System.Reflection.FieldInfo s_SavePathField; // member
15+
static System.Reflection.MethodInfo s_NormalizeRelativePath; // static
16+
static System.Reflection.MethodInfo s_GetRelativePath; // static
17+
18+
static FbxExportSettingsTest() {
19+
var privates = System.Reflection.BindingFlags.NonPublic
20+
| System.Reflection.BindingFlags.Static
21+
| System.Reflection.BindingFlags.Instance;
22+
var t = typeof(ExportSettings);
23+
24+
s_SavePathField = t.GetField("convertToModelSavePath", privates);
25+
Assert.IsNotNull(s_SavePathField, "convertToModelSavePath");
26+
27+
s_NormalizeRelativePath = t.GetMethod("NormalizeRelativePath", privates);
28+
Assert.IsNotNull(s_NormalizeRelativePath, "NormalizeRelativePath");
29+
30+
s_GetRelativePath = t.GetMethod("GetRelativePath", privates);
31+
Assert.IsNotNull(s_GetRelativePath, "GetRelativePath");
32+
33+
// static fields can't be found through inheritance with GetField.
34+
// if we change the inheritance diagram, we have to change t.BaseType here.
35+
s_InstanceField = t.BaseType.GetField("s_Instance", privates);
36+
Assert.IsNotNull(s_InstanceField, "s_Instance");
37+
38+
#if ENABLE_COVERAGE_TEST
39+
FbxSdk.CoverageTester.RegisterReflectionCall(
40+
typeof(UnitTests).GetMethod("NormalizePath"), s_NormalizeRelativePath);
41+
FbxSdk.CoverageTester.RegisterReflectionCall(
42+
typeof(UnitTests).GetMethod("GetRelativePath"), s_GetRelativePath);
43+
#endif
44+
}
45+
46+
#if ENABLE_COVERAGE_TEST
47+
[Test]
48+
public void TestCoverage()
49+
{
50+
FbxSdk.CoverageTester.TestCoverage(typeof(ExportSettings), this.GetType());
51+
}
52+
#endif
53+
54+
55+
string NormalizePath(string path) {
56+
return FbxSdk.Invoker.InvokeStatic<string>(s_NormalizeRelativePath, path);
57+
}
58+
59+
string GetRelativePath(string a, string b) {
60+
return FbxSdk.Invoker.InvokeStatic<string>(s_GetRelativePath, a, b);
61+
}
62+
63+
[NUnit.Framework.SetUp]
64+
public void SetUp()
65+
{
66+
var settings = (ExportSettings)s_InstanceField.GetValue(null);
67+
m_originalSettings = settings;
68+
69+
// Clear out the current instance and create a new one (but keep the original around).
70+
s_InstanceField.SetValue(null, null);
71+
s_InstanceField.SetValue(null, ScriptableObject.CreateInstance<ExportSettings>());
72+
}
73+
74+
[NUnit.Framework.TearDown]
75+
public void TearDown()
76+
{
77+
// Destroy the test settings and restore the original.
78+
// The original might be null -- not a problem.
79+
var settings = (ExportSettings)s_InstanceField.GetValue(null);
80+
ScriptableObject.DestroyImmediate(settings);
81+
82+
s_InstanceField.SetValue(null, m_originalSettings);
83+
}
84+
85+
86+
[Test]
87+
public void TestNormalizePath()
88+
{
89+
var path = "/a\\b/c/\\";
90+
var norm = NormalizePath(path);
91+
Assert.AreEqual(Path.Combine("a", Path.Combine("b", "c")), norm);
92+
93+
path = "";
94+
norm = NormalizePath(path);
95+
Assert.AreEqual(".", norm);
96+
97+
path = "///";
98+
norm = NormalizePath(path);
99+
Assert.AreEqual(".", norm);
100+
}
101+
102+
[Test]
103+
public void TestGetRelativePath()
104+
{
105+
var from = "file:///a/b/c";
106+
var to = "http://google.com";
107+
var relative = GetRelativePath(from, to);
108+
Assert.IsNull(relative);
109+
110+
from = "file:///a/b/c";
111+
to = "file:///a/b/c/d/e";
112+
relative = GetRelativePath(from, to);
113+
Assert.AreEqual(Path.Combine("d", "e"), relative);
114+
115+
from = "file:///a/b/c/";
116+
to = "file:///a/b/c/d/e/";
117+
relative = GetRelativePath(from, to);
118+
Assert.AreEqual(Path.Combine("d", "e"), relative);
119+
120+
from = "file:///aa/bb/cc/dd/ee";
121+
to = "file:///aa/bb/cc";
122+
relative = GetRelativePath(from, to);
123+
Assert.AreEqual(Path.Combine("..", ".."), relative);
124+
125+
from = "file:///a/b/c/d/e/";
126+
to = "file:///a/b/c/";
127+
relative = GetRelativePath(from, to);
128+
Assert.AreEqual(Path.Combine("..", ".."), relative);
129+
130+
from = Path.Combine(Application.dataPath, "foo");
131+
to = Application.dataPath;
132+
relative = GetRelativePath(from, to);
133+
Assert.AreEqual("..", relative);
134+
135+
to = Path.Combine(Application.dataPath, "foo");
136+
relative = ExportSettings.ConvertToAssetRelativePath(to);
137+
Assert.AreEqual("foo", relative);
138+
}
139+
140+
[Test]
141+
public void TestGetSetFields()
142+
{
143+
var defaultRelativePath = ExportSettings.GetRelativeSavePath();
144+
Assert.AreEqual(ExportSettings.kDefaultSavePath, defaultRelativePath);
145+
146+
var defaultAbsolutePath = ExportSettings.GetAbsoluteSavePath();
147+
Assert.AreEqual(Path.Combine(Application.dataPath, ExportSettings.kDefaultSavePath),
148+
defaultAbsolutePath);
149+
150+
// set; check that the saved value is platform-independent,
151+
// but the relative save path function is platform-specific.
152+
ExportSettings.SetRelativeSavePath("/a\\b/c/\\");
153+
var convertToModelSavePath = s_SavePathField.GetValue(ExportSettings.instance);
154+
Assert.AreEqual("a/b/c", convertToModelSavePath);
155+
var platformPath = Path.Combine("a", Path.Combine("b", "c"));
156+
Assert.AreEqual(platformPath, ExportSettings.GetRelativeSavePath());
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)