-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from nowsprinting/ignoreunityversion
Add UnityVersionAttribute
- Loading branch information
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2023-2024 Koji Hasegawa. | ||
// This software is released under the MIT License. | ||
|
||
using System; | ||
using NUnit.Framework; | ||
using NUnit.Framework.Interfaces; | ||
using NUnit.Framework.Internal; | ||
using UnityEngine; | ||
|
||
namespace TestHelper.Attributes | ||
{ | ||
/// <summary> | ||
/// Skip this test run if Unity version is older and/or newer than specified. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] | ||
public class UnityVersionAttribute : NUnitAttribute, IApplyToTest | ||
{ | ||
private readonly string _newerThanOrEqual; | ||
private readonly string _olderThan; | ||
|
||
/// <summary> | ||
/// Skip this test run if Unity version is older and/or newer than specified. | ||
/// Valid format, e.g., "2023.2.16f1", "2023.2", and "2023". | ||
/// </summary> | ||
/// <param name="newerThanOrEqual">This test will run if the Unity editor version is newer than or equal the specified version.</param> | ||
/// <param name="olderThan">This test will run if the Unity editor version is older than the specified version.</param> | ||
public UnityVersionAttribute(string newerThanOrEqual = null, string olderThan = null) | ||
{ | ||
_newerThanOrEqual = newerThanOrEqual; | ||
_olderThan = olderThan; | ||
} | ||
|
||
public void ApplyToTest(Test test) | ||
{ | ||
if (IsSkip(Application.unityVersion)) | ||
{ | ||
test.RunState = RunState.Ignored; | ||
test.Properties.Set("_SKIPREASON", "Unity editor version is out of range."); | ||
} | ||
} | ||
|
||
internal bool IsSkip(string unityVersion) | ||
{ | ||
if (!string.IsNullOrEmpty(_newerThanOrEqual) && !IsNewerThanEqual(unityVersion, _newerThanOrEqual)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(_olderThan) && !IsOlderThan(unityVersion, _olderThan)) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static bool IsNewerThanEqual(string unityVersion, string newerThanEqual) | ||
{ | ||
return string.Compare(unityVersion, newerThanEqual, StringComparison.Ordinal) >= 0; | ||
} | ||
|
||
private static bool IsOlderThan(string unityVersion, string olderThan) | ||
{ | ||
var digits = olderThan.Length; // Evaluate only up to olderThan`s digits | ||
return string.Compare(unityVersion.Substring(0, digits), olderThan, StringComparison.Ordinal) <= 0; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2023-2024 Koji Hasegawa. | ||
// This software is released under the MIT License. | ||
|
||
using NUnit.Framework; | ||
|
||
namespace TestHelper.Attributes | ||
{ | ||
[TestFixture] | ||
public class UnityVersionAttributeTest | ||
{ | ||
private const string UnityVersion = "2023.2.16f1"; | ||
|
||
[TestCase("2023.2.16f1")] // equal version is not skip | ||
[TestCase("2023.2.15f1")] | ||
[TestCase("2023.2")] | ||
[TestCase("2023")] | ||
[TestCase("2022")] | ||
public void IsSkip_newerThanOrEqual_NotSkip(string newerThanOrEqual) | ||
{ | ||
var sut = new UnityVersionAttribute(newerThanOrEqual); | ||
var actual = sut.IsSkip(UnityVersion); | ||
Assert.That(actual, Is.False); | ||
} | ||
|
||
[TestCase("2023.2.17f1")] | ||
[TestCase("2023.3")] | ||
[TestCase("6000")] | ||
public void IsSkip_newerThanOrEqual_Skip(string newerThanOrEqual) | ||
{ | ||
var sut = new UnityVersionAttribute(newerThanOrEqual); | ||
var actual = sut.IsSkip(UnityVersion); | ||
Assert.That(actual, Is.True); | ||
} | ||
|
||
[TestCase("2023.2.17f1")] | ||
[TestCase("2023.2.16f1")] // equal version is not skip | ||
[TestCase("2023.2")] | ||
[TestCase("2023")] | ||
[TestCase("6000")] | ||
public void IsSkip_olderThan_NotSkip(string olderThan) | ||
{ | ||
var sut = new UnityVersionAttribute(olderThan: olderThan); | ||
var actual = sut.IsSkip(UnityVersion); | ||
Assert.That(actual, Is.False); | ||
} | ||
|
||
[TestCase("2023.2.15f1")] | ||
[TestCase("2023.1")] | ||
[TestCase("2022")] | ||
[TestCase("2021")] | ||
public void IsSkip_olderThan_Skip(string olderThan) | ||
{ | ||
var sut = new UnityVersionAttribute(olderThan: olderThan); | ||
var actual = sut.IsSkip(UnityVersion); | ||
Assert.That(actual, Is.True); | ||
} | ||
|
||
[Test] | ||
[UnityVersion("2019")] | ||
public void Attach_newerThanOrEqual2019_NotSkip() | ||
{ | ||
} | ||
|
||
[Test] | ||
[UnityVersion(olderThan: "2019.4.0f1")] | ||
public void Attach_olderThan2019_4_0f1_Skip() | ||
{ | ||
Assert.Fail("This test should be skipped."); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.