package logger

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"
	"local/logb"
	"os"
	"testing"

	cron "github.com/robfig/cron/v3"
)

func TestInterface(t *testing.T) {
	l := New()
	var logger cron.Logger
	logger = l

	w := bytes.NewBuffer(nil)
	logb.SetWriter(w)
	f, err := ioutil.TempFile(os.TempDir(), "logger.testInterface")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(f.Name())
	was := os.Stderr
	os.Stderr = f
	defer func() {
		os.Stderr = was
	}()

	logger.Info(fmt.Sprintf("hello from %v", "me"))
	logger.Error(errors.New("bad"), fmt.Sprintf("error from %v", "me"))

	if !bytes.Contains(w.Bytes(), []byte(`error from me`)) {
		t.Errorf("%s", w.Bytes())
	}
	if !bytes.Contains(w.Bytes(), []byte(`hello from me`)) {
		t.Errorf("%s", w.Bytes())
	}
}
