// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.

// t_ast_test.go
package expr

import (
	"errors"
	"testing"

	"git.portale-stac.it/go-pkg/expr/scan"
	"git.portale-stac.it/go-pkg/expr/sym"
)

func TestAstString(t *testing.T) {
	tree := scan.NewAst()
	if gotResult := tree.String(); gotResult != "(nil)" {
		t.Errorf(`result: got %q, want "(nil)"`, gotResult)
	}
}

func TestAddTokensGood(t *testing.T) {
	tk1 := scan.NewValueToken(0, 0, sym.SymInteger, "100", 100)
	tk2 := scan.NewToken(0, 0, sym.SymPlus, "+")
	tk3 := scan.NewValueToken(0, 0, sym.SymInteger, "50", 500)

	tree := scan.NewAst()
	if gotErr := tree.AddTokens(tk1, tk2, tk3); gotErr != nil {
		t.Errorf("err: got <%v>, want <nil>", gotErr)
	}
}

func TestAddTokensBad(t *testing.T) {
	tk0 := scan.NewValueToken(0, 0, sym.SymInteger, "200", 200)
	tk1 := scan.NewValueToken(0, 0, sym.SymInteger, "100", 100)
	tk2 := scan.NewToken(0, 0, sym.SymPlus, "+")
	tk3 := scan.NewValueToken(0, 0, sym.SymInteger, "50", 500)

	wantErr := errors.New(`[0:0] two adjacent operators: "200" and "100"`)

	tree := scan.NewAst()
	if gotErr := tree.AddTokens(tk0, tk1, tk2, tk3); gotErr != nil && gotErr.Error() != wantErr.Error() {
		t.Errorf("err: got <%v>, want <nil>", gotErr)
	}
}

func TestAddUnknownTokens(t *testing.T) {
	tk0 := scan.NewToken(0, 0, sym.SymPercent, "%")

	wantErr := errors.New(`unexpected token "%"`)

	tree := scan.NewAst()
	if _, gotErr := tree.AddToken(tk0); gotErr != nil && gotErr.Error() != wantErr.Error() {
		t.Errorf("err: got <%v>, want <%v>", gotErr, wantErr)
	}
}
