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.assertTrue;

class PbsSemanticsApplyResolutionTest {

    @Test
    void shouldDifferentiateNonCallableUnresolvedAndAmbiguousApply() {
        final var source = """
                fn over(x: int) -> int { return x; }
                fn over(x: float) -> int { return 0; }
                fn pair(a: int, b: int) -> int { return a + b; }

                fn demo() -> int {
                  let a: int = 1 apply ();
                  let b: int = pair apply 1;
                  let c: int = over apply missing;
                  return 0;
                }
                """;
        final var diagnostics = DiagnosticSink.empty();

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

        assertTrue(diagnostics.stream().anyMatch(d ->
                d.getCode().equals(PbsSemanticsErrors.E_SEM_APPLY_NON_CALLABLE_TARGET.name())));
        assertTrue(diagnostics.stream().anyMatch(d ->
                d.getCode().equals(PbsSemanticsErrors.E_SEM_APPLY_UNRESOLVED_OVERLOAD.name())));
        assertTrue(diagnostics.stream().anyMatch(d ->
                d.getCode().equals(PbsSemanticsErrors.E_SEM_APPLY_AMBIGUOUS_OVERLOAD.name())));
    }
}
