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

// t_ast_test.go
package expr

import (
	"errors"
	"testing"
)

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

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

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

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

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

	tree := 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 := NewToken(0, 0, SymPercent, "%")

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

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