Skip to content

Commit

Permalink
Add more C# unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfbiedert committed Jan 31, 2025
1 parent 9a4a5c0 commit 850ff1f
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Linq;
using My.Company;
using My.Company.Common;
using Xunit;

public class TestPatternServicesBasic
{
[Fact]
public void service_basic()
{
var _ = BasicService.New();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Linq;
using My.Company;
using My.Company.Common;
using Xunit;

public class TestPatternServicesCallbacks
{

[Fact]
public void service_callbacks()
{
var callbacks = ServiceCallbacks.New();
var called = false;

callbacks.CallbackSimple((x) =>
{
called = true;
Assert.Equal(x, 0u);
return x;
});

Assert.True(called);
}

[Fact]
public void service_callbacks_with_slice()
{
var callbacks = ServiceCallbacks.New();
var called = false;
var slice = new[] { 1, 2, 3 };

callbacks.CallbackWithSlice((x, y) =>
{
Assert.Equal(x, 1);
Assert.Equal(y, 2);
called = true;
return FFIError.Ok;
}, slice);

Assert.True(called);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Linq;
using My.Company;
using My.Company.Common;
using Xunit;

public class TestPatternServicesLifetimes
{

[Fact]
public void service_lifetimes()
{
uint value = 123;
var bools = new[] { Bool.True, Bool.True, Bool.False };
var bytes = new byte[] { 0 };

var service = ServiceUsingLifetimes.NewWith(ref value);

service.Lifetime1(bools);
service.Lifetime2(bools);

var str = service.ReturnStringAcceptSlice(bytes);

Assert.True(string.IsNullOrEmpty(str));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using My.Company;
using My.Company.Common;
using Xunit;

public class TestPatternServicesMultipleCtors
{

[Fact]
public void service_ctors()
{
ServiceMultipleCtors.NewWith(123);
ServiceMultipleCtors.NewWithout();
ServiceMultipleCtors.NewWithString("hello world");

try
{
ServiceMultipleCtors.NewFailing(123);
Assert.True(false);
}
catch (InteropException<FFIError>) { }
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Linq;
using My.Company;
using My.Company.Common;
using Xunit;

public class TestPatternServicesOnPanic
{

[Fact]
public void service_onpanic()
{
var service = ServiceOnPanic.New();

service.ReturnResult(123);

Assert.Equal(service.ReturnDefaultValue(123u), 123u);
Assert.Equal(service.ReturnUbOnPanic(), "Hello new_with");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using My.Company.Common;
using Xunit;

public class TestPatternServices
public class TestPatternServicesSlices
{
[Fact]
public void service_slices()
Expand All @@ -24,13 +24,4 @@ public void service_slices()
var s3 = lt.ReturnStringAcceptSlice(System.Array.Empty<byte>());
var s4 = lt.ReturnStringAcceptSlice(System.Array.Empty<byte>());
}

[Fact]
public void service_lifetimes()
{
var service_slices = ServiceStrings.New();

var s1 = service_slices.ReturnString();
var s2 = service_slices.ReturnString();
}
}
16 changes: 16 additions & 0 deletions tests/tests/csharp_reference_project/Test.Pattern.Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@

public class TestPatternStrings
{
[Fact]
public void pattern_ascii_pointer_1()
{
var x = Interop.pattern_ascii_pointer_1("hello world");
Assert.Equal(x, 11u);
}

[Fact]
public void pattern_ascii_pointer_2()
{
// TODO: Why does this not codegen properly?
var rval = Interop.pattern_ascii_pointer_2();
// Assert.Equal(string.IsNullOrEmpty(rval));
}


[Fact]
public void string_slices()
{
Expand Down

0 comments on commit 850ff1f

Please sign in to comment.