-
Notifications
You must be signed in to change notification settings - Fork 1
test(core): make the README usage example compile-checked #9
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
core/src/test/kotlin/com/ams/fat12ex/core/ReadmeExampleTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.ams.fat12ex.core | ||
|
|
||
| import com.ams.fat12ex.core.testutil.InMemoryBlockDevice | ||
| import org.junit.jupiter.api.Assertions.assertArrayEquals | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertInstanceOf | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| /** | ||
| * Executes the README "Usage example" round-trip against the build so the | ||
| * documented snippet cannot silently bit-rot as the public API evolves. | ||
| * | ||
| * Mirrors the README flow: format -> open -> writeFile("/HELLO.TXT") -> | ||
| * list("/") -> readFile("/HELLO.TXT"), asserting the round-tripped bytes equal | ||
| * "hello fat12". Uses the in-memory device fixture at the README's 1.44 MB | ||
| * floppy geometry (2880 * 512-byte sectors) in place of the snippet's | ||
| * hand-written MemoryBlockDevice. | ||
| */ | ||
| class ReadmeExampleTest { | ||
|
|
||
| @Test | ||
| fun readmeUsageExample_roundTrips() { | ||
| // A minimal in-memory BlockDevice at 1.44 MB floppy geometry. | ||
| val device = InMemoryBlockDevice(blockSize = 512, blocks = 2880L).also { it.init() } | ||
| val volume = Fat12Volume(device) | ||
|
|
||
| // Format a fresh FAT12 volume, then open it. | ||
| volume.format("DEMO") | ||
| assertInstanceOf(Fat12Result.Ok::class.java, volume.open()) | ||
|
|
||
| // Atomic, verify-after-write streaming write of a file into the root directory. | ||
| val payload = "hello fat12".toByteArray() | ||
| val write = volume.writeFile("/HELLO.TXT", sequenceOf(payload)) | ||
| assertInstanceOf(Fat12Result.Ok::class.java, write) | ||
|
|
||
| // List the root directory — HELLO.TXT must be present at the written size. | ||
| val listing = volume.list("/") | ||
| assertInstanceOf(Fat12Result.Ok::class.java, listing) | ||
| val entry = (listing as Fat12Result.Ok).value | ||
| .firstOrNull { it.name.equals("HELLO.TXT", ignoreCase = true) } | ||
| assertTrue(entry != null, "HELLO.TXT must appear in the root listing") | ||
| assertEquals(payload.size.toLong(), entry!!.size) | ||
|
|
||
| // Read the file back — the raw bytes must round-trip exactly (the README's | ||
| // String(read.value) decode is the human-facing view of that same payload). | ||
| val read = volume.readFile("/HELLO.TXT") | ||
| assertInstanceOf(Fat12Result.Ok::class.java, read) | ||
| val readBytes = (read as Fat12Result.Ok).value | ||
| assertArrayEquals(payload, readBytes) | ||
| assertEquals("hello fat12", String(readBytes)) | ||
|
|
||
| volume.close() | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.