????????????TDD(2)
???????????? ???????[ 2014/1/17 11:30:54 ] ????????TDD ????????
???????????????衣???validator??????????е?CASE??????
public class Validator
{
public string ErrorMsg { get; private set; }
public bool Validate(string input)
{
if (string.IsNullOrEmpty(input))
{
ErrorMsg = "the input can't be empty.";
return false;
}
if (input.Length != 4)
{
ErrorMsg = "the input must be four digits.";
return false;
}
var regex = new Regex(@"^[0-9]*$");
if (!regex.IsMatch(input))
{
ErrorMsg = "the input must be fully digital.";
return false;
}
if (input.Distinct().Count() != 4)
{
ErrorMsg = "the input figures can't contain duplicate.";
return false;
}
return true;
}
}
Run...
???????CASE????????IF???????2??CASE????????"^d{4}$"?Cover"4λ????"??????????????????????
????С????????????ú?С?????????????????????????????????????????????????????????????С??????????ж??????ν"??????????????????????С????????"????????????????????????????á?
?????????IF?????????????в????????????????????????IF????????????????????????Щ??
public class Validator
{
public string ErrorMsg { get; private set; }
public bool Validate(string input)
{
return IsEmpty(input) && IsFourdigits(input) && IsDigital(input) && IsRepeat(input);
}
private bool IsEmpty(string input)
{
if (!string.IsNullOrEmpty(input))
{
return true;
}
ErrorMsg = "the input can't be empty.";
return false;
}
private bool IsFourdigits(string input)
{
if (input.Length == 4)
{
return true;
}
ErrorMsg = "the input must be four digits.";
return false;
}
private bool IsDigital(string input)
{
var regex = new Regex(@"^[0-9]*$");
if (regex.IsMatch(input))
{
return true;
}
ErrorMsg = "the input must be fully digital.";
return false;
}
private bool IsRepeat(string input)
{
if (input.Distinct().Count() == 4)
{
return true;
}
ErrorMsg = "the input figures can't contain duplicate.";
return false;
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11