<commit_before>package assert

import (
	"fmt"
	"path"
	"runtime"
	"strings"
	"testing"

	"github.com/daviddengcn/go-algs/ed"
	"github.com/daviddengcn/go-villa"
)

// escape unprintable chars
func showText(text string) string {
	var buf villa.ByteSlice
	for _, r := range text {
		if r > 0 && r < 27 {
			buf.WriteString(fmt.Sprintf("^%c", 'A'+r-1))
		} else {
			buf.WriteRune(r)
		}
	}
	buf.WriteRune('.')
	return string(buf)
}

// Default: skip == 0
func assertPos(skip int) string {
	_, file, line, ok := runtime.Caller(skip + 2)
	if !ok {
		return ""
	}
	return fmt.Sprintf("%s:%d: ", path.Base(file), line)
}

/*
	Equals fails the test and shows error message when act and exp are not
	equal
*/
func Equals(t *testing.T, name string, act, exp interface{}) {
	if act != exp {
		t.Errorf("%s%s is expected to be %s, but got %v", assertPos(0), name,
			showText(fmt.Sprint(exp)), showText(fmt.Sprint(act)))
	}
}

/*
	StringEquals fails the test and shows error message when string forms of
	act and exp are not equal
*/
func StringEquals(t *testing.T, name string, act, exp interface{}) {
	actS, expS := fmt.Sprint(act), fmt.Sprint(exp)
	if actS != expS {
		t.Errorf("%s%s is expected to be %s, but got %v", assertPos(0), name,
			showText(expS), showText(actS))
	} // if<commit_msg>Optimize StringEquals<commit_after>package assert

import (
	"fmt"
	"path"
	"runtime"
	"strings"
	"testing"

	"github.com/daviddengcn/go-algs/ed"
	"github.com/daviddengcn/go-villa"
)

// escape unprintable chars
func showText(text string) string {
	var buf villa.ByteSlice
	for _, r := range text {
		if r > 0 && r < 27 {
			buf.WriteString(fmt.Sprintf("^%c", 'A'+r-1))
		} else {
			buf.WriteRune(r)
		}
	}
	buf.WriteRune('.')
	return string(buf)
}

// Default: skip == 0
func assertPos(skip int) string {
	_, file, line, ok := runtime.Caller(skip + 2)
	if !ok {
		return ""
	}
	return fmt.Sprintf("%s:%d: ", path.Base(file), line)
}

/*
	Equals fails the test and shows error message when act and exp are not
	equal
*/
func Equals(t *testing.T, name string, act, exp interface{}) {
	if act != exp {
		t.Errorf("%s%s is expected to be %s, but got %v", assertPos(0), name,
			showText(fmt.Sprint(exp)), showText(fmt.Sprint(act)))
	}
}

/*
	StringEquals fails the test and shows error message when string forms of
	act and exp are not equal
*/
func StringEquals(t *testing.T, name string, act, exp interface{}) {
	actS, expS := fmt.Sprintf("%+v", act), fmt.Sprintf("%+v", exp)
	if actS != expS {
		if len(actS) + len(expS) < 70 {
			t.Errorf("%s%s is expected to be %s, but got %v", assertPos(0), name,
				showText(expS), showText(actS))
		} else {
			t.Errorf("%s%s is expected to be\n%s, but got\n%v", assertPos(0), name,
				showText(expS), showText(actS))
		}
	} // if
{'fraction_non_alphanumeric': 0.1646120377084844, 'fraction_numerical': 0.006889050036258158, 'mean_word_length': 3.660472972972973, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, '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': '5149287', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1054, 'n_words': 300}