c# - Removing duplication in Test Driven Development -
i developing small parser class tdd style. here tests:
... [testmethod] public void can_parse_a_float() { initializescanner("float a"); token expectedtoken = new token("float", "a"); assert.areequal(expectedtoken, scanner.nexttoken()); } [testmethod] public void can_parse_an_int() { initializescanner("int a"); token expectedtoken = new token("int", "a"); assert.areequal(expectedtoken, scanner.nexttoken()); } [testmethod] public void can_parse_multiple_tokens() { initializescanner("int float b"); token firstexpectedtoken = new token("int", "a"); token secondexpectedtoken = new token("float", "b"); assert.areequal(firstexpectedtoken, scanner.nexttoken()); assert.areequal(secondexpectedtoken, scanner.nexttoken()); }
what bugging me last test exercising same lines of code both can_parse_a_float()
, can_parse_an_int()
. on 1 hand, exercising both methods aren't: source code string, can several tokens. on other hand, can_parse_a_float()
, can_parse_an_int()
fail, can_parse_multiple_tokens()
fail too.
i feel there 4 goals @ stake here:
- i want tests show
parser
parses ints - i want tests show
parser
parses floats - i want tests show
parser
can parse several ints/floats in row - i want tests serve documentation mechanism (!)
i offering cookies shares opinions on how better approach scenario. thanks!
so, question -- did simplest thing possible when wrote code passed first 2 tests or did work ahead, knowing third requirement (test)? if wrote simplest code possible , passed third test without writing new code, test wasn't necessary. if had modify code, third test needed , served define code. yes, 3 exercising same lines of code, lines (should be) different you've written third test. see no problem this.
Comments
Post a Comment