Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Nov 17, 2023
1 parent f9b0dda commit b4107e4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
11 changes: 11 additions & 0 deletions buildSrc/src/main/kotlin/config-kotlin.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ dependencies {
compileOnly(kotlin("stdlib-jdk8"))
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
useKotlinTest(embeddedKotlinVersion)
dependencies {
implementation("org.junit.jupiter:junit-jupiter-engine:5.10.1")
}
}
}
}

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2023 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/

package io.papermc.paperweight.util

import java.nio.file.Path
import kotlin.io.path.*
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.junit.jupiter.api.io.TempDir

class FileUtilsTest {
@Test
fun testContentEquals(@TempDir tempDir: Path) {
val someBytes = classBytes<FileUtilsTest>()

// write same data to two files
val one = tempDir.resolve("${FileUtilsTest::class.simpleName}.class")
one.writeBytes(someBytes)
val two = tempDir.resolve("${FileUtilsTest::class.simpleName}.class_1")
two.writeBytes(someBytes)

// assert the files have matching content
assertTrue(one.contentEquals(two), "These files have the same content")
assertTrue(two.contentEquals(one), "These files have the same content")

val someDifferentBytes = classBytes<Map<*, *>>()

// write some different data to a third file
val three = tempDir.resolve("Map.class")
three.writeBytes(someDifferentBytes)

// assert it's content is different from the previously written files
assertFalse(one.contentEquals(three), "These files are different")
}

private inline fun <reified C> classBytes(): ByteArray {
val resourceName = C::class.java.name.replace(".", "/") + ".class"
return this::class.java.classLoader.getResource(resourceName)
?.openStream()?.readAllBytes() ?: error("Couldn't get resource $resourceName")
}
}

0 comments on commit b4107e4

Please sign in to comment.