Skip to content

Commit 6bbe523

Browse files
committed
feat: more tests for getter/setter detections
1 parent bd6c295 commit 6bbe523

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using NativeCodeGen.Core.Generation;
2+
using NativeCodeGen.Core.Models;
3+
using NativeCodeGen.Core.Utilities;
4+
using NativeCodeGen.TypeScript;
5+
6+
namespace NativeCodeGen.Tests.Generation;
7+
8+
public class GetterProxyTests
9+
{
10+
[Fact]
11+
public void GetterProxy_GeneratedWhenAllParamsOptional()
12+
{
13+
var emitter = new TypeScriptEmitter();
14+
var generator = new SharedClassGenerator(emitter);
15+
16+
// Create a native with optional parameters
17+
var native = new NativeDefinition
18+
{
19+
Name = "GET_COORDS",
20+
Hash = "0x12345678",
21+
Parameters =
22+
[
23+
new NativeParameter
24+
{
25+
Name = "alive",
26+
Type = new TypeInfo { Name = "BOOL", Category = TypeCategory.Primitive },
27+
DefaultValue = "false" // Has default value
28+
},
29+
new NativeParameter
30+
{
31+
Name = "realCoords",
32+
Type = new TypeInfo { Name = "BOOL", Category = TypeCategory.Primitive },
33+
DefaultValue = "false" // Has default value
34+
}
35+
],
36+
ReturnType = new TypeInfo { Name = "Vector3", Category = TypeCategory.Struct }
37+
};
38+
39+
var result = generator.GenerateHandleClass("Entity", null, [native]);
40+
41+
// Should have both the function and the getter proxy
42+
Assert.Contains("getCoords(alive?: boolean, realCoords?: boolean): Vector3", result);
43+
Assert.Contains("get Coords(): Vector3 {", result);
44+
Assert.Contains("return this.getCoords();", result);
45+
}
46+
47+
[Fact]
48+
public void GetterProxy_NotGeneratedWhenParamsRequired()
49+
{
50+
var emitter = new TypeScriptEmitter();
51+
var generator = new SharedClassGenerator(emitter);
52+
53+
// Create a native with required parameters (no defaults)
54+
var native = new NativeDefinition
55+
{
56+
Name = "GET_COORDS",
57+
Hash = "0x12345678",
58+
Parameters =
59+
[
60+
new NativeParameter
61+
{
62+
Name = "alive",
63+
Type = new TypeInfo { Name = "BOOL", Category = TypeCategory.Primitive }
64+
// No DefaultValue - required param
65+
}
66+
],
67+
ReturnType = new TypeInfo { Name = "Vector3", Category = TypeCategory.Struct }
68+
};
69+
70+
var result = generator.GenerateHandleClass("Entity", null, [native]);
71+
72+
// Should have the function but NOT the getter proxy
73+
Assert.Contains("getCoords(alive: boolean): Vector3", result);
74+
Assert.DoesNotContain("get Coords()", result);
75+
}
76+
77+
[Fact]
78+
public void GetterProxy_NotGeneratedWhenMixedParams()
79+
{
80+
var emitter = new TypeScriptEmitter();
81+
var generator = new SharedClassGenerator(emitter);
82+
83+
// Create a native with mixed params (one required, one optional)
84+
var native = new NativeDefinition
85+
{
86+
Name = "GET_DATA",
87+
Hash = "0x12345678",
88+
Parameters =
89+
[
90+
new NativeParameter
91+
{
92+
Name = "id",
93+
Type = new TypeInfo { Name = "int", Category = TypeCategory.Primitive }
94+
// Required
95+
},
96+
new NativeParameter
97+
{
98+
Name = "includeExtra",
99+
Type = new TypeInfo { Name = "BOOL", Category = TypeCategory.Primitive },
100+
DefaultValue = "true" // Optional
101+
}
102+
],
103+
ReturnType = new TypeInfo { Name = "int", Category = TypeCategory.Primitive }
104+
};
105+
106+
var result = generator.GenerateHandleClass("Test", null, [native]);
107+
108+
// Should have the function but NOT the getter proxy (not all params optional)
109+
Assert.Contains("getData(", result);
110+
Assert.DoesNotContain("get Data()", result);
111+
}
112+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using NativeCodeGen.Core.Utilities;
2+
3+
namespace NativeCodeGen.Tests.Utilities;
4+
5+
public class NameConverterGetterTests
6+
{
7+
[Theory]
8+
[InlineData("getHealth", true, "Health")]
9+
[InlineData("getName", true, "Name")]
10+
[InlineData("getTeam", true, "Team")]
11+
[InlineData("isMale", true, "IsMale")]
12+
[InlineData("isAttached", true, "IsAttached")]
13+
[InlineData("isAttachedToAnyObject", true, "IsAttachedToAnyObject")]
14+
[InlineData("setHealth", false, "setHealth")]
15+
[InlineData("get", false, "get")]
16+
[InlineData("getA", true, "A")]
17+
[InlineData("is", false, "is")]
18+
[InlineData("isA", true, "IsA")]
19+
public void IsGetterName_DetectsGettersCorrectly(string methodName, bool expectedIsGetter, string expectedPropertyName)
20+
{
21+
Assert.Equal(expectedIsGetter, NameConverter.IsGetterName(methodName));
22+
Assert.Equal(expectedPropertyName, NameConverter.GetterToPropertyName(methodName));
23+
}
24+
25+
[Theory]
26+
[InlineData("setHealth", true, "Health")]
27+
[InlineData("setName", true, "Name")]
28+
[InlineData("getHealth", false, "getHealth")]
29+
[InlineData("set", false, "set")]
30+
[InlineData("setA", true, "A")]
31+
public void IsSetterName_DetectsSettersCorrectly(string methodName, bool expectedIsSetter, string expectedPropertyName)
32+
{
33+
Assert.Equal(expectedIsSetter, NameConverter.IsSetterName(methodName));
34+
Assert.Equal(expectedPropertyName, NameConverter.SetterToPropertyName(methodName));
35+
}
36+
}

0 commit comments

Comments
 (0)