Advertisement
bakhridinova

Untitled

Sep 8th, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package com.epam.rd.autocode.bstprettyprint;
  2.  
  3. import org.junit.jupiter.params.ParameterizedTest;
  4. import org.junit.jupiter.params.provider.Arguments;
  5. import org.junit.jupiter.params.provider.MethodSource;
  6.  
  7. import java.io.IOException;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11. import java.util.stream.Collectors;
  12. import java.util.stream.Stream;
  13.  
  14. import static org.junit.jupiter.api.Assertions.assertEquals;
  15. import static org.junit.jupiter.params.provider.Arguments.arguments;
  16.  
  17. public class PrintableTreeTest {
  18.  
  19.     @ParameterizedTest
  20.     @MethodSource("testCases")
  21.     public void test(String testCaseName, int[] elements, String prettyPrint) {
  22.         final PrintableTree tree = PrintableTree.getInstance();
  23.         for (int element : elements) {
  24.             tree.add(element);
  25.         }
  26.  
  27.         assertEquals(
  28.                 prettyPrint,
  29.                 tree.prettyPrint(),
  30.                 () -> testCaseName + " test case has failed.");
  31.  
  32.     }
  33.  
  34.     static Stream<Arguments> testCases() throws IOException {
  35.         final Path testCaseRoot = Paths.get("src", "test", "resources", "test-cases");
  36.  
  37.         return Files.walk(testCaseRoot, 1)
  38.                 .filter(Files::isDirectory)
  39.                 .filter(path -> !testCaseRoot.equals(path))
  40.                 .map(testCase -> arguments(
  41.                         testCase.getFileName().toString(),
  42.                         readElements(testCase),
  43.                         readPrettyPrint(testCase)
  44.                 ));
  45.  
  46.     }
  47.  
  48.     private static int[] readElements(final Path testCase) {
  49.         try (Stream<String> lines = Files.lines(testCase.resolve("elements.txt"))) {
  50.             return lines.mapToInt(Integer::valueOf).toArray();
  51.         } catch (IOException e) {
  52.             throw new IllegalStateException(e);
  53.         }
  54.     }
  55.  
  56.     private static String readPrettyPrint(final Path testCase) {
  57.         try (Stream<String> lines = Files.lines(testCase.resolve("prettyprint.txt"))) {
  58.             return lines.collect(Collectors.joining("\n")) + "\n";
  59.         } catch (IOException e) {
  60.             throw new IllegalStateException(e);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement