Jeremy S Nero
Jeremy S Nero

Reputation: 45

Software design concept, is there a way to not repeat this or pass the DRY principle?

    @Test
  void upperCaseSequenceTest() {
    assertAll(
      () -> assertEquals("A", Processor.upperCaseConverterBlock("A")),
      () -> assertEquals("A", Processor.upperCaseConverterBlock("a")),
      () -> assertEquals("1", Processor.upperCaseConverterBlock("1")),
      () -> assertEquals("~", Processor.upperCaseConverterBlock("~")),
      () -> assertEquals("", Processor.upperCaseConverterBlock(""))
    );
  }

The above and below are doing the same things for two different functions and data.

@Test
  void lowerCaseSequenceTest() {
    assertAll(
      () -> assertEquals("a", Processor.lowerCaseConverterBlock("a")),
      () -> assertEquals("a", Processor.lowerCaseConverterBlock("A")),
      () -> assertEquals("1", Processor.lowerCaseConverterBlock("1")),
      () -> assertEquals("~", Processor.lowerCaseConverterBlock("~")),
      () -> assertEquals("", Processor.lowerCaseConverterBlock(""))
    );
  }

Question is, they differ in how it manipulates the data, is there a way to not repeat the set of input?

Upvotes: 0

Views: 50

Answers (1)

Vikash Madhow
Vikash Madhow

Reputation: 1344

Not without introducing more complexity in your test cases. Although it is a good idea to follow the DRY principle, this should be balanced with the cost of introducing additional abstractions that will allow the repeated code to be reused. In tests, code is often repeated because you want to use the simplest possible code to ensure that your tests themselves are not buggy (if your tests are too complex, you would need to test them!).

Upvotes: 2

Related Questions