1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-24 22:23:21 +00:00
CC-Tweaked/src/test/java/dan200/computercraft/ingame/mod/CCTestCommand.java
Jonathan Coates 331031be45 Run integration tests in-game
Name a more iconic duo than @SquidDev and over-engineered test
frameworks.

This uses Minecraft's test core[1] plus a home-grown framework to run
tests against computers in-world.

The general idea is:
 - Build a structure in game.
 - Save the structure to a file. This will be spawned in every time the
   test is run.
 - Write some code which asserts the structure behaves in a particular
   way. This is done in Kotlin (shock, horror), as coroutines give us a
   nice way to run asynchronous code while still running on the main
   thread.

As with all my testing efforts, I still haven't actually written any
tests!  It'd be good to go through some of the historic ones and write
some tests though. Turtle block placing and computer redstone
interactions are probably a good place to start.

[1]: https://www.youtube.com/watch?v=vXaWOJTCYNg
2021-01-09 19:50:27 +00:00

57 lines
1.7 KiB
Java

package dan200.computercraft.ingame.mod;
import com.mojang.brigadier.CommandDispatcher;
import dan200.computercraft.utils.Copier;
import net.minecraft.command.CommandSource;
import net.minecraft.server.MinecraftServer;
import java.io.IOException;
import java.io.UncheckedIOException;
import static dan200.computercraft.shared.command.builder.HelpingArgumentBuilder.choice;
import static net.minecraft.command.Commands.literal;
/**
* Helper commands for importing/exporting the computer directory.
*/
class CCTestCommand
{
public static void register( CommandDispatcher<CommandSource> dispatcher )
{
dispatcher.register( choice( "cctest" )
.then( literal( "import" ).executes( context -> {
importFiles( context.getSource().getServer() );
return 0;
} ) )
.then( literal( "export" ).executes( context -> {
exportFiles( context.getSource().getServer() );
return 0;
} ) )
);
}
public static void importFiles( MinecraftServer server )
{
try
{
Copier.replicate( TestMod.sourceDir.resolve( "computers" ), server.getServerDirectory().toPath().resolve( "world/computercraft" ) );
}
catch( IOException e )
{
throw new UncheckedIOException( e );
}
}
public static void exportFiles( MinecraftServer server )
{
try
{
Copier.replicate( server.getServerDirectory().toPath().resolve( "world/computercraft" ), TestMod.sourceDir.resolve( "computers" ) );
}
catch( IOException e )
{
throw new UncheckedIOException( e );
}
}
}