Skip to main content
added 50 characters in body
Source Link
Christophe
  • 82.2k
  • 11
  • 136
  • 202

Your first test is a good start. Maybe think about error checking: is there a return code from add, to tell that it did work or not ? Or is it supposed to throw an exception if something goes wrong ?

Preferred approach

Knowing that you'll need a method has_component() for the Entity class, just add it to the class definition, with a stub implementation that always return false (and a big comment // TO DO !!).

Your next test would then be:

assert(! e.has_component(c) ); 
e.add(c);
assert(e.has_component(c)); // or return FAIL to your test framework.  

The principleprinciple is that your test will fail until both functions are correct.

What are the alternatives

  • use a simpler member, such as number_of_components() to check that you're on the right track (i.e. number increases when adding a new component; number remains same when adding same component twice).
  • let the test code inspect the internals of your Entity. I don't like this approach, because it breaks the rules of encapsulation.
  • make a a test double that provides emulated/simulated answer for has_component(). Would it be a very complex feature, this would be the way to go. But for simple features like here, this might be overkill.
  • if you'd have a dispatching function that forwards calls from Entity to Components you could use it to check if c reacts.

Your first test is a good start. Maybe think about error checking: is there a return code from add, to tell that it did work or not ? Or is it supposed to throw an exception if something goes wrong ?

Preferred approach

Knowing that you'll need a method has_component() for the Entity class, just add it to the class definition, with a stub implementation that always return false (and a big comment // TO DO !!).

Your next test would then be:

assert(! e.has_component(c) ); 
e.add(c);
assert(e.has_component(c)); // or return FAIL to your test framework.  

The principle is that your test will fail until both functions are correct.

What are the alternatives

  • use a simpler member, such as number_of_components() to check that you're on the right track (i.e. number increases when adding a new component; number remains same when adding same component twice).
  • let the test code inspect the internals of your Entity. I don't like this approach, because it breaks the rules of encapsulation.
  • make a a test double that provides emulated/simulated answer for has_component(). Would it be a very complex feature, this would be the way to go. But for simple features like here, this might be overkill.
  • if you'd have a dispatching function that forwards calls from Entity to Components you could use it to check if c reacts.

Your first test is a good start. Maybe think about error checking: is there a return code from add, to tell that it did work or not ? Or is it supposed to throw an exception if something goes wrong ?

Preferred approach

Knowing that you'll need a method has_component() for the Entity class, just add it to the class definition, with a stub implementation that always return false (and a big comment // TO DO !!).

Your next test would then be:

assert(! e.has_component(c) ); 
e.add(c);
assert(e.has_component(c)); // or return FAIL to your test framework.  

The principle is that your test will fail until both functions are correct.

What are the alternatives

  • use a simpler member, such as number_of_components() to check that you're on the right track (i.e. number increases when adding a new component; number remains same when adding same component twice).
  • let the test code inspect the internals of your Entity. I don't like this approach, because it breaks the rules of encapsulation.
  • make a a test double that provides emulated/simulated answer for has_component(). Would it be a very complex feature, this would be the way to go. But for simple features like here, this might be overkill.
  • if you'd have a dispatching function that forwards calls from Entity to Components you could use it to check if c reacts.
added 158 characters in body
Source Link
Christophe
  • 82.2k
  • 11
  • 136
  • 202

Your first test is a good start. Maybe think about error checking: is there a return code from add, to tell that it did work or not ? Or is it supposed to throw an exception if something goes wrong ?

Preferred approach

Knowing that you'll need a method has_component() for the Entity class, you could already just add it to the class definition, with a stub implementation that always return false (and a big comment // TO DO !!). Your

Your next test would then be:

assert(! e.has_component(c) ); 
e.add(c);
assert(e.has_component(c)); // or return FAIL to your test framework.  

The thingprinciple is that you don't have a has_component() implementation now. Just make a first stub implementation that always return false. Youryour test will fail until both functions are correct.

What are the alternatives

  • use a simpler propertymember, such as number_of_components() to check that you're on the right track (i.e. number increases when adding a new component; number remains same when adding same componenttcomponent twice).
  • let the test code inspect the internals of your Entity. I don't like this approach, because it breaks the rules of encapsulation.
  • don't write the has_component() yet but make a a test double that provides emulated answers/simulated answer for has_component(). Would it be a very complex feature, this would be the way to go. But for simple features like here, this might be overkill.
  • if you'd have a dispatching function that forwards calls from Entity to Components you could use it to check if c reacts.

Preferred approach

Knowing that you'll need a method has_component() for the Entity class, you could already add it to the class definition. Your test would then be:

assert(! e.has_component(c) ); 
e.add(c);
assert(e.has_component(c)); // or return FAIL to your test framework.  

The thing is that you don't have a has_component() implementation now. Just make a first stub implementation that always return false. Your test will fail until both functions are correct.

What are the alternatives

  • use a simpler property, such as number_of_components() to check that you're on the right track (i.e. number increases when adding a new component; number remains same when adding same componentt twice).
  • let the test code inspect the internals of your Entity. I don't like this approach, because it breaks the rules of encapsulation.
  • don't write the has_component() yet but make a a test double that provides emulated answers. Would it be a very complex feature, this would be the way to go. But for simple features like here, this might be overkill.
  • if you'd have a dispatching function that forwards calls from Entity to Components you could use it to check if reacts.

Your first test is a good start. Maybe think about error checking: is there a return code from add, to tell that it did work or not ? Or is it supposed to throw an exception if something goes wrong ?

Preferred approach

Knowing that you'll need a method has_component() for the Entity class, just add it to the class definition, with a stub implementation that always return false (and a big comment // TO DO !!).

Your next test would then be:

assert(! e.has_component(c) ); 
e.add(c);
assert(e.has_component(c)); // or return FAIL to your test framework.  

The principle is that your test will fail until both functions are correct.

What are the alternatives

  • use a simpler member, such as number_of_components() to check that you're on the right track (i.e. number increases when adding a new component; number remains same when adding same component twice).
  • let the test code inspect the internals of your Entity. I don't like this approach, because it breaks the rules of encapsulation.
  • make a a test double that provides emulated/simulated answer for has_component(). Would it be a very complex feature, this would be the way to go. But for simple features like here, this might be overkill.
  • if you'd have a dispatching function that forwards calls from Entity to Components you could use it to check if c reacts.
Source Link
Christophe
  • 82.2k
  • 11
  • 136
  • 202

Preferred approach

Knowing that you'll need a method has_component() for the Entity class, you could already add it to the class definition. Your test would then be:

assert(! e.has_component(c) ); 
e.add(c);
assert(e.has_component(c)); // or return FAIL to your test framework.  

The thing is that you don't have a has_component() implementation now. Just make a first stub implementation that always return false. Your test will fail until both functions are correct.

What are the alternatives

  • use a simpler property, such as number_of_components() to check that you're on the right track (i.e. number increases when adding a new component; number remains same when adding same componentt twice).
  • let the test code inspect the internals of your Entity. I don't like this approach, because it breaks the rules of encapsulation.
  • don't write the has_component() yet but make a a test double that provides emulated answers. Would it be a very complex feature, this would be the way to go. But for simple features like here, this might be overkill.
  • if you'd have a dispatching function that forwards calls from Entity to Components you could use it to check if reacts.