c# - DateTime.AddDays what value to pass to generate an exception -
i have little utility class generate hash code input data. class & algorithm not important question looks below:
public static class hashcodehelper { /// <summary> /// homecoming hash code added link in download data. /// </summary> /// <param name="basedatetime">today's date</param> /// <param name="rrid">profile id</param> /// <param name="downloadcode">download code used hash code generation.</param> /// <returns>final result string in format "20140715385"</returns> public static string generatehashcodeforexportlink(datetime basedatetime, int rrid, string downloadcode) { var todaydate = basedatetime; int expireindays; if (!int.tryparse(configurationmanager.appsettings["exportlinkexpireindays"], out expireindays)) expireindays = 30; // if not specified in web.config file utilize default value var expirydate = todaydate.adddays(expireindays); var currentday = todaydate.day; var expirymonth = expirydate.month; char nthchar = convert.tochar(downloadcode.substring(expirymonth - 1, 1)); var asciivalue = (int)nthchar; var mod = (rrid % currentday); var computedhash = (asciivalue * expirymonth) + currentday + mod; var fullhashcode = todaydate.tostring("yyyymmdd") + computedhash; homecoming fullhashcode; } }
i writing unit test cases class , realized adddays()
can throw argumentoutofrangeexception
, below line:
var expirydate = todaydate.adddays(expireindays);
so should write test , did write below test case:
[testmethod] public void generatehashcodeforexportlink_incorrectdate_throw() { seek { hashcodehelper.generatehashcodeforexportlink(new datetime(2015, 1, 31), 501073, "001-345-673042"); assert.fail("exception not thrown"); } grab (argumentoutofrangeexception) { } grab (exception) { assert.fail("incorrect exception thrown"); } }
the problem is, not know pass cause adddays()
method throw exception? tried passing random dates e.g. 1-1-1800, 30-jan-2015, etc.
i looked @ adddays()
method implementation not create out. idea?
if pass in int.maxvalue
, should end value outside representable range datetime
, whatever original datetime
is.
sample code, tested on csharppad.com:
datetime.minvalue.adddays(int.maxvalue);
using assert.fail
in finally
block error though... always called. it's not clear test framework is, i'd expect like:
assert.throws<argumentoutofrangeexception>(() => /* code here */);
c# unit-testing
No comments:
Post a Comment