Hello everyone:
Today's TDD exercises are starting again. Look back at the last task left.
To-do-list:
Guessing numbers
Input validation
Generate answers
Number of inputs
Output guessing results
Today we take input validation and random generation answers.
Create a new Validationtest file.
Analysis Requirements: (1) do not repeat. (2) 4 digits (3) digits. (4) Not empty.
According to our analysis of the 4 clear points we began to write case.
Note the naming.
[CSharp] View plain copy [testclass] public class validatortest { & nbsp; private validator validator; [TestInitialize] public void init () { validator = new validator (); } [TestMethod] public void should_return_input_must_be_four_digits_when_input_figures_digit_is_not_four_ Digits () { var input = "29546"; validator. Validate (input); var actual = Validator. errormsg; assert.areequal ("The input must be four digits. ", actual); } [TestMethod] public void should_return_input_ Must_be_fully_digital_when_input_is_not_all_digital () { var input = "A4S5"; validator. Validate (input); var actual = Validator. errormsg; assert.areequal ("The input must be fully digital. ", actual); } [TestMethod] publIc void should_return_input_can_not_be_empty_when_input_is_empty () { var input = ""; validator. Validate (input); var actual = Validator. errormsg; assert.areequal ("the input Can ' T be empty, actual); } [TestMethod] public void should_return_input_ Can_not_contain_duplicate_when_input_figures_contain_duplicate () { var input = "2259"; validator. Validate (input); var actual = validator. errormsg; assert.areequal ("the input Figures can ' T contain duplicate, actual); } } Follow the steps in the first article. Implement Validator. Try to get all the case over.
[CSharp] View plain copy 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;       &Nbsp; } 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;         &NBsp; } return true; } }
Run ...
A case corresponds to this if. You can also merge 2 case. You can use "^\d{4}$" to cover "4 digits". Can be determined according to their own circumstances.
Small steps do not have to use very little granularity to go step-by-step. This can be very slow to develop. Rely on your own circumstances to decide how big a small step should be. The so-called "stride Big Easy to pull the egg, step small forward too slow". Just find the right steps for you. Will go better.
So many if it looks like a sore egg. There are tests. Can be assured of bold refactoring. Pull out a method for each if. It seems to be a little clearer.
[CSharp] View plain copy 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)            &NBSp { return true; } ErrorMsg = "the Input figures can ' T contain duplicate. "; return false; } }
To ensure that the refactoring is correct. After refactoring, be sure to run through all the case to determine all the pass.
To-do-list:
Guessing numbers
Input validation
Generate answers
Number of inputs
Output guessing results
The verification is done. Let's take a full random number.
Analysis Requirements: The product code requires a randomly generated answer. (1) Do not repeat. (2) 4 digits (3) digits.
Here's a question: we all know that random numbers are a matter of probability. Because the number generated each time is different. Look at the code of the previous Guesser class.[CSharp] View plain copy public class guesser { private const string AnswerNumber = "2975"; public string guess (String inputNumber) { var aCount = 0; var bCount = 0; for (var index = 0; index < answernumber.length; index++) { if (Answernumber[index]==inputnumber[index]) { aCount++; continue; } if (Answernumber.contains (Inputnumber[index). ToString ())) { bCount++; } } return string. Format ("{0}a{1}b", acount, bcount); } }
Here, if we put the private const string answernumber = "2975", instead of random, then the result of the Guesser class test is not deterministic. This means that the test relies on something mutable. such as: random number, time and so on.
What should I do in this situation? A random number is for the product code, we can mock another "fixed random number" (but to meet the conditions of generating random numbers) to test.
Just write the test first. [CSharp] view plain copy [TestClass] public class Answergeneratortest {[TestMethod] public void Should_pass_when_answer_generator_number_is_four_digits_and_fully_digital () {Rege X Regex =&NB