Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 282 additions & 0 deletions Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using NUnit.Framework;

using UnityEngine;

using io.github.hatayama.UnityCliLoop.FirstPartyTools;
using io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.@event;

namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload
{
/// <summary>
/// End-to-end coverage for hot reload of types declared in a namespace with a keyword-named
/// segment (@event): a method body, a property getter, a call to an added method and a nested
/// type's method are all patched, and RevertAll restores the compiled values.
/// </summary>
public class HotReloadKeywordNamespaceE2ETests
{
private const string FixtureFileName = "HotReloadKeywordNamespaceFixtures.cs";

private const string AddedMethodAnchor = " public int CallsAddedMethod()";

private const string AddedMethodSource =
" private int AddedKeywordNamespaceValue()\n"
+ " {\n"
+ " return 35998;\n"
+ " }\n\n";

private HotReloadDomainTestScope _scope;

[SetUp]
public void SetUp()
{
_scope = new HotReloadDomainTestScope();
HotReloadAutoRefreshHold.SyncToActiveChanges();
}

[TearDown]
public void TearDown()
{
_scope.Dispose();
HotReloadAutoRefreshHold.SyncToActiveChanges();
}

/// <summary>
/// What: an edited method body of a type in a keyword-named namespace is Patched and returns
/// the edited value; RevertAll restores the compiled value.
/// </summary>
[Test]
public async Task Run_MethodOfTypeInKeywordNamedNamespace_IsPatchedAndReverts()
{
HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost();
Assert.That(host.MethodValue(), Is.EqualTo(35990));

HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk.Replace("return 35990;", "return 35991;"));

AssertKind(result, "MethodValue", HotReloadMethodOutcomeKind.Patched);
Assert.That(host.MethodValue(), Is.EqualTo(35991));
HotReloadCompositionRoot.Services.Patcher.RevertAll();
Assert.That(host.MethodValue(), Is.EqualTo(35990));
}

/// <summary>
/// What: an edited property getter of a type in a keyword-named namespace is Patched and
/// returns the edited value, so the getter shim declared in that namespace compiles.
/// </summary>
[Test]
public async Task Run_PropertyGetterOfTypeInKeywordNamedNamespace_IsPatched()
{
HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost();
Assert.That(host.GetterValue, Is.EqualTo(35993));

HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk.Replace("return 35993;", "return 35994;"));

AssertKind(result, "get_GetterValue", HotReloadMethodOutcomeKind.Patched);
Assert.That(host.GetterValue, Is.EqualTo(35994));
}

/// <summary>
/// What: a method of a type in a keyword-named namespace that now calls a method this reload
/// adds is Patched and returns what the added method computes, so the call the rewrite
/// qualifies with the shim's namespace compiles.
/// </summary>
[Test]
public async Task Run_CallToAddedMethodOfTypeInKeywordNamedNamespace_IsPatched()
{
HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost();
Assert.That(host.CallsAddedMethod(), Is.EqualTo(35996));

HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk
.Replace(AddedMethodAnchor, AddedMethodSource + AddedMethodAnchor)
.Replace("return 35996;", "return AddedKeywordNamespaceValue();"));

AssertKind(result, "CallsAddedMethod", HotReloadMethodOutcomeKind.Patched);
Assert.That(host.CallsAddedMethod(), Is.EqualTo(35998));
}

/// <summary>
/// What: an edited method body of a type nested in a type of a keyword-named namespace is
/// Patched and returns the edited value; RevertAll restores the compiled value.
/// </summary>
[Test]
public async Task Run_MethodOfNestedTypeInKeywordNamedNamespace_IsPatchedAndReverts()
{
HotReloadKeywordNamespaceHost.Nested nested = new HotReloadKeywordNamespaceHost.Nested();
Assert.That(nested.NestedValue(), Is.EqualTo(35999));

HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk.Replace("return 35999;", "return 36000;"));

AssertKind(result, "NestedValue", HotReloadMethodOutcomeKind.Patched);
Assert.That(nested.NestedValue(), Is.EqualTo(36000));
HotReloadCompositionRoot.Services.Patcher.RevertAll();
Assert.That(nested.NestedValue(), Is.EqualTo(35999));
}

/// <summary>
/// What: a changed const of a type in a keyword-named namespace produces the const drift
/// warning naming that const, so the compiled type is found by its metadata name.
/// </summary>
[Test]
public async Task Run_ConstChangedInTypeOfKeywordNamedNamespace_WarnsAboutTheDrift()
{
HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk.Replace("KeywordTuning = 4;", "KeywordTuning = 6;"));

Assert.That(
FindWarningContaining(result, "KeywordTuning"),
Does.Contain("is 6 in the edited source but 4 in the compiled assembly"),
string.Join("\n", result.Warnings));
}

/// <summary>
/// What: removing a compiled property of a type in a keyword-named namespace produces the
/// kind-change warning naming that property, and no outside-method-body drift warning,
/// because the removed declaration is matched to the kind-change key and left out of the
/// drift comparison.
/// </summary>
[Test]
public async Task Run_PropertyRemovedFromTypeOfKeywordNamedNamespace_WarnsAboutTheKindChangeOnly()
{
HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk.Replace(" public int KindChangeValue => 36002;\n", string.Empty));

Assert.That(
FindWarningContaining(result, "KindChangeValue"),
Does.StartWith("Compiled property '"),
string.Join("\n", result.Warnings));
AssertNoOutsideMethodBodyDriftWarning(result);
}

