Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Created July 26, 2020 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wingyplus/08656ff8da061445f39a0af8c052e0c4 to your computer and use it in GitHub Desktop.
Save wingyplus/08656ff8da061445f39a0af8c052e0c4 to your computer and use it in GitHub Desktop.
Learn C# from Unity. Use NUnit for test framework.
using System;
using NUnit.Framework;
namespace Syntax.Tests
{
public struct Vector2 : IFormattable
{
public float x, y;
public static Vector2 operator -(Vector2 a, Vector2 b)
=> new Vector2 {x = a.x - b.x, y = a.y - b.y};
public string ToString(string format, IFormatProvider formatProvider)
{
if (string.IsNullOrEmpty(format))
{
format = "F1";
}
return $"({x.ToString(format, formatProvider)}, {y.ToString(format, formatProvider)})";
}
}
[TestFixture]
public class LearnStructTests
{
private void ModifyVector2(Vector2 vec)
{
vec.x = 2;
}
[Test]
public void TestStruct()
{
var vec = new Vector2
{
x = 1,
y = 20
};
ModifyVector2(vec);
Assert.AreEqual(1, vec.x);
}
[Test]
public void TestOperatorOverloading()
{
var target = new Vector2 {x = 2, y = 30};
var player = new Vector2 {x = 1, y = 20};
Assert.AreEqual(new Vector2 {x = 1, y = 10}, target - player);
}
[Test]
public void TestFormatString()
{
var vec = new Vector2 {x = 1, y = 2};
Assert.AreEqual("(1.0, 2.0)", $"{vec}");
Assert.AreEqual("(1.00, 2.00)", $"{vec:F2}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment