, . . ( , ), , :
, :
public class ColorParser {
public static bool TryParseHex(string value, out Color color) {
}
}
- :
public class ColorParserTests
{
public class TryParseHexTests
{
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNullValue()
{
Color color = Color.Empty;
ColorParser.TryParseHex(null, out color);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestEmptyValue()
{
Color color = Color.Empty;
ColorParser.TryParseHex(string.Empty, out color);
}
[TestMethod]
public void TestInvalidValues()
{
string[] invalidValues = new string[]
{
"#",
"FF",
"#FF",
"#FFAA",
"asdf"
};
foreach (var invalidValue in invalidValues)
{
Color color = Color.Empty;
bool result = ColorParser.TryParseHex(invalidValue, out color);
Assert.IsFalse(result, string.Format("Value '{0}' must not pass the test!", invalidValue));
}
}
[TestMethod]
public void TestValidValues()
{
Dictionary<string, Color> validValues = new Dictionary<string, Color>()
{
{" #000000", Color.FromArgb(0,0,0)},
{" #010203 ", Color.FromArgb(1,2,3)},
{"#00FFFF", Color.FromArgb(0,255,255)},
{"#FF00FFFF", Color.FromArgb(255,0,255,255)},
};
foreach (var validValue in validValues)
{
Color color = Color.Empty;
bool result = ColorParser.TryParseHex(validValue.Key, out color);
Assert.IsTrue(result, string.Format("Value '{0}' must pass the test!", validValue.Key));
Assert.AreEqual(validValue.Value, color, "Parsed color must be the same.");
}
}
}
}
, ( ), . , . , , , , exacly . .
, , , , , , - , .
Nulls Emptys , , , .
You can break the tests and write more tests, such as TestValuesWithSpacesor TestNegativeDepositor TestUserNameWithInvalidCharactersetc. It always depends how much you need to check and how exactly you want to do it. In this case, I found this sufficient. In any case, I think it is very descriptive.