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

// t_fractions_test.go
package expr

import (
	"testing"

	"git.portale-stac.it/go-pkg/expr/kern"
	"git.portale-stac.it/go-pkg/expr/types/fract"
)

func TestFractionsParser(t *testing.T) {
	section := "Fraction"
	inputs := []inputType{
		/*   1 */ {`1:2`, fract.NewFraction(1, 2), nil},
		/*   2 */ {`1:2 + 1`, fract.NewFraction(3, 2), nil},
		/*   3 */ {`1:2 - 1`, fract.NewFraction(-1, 2), nil},
		/*   4 */ {`1:2 * 1`, fract.NewFraction(1, 2), nil},
		/*   5 */ {`1:2 * 2:3`, fract.NewFraction(2, 6), nil},
		/*   6 */ {`1:2 / 2:3`, fract.NewFraction(3, 4), nil},
		/*   7 */ {`1:"5"`, nil, `denominator must be integer, got string (5)`},
		/*   8 */ {`"1":5`, nil, `numerator must be integer, got string (1)`},
		/*   9 */ {`1:+5`, fract.NewFraction(1, 5), nil},
		/*  10 */ {`1:(-2)`, fract.NewFraction(-1, 2), nil},
		/*  11 */ {`builtin "math.arith"; add(1:2, 2:3)`, fract.NewFraction(7, 6), nil},
		/*  12 */ {`builtin "math.arith"; add(1:2, 1.0, 2)`, float64(3.5), nil},
		/*  13 */ {`builtin "math.arith"; mul(1:2, 2:3)`, fract.NewFraction(2, 6), nil},
		/*  14 */ {`builtin "math.arith"; mul(1:2, 1.0, 2)`, float64(1.0), nil},
		/*  15 */ {`1:0`, nil, `[1:3] division by zero`},
		/*  16 */ {`1+1:2+0.5`, float64(2), nil},
		/*  17 */ {`1:(2-2)`, nil, `[1:3] division by zero`},
		/*  18 */ {`[0,1][1-1]:1`, fract.NewFraction(0, 1), nil},
		/*  19 */ {`1:2 == 0.5`, true, nil},
	}
	// runTestSuiteSpec(t, section, inputs, 26)
	RunTestSuite(t, section, inputs)
}

func TestFractionToStringSimple(t *testing.T) {
	source := fract.NewFraction(1, 2)
	want := "1:2"
	got := source.ToString(0)
	if got != want {
		t.Errorf(`(1,2) -> result = %v [%T], want = %v [%T]`, got, got, want, want)
	}
}

func TestFractionToStringMultiline(t *testing.T) {
	source := fract.NewFraction(1, 2)
	want := "1\n-\n2"
	got := source.ToString(kern.MultiLine)
	if got != want {
		t.Errorf(`(1,2) -> result = %v [%T], want = %v [%T]`, got, got, want, want)
	}
}

func TestToStringMultilineTty(t *testing.T) {
	source := fract.NewFraction(-1, 2)
	want := "\x1b[4m-1\x1b[0m\n 2"
	got := source.ToString(kern.MultiLine | kern.TTY)
	if got != want {
		t.Errorf(`(1,2) -> result = %#v [%T], want = %#v [%T]`, got, got, want, want)
	}
}
