Skip to content

Commit

Permalink
Create unit tests for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
AMereBagatelle committed May 24, 2024
1 parent 948b752 commit ce7ed95
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 6 deletions.
5 changes: 3 additions & 2 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ dependencies {
// Architectury API. This is optional, and you can comment it out if you don't need it.
//modImplementation "dev.architectury:architectury:$rootProject.architectury_api_version"

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import com.mojang.serialization.JsonOps;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class AnimatableTextureTest {
@ParameterizedTest
@ValueSource(strings = {
"""
{
"texture": "test:resourcelocation"
}
""",
"""
{
"texture": "test:resourcelocation",
"gridColumns": 1
}
""",
"""
{
"texture": "test:resourcelocation",
"gridRows": 1
}
""",
"""
{
"texture": "test:resourcelocation",
"duration": 1
}
""",
"""
{
"texture": "test:resourcelocation",
"interpolate": true
}
""",
"""
{
"texture": "test:resourcelocation",
"frameDuration": {
"1": 10,
"2": 20
}
}
"""
})
public void testCorrectParse(String json) {
var jsonOb = JsonTestHelper.readJson(json);
assertDoesNotThrow(() -> AnimatableTexture.CODEC.decode(JsonOps.INSTANCE, jsonOb).getOrThrow().getFirst());
}

@ParameterizedTest
@ValueSource(strings = {
"""
{
}
""",
})
public void testIncorrectParse(String json) {
var jsonOb = JsonTestHelper.readJson(json);
assertThrows(IllegalStateException.class, () -> AnimatableTexture.CODEC.decode(JsonOps.INSTANCE, jsonOb).getOrThrow().getFirst());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import com.mojang.serialization.JsonOps;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class BlendTest {
@ParameterizedTest
@ValueSource(strings = {
"""
{
}
""",
"""
{
"blend": "add"
}
""",
"""
{
"blend": "subtract"
}
""",
"""
{
"blend": "multiply"
}
""",
"""
{
"blend": "screen"
}
""",
"""
{
"blend": "replace"
}
""",
"""
{
"blend": "normal"
}
""",
"""
{
"blend": "burn"
}
""",
"""
{
"blend": "dodge"
}
""",
"""
{
"blend": "disable"
}
""",
"""
{
"blend": "decorations"
}
""",
"""
{
"blend": "custom",
"blender": {
}
}
""",
"""
{
"blend": "custom",
"blender": {
"separateFunction": true
}
}
""",
"""
{
"blend": "custom",
"blender": {
"sourceFactor": 1
}
}
""",
"""
{
"blend": "custom",
"blender": {
"equation": 32775
}
}
""",
"""
{
"blend": "custom",
"blender": {
"alphaEnabled": false
}
}
"""
})
public void testCorrectParse(String json) {
var jsonOb = JsonTestHelper.readJson(json);
assertDoesNotThrow(() -> Blend.CODEC.decode(JsonOps.INSTANCE, jsonOb).getOrThrow().getFirst());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import com.mojang.serialization.JsonOps;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class BlenderTest {
@ParameterizedTest
@ValueSource(strings = {
"""
{
}
""",
"""
{
"separateFunction": true
}
""",
"""
{
"sourceFactor": 1
}
""",
"""
{
"equation": 32775
}
""",
"""
{
"alphaEnabled": false
}
"""
})
public void testCorrectParse(String json) {
var jsonOb = JsonTestHelper.readJson(json);
assertDoesNotThrow(() -> Blender.CODEC.decode(JsonOps.INSTANCE, jsonOb).getOrThrow().getFirst());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import com.mojang.serialization.JsonOps;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class FadeTest {
@ParameterizedTest
@ValueSource(strings = {
"""
{
}
""",
"""
{
"alwaysOn": false
}
""",
"""
{
"duration": 24000
}
""",
"""
{
"keyFrames": {
"1000": 10,
"3000": 20
}
}"""
})
public void testCorrectParse(String json) {
var jsonOb = JsonTestHelper.readJson(json);
assertDoesNotThrow(() -> Fade.CODEC.decode(JsonOps.INSTANCE, jsonOb).getOrThrow().getFirst());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

public class JsonTestHelper {
public static final Gson GSON = new GsonBuilder()
.setLenient()
.create();

public static JsonObject readJson(String json) {
return GSON.fromJson(json, JsonObject.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import com.mojang.serialization.JsonOps;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class MetadataTest {
@ParameterizedTest
@ValueSource(strings = {
"""
{
"schemaVersion": 1,
"type": "monocolor"
}
""",
"""
{
"schemaVersion": 1,
"type": "square-textured"
}
""",
"""
{
"schemaVersion": 1,
"type": "multi-textured"
}
""",
"""
{
"schemaVersion": 1,
"type": "overworld"
}
""",
"""
{
"schemaVersion": 1,
"type": "end"
}
"""
})
public void testCorrectParse(String json) {
var jsonOb = JsonTestHelper.readJson(json);
assertDoesNotThrow(() -> Metadata.CODEC.decode(JsonOps.INSTANCE, jsonOb).getOrThrow().getFirst());
}

@ParameterizedTest
@ValueSource(strings = {
"""
{
}
""",
"""
{
"schemaVersion": 1
}
""",
"""
{
"type": "overworld"
}
"""
})
public void testIncorrectParse(String json) {
var jsonOb = JsonTestHelper.readJson(json);
assertThrows(IllegalStateException.class, () -> Metadata.CODEC.decode(JsonOps.INSTANCE, jsonOb).getOrThrow().getFirst());
}
}
Loading

0 comments on commit ce7ed95

Please sign in to comment.