-
Notifications
You must be signed in to change notification settings - Fork 817
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[URP][2023.2] Adding URPHelpURLAttribute tests
Adds editor tests for the `URPHelpURL `Attribute that is used to give documentation links in various places inside the editor. These attributes were recently broken and undetected until package validation so this test was made to ensure that doesn't happen again.
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
Packages/com.unity.render-pipelines.universal/Tests/Editor/DocumentationTests.cs
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,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using NUnit.Framework; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.Rendering.Universal; | ||
|
||
namespace Documentation | ||
{ | ||
class DocumentationTests | ||
{ | ||
private bool TestURL(string url) | ||
{ | ||
try | ||
{ | ||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; | ||
|
||
// Timeout set to 10 seconds | ||
request.Timeout = 10000; | ||
|
||
// Only get header information | ||
request.Method = "HEAD"; | ||
|
||
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) | ||
{ | ||
int statusCode = (int)response.StatusCode; | ||
|
||
// Successful requests | ||
if (statusCode >= 100 && statusCode < 400) | ||
return true; | ||
|
||
// Server Errors | ||
if (statusCode >= 500 && statusCode <= 510) | ||
return false; | ||
} | ||
} | ||
catch | ||
{ | ||
// ignored | ||
} | ||
|
||
return false; | ||
} | ||
|
||
[Test] | ||
public void TestURPHelpURLAttributes() | ||
{ | ||
// Start with checking a bad URL to make sure TestURL() works. | ||
string badURL = "https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@16.0/manual/incorrect-url"; | ||
bool badURLResults = TestURL(badURL); | ||
Assert.IsFalse(badURLResults, "TestURL() failed. A broken URL should have been detected."); | ||
|
||
// Find everything that uses URPHelpURLAttribute | ||
TypeCache.TypeCollection typesWithHelpUrls = TypeCache.GetTypesWithAttribute<URPHelpURLAttribute>(); | ||
|
||
// Check if all the URLs are valid. | ||
bool allURLsAreValid = true; | ||
StringBuilder failures = new (); | ||
foreach (Type type in typesWithHelpUrls) | ||
{ | ||
Debug.Log("Checking \"" + type.FullName + "\""); | ||
object[] attributes = type.GetCustomAttributes(typeof(URPHelpURLAttribute), false); | ||
for (int i = 0; i < attributes.Length; i++) | ||
{ | ||
URPHelpURLAttribute attribute = attributes[i] as URPHelpURLAttribute; | ||
Assert.IsNotNull(attribute); | ||
|
||
string url = attribute.URL; | ||
bool urlIsValid = TestURL(url); | ||
|
||
Debug.Log("\t" + url); | ||
Debug.Log(urlIsValid ? "\tSuccess!\n" : "\tFailure!\n"); | ||
|
||
if (urlIsValid) | ||
continue; | ||
|
||
allURLsAreValid = false; | ||
failures.AppendLine("\t" + type.FullName); | ||
failures.AppendLine("\t" + url); | ||
failures.AppendLine(); | ||
} | ||
} | ||
|
||
if (!allURLsAreValid) | ||
Assert.Fail("The following URLs failed:\n" + failures); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.unity.render-pipelines.universal/Tests/Editor/DocumentationTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.