<commit_before>package log

import (
	"bytes"
	"errors"
	"fmt"
	"regexp"
	"testing"
)

func TestSetLevel(t *testing.T) {
	// Check default level is INFO & Is* flags are set appropriately
	if level != INFO || !IsError || !IsWarn || !IsInfo || IsDebug || IsTrace {
		fmt.Println("Default level is not set as expected")
		t.Fail()
	}

	// Check TRACE
	SetLogLevel(TRACE)
	if level != TRACE || !IsError || !IsWarn || !IsInfo || !IsDebug || !IsTrace {
		fmt.Println("TRACE level is not set as expected")
		t.Fail()
	}

	// Check DEBUG
	SetLogLevel(DEBUG)
	if level != DEBUG || !IsError || !IsWarn || !IsInfo || !IsDebug || IsTrace {
		fmt.Println("DEBUG level is not set as expected")
		t.Fail()
	}

	// Check INFO
	SetLogLevel(INFO)
	if level != INFO || !IsError || !IsWarn || !IsInfo || IsDebug || IsTrace {
		fmt.Println("INFO level is not set as expected")
		t.Fail()
	}

	// Check WARN
	SetLogLevel(WARN)
	if level != WARN || !IsError || !IsWarn || IsInfo || IsDebug || IsTrace {
		fmt.Println("WARN level is not set as expected")
		t.Fail()
	}

	// Check ERROR
	SetLogLevel(ERROR)
	if level != ERROR || !IsError || IsWarn || IsInfo || IsDebug || IsTrace {
		fmt.Println("ERROR level is not set as expected")
		t.Fail()
	}

	// Check FATAL
	SetLogLevel(FATAL)
	if level != FATAL || IsError || IsWarn || IsInfo || IsDebug || IsTrace {
		fmt.Println("FATAL level is not set as expected")
		t.Fail()
	}
}

func TestLoggingF(t *testing.T) {
	// Tests Tracef(), Debugf(), Infof(), Warnf(), Errorf() logging methods.
	type TestCase struct {
		loggerLevel Level
		msgLevel    Level
		msg         string
		msgArgs     []interface{}
		expected    string // regex pattern
	}

	var testCases []TestCase = []TestCase{
		// These test cases use ".*" to avoid specifics of timestamp, file location, and line number.
		{
			loggerLevel: INFO,
			msgLevel:    INFO,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v•INFO•.*log_test.go•.*•Hello", TimestampRegex),
		},
		// Logging with logger's level set higher than message should result in no output.
		{
			loggerLevel: WARN,
			msgLevel:    INFO,
			msg:         "Hello",
			expected:    "",
		},
		{
			loggerLevel: TRACE,
			msgLevel:    TRACE,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v•TRACE•.*log_test.go•.*•Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    DEBUG,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v•DEBUG•.*log_test.go•.*•Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    INFO,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v•INFO•.*log_test.go•.*•Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    WARN,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v•WARN•.*log_test.go•.*•Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    ERROR,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v•ERROR•.*log_test.go•.*•Hello", TimestampRegex),
		},
		// Check use of formatting args.
		{
			loggerLevel: TRACE,
			msgLevel:    ERROR,
			msg:         "Hello #%v %v",
			msgArgs:     []interface{}{1, "Joe"},
			expected:    fmt.Sprintf("%v•ERROR•.*log_test.go•.*•Hello", TimestampRegex),
		},
	}

	loggerCalls := map[Level]func(string, ...interface{}){
		FATAL: Fatalf,
		ERROR: Errorf,
		WARN:  Warnf,
		INFO:  Infof,
		DEBUG: Debugf,
		TRACE: Tracef,
	}

	for i, tc := range testCases {
		testOut := bytes.NewBufferString("")
		SetOutput(testOut)
		SetLogLevel(tc.loggerLevel)
		loggerCalls[tc.msgLevel](tc.msg, tc.msgArgs...)

		resultMsg := testOut.String()
		matched, err := regexp.MatchString(tc.expected, resultMsg)
		if err != nil || !matched {
			fmt.Printf("TestCase[%v] failed, \n'%v'\ndoesn't match\n'%v'\n", i, tc.expected, resultMsg)
			t.Fail()
		}
	}
}

