<commit_before>package pdebug

import (
	"bytes"
	"errors"
	"io"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func setw(ctx *pdctx, w io.Writer) func() {
	oldw := ctx.Writer
	ctx.Writer = w
	return func() { ctx.Writer = oldw }
}

func TestPrintf(t *testing.T) {
	buf := &bytes.Buffer{}
	wg := setw(DefaultCtx, buf)
	defer wg()

	Printf("Hello, World!")

	if Enabled && Trace {
		if !assert.Equal(t, "|DEBUG| Hello, World!\n", buf.String(), "Simple Printf works") {
			return
		}
	} else {
		if !assert.Equal(t, "", buf.String(), "Simple Printf should be supressed") {
			return
		}
	}
}

func TestMarker(t *testing.T) {
	buf := &bytes.Buffer{}
	wg := setw(DefaultCtx, buf)
	defer wg()

	f2 := func() (err error) {
		g := Marker("f2").BindError(&err)
		defer g.End()
		Printf("Hello, World!")
		return errors.New("dummy error")
	}

	f1 := func() {
		g := Marker("f1")
		defer g.End()
		f2()
	}

	f1()

	if Enabled && Trace {
		const expected = "|DEBUG| START f1\n|DEBUG| \tSTART f2\n|DEBUG| \t\tHello, World!\n|DEBUG| \tEND f2 ("
		if !assert.True(t, strings.HasPrefix(buf.String(), expected), "Markers should work") {
			t.Logf("Expected '%v'", expected)
			t.Logf("Actual   '%v'", buf.String())
			return
		}
	} else {
		if !assert.Equal(t, "", buf.String(), "Markers should work") {
			return
		}
	}
}<commit_msg>Test IPrintf/IRelease<commit_after>package pdebug

import (
	"bytes"
	"errors"
	"io"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func setw(ctx *pdctx, w io.Writer) func() {
	oldw := ctx.Writer
	ctx.Writer = w
	return func() { ctx.Writer = oldw }
}

func TestPrintf(t *testing.T) {
	buf := &bytes.Buffer{}
	wg := setw(DefaultCtx, buf)
	defer wg()

	Printf("Hello, World!")

	if Enabled && Trace {
		if !assert.Equal(t, "|DEBUG| Hello, World!\n", buf.String(), "Simple Printf works") {
			return
		}
	} else {
		if !assert.Equal(t, "", buf.String(), "Simple Printf should be supressed") {
			return
		}
	}
}

func TestMarker(t *testing.T) {
	buf := &bytes.Buffer{}
	wg := setw(DefaultCtx, buf)
	defer wg()

	f2 := func() (err error) {
		g := Marker("f2").BindError(&err)
		defer g.End()
		Printf("Hello, World!")
		return errors.New("dummy error")
	}

	f1 := func() {
		g := Marker("f1")
		defer g.End()
		f2()
	}

	f1()

	if Enabled && Trace {
		const expected = "|DEBUG| START f1\n|DEBUG| \tSTART f2\n|DEBUG| \t\tHello, World!\n|DEBUG| \tEND f2 ("
		if !assert.True(t, strings.HasPrefix(buf.String(), expected), "Markers should work") {
			t.Logf("Expected '%v'", expected)
			t.Logf("Actual   '%v'", buf.String())
			return
		}
	} else {
		if !assert.Equal(t, "", buf.String(), "Markers should work") {
			return
		}
	}
}

func TestLegacyMarker(t *testing.T) {
	buf := &bytes.Buffer{}
	wg := setw(DefaultCtx, buf)
	defer wg()

	f2 := func() (err error) {
		g := IPrintf("START f2")
		defer func() {
			if err == nil {
				g.IRelease("END f2")
		  } else {
			  g.IRelease("END f2: %s", err)
			}
		}()
		Printf("Hello, World!")
		return errors.New("dummy error")
	}

	f1 := func() {
		g := IPrintf("START f1")
		defer g.IRelease("END f1")
		f2()
	}

	f1()

	if Enabled && Trace {
		const expected = "|DEBUG| START f1\n|DEBUG| \tSTART f2\n|DEBUG| \t\tHello, World!\n|DEBUG| \tEND f2"
		if !assert.True(t, strings.HasPrefix(buf.String(), expected), "Markers should work") {
			t.Logf("Expected '%v'", expected)
			t.Logf("Actual   '%v'", buf.String())
			return
		}

		// TODO: check for error and timestamp
	} else {
		if !assert.Equal(t, "", buf.String(), "Markers should work") {
			return
		}
	}
}

{'fraction_non_alphanumeric': 0.19521800281293952, 'fraction_numerical': 0.008438818565400843, 'mean_word_length': 3.357843137254902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5781019', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1439, 'n_words': 337}