Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my opinion, your concern shows that there is a design problem. You have two separate responsibilities:</p> <ul> <li>Transforming a prefix of the input string to a token</li> <li>Repeating the above operation keeping track of "where I am" in the input string</li> </ul> <p>Your current design assigns these two to a single Parser object. This is why you can't test the two responsibilities separately. A better design would be to define a Tokenizer for the first responsibility and a Parser for the second. Sorry I'm too rusty on C# so I will write Java.</p> <p>One design problem in the tokenizer is that it should return two values: the token, and how much of the string was consumed. In Java Strings are immutable, so I can't change the input argument. I would use a StringReader instead, which is a stream of characters that you can consume. My first test could be:</p> <pre><code>@Test public void tokenizes_an_integer() { Tokenizer tokenizer = new Tokenizer(); StringReader input = new StringReader("int a rest of the input"); Token token = tokenizer.tokenize(input); assertEquals(new Token("a", "int"), token); assertEquals(" rest of the input", input.toString()); } </code></pre> <p>When this passes, I could write a test for the second responsibility:</p> <pre><code>@Test public void calls_tokenizer_repeatedly_consuming_the_input() { StringReader input = new StringReader("int a int b"); Parser parser = new Parser(input, new Tokenizer()); assertEquals(new Token("a", "int"), parser.nextToken()); assertEquals(new Token("b", "int"), parser.nextToken()); assertEquals(null, parser.nextToken()); } </code></pre> <p>This is better, but still is not perfect from the point of view of test maintainability. If you decide to change the syntax of the "int" token, both tests will break. Ideally you would like only the first one to break. One solution would be to use a fake tokenizer in the second test, that does not depend on the real one. </p> <p>This is something I'm still trying to master. One useful resource is the "Growing Object-Oriented Software" book, which is very good on test independence and expressivity. </p> <p>Where's the cookies? :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload