DEV Community

Jesse Phillips
Jesse Phillips

Posted on

Hobby Project: Generator for Testing

Here I ended up creating an article generator for unittesting. I did start the work which prompted this need.

Building an article generator starts with how to build an single article. I have chosen to start with a json string which has some data placed in it. But having the exact same data when testing isn't advisable, though controlling what is different is also important.

In order to build out a list of articles, this single function can be utilized with the range generate template.

Enter fullscreen mode Exit fullscreen mode

This uses generate instead repeat because it will make a new object instead of repeating the same reference. But it does not change the article content.

I decided that I wanted to make modifications over the range. sequence will modify each article to create a descending sequence of articles. This is to simulate what the api would return.

Enter fullscreen mode Exit fullscreen mode

What I find interesting is that I can place this in a helping function and I would call that function 'generateFakeArticleMeSequence' true I might not utilize the term 'sequence' but the rest is accurate.

It appears I forgot to fill in the doc comment.

I've never really figured out a good organization for imports. I that standard library, third party, local organization makes sense.

While I didn't implement this functionality, I do have an initial test to show the concept.

auto devreq = generate!fakeArticleMe
    .map!(x => x.deserializeJson!(ArticleMe))
assert(devreq.filter!(x => x.isNewerArticle(ps)).empty);
Enter fullscreen mode Exit fullscreen mode

I am looking to design the short circuit to use general range methods. And as I write this I realize that filter is the wrong choice as it will walk all articles. Instead I should have used until.

assert(devreq.until!(x => !x.isNewerArticle(ps)).empty);
Enter fullscreen mode Exit fullscreen mode

The list would come out the same but this will rely on order of items to stop.

I think I wrote tests first, but I think I was adjusting the test as I developed.

Top comments (1)

jessekphillips profile image
Jesse Phillips

The idea is that once I have the logic to grab newer articles, I will have all the pieces and only need to abstract the dev api into a range.