From 949fd3fc3680b6774e143601bd79d3c7cdf036fb Mon Sep 17 00:00:00 2001 From: Favo Yang Date: Sun, 4 Aug 2019 21:44:03 +0800 Subject: [PATCH] Fixed simplified behavior and added unit tests. --- CHANGELOG.md | 5 + Editor/AddressableImportRule.cs | 6 +- Tests.meta | 8 + Tests/Editor.meta | 8 + Tests/Editor/AddressableImportRuleTests.cs | 170 ++++++++++++++++++ .../Editor/AddressableImportRuleTests.cs.meta | 11 ++ ...ty.AddressableImporter.Editor.Tests.asmdef | 21 +++ ...dressableImporter.Editor.Tests.asmdef.meta | 7 + package.json | 2 +- 9 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 Tests.meta create mode 100644 Tests/Editor.meta create mode 100644 Tests/Editor/AddressableImportRuleTests.cs create mode 100644 Tests/Editor/AddressableImportRuleTests.cs.meta create mode 100644 Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef create mode 100644 Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef.meta diff --git a/CHANGELOG.md b/CHANGELOG.md index 900703a..be567da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,17 @@ All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.2.2] - 2019-08-05 + - Fixed simplified behavior. + - Added unit tests. + ## [0.2.1] - 2019-08-04 - Added address/group replacement. Working with regex rule to build the address based on information captured from the path. - Added path elements extraction for address/group replacement (i.e. `${PATH[0]}`) - Added option to automatically create groups if not exist, default False. - Added option to remove empty Asset Groups except the default group, default False. - Added option to define if labels from ruleset are added or replace the current ones. + - Added save, documentation buttons. - Improved documentation. ## [0.1.1] - 2019-07-19 diff --git a/Editor/AddressableImportRule.cs b/Editor/AddressableImportRule.cs index b30e9a6..608458e 100644 --- a/Editor/AddressableImportRule.cs +++ b/Editor/AddressableImportRule.cs @@ -131,9 +131,13 @@ public string ParseGroupReplacement(string assetPath) /// public string ParseAddressReplacement(string assetPath) { - if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(addressReplacement)) + if (string.IsNullOrWhiteSpace(path)) + return assetPath; + if (!simplified && string.IsNullOrWhiteSpace(addressReplacement)) return assetPath; // Parse path elements. + if (addressReplacement == null) + addressReplacement = ""; var replacement = AddressableImportRegex.ParsePath(assetPath, addressReplacement); // Parse this.path regex. // If Simplified is ticked, it's a pattern that matches any path, capturing the path, filename and extension. diff --git a/Tests.meta b/Tests.meta new file mode 100644 index 0000000..e0baf52 --- /dev/null +++ b/Tests.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a312f3ac4562e80499e7961787834959 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor.meta b/Tests/Editor.meta new file mode 100644 index 0000000..3f79b84 --- /dev/null +++ b/Tests/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: abf7fa99e4a685143b8c8ae6364d824c +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor/AddressableImportRuleTests.cs b/Tests/Editor/AddressableImportRuleTests.cs new file mode 100644 index 0000000..e5923dc --- /dev/null +++ b/Tests/Editor/AddressableImportRuleTests.cs @@ -0,0 +1,170 @@ + +using UnityEngine; +using UnityEditor; +using UnityEngine.AddressableAssets; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using UnityAddressableImporter.Helper; +using NUnit.Framework; +using UnityEngine.TestTools; + +namespace UnityAddressableImporter.Tests +{ + public class AddressableImportRuleTests + { + + [Test] + public void MatchWildcardTest() + { + AddressableImportRule rule = new AddressableImportRule(); + rule.matchType = AddressableImportRuleMatchType.Wildcard; + // Raw path + rule.path = "Assets/Sprites/"; + Assert.IsTrue(rule.Match("Assets/Sprites/cat/cat.png")); + Assert.IsFalse(rule.Match("Assets/Fbx/cat/cat.fbx")); + // '*' wildcard + rule.path = "Assets/Sprites/*/*.png"; + Assert.IsTrue(rule.Match("Assets/Sprites/cat/cat.png")); + Assert.IsFalse(rule.Match("Assets/Sprites/cat/cat.jpg")); + // '?' wildcard + rule.path = "Assets/Sprites/*/???.png"; + Assert.IsTrue(rule.Match("Assets/Sprites/cat/cat.png")); + Assert.IsFalse(rule.Match("Assets/Sprites/cat/bird.png")); + // rule.groupName = ""; + // rule.LabelMode = LabelWriteMode.Add; + // rule.simplified = true; + // rule.addressReplacement = ""; + } + + [Test] + public void MatchRegexTest() + { + AddressableImportRule rule = new AddressableImportRule(); + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/.*/.*\.png"; + Assert.IsTrue(rule.Match("Assets/Sprites/cat/cat.png")); + Assert.IsFalse(rule.Match("Assets/Sprites/cat/cat.jpg")); + } + + [Test] + public void ParseGroupReplacementTest() + { + AddressableImportRule rule = new AddressableImportRule(); + // Test empty path + rule.matchType = AddressableImportRuleMatchType.Wildcard; + rule.path = ""; + rule.groupName = "somegroup"; + Assert.IsNull(rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.path = " "; + rule.groupName = "somegroup"; + Assert.IsNull(rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + // Test empy groupName + rule.matchType = AddressableImportRuleMatchType.Wildcard; + rule.path = @"Assets/Sprites/*/*.png"; + rule.groupName = ""; + Assert.IsNull(rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.path = "Assets/Sprites/*/*.png"; + rule.groupName = " "; + Assert.IsNull(rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + // Test static groupName + rule.matchType = AddressableImportRuleMatchType.Wildcard; + rule.path = "Assets/Sprites/*/*.png"; + rule.groupName = "group-a"; + Assert.AreEqual("group-a", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + // Test wildcard + path elements + rule.matchType = AddressableImportRuleMatchType.Wildcard; + rule.path = "Assets/Sprites/*/*.png"; + rule.groupName = "${PATH[0]}"; + Assert.AreEqual("Assets", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.groupName = "${PATH[1]}"; + Assert.AreEqual("Sprites", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.groupName = "${PATH[-1]}"; + Assert.AreEqual("cat", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.groupName = "${PATH[0]}:${PATH[-1]}-group"; + Assert.AreEqual("Assets:cat-group", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + // Test regex + path elements + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(.*)/(.*)\.png"; + rule.groupName = "${PATH[0]}"; + Assert.AreEqual("Assets", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.groupName = "${PATH[1]}"; + Assert.AreEqual("Sprites", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.groupName = "${PATH[-1]}"; + Assert.AreEqual("cat", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + rule.groupName = "${PATH[0]}:${PATH[-1]}-group"; + Assert.AreEqual("Assets:cat-group", rule.ParseGroupReplacement("Assets/Sprites/cat/cat.png")); + // Test regex + unnamed groups + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(.*)/(.*)\.png"; + rule.groupName = "${PATH[0]}:$1-$2"; + Assert.AreEqual("Assets:cat-foo", rule.ParseGroupReplacement("Assets/Sprites/cat/foo.png")); + // Test regex + named groups + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(?.*)/(?.*)\.png"; + rule.groupName = "${PATH[0]}:${category}-${asset}"; + Assert.AreEqual("Assets:cat-foo", rule.ParseGroupReplacement("Assets/Sprites/cat/foo.png")); + } + + [Test] + public void AddressSimplifyTest() + { + AddressableImportRule rule = new AddressableImportRule(); + // Test wildcard + simplify + rule.matchType = AddressableImportRuleMatchType.Wildcard; + rule.path = "Assets/Sprites/*/*.png"; + rule.simplified = true; + Assert.AreEqual("cat", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + // Test regex + simplify + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(.*)/(.*)\.png"; + rule.simplified = true; + Assert.AreEqual("cat", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + // Test simplify + non empty address replacement + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.addressReplacement = "somevalue"; + rule.path = @"Assets/Sprites/(.*)/(.*)\.png"; + rule.simplified = true; + Assert.AreEqual("cat", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + } + + [Test] + public void ParseAddressReplacementTest() + { + AddressableImportRule rule = new AddressableImportRule(); + // Test empty path + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = ""; + Assert.AreEqual("Assets/Sprites/cat/cat.png", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + rule.path = " "; + Assert.AreEqual("Assets/Sprites/cat/cat.png", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + // Test empty address replacement + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(.*)/(.*)\.png"; + rule.addressReplacement = ""; + Assert.AreEqual("Assets/Sprites/cat/cat.png", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + rule.addressReplacement = " "; + Assert.AreEqual("Assets/Sprites/cat/cat.png", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + // Test regex + path elements + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(.*)/(.*)\.png"; + rule.addressReplacement = "${PATH[0]}"; + Assert.AreEqual("Assets", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + rule.addressReplacement = "${PATH[1]}"; + Assert.AreEqual("Sprites", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + rule.addressReplacement = "${PATH[-1]}"; + Assert.AreEqual("cat", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + rule.addressReplacement = "${PATH[0]}:${PATH[-1]}-element"; + Assert.AreEqual("Assets:cat-element", rule.ParseAddressReplacement("Assets/Sprites/cat/cat.png")); + // Test regex + unnamed groups + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(.*)/(.*)\.png"; + rule.addressReplacement = "${PATH[0]}:$1-$2"; + Assert.AreEqual("Assets:cat-foo", rule.ParseAddressReplacement("Assets/Sprites/cat/foo.png")); + // Test regex + named groups + rule.matchType = AddressableImportRuleMatchType.Regex; + rule.path = @"Assets/Sprites/(?.*)/(?.*)\.png"; + rule.addressReplacement = "${PATH[0]}:${category}-${asset}"; + Assert.AreEqual("Assets:cat-foo", rule.ParseAddressReplacement("Assets/Sprites/cat/foo.png")); + } + } +} \ No newline at end of file diff --git a/Tests/Editor/AddressableImportRuleTests.cs.meta b/Tests/Editor/AddressableImportRuleTests.cs.meta new file mode 100644 index 0000000..372e55d --- /dev/null +++ b/Tests/Editor/AddressableImportRuleTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0805d3b7bcf8e88488938813fd44d6ef +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef b/Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef new file mode 100644 index 0000000..3b6dd03 --- /dev/null +++ b/Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef @@ -0,0 +1,21 @@ +{ + "name": "Unity.AddressableImporter.Editor.Tests", + "references": [ + "Unity.Addressables.Editor", + "Unity.Addressables", + "Unity.AddressableImporter.Editor" + ], + "optionalUnityReferences": [ + "TestAssemblies" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [] +} \ No newline at end of file diff --git a/Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef.meta b/Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef.meta new file mode 100644 index 0000000..88f629a --- /dev/null +++ b/Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 1ddb623ed29d1984d93977489a666407 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index ce40fc2..937dfaa 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.littlebigfun.addressable-importer", "displayName": "Unity Addressable Importer", - "version": "0.2.1", + "version": "0.2.2", "unity": "2018.3", "description": "A simple rule based addressable asset importer.", "dependencies": {