Skip to content

Commit 165b4d4

Browse files
committed
[swift] Fix primitive extensions
Fixes #107
1 parent ee44c0b commit 165b4d4

File tree

9 files changed

+369
-27
lines changed

9 files changed

+369
-27
lines changed

Generator/Beyond.NET.CodeGenerator/Syntax/Kotlin/KotlinTypeSyntaxWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ List<GeneratedMember> generatedMembers
982982
string? codeForOptional = null;
983983

984984
/* if (!extendedType.IsEnum &&
985-
!extendedType.IsStruct()) {
985+
!extendedType.IsStruct() &&
986+
!extendedType.IsPrimitive) {
986987
codeForOptional = GetTypeExtensionsCode(
987988
extendedType,
988989
true,

Generator/Beyond.NET.CodeGenerator/Syntax/Swift/SwiftTypeSyntaxWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ List<GeneratedMember> generatedMembers
710710
string? codeForOptional;
711711

712712
if (!extendedType.IsEnum &&
713-
!extendedType.IsStruct()) {
713+
!extendedType.IsStruct() &&
714+
!extendedType.IsPrimitive) {
714715
codeForOptional = GetTypeExtensionsCode(
715716
extendedType,
716717
true,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Beyond.NET.Sample.Source;
2+
3+
public static class PrimitiveExtensionsTests
4+
{
5+
public static int RoundToInt(this double value)
6+
{
7+
return (int)Math.Round(value, 0);
8+
}
9+
10+
public static int RoundToInt(this float value)
11+
{
12+
return (int)Math.Round(value, 0);
13+
}
14+
}

Samples/Beyond.NET.Sample.Swift/BeyondNETSampleSwift.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
E787AA8829EDC2C400601F1A /* SystemTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E787AA8729EDC2C400601F1A /* SystemTypeTests.swift */; };
4141
E787AA8A29EDC55D00601F1A /* SystemExceptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E787AA8929EDC55D00601F1A /* SystemExceptionTests.swift */; };
4242
E790871629EE80E2001F776F /* TypeConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E790871529EE80E2001F776F /* TypeConversionTests.swift */; };
43-
E790871829EE8591001F776F /* PrimitivesBoxingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E790871729EE8591001F776F /* PrimitivesBoxingTests.swift */; };
43+
E790871829EE8591001F776F /* PrimitivesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E790871729EE8591001F776F /* PrimitivesTests.swift */; };
4444
E790871A29EEB8A5001F776F /* SystemStringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E790871929EEB8A5001F776F /* SystemStringTests.swift */; };
4545
E790871C29EEC404001F776F /* SystemEnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E790871B29EEC404001F776F /* SystemEnumTests.swift */; };
4646
E790871E29EECA06001F776F /* SystemArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E790871D29EECA06001F776F /* SystemArrayTests.swift */; };
@@ -123,7 +123,7 @@
123123
E787AA8729EDC2C400601F1A /* SystemTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemTypeTests.swift; sourceTree = "<group>"; };
124124
E787AA8929EDC55D00601F1A /* SystemExceptionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemExceptionTests.swift; sourceTree = "<group>"; };
125125
E790871529EE80E2001F776F /* TypeConversionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeConversionTests.swift; sourceTree = "<group>"; };
126-
E790871729EE8591001F776F /* PrimitivesBoxingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimitivesBoxingTests.swift; sourceTree = "<group>"; };
126+
E790871729EE8591001F776F /* PrimitivesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimitivesTests.swift; sourceTree = "<group>"; };
127127
E790871929EEB8A5001F776F /* SystemStringTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemStringTests.swift; sourceTree = "<group>"; };
128128
E790871B29EEC404001F776F /* SystemEnumTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemEnumTests.swift; sourceTree = "<group>"; };
129129
E790871D29EECA06001F776F /* SystemArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemArrayTests.swift; sourceTree = "<group>"; };
@@ -233,7 +233,7 @@
233233
isa = PBXGroup;
234234
children = (
235235
E790871529EE80E2001F776F /* TypeConversionTests.swift */,
236-
E790871729EE8591001F776F /* PrimitivesBoxingTests.swift */,
236+
E790871729EE8591001F776F /* PrimitivesTests.swift */,
237237
E7E517192AD9289200634B3E /* ComparisonTests.swift */,
238238
E787AA8529EDC0BA00601F1A /* SystemObjectTests.swift */,
239239
E787AA8729EDC2C400601F1A /* SystemTypeTests.swift */,
@@ -456,7 +456,7 @@
456456
E756F70F29F173E500CA039F /* SystemThreadingThreadTests.swift in Sources */,
457457
E7AC3D8C29E0391200C5609B /* XCTestCase+Extensions.swift in Sources */,
458458
E7FBD6A52B0365A3006BDB53 /* SpanTestTests.swift in Sources */,
459-
E790871829EE8591001F776F /* PrimitivesBoxingTests.swift in Sources */,
459+
E790871829EE8591001F776F /* PrimitivesTests.swift in Sources */,
460460
E7B905DA2A865C6000B5DC75 /* DelegateTestTests.swift in Sources */,
461461
E7C16FCB2B21D2C10021DCB3 /* SystemSecurityCryptographyTests.swift in Sources */,
462462
E790871E29EECA06001F776F /* SystemArrayTests.swift in Sources */,

