Skip to content

Commit

Permalink
Traverse now handles static fields/properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jan 24, 2020
1 parent f8b9156 commit 5264949
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Harmony/Tools/Traverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public Traverse Property(string name, object[] index = null)
{
if (name == null) throw new ArgumentNullException(nameof(name));
var resolved = Resolve();
if (resolved._root == null || resolved._type == null) return new Traverse();
if (resolved._type == null) return new Traverse();
var info = Cache.GetPropertyInfo(resolved._type, name);
if (info == null) return new Traverse();
return new Traverse(resolved._root, info, index);
Expand Down
13 changes: 13 additions & 0 deletions HarmonyTests/Traverse/Assets/TraverseProperties_AccessModifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public string BaseProperty3
get => throw new Exception();
set => throw new Exception();
}

static string StaticProperty => "test1";
#pragma warning disable CS0414
static readonly string staticField = "test1";
#pragma warning restore CS0414
}

public static class TraverseProperties_Static
{
static string StaticProperty => "test2";
#pragma warning disable CS0414
static readonly string staticField = "test2";
#pragma warning restore CS0414
}

public class TraverseProperties_AccessModifiers : TraverseProperties_BaseClass
Expand Down
15 changes: 15 additions & 0 deletions HarmonyTests/Traverse/TestTraverse_Fields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public void Traverse_Field_GetValue()
}
}

// Traverse.Property() should return the value of a traversed static field
//
[Test]
public void Traverse_Field_Static()
{
var instance = new TraverseProperties_BaseClass();

var trv1 = Traverse.Create(instance).Field("staticField");
Assert.AreEqual("test1", trv1.GetValue());


var trv2 = Traverse.Create(typeof(TraverseProperties_Static)).Field("staticField");
Assert.AreEqual("test2", trv2.GetValue());
}

// Traverse.SetValue() should set the value of a traversed field
// regardless of its access modifier
//
Expand Down
17 changes: 16 additions & 1 deletion HarmonyTests/Traverse/TestTraverse_Properties.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HarmonyLib;
using HarmonyLib;
using HarmonyLibTests.Assets;
using NUnit.Framework;

Expand All @@ -18,6 +18,21 @@ public void Traverse_Property_ToString()
Assert.AreEqual(TraverseProperties.testStrings[0], trv.ToString());
}

// Traverse.Property() should return static properties
//
[Test]
public void Traverse_Property_Static()
{
var instance = new TraverseProperties_BaseClass();

var trv1 = Traverse.Create(instance).Property("StaticProperty");
Assert.AreEqual("test1", trv1.GetValue());


var trv2 = Traverse.Create(typeof(TraverseProperties_Static)).Property("StaticProperty");
Assert.AreEqual("test2", trv2.GetValue());
}

// Traverse.GetValue() should return the value of a traversed property
// regardless of its access modifier
//
Expand Down

0 comments on commit 5264949

Please sign in to comment.