add unit tests

This commit is contained in:
PoChangSu
2020-06-25 19:27:09 +09:00
parent 3ef81b2c8e
commit 73309bff07
4 changed files with 78 additions and 17 deletions

View File

@@ -93,7 +93,10 @@ namespace VRM
public bool RemoveVertexColor = false;
#endregion
private const int MAX_LENGTH = 64;
public static bool IsFileNameLengthTooLong(string fileName)
{
return fileName.Length > 64;
}
public struct Validation
{
@@ -234,7 +237,7 @@ namespace VRM
foreach (var material in materials)
{
if (material.name.Length > MAX_LENGTH)
if (IsFileNameLengthTooLong(material.name))
yield return Validation.Error(string.Format("FileName '{0}' is too long. ", material.name));
}
@@ -259,29 +262,23 @@ namespace VRM
foreach (var textureName in textureNameList)
{
if (textureName.Length > MAX_LENGTH)
if (IsFileNameLengthTooLong(textureName))
yield return Validation.Error(string.Format("FileName '{0}' is too long. ", textureName));
}
var vrmMeta = Source.GetComponent<VRMMeta>();
if (vrmMeta != null)
if (vrmMeta != null && vrmMeta.Meta != null && vrmMeta.Meta.Thumbnail != null)
{
if (vrmMeta.Meta != null)
{
if (vrmMeta.Meta.Thumbnail != null)
{
var thumbnailName = vrmMeta.Meta.Thumbnail.name;
if (thumbnailName.Length > MAX_LENGTH)
yield return Validation.Error(string.Format("FileName '{0}' is too long. ", thumbnailName));
}
}
var thumbnailName = vrmMeta.Meta.Thumbnail.name;
if (IsFileNameLengthTooLong(thumbnailName))
yield return Validation.Error(string.Format("FileName '{0}' is too long. ", thumbnailName));
}
var meshFilters = Source.GetComponentsInChildren<MeshFilter>();
var meshesName = meshFilters.Select(x => x.sharedMesh.name).Distinct();
foreach (var meshName in meshesName)
{
if (meshName.Length > MAX_LENGTH)
if (IsFileNameLengthTooLong(meshName))
yield return Validation.Error(string.Format("FileName '{0}' is too long. ", meshName));
}
@@ -289,7 +286,7 @@ namespace VRM
var skinnedmeshesName = skinnedmeshRenderers.Select(x => x.sharedMesh.name).Distinct();
foreach (var skinnedmeshName in skinnedmeshesName)
{
if (skinnedmeshName.Length > MAX_LENGTH)
if (IsFileNameLengthTooLong(skinnedmeshName))
yield return Validation.Error(string.Format("FileName '{0}' is too long. ", skinnedmeshName));
}
}

View File

@@ -0,0 +1,48 @@
using NUnit.Framework;
using System.Linq;
using System.IO;
namespace VRM
{
public class InvalidFileNameTest
{
[Test]
[TestCase("VRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMVRMV", true)]
[TestCase("VRMFormatVRMFormatVRMFormatVRMFormatVRMFormatVRMFormatVRMFormat", false)]
[TestCase("UniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRM", true)]
[TestCase("UniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniVRMUniV", false)]
[TestCase("AliciaAliciaAliciaAliciaAliciaAliciaAliciaAliciaAliciaAliciaAliciaAlicia", true)]
public void DetectFileNameLength(string fileName, bool isIllegal)
{
var result = VRMExportSettings.IsFileNameLengthTooLong(fileName);
Assert.AreEqual(result, isIllegal);
}
[Test]
[TestCase("\u0000\u0042\u0062", true)]
[TestCase("\u0045\u0046\u0047\u0065\u0068\u0036", false)]
[TestCase("\u0043\u0045\u0047\u007F", true)]
[TestCase("\u0000\u0042\u0062", true)]
[TestCase("\u003A\u0039\u005C\u0060\u0074", false)]
[TestCase("\u005D\u006F\u001C\u007A\u0036\u0049", true)]
public void DetectControlCharacters(string fileName, bool isIllegal)
{
var result = fileName.Any(x => char.IsControl(x));
Assert.AreEqual(result, isIllegal);
}
[Test]
[TestCase("VRM|Alicia?VRM", true)]
[TestCase("UniVRMUniVRM:UniVRM", true)]
[TestCase("VRMIsVRFileFormat", false)]
[TestCase("Alicia<Alicia>Alicia", true)]
[TestCase("UniVRMIsVRMImplementationInUnityPlatform", false)]
[TestCase("Avator*Avator/Avator", true)]
public void DetectInvalidCharacters(string fileName, bool isIllegal)
{
char[] invalidPathChars = Path.GetInvalidFileNameChars();
var result = fileName.Any(x => invalidPathChars.Contains(x));
Assert.AreEqual(result, isIllegal);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8511ed091b59bca4da4fd280693b7c82
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -2,7 +2,8 @@
"name": "UniVRM.Editor.Tests",
"references": [
"VRM",
"UniJSON"
"UniJSON",
"UniVRM.Editor"
],
"optionalUnityReferences": [
"TestAssemblies"
@@ -11,5 +12,9 @@
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
}