package common

import (
	"regexp"
	"testing"
)

type TestWriter struct {
	str string
}

func (s *TestWriter) WriteString(str2 string) (int, error) {
	s.str += str2
	return len(s.str), nil
}

func TestLogging(t *testing.T) {
	t.Run("basic logging", func(t *testing.T) {
		var strWriter, errWriter TestWriter

		if len(strWriter.str) != 0 || len(errWriter.str) != 0 {
			t.Fatal("non-null empty logging?")
		}

		logger := CreateStdoutLogger(&strWriter, &errWriter)
		logger.LogError("%d", 100)
		logger.Log("OKA %d Done", 77)
		logger.Log("Another line")

		const prefixMatch = `\[\d+\] \[[^\]]+\]: `

		if ok, err := regexp.MatchString("^"+prefixMatch+"100\n$", errWriter.str); !ok {
			if err != nil {
				t.Logf("err: %v", err)
			}
			t.Fatal(errWriter)
		}
		if ok, err := regexp.MatchString("^"+prefixMatch+"OKA 77 Done\n"+prefixMatch+"Another line\n$", strWriter.str); !ok {
			if err != nil {
				t.Logf("err: %v", err)
			}
			t.Fatal(strWriter.str)
		}
	})
}

