I have to write my question in other words.
I'm developing my own geometric primitive structs for some reasons: Point and Size:
public struct Point : IEquatable<Point> {
public static bool operator ==(Point left, Point right) {
return left.Equals(right);
}
public static bool operator !=(Point left, Point right) {
return !left.Equals(right);
}
public static implicit operator Point(System.Drawing.Point point) {
return new Point(point.X, point.Y);
}
int x;
int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public bool Equals(Point other) {
return
x == other.x &&
y == other.y;
}
public override bool Equals(object obj) {
if (obj.GetType() != typeof(Point))
return false;
return Equals((Point)obj);
}
public override int GetHashCode() {
return x^y;
}
public override string ToString() {
return $"{{X={x.ToString(CultureInfo.CurrentCulture)}, Y={y.ToString(CultureInfo.CurrentCulture)}}}";
}
public string ToStringInvariant() {
return $"{{X={x.ToString(CultureInfo.InvariantCulture)}, Y={y.ToString(CultureInfo.InvariantCulture)}}}";
}
}
public struct Size : IEquatable<Size> {
public static bool operator ==(Size size1, Size size2) {
return size1.Equals(size2);
}
public static bool operator !=(Size size1, Size size2) {
return !size1.Equals(size2);
}
public static implicit operator Size(System.Drawing.Size size) {
return new Size(size.Width, size.Height);
}
int width;
int height;
public int Width {
get { return width; }
set {
if (value < 0)
throw new ArgumentException("The Width property value must not be negative.");
width = value;
}
}
public int Height {
get { return height; }
set {
if (value < 0)
throw new ArgumentException("The Height property value must not be negative.");
height = value;
}
}
public Size(int width, int height) {
if (width < 0 || height < 0)
throw new ArgumentException("The Width and Height property values must not be negative.");
this.width = width;
this.height = height;
}
public bool Equals(Size other) {
return
width == other.width &&
height == other.height;
}
public override bool Equals(object obj) {
if (obj.GetType() != typeof(Size))
return false;
return Equals((Size)obj);
}
public override int GetHashCode() {
return HashCodeHelper.CalculateGeneric(width, height);
}
public override string ToString() {
return $"{{Width={width.ToString(CultureInfo.CurrentCulture)}, Height={height.ToString(CultureInfo.CurrentCulture)}}}";
}
public string ToStringInvariant() {
return $"{{Width={width.ToString(CultureInfo.InvariantCulture)}, Height={height.ToString(CultureInfo.InvariantCulture)}}}";
}
}
Now I need to write tests, particularry I should test my ToString and ToStringInvariant() methods.
- Test should document current behaviour to avoid unexpected metod behaviour change in the feature.
- Assert, that ToString returns string, which contains numbers in Current Culture, but ToStringInvariant always contains string with invariant-formated numbers.
To do this I need some Culture, where integer formatting are differ from InvariantCulture integer number formatting.
Since the Point class can has negative numbers, I can just change the negative sign representation and write the tests:
[Test]
public void ToStringTest() {
Point point = new Point(-1, 2);
var preCulture = Thread.CurrentThread.CurrentCulture;
try {
var enCulture = new CultureInfo("en-US");
enCulture.NumberFormat.NegativeSign = "Minus!";
Thread.CurrentThread.CurrentCulture = enCulture;
Assert.That(point.ToString() == "{X=Minus!1, Y=2}");
}
finally {
Thread.CurrentThread.CurrentCulture = preCulture;
}
}
[Test]
public void ToStringInvariantTest() {
Point point = new Point(1, 2);
var preCulture = Thread.CurrentThread.CurrentCulture;
try {
var enCulture = new CultureInfo("en-US");
enCulture.NumberFormat.NegativeSign = "Minus!";
Thread.CurrentThread.CurrentCulture = enCulture;
Assert.That(point.ToStringInvariant() == "{X=1, Y=2}");
}
finally {
Thread.CurrentThread.CurrentCulture = preCulture;
}
}
The Size structure can have only positive or zero fields, so I should change the digit representation. I tried to use the "prs-AF" culture. But 1.ToString("prs-AF") is still "1". How to test ToString and ToStringInvariant methods for the Size struct?
OLD TEXT:
I have primitives: Point and Size classes. I override the ToString methods in this way:
public override string ToString() {
return $"{{X={x.ToString(CultureInfo.CurrentCulture)}, Y={y.ToString(CultureInfo.CurrentCulture)}}}";
}
I also have a
public string ToStringInvariant() {
return $"{{X={x.ToString(CultureInfo.InvariantCulture)}, Y={y.ToString(CultureInfo.InvariantCulture)}}}";
}
I need a test for this method. For the Point class I can set the NegativeSign in a CultureInfo instance:
[Test]
public void ToStringTest() {
Point point = new Point(-1, 2);
var preCulture = Thread.CurrentThread.CurrentCulture;
try {
var enCulture = new CultureInfo("en-US");
enCulture.NumberFormat.NegativeSign = "Minus!";
Thread.CurrentThread.CurrentCulture = enCulture;
Assert.That(point.ToString() == "{X=Minus!1, Y=2}");
}
finally {
Thread.CurrentThread.CurrentCulture = preCulture;
}
}
But for the Size class I can't do this, because Width and Height can not be negative, so I need to create a CultureInfo instance, where positive numbers are not consist of {1,2,3,4,5,6,7,8,9,0}.
If there is no such Culture, I just don't need this test for the Size class.