Skip to content

Commit

Permalink
more polished
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 8, 2025
1 parent 2c575d1 commit 3f6dfe9
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,12 @@
*/
@ExtendWith(MktmpResolver.class)
final class FilesTest {
/**
* Files instance.
*/
private static final Files INSTANCE = Files.INSTANCE;

@Test
void throwsOnReadingWithoutOpening(@Mktmp final Path dir) {
Assertions.assertThrows(
ExFailure.class,
() -> FilesTest.INSTANCE.read(
FilesTest.tempFile(dir), 10
() -> Files.INSTANCE.read(
dir.resolve("c.txt").toFile().getAbsolutePath(), 10
),
"File should not allow to read before opening"
);
Expand All @@ -68,8 +63,9 @@ void throwsOnReadingWithoutOpening(@Mktmp final Path dir) {
void throwsOnWritingWithoutOpening(@Mktmp final Path dir) {
Assertions.assertThrows(
ExFailure.class,
() -> FilesTest.INSTANCE.write(
FilesTest.tempFile(dir), new byte[]{0x01}
() -> Files.INSTANCE.write(
dir.resolve("b.txt").toFile().getAbsolutePath(),
new byte[]{0x01}
),
"File should not allow to write before opening"
);
Expand All @@ -79,55 +75,43 @@ void throwsOnWritingWithoutOpening(@Mktmp final Path dir) {
void throwsOnClosingWithoutOpening(@Mktmp final Path dir) {
Assertions.assertThrows(
ExFailure.class,
() -> FilesTest.INSTANCE.close(
FilesTest.tempFile(dir)
() -> Files.INSTANCE.close(
dir.resolve("a.txt").toFile().getAbsolutePath()
),
"File should not allow to close before opening"
);
}

@Test
void readsFromFile(@Mktmp final Path dir) throws IOException {
final String file = FilesTest.tempFile(dir);
final String file = dir.resolve("bar.txt").toFile().getAbsolutePath();
try (BufferedWriter writer =
java.nio.file.Files.newBufferedWriter(Paths.get(file))) {
writer.write("Hello, world");
}
FilesTest.INSTANCE.open(file);
Files.INSTANCE.open(file);
MatcherAssert.assertThat(
"The string should have been read from file",
FilesTest.INSTANCE.read(file, 12),
Files.INSTANCE.read(file, 12),
Matchers.equalTo("Hello, world".getBytes(StandardCharsets.UTF_8))
);
FilesTest.INSTANCE.close(file);
Files.INSTANCE.close(file);
}

@Test
void writesToFile(@Mktmp final Path dir) throws IOException {
final String file = FilesTest.tempFile(dir);
final String file = dir.resolve("foo.txt").toFile().getAbsolutePath();
try (BufferedWriter writer =
java.nio.file.Files.newBufferedWriter(Paths.get(file))) {
writer.write("Hello, world");
}
FilesTest.INSTANCE.open(file);
FilesTest.INSTANCE.write(file, "!".getBytes(StandardCharsets.UTF_8));
Files.INSTANCE.open(file);
Files.INSTANCE.write(file, "!".getBytes(StandardCharsets.UTF_8));
MatcherAssert.assertThat(
"The string should have been read from file",
FilesTest.INSTANCE.read(file, 13),
Files.INSTANCE.read(file, 13),
Matchers.equalTo("Hello, world!".getBytes(StandardCharsets.UTF_8))
);
FilesTest.INSTANCE.close(file);
}

/**
* Creates temporary file in directory.
* @param dir Directory
* @return Absolute path to temp file
* @throws IOException If fails to create temp file
*/
private static String tempFile(final Path dir) throws IOException {
final Path file = java.nio.file.Files.createTempFile(dir, null, null);
file.toFile().deleteOnExit();
return file.toAbsolutePath().toString();
Files.INSTANCE.close(file);
}
}

0 comments on commit 3f6dfe9

Please sign in to comment.