package org.shedlang.compiler.shidtopatio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.shedlang.compiler.patio.operands.PatioLocalRef; import org.shedlang.compiler.shid.instructions.Shid; import org.shedlang.compiler.testing.snapshots.SnapshotResolver; import org.shedlang.compiler.testing.snapshots.Snapshotter; import java.io.IOException; import java.util.List; import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize; @ExtendWith({SnapshotResolver.class}) public class ShidToPatioIfTests { @Test public void canGenerateCodeForIfWithNoResults(Snapshotter snapshotter) throws IOException { var instruction = Shid.if_( 0, Shid.block(List.of( Shid.pushInt(1), Shid.discard() )), Shid.block(List.of( Shid.pushInt(101), Shid.discard() )) ); var context = ShidToPatioContext.EMPTY .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("condition")); var result = ShidToPatio.generateInstruction(instruction, context); snapshotter.assertSnapshot(serialize(result)); } @Test public void canGenerateCodeForIfWithSingleResult(Snapshotter snapshotter) throws IOException { var instruction = Shid.if_( 1, Shid.block(List.of( Shid.pushInt(1), Shid.pushInt(2), Shid.intAdd() )), Shid.block(List.of( Shid.pushInt(101), Shid.pushInt(102), Shid.intAdd() )) ); var context = ShidToPatioContext.EMPTY .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("condition")); var result = ShidToPatio.generateInstruction(instruction, context); snapshotter.assertSnapshot(serialize(result)); } @Test public void canGenerateCodeForIfWithMultipleResults(Snapshotter snapshotter) throws IOException { var instruction = Shid.if_( 2, Shid.block(List.of( Shid.pushInt(1), Shid.pushInt(2) )), Shid.block(List.of( Shid.pushInt(101), Shid.pushInt(102) )) ); var context = ShidToPatioContext.EMPTY .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("condition")); var result = ShidToPatio.generateInstruction(instruction, context); snapshotter.assertSnapshot(serialize(result)); } }