Читать книгу Practical Go - Amit Saha - Страница 39
Testing the Parsing Logic
ОглавлениеThe primary change in our greeter program, as compared to the first version, is in how we are parsing the command-line arguments using the flag
package. You will notice that you have already written the greeter program, specifically the parseArgs()
function, in a unit testing friendly fashion:
1 A new FlagSet object is created in the function.
2 Using the Output() method of the FlagSet object , you made sure that any messages from the FlagSet methods were written to the specified io.Writer object, w.
3 The arguments to parse were being passed as a parameter, args .
The function is well encapsulated and avoids using any global state. A test for the function is shown in Listing 1.6.