Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): cowtowncoder#87 Add tests to jug #113

Merged
merged 4 commits into from
Jun 8, 2024

Conversation

SquireOfSoftware
Copy link
Contributor

What did I do?

As per this comment: #87 (comment), I wrote some tests for Jug.

I took the liberty to "destaticise" the main static methods so that it was easier to target inside of the tests. Take note that some of the styling changes were automatically applied by Intellij so I apologise in advance for that, if it needs to be rolled back let me know.

The general gist of the work is, run through the run method, swap out the System.out and System.err outputStreams using: https://stackoverflow.com/questions/1119385/junit-test-for-system-out-println

Extra notes

  • I leveraged Parameterized testing to allow for a broader spectrum of coverage without too much fixture setup
  • The tests are laid out in the division of options and type, I have split the named types from the noarg types

Let me know if the coverage is not good enough and I can add more usecases to the tests, these tests are just covering the "happy path". Also I did not test every permutation of options.

- This should make it a bit easier to test
- Placed this in a parameterized test suite to allow for a wide range of assertions
Comment on lines 103 to +108
public static void main(String[] args)
{
new Jug().run(args);
}

public void run(String[] args) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively the core of this change on this file, I destaticised most of the methods, the only one that is still static is main.

Take note that I have tried my best to not touch too much of the main file, just shuffling the methods around so it is easier to test.

Also take note that Intellij appears to have applied its own automated styling guidelines, so I would like to apologise in advance for the changes.

Comment on lines +147 to +148
new UseCase("n", "-n", "world", "-s", "url"),
new UseCase("n", "-n", "world", "-s", "dns")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is some of the parameterized tests that I have applied, it looks kinda like this:

image

}

@Test
public void run_givenNoOptions_shouldProduceUUID() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each subtest is testing an option on that UUID type.

Comment on lines +53 to +58
// then - if it is a UUID then we should be able to parse it back out
String actualUuid = outContent.toString();
assertEquals('\n', actualUuid.charAt(actualUuid.length() - 1));

assertEquals(UUID.class,
UUID.fromString(actualUuid.substring(0, actualUuid.length() - 1)).getClass());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't super sure how I was going to validate a UUID version, without digging into the actual spec expectation...like what does the first 6 bits mean etc.

But I figured, I would just hook myself into the output stream and as long as I can feed the UUID back into the UUID class then it means its a UUID of some kind...but I am unsure if that makes sense because you actually want to know if the UUID spec is met right? Rather than just checking to see if its UUID-parseable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to thoroughly test validity at this level: these aspects are verified by other, existing unit tests. So "looks about right" is pretty good at CLI level I think. Use of JDK UUID constructor makes sense to me.

Copy link
Owner

@cowtowncoder cowtowncoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cowtowncoder cowtowncoder merged commit 4bd4b29 into cowtowncoder:master Jun 8, 2024
4 checks passed
@SquireOfSoftware SquireOfSoftware deleted the add_tests_to_jug branch June 8, 2024 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants