Skip to content

Commit

Permalink
chore: reformat csharp code using csharpier
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Meenzen <[email protected]>
  • Loading branch information
meenzen committed Dec 29, 2023
1 parent b684fbf commit 77185ad
Show file tree
Hide file tree
Showing 17 changed files with 443 additions and 247 deletions.
17 changes: 17 additions & 0 deletions dotnet-tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# CSharpier: https://csharpier.com/docs/Configuration
[*]
# Non-configurable behaviors
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

# Configurable behaviors
end_of_line = lf
indent_style = space
indent_size = 4
max_line_length = 120
8 changes: 5 additions & 3 deletions dotnet-tests/UniffiCS.BindingTests/TestArithmetic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace UniffiCS.BindingTests;

public class TestArithmetic {
public class TestArithmetic
{
[Fact]
public void ArithmeticWorks() {
public void ArithmeticWorks()
{
Assert.Equal(6ul, ArithmeticMethods.Add(2, 4));
Assert.Equal(12ul, ArithmeticMethods.Add(4, 8));

Expand All @@ -28,4 +30,4 @@ public void ArithmeticWorks() {
Assert.False(ArithmeticMethods.Equal(2, 4));
Assert.False(ArithmeticMethods.Equal(4, 8));
}
}
}
48 changes: 32 additions & 16 deletions dotnet-tests/UniffiCS.BindingTests/TestCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,54 @@

namespace UniffiCS.BindingTests;

class SomeOtherError: Exception {}
class SomeOtherError : Exception { }

class CallAnswererImpl: CallAnswerer {
class CallAnswererImpl : CallAnswerer
{
public String mode;
public CallAnswererImpl(String mode) {

public CallAnswererImpl(String mode)
{
this.mode = mode;
}

public String Answer() {
if (mode == "normal") {
public String Answer()
{
if (mode == "normal")
{
return "Bonjour";
} else if (mode == "busy") {
}
else if (mode == "busy")
{
throw new TelephoneException.Busy("I'm busy");
} else {
}
else
{
throw new SomeOtherError();
}
}
}

public class TestCallbacks {
public class TestCallbacks
{
[Fact]
public void CallbackWorks() {
using (var telephone = new Telephone()) {
public void CallbackWorks()
{
using (var telephone = new Telephone())
{
Assert.Equal("Bonjour", telephone.Call(new CallAnswererImpl("normal")));

Assert.Throws<TelephoneException.Busy>(() => telephone.Call(new CallAnswererImpl("busy")));

Assert.Throws<TelephoneException.InternalTelephoneException>(() => telephone.Call(new CallAnswererImpl("something-else")));
Assert.Throws<TelephoneException.InternalTelephoneException>(
() => telephone.Call(new CallAnswererImpl("something-else"))
);
}
}

[Fact]
public void CallbackRegistrationIsNotAffectedByGC() {
public void CallbackRegistrationIsNotAffectedByGC()
{
// See `static ForeignCallback INSTANCE` at `templates/CallbackInterfaceTemplate.cs`

var callback = new CallAnswererImpl("normal");
Expand All @@ -52,12 +67,13 @@ public void CallbackRegistrationIsNotAffectedByGC() {
telephone.Call(callback);
}


[Fact]
public void CallbackReferenceIsDropped() {
public void CallbackReferenceIsDropped()
{
var telephone = new Telephone();

var weak_callback = CallInItsOwnScope(() => {
var weak_callback = CallInItsOwnScope(() =>
{
var callback = new CallAnswererImpl("normal");
telephone.Call(callback);
return new WeakReference(callback);
Expand All @@ -72,4 +88,4 @@ private T CallInItsOwnScope<T>(Func<T> getter)
{
return getter();
}
}
}
132 changes: 93 additions & 39 deletions dotnet-tests/UniffiCS.BindingTests/TestCallbacksFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,115 @@

namespace UniffiCS.BindingTests;

class CsharpGetters: ForeignGetters {
public Boolean GetBool(Boolean v, Boolean argumentTwo) {
class CsharpGetters : ForeignGetters
{
public Boolean GetBool(Boolean v, Boolean argumentTwo)
{
return v ^ argumentTwo;
}

public String GetString(String v, Boolean arg2) {
if (v == "bad-argument") {
public String GetString(String v, Boolean arg2)
{
if (v == "bad-argument")
{
throw new SimpleException.BadArgument("bad argument");
}
if (v == "unexpected-error") {
if (v == "unexpected-error")
{
throw new Exception("something failed");
}
return arg2 ? "1234567890123" : v;
}

public String? GetOption(String? v, Boolean arg2) {
if (v == "bad-argument") {
public String? GetOption(String? v, Boolean arg2)
{
if (v == "bad-argument")
{
throw new ComplexException.ReallyBadArgument(20);
}
if (v == "unexpected-error") {
if (v == "unexpected-error")
{
throw new Exception("something failed");
}
return arg2 && v != null ? v.ToUpper() : v;
}

public List<Int32> GetList(List<Int32> v, Boolean arg2) {
public List<Int32> GetList(List<Int32> v, Boolean arg2)
{
return arg2 ? v : new List<Int32>();
}

public void GetNothing(String v) {
if (v == "bad-argument") {
public void GetNothing(String v)
{
if (v == "bad-argument")
{
throw new SimpleException.BadArgument("bad argument");
}
if (v == "unexpected-error") {
if (v == "unexpected-error")
{
throw new Exception("something failed");
}
}
}

class CsharpStringifier: StoredForeignStringifier {
public String FromSimpleType(Int32 value) {
class CsharpStringifier : StoredForeignStringifier
{
public String FromSimpleType(Int32 value)
{
return "C#: " + value.ToString();
}

public String FromComplexType(List<Double?>? values) {
if (values == null) {
return "C#: null";
} else {
var stringValues = values.Select(number => {
public String FromComplexType(List<Double?>? values)
{
if (values == null)
{
return "C#: null";
}
else
{
var stringValues = values.Select(number =>
{
return number == null ? "null" : number.ToString();
});
return "C#: " + string.Join(" ", stringValues);
}
}
}

public class TestCallbacksFixture {
public class TestCallbacksFixture
{
[Fact]
public void CallbackRoundTripValues() {
public void CallbackRoundTripValues()
{
var callback = new CsharpGetters();
using (var rustGetters = new RustGetters()) {
foreach (var v in new List<Boolean>{true, false}) {
using (var rustGetters = new RustGetters())
{
foreach (var v in new List<Boolean> { true, false })
{
var flag = true;
Assert.Equal(callback.GetBool(v, flag), rustGetters.GetBool(callback, v, flag));
}

foreach (var v in new List<List<Int32>>{new List<Int32>{1, 2}, new List<Int32>{0, 1}}) {
foreach (
var v in new List<List<Int32>>
{
new List<Int32> { 1, 2 },
new List<Int32> { 0, 1 }
}
)
{
var flag = true;
Assert.Equal(callback.GetList(v, flag), rustGetters.GetList(callback, v, flag));
}

foreach (var v in new List<String>{"Hello", "world"}) {
foreach (var v in new List<String> { "Hello", "world" })
{
var flag = true;
Assert.Equal(callback.GetString(v, flag), rustGetters.GetString(callback, v, flag));
}

foreach (var v in new List<String?>{"Some", null}) {
foreach (var v in new List<String?> { "Some", null })
{
var flag = true;
Assert.Equal(callback.GetOption(v, flag), rustGetters.GetOption(callback, v, flag));
}
Expand All @@ -96,43 +128,65 @@ public void CallbackRoundTripValues() {
}

[Fact]
public void CallbackRoundTripErrors() {
public void CallbackRoundTripErrors()
{
var callback = new CsharpGetters();
using (var rustGetters = new RustGetters()) {
using (var rustGetters = new RustGetters())
{
Assert.Throws<SimpleException.BadArgument>(() => rustGetters.GetString(callback, "bad-argument", true));
Assert.Throws<SimpleException.UnexpectedException>(() => rustGetters.GetString(callback, "unexpected-error", true));
Assert.Throws<SimpleException.UnexpectedException>(
() => rustGetters.GetString(callback, "unexpected-error", true)
);

var reallyBadArgument = Assert.Throws<ComplexException.ReallyBadArgument>(() => rustGetters.GetOption(callback, "bad-argument", true));
var reallyBadArgument = Assert.Throws<ComplexException.ReallyBadArgument>(
() => rustGetters.GetOption(callback, "bad-argument", true)
);
Assert.Equal(20, reallyBadArgument.code);

var unexpectedException = Assert.Throws<ComplexException.UnexpectedErrorWithReason>(() => rustGetters.GetOption(callback, "unexpected-error", true));
var unexpectedException = Assert.Throws<ComplexException.UnexpectedErrorWithReason>(
() => rustGetters.GetOption(callback, "unexpected-error", true)
);
Assert.Equal(new Exception("something failed").Message, unexpectedException.reason);
}
}

[Fact]
public void CallbackMayBeStoredInObject() {
public void CallbackMayBeStoredInObject()
{
var stringifier = new CsharpStringifier();
using (var rustStringifier = new RustStringifier(stringifier)) {
foreach (var v in new List<Int32>{1, 2}) {
using (var rustStringifier = new RustStringifier(stringifier))
{
foreach (var v in new List<Int32> { 1, 2 })
{
Assert.Equal(stringifier.FromSimpleType(v), rustStringifier.FromSimpleType(v));
}

foreach (var v in new List<List<Double?>?>{null, new List<Double?>{null, 3.14}}) {
foreach (
var v in new List<List<Double?>?>
{
null,
new List<Double?> { null, 3.14 }
}
)
{
Assert.Equal(stringifier.FromComplexType(v), rustStringifier.FromComplexType(v));
}
}
}

[Fact]
public void VoidCallbackExceptions() {
public void VoidCallbackExceptions()
{
var callback = new CsharpGetters();
using (var rustGetters = new RustGetters()) {
using (var rustGetters = new RustGetters())
{
// no exception
rustGetters.GetNothing(callback, "foo");

Assert.Throws<SimpleException.BadArgument>(() => rustGetters.GetNothing(callback, "bad-argument"));
Assert.Throws<SimpleException.UnexpectedException>(() => rustGetters.GetNothing(callback, "unexpected-error"));
Assert.Throws<SimpleException.UnexpectedException>(
() => rustGetters.GetNothing(callback, "unexpected-error")
);
}
}
}
}
Loading

0 comments on commit 77185ad

Please sign in to comment.