Hi there!

 

I've just begun writing a testcase for System.Convert and got some questions since it is my first test.

I would like to sign up to do the test for System.Convert. If it is available.

 

I need some feedback on this code if there is someone who has the time to help. (what have I missed, coding conventions and so on.)

What from the specification do I need to test? (Is there anything that is more important?)

Do I need to test all the overloaded methods in I.E. Convert.ToBoolean()? If not, what should I focus on.

 

Any help is welcome.

 

What is preferable, attaching code or pasting code in the mail body?
 
//Krister
 
 
 
The code
 
----------------------------------------------
 
using NUnit.Framework;
using System;
using System.Globalization;
 
namespace MonoTests.System
{
public class ConvertTest : TestCase
{
public ConvertTest() : base ("MonoTests.System.ConvertTest testsuite") {}
public ConvertTest(string name) : base(name) {}
 
bool tryBool;
byte tryByte;
char tryChar;
DateTime tryDT;
decimal tryDec;
double tryDbl;
short tryInt16;
int tryInt32;
long tryInt64;
object tryObj;
sbyte trySByte;
Single trySngl;
string falseString;
string trueString;
string nullString;
string tryStr;
ushort tryUI16;
uint tryUI32;
ulong tryUI64;
CultureInfo ci;
 
protected override void SetUp() {
tryBool = true;
tryByte = 0;
tryChar = 'a';
tryDT = new DateTime(2002,5,7);
tryDec = 1234.2345m;
tryDbl = 0;
tryInt16 = 1234;
tryInt32 = 12345678;
tryInt64 = 123456789012;
tryObj = (object)tryDbl;
trySByte = 123;
trySngl = 1234.2345f;
falseString = "false";
trueString = "true";
nullString = null;
tryStr = "foobar";
tryUI16 = 34567;
tryUI32 = 567891234;
tryUI64 = 0;
ci = new CultureInfo("sv-SE");
ci.NumberFormat.NumberDecimalDigits = 3;
}
protected override void TearDown() {}
public static ITest Suite {
get {
return new TestSuite(typeof(ConvertTest));
}
}
public void TestChangeType() {
TypeCode tcInt16 = new Int16().GetTypeCode();
 
AssertEquals("#A01", new short().GetType(),
Convert.ChangeType(tryInt32, tcInt16).GetType());
AssertEquals("#A02", 'A', Convert.ChangeType(65, typeof(char)));
AssertEquals("#A03", 66, Convert.ChangeType('B', typeof(int)));
try {
Convert.ChangeType(myBool, typeof(char));
}
catch (Exception e) {
AssertEquals("ChangeType should be a InvalidCastException",
typeof(InvalidCastException), e.GetType());
}
 
try {
Convert.ChangeType(myBool, null);
}
catch (Exception e) {
AssertEquals("ChangeType should be a ArgumentNullException",
typeof(ArgumentNullException), e.GetType());
}
}
 
public void TestGetTypeCode()
{
TypeCode tc1 = new Int32().GetTypeCode();
TypeCode tc2 = new Boolean().GetTypeCode();
int tcInt32 = 1234;
bool tcBool = true;
AssertEquals("#B01", tc1, tcInt32.GetTypeCode());
AssertEquals("#B02", tc2, tcBool.GetTypeCode());
}
public void TestIsDBNull() {
DBNull db = (DBNull)Convert.DBNull;
int dbNullInt = 1234;
 
AssertEquals("#C01", false, Convert.IsDBNull(dbNullInt));
AssertEquals("#C02", true, Convert.IsDBNull(db));
}
 
public void TestToBoolean() {
 
AssertEquals("#D01", true, Convert.ToBoolean(tryBool));
AssertEquals("#D02", false, Convert.ToBoolean(tryByte));
 
try {
Convert.ToBoolean(tryChar);
}
catch (Exception e) {
AssertEquals("#D03 should throw a InvalidCastException",
typeof(InvalidCastException), e.GetType());
}
 
try {
Convert.ToBoolean(tryDT);
}
catch (Exception e) {
AssertEquals("#D04 should throw a InvalidCastException",
typeof(InvalidCastException), e.GetType());
}
AssertEquals("#D05", true, Convert.ToBoolean(tryDec));
AssertEquals("#D06", false, Convert.ToBoolean(tryDbl));
AssertEquals("#D07", true, Convert.ToBoolean(tryInt16));
AssertEquals("#D08", true, Convert.ToBoolean(tryInt32));
AssertEquals("#D09", true, Convert.ToBoolean(tryInt64));
AssertEquals("#D10", false, Convert.ToBoolean(tryObj));
AssertEquals("#D11", true, Convert.ToBoolean(trySByte));
AssertEquals("#D12", true, Convert.ToBoolean(trySngl));
AssertEquals("#D13", true, Convert.ToBoolean(trueString));
AssertEquals("#D14", false, Convert.ToBoolean(falseString));
try {
Convert.ToBoolean(tryStr);
}
catch (Exception e) {
AssertEquals("#D15 should throw a FormatException",
typeof(FormatException), e.GetType());
}
try {
Convert.ToBoolean(nullString);
}
catch (Exception e) {
AssertEquals("#D16 should throw a ArgumentNullException",
typeof(ArgumentNullException), e.GetType());
}
AssertEquals("#D17", true, Convert.ToBoolean(tryUI16));
AssertEquals("#D18", true, Convert.ToBoolean(tryUI32));
AssertEquals("#D19", false, Convert.ToBoolean(tryUI64));
AssertEquals("#D20", false, Convert.ToBoolean(tryObj,ci));
AssertEquals("#D21", false, Convert.ToBoolean(tryObj));
 
}
}
}
 
--------------------------------------------------------------------

Reply via email to