func TestLogging(t *testing.T) {
	// Tests Trace(), Debug(), Info(), Warn(), Error() logging methods.
	type TestCase struct {
		loggerLevel Level
		msgLevel    Level
		msgArgs     []interface{}
		expected    string // regex pattern
	}

	var testCases []TestCase = []TestCase{
		// These test cases use regex ".*" to avoid specifics of file location, and line number.
		{ // Check string as arg
			loggerLevel: TRACE,
			msgLevel:    ERROR,
			msgArgs:     []interface{}{"Hello"},
			expected:    fmt.Sprintf("%v•ERROR•.*log_test.go•.*•Hello", TimestampRegex),
		},
		{ // Check numbers as args
			loggerLevel: INFO,
			msgLevel:    INFO,
			msgArgs:     []interface{}{1, 2, 3.3, "joe"},
			expected:    fmt.Sprintf("%v•INFO•.*log_test.go•.*•1 2 3.3", TimestampRegex),
		},
		{ // Check error as arg
			loggerLevel: INFO,
			msgLevel:    INFO,
			msgArgs:     []interface{}{errors.New("Test error message")},
			expected:    fmt.Sprintf("%v•INFO•.*log_test.go•.*•Test error message", TimestampRegex),
		},
		{ // Check mix of numbers, errors, and strings as args
			loggerLevel: TRACE,
			msgLevel:    TRACE,
			msgArgs:     []interface{}{1.1, errors.New("Test error message"), 42, " is the answer"},
			expected:    fmt.Sprintf("%v•TRACE•.*log_test.go•.*•1.1 Test error message 42 is the answer", TimestampRegex),
		},
		{ // Check that a format string gets interpretted literally
			loggerLevel: TRACE,
			msgLevel:    TRACE,
			msgArgs:     []interface{}{"This %v could be a %v format string"},
			expected:    fmt.Sprintf("%v•TRACE•.*log_test.go•.*•This %%v could be a %%v format string", TimestampRegex),
		},
	}
<commit_msg>modified internal logger tests. #161<commit_after>package log

import (
	"bytes"
	"errors"
	"fmt"
	"regexp"
	"testing"
)

func TestSetLevel(t *testing.T) {
	// Check default level is INFO & Is* flags are set appropriately
	if level != INFO || !IsError || !IsWarn || !IsInfo || IsDebug || IsTrace {
		fmt.Println("Default level is not set as expected")
		t.Fail()
	}

	// Check TRACE
	SetLogLevel(TRACE)
	if level != TRACE || !IsError || !IsWarn || !IsInfo || !IsDebug || !IsTrace {
		fmt.Println("TRACE level is not set as expected")
		t.Fail()
	}

	// Check DEBUG
	SetLogLevel(DEBUG)
	if level != DEBUG || !IsError || !IsWarn || !IsInfo || !IsDebug || IsTrace {
		fmt.Println("DEBUG level is not set as expected")
		t.Fail()
	}

	// Check INFO
	SetLogLevel(INFO)
	if level != INFO || !IsError || !IsWarn || !IsInfo || IsDebug || IsTrace {
		fmt.Println("INFO level is not set as expected")
		t.Fail()
	}

	// Check WARN
	SetLogLevel(WARN)
	if level != WARN || !IsError || !IsWarn || IsInfo || IsDebug || IsTrace {
		fmt.Println("WARN level is not set as expected")
		t.Fail()
	}

	// Check ERROR
	SetLogLevel(ERROR)
	if level != ERROR || !IsError || IsWarn || IsInfo || IsDebug || IsTrace {
		fmt.Println("ERROR level is not set as expected")
		t.Fail()
	}

	// Check FATAL
	SetLogLevel(FATAL)
	if level != FATAL || IsError || IsWarn || IsInfo || IsDebug || IsTrace {
		fmt.Println("FATAL level is not set as expected")
		t.Fail()
	}
}

func TestLoggingF(t *testing.T) {
	// Tests Tracef(), Debugf(), Infof(), Warnf(), Errorf() logging methods.
	type TestCase struct {
		loggerLevel Level
		msgLevel    Level
		msg         string
		msgArgs     []interface{}
		expected    string // regex pattern
	}

	testCases := []TestCase{
		// These test cases use ".*" to avoid specifics of timestamp, file location, and line number.
		{
			loggerLevel: INFO,
			msgLevel:    INFO,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v.*[INFO].*log_test.go:.*Hello", TimestampRegex),
		},
		// Logging with logger's level set higher than message should result in no output.
		{
			loggerLevel: WARN,
			msgLevel:    INFO,
			msg:         "Hello",
			expected:    "",
		},
		{
			loggerLevel: TRACE,
			msgLevel:    TRACE,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v.*[TRACE].*log_test.go:.*Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    DEBUG,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v.*[DEBUG].*log_test.go:.*Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    INFO,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v.*[INFO].*log_test.go.*Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    WARN,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v.*[WARN].*log_test.go.*Hello", TimestampRegex),
		},
		{
			loggerLevel: TRACE,
			msgLevel:    ERROR,
			msg:         "Hello",
			expected:    fmt.Sprintf("%v.*[ERROR].*log_test.go.*Hello", TimestampRegex),
		},
		// Check use of formatting args.
		{
			loggerLevel: TRACE,
			msgLevel:    ERROR,
			msg:         "Hello #%v %v",
			msgArgs:     []interface{}{1, "Joe"},
			expected:    fmt.Sprintf("%v.*[ERROR].*log_test.go.*Hello", TimestampRegex),
		},
	}

	loggerCalls := map[Level]func(string, ...interface{}){
		FATAL: Fatalf,
		ERROR: Errorf,
		WARN:  Warnf,
		INFO:  Infof,
		DEBUG: Debugf,
		TRACE: Tracef,
	}

	for i, tc := range testCases {
		testOut := bytes.NewBufferString("")
		SetOutput(testOut)
		SetLogLevel(tc.loggerLevel)

		loggerCalls[tc.msgLevel](tc.msg, tc.msgArgs...)

		resultMsg := testOut.String()

		matched, err := regexp.MatchString(tc.expected, resultMsg)
		if err != nil || !matched {
			t.Errorf("TestCase[%v] failed, expected:\n %v \ngot\n %v\n", i, tc.expected, resultMsg)
			continue
		}
	}
}

func TestLogging(t *testing.T) {
	// Tests Trace(), Debug(), Info(), Warn(), Error() logging methods.
	type TestCase struct {
		loggerLevel Level
		msgLevel    Level
		msgArgs     []interface{}
		expected    string // regex pattern
	}

	var testCases []TestCase = []TestCase{
		// These test cases use regex ".*" to avoid specifics of file location, and line number.
		{ // Check string as arg
			loggerLevel: TRACE,
			msgLevel:    ERROR,
			msgArgs:     []interface{}{"Hello"},
			expected:    fmt.Sprintf("%v.*[ERROR].*log_test.go.*Hello", TimestampRegex),
		},
		{ // Check numbers as args
			loggerLevel: INFO,
			msgLevel:    INFO,
			msgArgs:     []interface{}{1, 2, 3.3, "joe"},
			expected:    fmt.Sprintf("%v.*[INFO].*log_test.go.*1 2 3.3", TimestampRegex),
		},
		{ // Check error as arg
			loggerLevel: INFO,
			msgLevel:    INFO,
			msgArgs:     []interface{}{errors.New("Test error message")},
			expected:    fmt.Sprintf("%v.*[INFO].*log_test.go.*Test error message", TimestampRegex),
		},
		{ // Check mix of numbers, errors, and strings as args
			loggerLevel: TRACE,
			msgLevel:    TRACE,
			msgArgs:     []interface{}{1.1, errors.New("Test error message"), 42, " is the answer"},
			expected:    fmt.Sprintf("%v.*[TRACE].*log_test.go.*1.1 Test error message 42 is the answer", TimestampRegex),
		},
		{ // Check that a format string gets interpretted literally
			loggerLevel: TRACE,
			msgLevel:    TRACE,
			msgArgs:     []interface{}{"This %v could be a %v format string"},
			expected:    fmt.Sprintf("%v.*[TRACE].*log_test.go.*This %%v could be a %%v format string", TimestampRegex),
		},
	}

{'fraction_non_alphanumeric': 0.15995919881305637, 'fraction_numerical': 0.0034310089020771514, 'mean_word_length': 3.0713476783691958, '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': '3066692', 'n_tokens_mistral': 4113, 'n_tokens_neox': 3700, 'n_words': 974}