package p.studio.compiler.pbs.semantics;

import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PbsIgnoredValueSemanticsTest {

    @Test
    void shouldWarnWhenExpressionStatementIgnoresMaterializedValue() {
        final var source = """
                fn pure() -> int { return 1; }

                fn frame() -> void {
                  pure();
                  return;
                }
                """;
        final var diagnostics = DiagnosticSink.empty();

        new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);

        assertEquals(1, diagnostics.warningCount());
        assertTrue(diagnostics.stream().anyMatch(d ->
                d.getCode().equals(PbsSemanticsErrors.W_SEM_IGNORED_VALUE.name())));
    }

    @Test
    void shouldNotWarnWhenExpressionStatementIsUnitLike() {
        final var source = """
                fn log() -> void { return; }

                fn frame() -> void {
                  log();
                  return;
                }
                """;
        final var diagnostics = DiagnosticSink.empty();

        new PbsFrontendCompiler().compileFile(new FileId(1), source, diagnostics);

        assertEquals(0, diagnostics.warningCount());
    }
}
