A New Story

Story: Enhance the Descriptor

Background: In its penultimate step, the open function sends a message to a WavDescriptor object, whose job is to append a formatted data record to a descriptor file. The WAV publisher user interface uses the descriptor file’s contents to display available WAVs.

The descriptor accepts the WAV filename, total seconds (before snipping), samples per second, and number of channels.

Story: As a content reseller, I want the descriptors for WAV files to also include the new snippet file length. Alter the descriptor object to accept the length.

Someone else will change the implementation of WavDescriptor to include the snippet file length in each record (let’s hope they’re using TDD!). Our job will involve only making changes to WavReader.

Our effort to complete this task involves two things. First, calculate or obtain the file size. Second, prove that we pass this value across to the WavDescriptor.

When adding new functionality, your best bet is to first seek to test-drive it into a new method or even a new class. This ensures that code coverage does not decrease as your codebase grows. It also better helps you adhere to the SRP and reap all the benefits of small functions and classes.

We need a function that returns a file’s size given its name. For our test-driven new functionality, do this:

wav/12/FileUtilTest.cpp
 
// slow tests that must interact with the filesystem
 
TEST_GROUP_BASE(FileUtil_Size, FileUtilTests) {
 
};
 
 
TEST(FileUtil_Size, AnswersFileContentSize) {
 
string​ content(​"12345"​);
 
createTempFile(content);
 
 
size_t expectedSize { content.length() + ​sizeof​(​''​) };
 
LONGS_EQUAL(expectedSize, (​unsigned​)util.size(TempFileName));
 
}
wav/12/FileUtil.h
 
class​ FileUtil {
 
public​:
 
std::streamsize size(​const​ std::​string​& name) {
 
std::ifstream stream{name, std::ios::in | std::ios::binary};
 
stream.seekg(0, std::ios::end);
 
return​ stream.tellg();
 
}
 
};

What stinks about this test? It’s not fast (see Fast Tests, Slow Tests, Filters, and Suites).

You might end up with a small number of slow tests that you arrive at by test-driving. Perhaps that’s OK, but strive to make their number very small—as close to zero as possible. More importantly, designate such tests as slow and ensure that you have a way to run fast, slow, or fast and slow suites.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87