All right, it's time for an exercise you may have noticed at this point that for all of these packages,
there is one that we've written no test for whatsoever, and that's generally a bad thing.
So I'm going to ask you to write some tests for the forms package.
We don't need one for the config package because this is just the type that there's no way to test that
either a type is syntactically correct or it's not.
And this one is syntactically correct or it wouldn't compile.
But the forms package actually has, as you can see here, quite a few functions and there are no tests
for any of them.
So to get you started, I'm actually going to take you through the process for writing a couple of quick
tests.
So in the forms package, for example, we have this function valid and it is taken as a receiver of
type type pointer to form and it returns either true or false, either the form is valid or it is not.
And we can write a really simple test for that as I've done here.
And the syntax for the name this is the convention is test.
It would normally be test valid if the function didn't have a receiver, but because this function has
a receiver of type form, the naming convention is test and then form or the receiver name, followed
by an underscore, followed by the function.
So the test that we need to give it, if you can look at the form of the function itself, we need to
have a form to test in the first place.
So we'll create a request.
And I'm just creating a new HDB test, new request of type post with any new URL I want and Knill for
the body that gives me the variable error, which is of type request.
And then I just say create a new form by passing in our post form, which is of type you are of values
and in this case it's empty, but that's immaterial.
And then I just call form dot valid and store the result is valid.
And since there's nothing in the form to cause it to fail validation, it's empty.
I just checked to see if it's valid is false.
If it's false, I throw an error.
So there's a really simple test for one now a slightly more complex one is this one called required
and required is again it has a receiver of type pointer to form and it takes it's a very ADIC function
that takes none or many, so as many as you want strings and then it determines whether they're valid
or not, whether they're there or not.
So in this case, again, I create a pointer to a request by calling HDB test new request and just passing
in post for the type and anything I want for the URL and no body whatsoever.
Then I instantiate a new form object by passing in our post form, which is of type zero values.
And then I say for this form, require the fields A, B and C.
If the form is valid, then it should fail the test because form shows valid when required fields are
missing.
Then I just create my own type of U.S. values.
So I create a variable called posted data that has is of type orld up values and I add three three values
in there, A, B and C and then I call the request again.
This time I don't have to assign it because I already have the variable error which I declared up here.
So I'm just overrating it with a new value, a new request.
And then I set the post form field on that request object to posted data which we defined right here.
Then I run the test again.
So we create a new form, archtop post form.
This time our post form has these values.
So we actually have the values in there.
And I say form required A, B, C and I should return valid if the form is is valid and not valid or
false.
If the form is not valid.
If we get a false from here, then we have an error.
So let's run these tests and see what happens.
And I'll tell you right now, when we wrote the forms package I introduced to deliberate errors that
are not immediately obvious until you write the necessary test to find them.
And that's part of what we're doing right now.
So I'm in the forms package.
Let's right.
Go test Dashty and see what we get.
And I got a pass for everything that's great, so no surprise here, I didn't fix one of the deliberate
errors for you.
I'm hoping that you'll find it.
So you need to go through and look at the functions we've not tested that would be has min length and
is email and write tests for them.
And when you write the test, bear in mind you're going to test two cases, one, when you should get
a valid response from the validator and one when you should get an invalid response from the validator.
Good luck.
So take some time, work through it on your own, bang your head against the wall for a little while.
And once that callus on your forehead is nice and thick, if you haven't figured it out, go to the
next lecture where I give you the solutions.