Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: follow csharp conventions #63

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dotnet-tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]

# License header
file_header_template = This Source Code Form is subject to the terms of the Mozilla Public\nLicense, v. 2.0. If a copy of the MPL was not distributed with this\nfile, You can obtain one at http://mozilla.org/MPL/2.0/.

# 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
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

using System.Diagnostics;
using System.IO;
using System;
using uniffi.arithmetic;

using ArithmeticException = uniffi.arithmetic.ArithmeticException;

public class TestArithmetic {
namespace UniffiCS.BindingTests;

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 Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

using System;
using uniffi.callbacks;

class SomeOtherError: Exception {}
namespace UniffiCS.BindingTests;

class CallAnswererImpl: CallAnswerer {
class SomeOtherError : Exception { }

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 @@ -50,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 @@ -71,4 +89,3 @@ private T CallInItsOwnScope<T>(Func<T> getter)
return getter();
}
}

Original file line number Diff line number Diff line change
@@ -1,89 +1,123 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

using System;
using System.Collections.Generic;
using System.Linq;
using System;
using uniffi.fixture_callbacks;

class CsharpGetters: ForeignGetters {
public Boolean GetBool(Boolean v, Boolean argumentTwo) {
namespace UniffiCS.BindingTests;

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 @@ -94,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