1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-26 03:28:02 +02:00

More reflection tests

This commit is contained in:
Brian Fiete 2020-07-18 05:27:09 -07:00
parent b24625d9a3
commit bcef4a975d

View file

@ -12,7 +12,7 @@ namespace Tests
public String mC; public String mC;
public String mD; public String mD;
public this(int32 a, float b, String c, String d) public this(int32 a, float b, String c, String d = "D")
{ {
PrintF("this: %p A: %d B: %f", this, a, (double)b); PrintF("this: %p A: %d B: %f", this, a, (double)b);
@ -49,6 +49,11 @@ namespace Tests
} }
} }
[AttributeUsage(.Class | .Method, ReflectUser=.All)]
struct AttrDAttribute : Attribute
{
}
[Reflect] [Reflect]
class ClassA class ClassA
@ -131,7 +136,7 @@ namespace Tests
[Reflect(.All), AttrC(1, 2)] [Reflect(.All), AttrC(1, 2)]
class ClassB class ClassB
{ {
[AttrA(11, 22, "StrA", "StrB")] [AttrA(11, 22, "StrA")]
public int mA = 1; public int mA = 1;
[AttrB(44, 55)] [AttrB(44, 55)]
public int mB = 2; public int mB = 2;
@ -139,15 +144,35 @@ namespace Tests
public String mStr = "ABC"; public String mStr = "ABC";
} }
[Reflect(.Type)] [AttrD]
class ClassC class ClassC
{ {
[AttrA(11, 22, "StrA", "StrC")]
public int mA = 1; public int mA = 1;
[AttrB(44, 55)]
public int mB = 2; public int mB = 2;
public int mC = 3; }
public float mD = 4;
class ClassD
{
public int mA = 1;
public int mB = 2;
[AttrD]
public void MethodA()
{
}
}
class ClassE
{
public int mA = 1;
public int mB = 2;
[AttrD]
public void MethodA()
{
}
} }
[Test] [Test]
@ -323,12 +348,13 @@ namespace Tests
Test.Assert(attrA.mA == 11); Test.Assert(attrA.mA == 11);
Test.Assert(attrA.mB == 22); Test.Assert(attrA.mB == 22);
Test.Assert(attrA.mC == "StrA"); Test.Assert(attrA.mC == "StrA");
Test.Assert(attrA.mD == "StrB"); Test.Assert(attrA.mD == "D");
} }
fieldIdx++; fieldIdx++;
} }
Test.Assert(fieldIdx == 4);
var fieldInfo = cb.GetType().GetField("mC").Value; var fieldInfo = cb.GetType().GetField("mC").Value;
int cVal = 0; int cVal = 0;
fieldInfo.GetValue(cb, out cVal); fieldInfo.GetValue(cb, out cVal);
@ -353,10 +379,22 @@ namespace Tests
let attrC = typeof(ClassB).GetCustomAttribute<AttrCAttribute>().Get(); let attrC = typeof(ClassB).GetCustomAttribute<AttrCAttribute>().Get();
Test.Assert(attrC.mA == 1); Test.Assert(attrC.mA == 1);
Test.Assert(attrC.mB == 2); Test.Assert(attrC.mB == 2);
ClassC c = scope .();
Test.Assert(typeof(ClassC).GetField("mA").Value.Name == "mA");
Test.Assert(typeof(ClassC).GetField("mB").Value.Name == "mB");
ClassD cd = scope .();
Test.Assert(typeof(ClassD).GetField("mA") case .Err);
// Should this reified call cause fields to be reflected?
/*ClassE ce = scope .();
ce.MethodA();
Test.Assert(typeof(ClassE).GetField("mA").Value.Name == "mA");*/
} }
[Test] [Test]
static void TestD() static void TestStructA()
{ {
StructA sa = .() { mA = 12, mB = 23 }; StructA sa = .() { mA = 12, mB = 23 };
var typeInfo = typeof(StructA); var typeInfo = typeof(StructA);