/// <summary>
/// What: removing a compiled property of a keyword-named type (@class) in a keyword-named
/// namespace produces the kind-change warning naming that property and no
/// outside-method-body drift warning.
/// </summary>
[Test]
public async Task Run_PropertyRemovedFromKeywordNamedType_WarnsAboutTheKindChangeOnly()
{
HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk.Replace(" public int ClassKindChangeValue => 36005;\n", string.Empty));

Assert.That(
FindWarningContaining(result, "ClassKindChangeValue"),
Does.StartWith("Compiled property '"),
string.Join("\n", result.Warnings));
AssertNoOutsideMethodBodyDriftWarning(result);
}

/// <summary>
/// What: removing a compiled keyword-named property (@checked) produces the kind-change
/// warning naming it and no outside-method-body drift warning.
/// </summary>
[Test]
public async Task Run_KeywordNamedPropertyRemoved_WarnsAboutTheKindChangeOnly()
{
HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk.Replace(" public int @checked => 36007;\n", string.Empty));

Assert.That(
FindWarningContaining(result, ".checked'"),
Does.StartWith("Compiled property '"),
string.Join("\n", result.Warnings));
AssertNoOutsideMethodBodyDriftWarning(result);
}

/// <summary>
/// What: a keyword-named field (@event) that the edit adds and a method now reads is Patched
/// with the field's initial value, and raises no outside-method-body drift warning.
/// </summary>
[Test]
public async Task Run_KeywordNamedFieldAdded_IsPatchedWithoutDriftWarning()
{
HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost();
Assert.That(host.ReadsAddedField(), Is.EqualTo(36008));

HotReloadOrchestratorResult result = await RunEditAsync(
onDisk => onDisk
.Replace(
" public int ReadsAddedField()",
" private int @event = 36009;\n\n public int ReadsAddedField()")
.Replace("return 36008;", "return @event;"));

AssertKind(result, "ReadsAddedField", HotReloadMethodOutcomeKind.Patched);
Assert.That(host.ReadsAddedField(), Is.EqualTo(36009));
AssertNoOutsideMethodBodyDriftWarning(result);
}

private static void AssertNoOutsideMethodBodyDriftWarning(HotReloadOrchestratorResult result)
{
foreach (string warning in result.Warnings)
{
Assert.That(
warning,
Does.Not.StartWith("Edits outside method bodies"),
string.Join("\n", result.Warnings));
}
}

private static string FindWarningContaining(HotReloadOrchestratorResult result, string fragment)
{
foreach (string warning in result.Warnings)
{
if (warning.Contains(fragment))
{
return warning;
}
}

Assert.Fail("No warning names " + fragment + ".\n" + string.Join("\n", result.Warnings));
return null;
}

private static async Task<HotReloadOrchestratorResult> RunEditAsync(Func<string, string> edit)
{
string fixturePath = Path.GetFullPath(
Path.Combine(Application.dataPath, "Tests", "Editor", "HotReload", FixtureFileName));
string onDisk = File.ReadAllText(fixturePath);
string edited = edit(onDisk);
Assert.That(edited, Is.Not.EqualTo(onDisk), "Precondition: the edit must change the fixture.");
string editedPath = HotReloadTestSourceWriter.WriteEditedSource("KeywordNamespaceE2E.cs", edited);
return await HotReloadCompositionRoot.Services.Orchestrator.RunAsync(
new[] { fixturePath },
editedPath,
CancellationToken.None);
}

private static void AssertKind(
HotReloadOrchestratorResult result,
string methodName,
HotReloadMethodOutcomeKind expectedKind)
{
foreach (HotReloadMethodOutcome outcome in result.Methods)
{
if (outcome.Method.Contains("." + methodName + "("))
{
Assert.That(outcome.Kind, Is.EqualTo(expectedKind), outcome.Method + ": " + outcome.Reason);
return;
}
}

Assert.Fail("No row for " + methodName + ". Rows: " + DescribeRows(result));
}

private static string DescribeRows(HotReloadOrchestratorResult result)
{
string rows = string.Empty;
foreach (HotReloadMethodOutcome outcome in result.Methods)
{
rows += outcome.Method + " = " + outcome.Kind + " (" + outcome.Reason + "); ";
}

return rows;
}
}
}

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

64 changes: 64 additions & 0 deletions Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.@event
{
/// <summary>
/// Host types declared in a namespace whose last segment is a C# keyword. Tests edit their
/// bodies to check that hot reload patches them: the escaped spelling (@event) appears only in
/// source, while the compiled metadata names the namespace "event".
/// </summary>
public sealed class HotReloadKeywordNamespaceHost
{
public const int KeywordTuning = 4;

public int MethodValue()
{
return 35990;
}

public int GetterValue
{
get
{
return 35993;
}
}

public int CallsAddedMethod()
{
return 35996;
}

public int CallsIntroducedType()
{
return 36001;
}

public int KindChangeValue => 36002;

public int @checked => 36007;

public int ReadsAddedField()
{
return 36008;
}

/// <summary>
/// Nested host, so the conversion of a nested type name in that namespace is covered too.
/// </summary>
public sealed class Nested
{
public int NestedValue()
{
return 35999;
}
}
}

/// <summary>
/// Host whose own name is a C# keyword, so a key built from its identifier must drop the @
/// as well as the namespace's.
/// </summary>
public sealed class @class
{
public int ClassKindChangeValue => 36005;
}
}

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

Loading
Loading