Skip to content

Commit

Permalink
Fixed simplified behavior and added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Aug 4, 2019
1 parent 74ef3d7 commit 949fd3f
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Editor/AddressableImportRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ public string ParseGroupReplacement(string assetPath)
/// </summary>
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.
Expand Down
8 changes: 8 additions & 0 deletions Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Tests/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 170 additions & 0 deletions Tests/Editor/AddressableImportRuleTests.cs
Original file line number Diff line number Diff line change
@@ -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/(?<category>.*)/(?<asset>.*)\.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/(?<category>.*)/(?<asset>.*)\.png";
rule.addressReplacement = "${PATH[0]}:${category}-${asset}";
Assert.AreEqual("Assets:cat-foo", rule.ParseAddressReplacement("Assets/Sprites/cat/foo.png"));
}
}
}
11 changes: 11 additions & 0 deletions Tests/Editor/AddressableImportRuleTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Tests/Editor/Unity.AddressableImporter.Editor.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -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": []
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 949fd3f

Please sign in to comment.