Created
July 26, 2020 15:27
-
-
Save wingyplus/08656ff8da061445f39a0af8c052e0c4 to your computer and use it in GitHub Desktop.
Learn C# from Unity. Use NUnit for test framework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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