Samples/Beyond.NET.Sample.Swift/Tests/System/PrimitivesBoxingTests.swift renamed to Samples/Beyond.NET.Sample.Swift/Tests/System/PrimitivesTests.swift

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import XCTest
22
import BeyondDotNETSampleKit
33

4-
final class PrimitivesBoxingTests: XCTestCase {
4+
final class PrimitivesTests: XCTestCase {
55
override class func setUp() {
66
Self.sharedSetUp()
77
}
@@ -10,7 +10,7 @@ final class PrimitivesBoxingTests: XCTestCase {
1010
Self.sharedTearDown()
1111
}
1212

13-
func testBool() {
13+
func testBoolBoxing() {
1414
boxAndUnbox(value: true,
1515
expectedTypeName: System.Boolean.fullTypeName,
1616
boxFunc: { $0.dotNETObject() },
@@ -22,78 +22,102 @@ final class PrimitivesBoxingTests: XCTestCase {
2222
unboxFunc: { try $0.castToBool() })
2323
}
2424

25-
func testFloat() {
25+
func testFloatBoxing() {
2626
boxAndUnbox(value: -123.123 as Float,
2727
expectedTypeName: System.Single.fullTypeName,
2828
boxFunc: { $0.dotNETObject() },
2929
unboxFunc: { try $0.castToFloat() })
3030
}
3131

32-
func testDouble() {
32+
func testDoubleBoxing() {
3333
boxAndUnbox(value: -123456789.123456789 as Double,
3434
expectedTypeName: System.Double.fullTypeName,
3535
boxFunc: { $0.dotNETObject() },
3636
unboxFunc: { try $0.castToDouble() })
3737
}
3838

39-
func testInt8() {
39+
func testInt8Boxing() {
4040
boxAndUnbox(value: -123 as Int8,
4141
expectedTypeName: System.SByte.fullTypeName,
4242
boxFunc: { $0.dotNETObject() },
4343
unboxFunc: { try $0.castToInt8() })
4444
}
4545

46-
func testUInt8() {
46+
func testUInt8Boxing() {
4747
boxAndUnbox(value: 123 as UInt8,
4848
expectedTypeName: System.Byte.fullTypeName,
4949
boxFunc: { $0.dotNETObject() },
5050
unboxFunc: { try $0.castToUInt8() })
5151
}
5252

53-
func testInt16() {
53+
func testInt16Boxing() {
5454
boxAndUnbox(value: -1234 as Int16,
5555
expectedTypeName: System.Int16.fullTypeName,
5656
boxFunc: { $0.dotNETObject() },
5757
unboxFunc: { try $0.castToInt16() })
5858
}
5959

60-
func testUInt16() {
60+
func testUInt16Boxing() {
6161
boxAndUnbox(value: 1234 as UInt16,
6262
expectedTypeName: System.UInt16.fullTypeName,
6363
boxFunc: { $0.dotNETObject() },
6464
unboxFunc: { try $0.castToUInt16() })
6565
}
6666

67-
func testInt32() {
67+
func testInt32Boxing() {
6868
boxAndUnbox(value: -123456789 as Int32,
6969
expectedTypeName: System.Int32.fullTypeName,
7070
boxFunc: { $0.dotNETObject() },
7171
unboxFunc: { try $0.castToInt32() })
7272
}
7373

74-
func testUInt32() {
74+
func testUInt32Boxing() {
7575
boxAndUnbox(value: 123456789 as UInt32,
7676
expectedTypeName: System.UInt32.fullTypeName,
7777
boxFunc: { $0.dotNETObject() },
7878
unboxFunc: { try $0.castToUInt32() })
7979
}
8080

81-
func testInt64() {
81+
func testInt64Boxing() {
8282
boxAndUnbox(value: -123456789123456789 as Int64,
8383
expectedTypeName: System.Int64.fullTypeName,
8484
boxFunc: { $0.dotNETObject() },
8585
unboxFunc: { try $0.castToInt64() })
8686
}
8787

88-
func testUInt64() {
88+
func testUInt64Boxing() {
8989
boxAndUnbox(value: 123456789123456789 as UInt64,
9090
expectedTypeName: System.UInt64.fullTypeName,
9191
boxFunc: { $0.dotNETObject() },
9292
unboxFunc: { try $0.castToUInt64() })
9393
}
94+
95+
func testDoubleExtensions() throws {
96+
func check(input: Double, expected: Int32) throws {
97+
let result = try input.roundToInt()
98+
XCTAssertEqual(expected, result)
99+
}
100+
101+
try check(input: 1.1, expected: 1)
102+
try check(input: 1.6, expected: 2)
103+
try check(input: 999.4, expected: 999)
104+
try check(input: 9999.9, expected: 10_000)
105+
}
106+
107+
func testFloatExtensions() throws {
108+
func check(input: Float, expected: Int32) throws {
109+
let result = try input.roundToInt()
110+
XCTAssertEqual(expected, result)
111+
}
112+
113+
try check(input: 1.1, expected: 1)
114+
try check(input: 1.6, expected: 2)
115+
try check(input: 999.4, expected: 999)
116+
try check(input: 9999.9, expected: 10_000)
117+
}
94118
}
95119

96-
private extension PrimitivesBoxingTests {
120+
private extension PrimitivesTests {
97121
func boxAndUnbox<T>(value: T,
98122
expectedTypeName: String,
99123
boxFunc: (_ input: T) -> System.Object?,

Samples/Generated/Generated_C.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Number of generated types: 1637
2-
// Number of generated members: 7778
1+
// Number of generated types: 1638
2+
// Number of generated members: 7782
33

44
#pragma mark - BEGIN Header
55
#ifndef TypeDefinitions_h
@@ -6167,6 +6167,8 @@ typedef void* Beyond_NET_Sample_Source_OverrideTestsBaseClass_t;
61676167

61686168
typedef void* Beyond_NET_Sample_Source_OverrideTestsDerivedClass_t;
61696169

6170+
typedef void* Beyond_NET_Sample_Source_PrimitiveExtensionsTests_t;
6171+
61706172
typedef void* Beyond_NET_Sample_Transformer_BuiltInTransformers_t;
61716173

61726174
typedef void* System_Action_t;
@@ -61915,6 +61917,31 @@ Beyond_NET_Sample_Source_OverrideTestsDerivedClass_Destroy(
6191561917

6191661918
#pragma mark - END APIs of Beyond.NET.Sample.Source.OverrideTestsDerivedClass
6191761919

61920+
#pragma mark - BEGIN APIs of Beyond.NET.Sample.Source.PrimitiveExtensionsTests
61921+
int32_t /* System.Int32 */
61922+
Beyond_NET_Sample_Source_PrimitiveExtensionsTests_RoundToInt(
61923+
double /* System.Double */ value,
61924+
System_Exception_t _Nullable* _Nullable /* System.Exception */ outException
61925+
);
61926+
61927+
int32_t /* System.Int32 */
61928+
Beyond_NET_Sample_Source_PrimitiveExtensionsTests_RoundToInt_1(
61929+
float /* System.Single */ value,
61930+
System_Exception_t _Nullable* _Nullable /* System.Exception */ outException
61931+
);
61932+
61933+
System_Type_t _Nonnull /* System.Type */
61934+
Beyond_NET_Sample_Source_PrimitiveExtensionsTests_TypeOf(
61935+
void
61936+
);
61937+
61938+
void /* System.Void */
61939+
Beyond_NET_Sample_Source_PrimitiveExtensionsTests_Destroy(
61940+
Beyond_NET_Sample_Source_PrimitiveExtensionsTests_t _Nullable /* Beyond.NET.Sample.Source.PrimitiveExtensionsTests */ self
61941+
);
61942+
61943+
#pragma mark - END APIs of Beyond.NET.Sample.Source.PrimitiveExtensionsTests
61944+
6191861945
#pragma mark - BEGIN APIs of Beyond.NET.Sample.Transformer.BuiltInTransformers
6191961946
Beyond_NET_Sample_Transformer_StringTransformerDelegate_t _Nonnull /* Beyond.NET.Sample.Transformer.StringTransformerDelegate */
6192061947
Beyond_NET_Sample_Transformer_BuiltInTransformers_UppercaseStringTransformer_Get(

Samples/Generated/Generated_CS.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// <auto-generated />
22

3-
// Number of generated types: 1637
4-
// Number of generated members: 7778
3+
// Number of generated types: 1638
4+
// Number of generated members: 7782
55

66
// <Header>
77
using System;
@@ -230186,6 +230186,75 @@ internal unsafe class Beyond_NET_Sample_Source_OverrideTestsDerivedClass
230186230186
}
230187230187

230188230188

230189+
internal unsafe class Beyond_NET_Sample_Source_PrimitiveExtensionsTests
230190+
{
230191+
[UnmanagedCallersOnly(EntryPoint = "Beyond_NET_Sample_Source_PrimitiveExtensionsTests_RoundToInt")]
230192+
internal static int /* System.Int32 */ Beyond_NET_Sample_Source_PrimitiveExtensionsTests_RoundToInt(double /* System.Double */ value, void** /* System.Exception */ __outException)
230193+
{
230194+
230195+
try {
230196+
System.Int32 __returnValue = Beyond.NET.Sample.Source.PrimitiveExtensionsTests.RoundToInt(value);
230197+
230198+
if (__outException is not null) {
230199+
*__outException = null;
230200+
}
230201+
230202+
return __returnValue;
230203+
} catch (Exception __exception) {
230204+
if (__outException is not null) {
230205+
void* __exceptionHandleAddress = __exception.AllocateGCHandleAndGetAddress();
230206+
230207+
*__outException = __exceptionHandleAddress;
230208+
}
230209+
230210+
return -1;
230211+
} finally {
230212+
}
230213+
}
230214+
230215+
[UnmanagedCallersOnly(EntryPoint = "Beyond_NET_Sample_Source_PrimitiveExtensionsTests_RoundToInt_1")]
230216+
internal static int /* System.Int32 */ Beyond_NET_Sample_Source_PrimitiveExtensionsTests_RoundToInt_1(float /* System.Single */ value, void** /* System.Exception */ __outException)
230217+
{
230218+
230219+
try {
230220+
System.Int32 __returnValue = Beyond.NET.Sample.Source.PrimitiveExtensionsTests.RoundToInt(value);
230221+
230222+
if (__outException is not null) {
230223+
*__outException = null;
230224+
}
230225+
230226+
return __returnValue;
230227+
} catch (Exception __exception) {
230228+
if (__outException is not null) {
230229+
void* __exceptionHandleAddress = __exception.AllocateGCHandleAndGetAddress();
230230+
230231+
*__outException = __exceptionHandleAddress;
230232+
}
230233+
230234+
return -1;
230235+
} finally {
230236+
}
230237+
}
230238+
230239+
[UnmanagedCallersOnly(EntryPoint = "Beyond_NET_Sample_Source_PrimitiveExtensionsTests_TypeOf")]
230240+
internal static void* /* System.Type */ Beyond_NET_Sample_Source_PrimitiveExtensionsTests_TypeOf()
230241+
{
230242+
230243+
System.Type __returnValue = typeof(Beyond.NET.Sample.Source.PrimitiveExtensionsTests);
230244+
void* __returnValueNative = __returnValue.AllocateGCHandleAndGetAddress();
230245+
return __returnValueNative;
230246+
}
230247+
230248+
[UnmanagedCallersOnly(EntryPoint = "Beyond_NET_Sample_Source_PrimitiveExtensionsTests_Destroy")]
230249+
internal static void /* System.Void */ Beyond_NET_Sample_Source_PrimitiveExtensionsTests_Destroy(void* /* Beyond.NET.Sample.Source.PrimitiveExtensionsTests */ __self)
230250+
{
230251+
230252+
InteropUtils.FreeIfAllocated(__self);
230253+
}
230254+
230255+
}
230256+
230257+
230189230258
internal unsafe class Beyond_NET_Sample_DelegatesTest_DebugEventHandler
230190230259
{
230191230260
internal void* Context { get; }

0 commit comments

Comments
 (0)