<commit_before>package conflag

import (
	"reflect"
	"regexp"
	"strings"
	"testing"
)

type ParseTest struct {
	configString string
	expected     map[string]interface{}
}

type ParseErrorTest struct {
	configString string
	expected     *regexp.Regexp
}

// helper
func (c conf) asMap() map[string]interface{} {
	return c
}

// JSON parser tests

func TestParseJson(t *testing.T) {
	asserts := []ParseTest{
		ParseTest{
			`{"flag":"value"}`,
			map[string]interface{}{"flag": "value"},
		},
		ParseTest{
			`{"flag1":"value1", "flag2":"value2"}`,
			map[string]interface{}{"flag1": "value1", "flag2": "value2"},
		},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)

		actual, err := parseAsJson(reader)
		if err != nil {
			t.Errorf("parse error: %v", a.configString)
		}

		if !reflect.DeepEqual(a.expected, actual.asMap()) {
			t.Errorf("not match: %#v %#v", a.expected, actual.asMap())
		}
	}
}

func TestParseJson_ParseError(t *testing.T) {
	asserts := []ParseErrorTest{
		ParseErrorTest{`{'flag':'value'}`, regexp.MustCompile("invalid character '\\'' looking for beginning of object key string")},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)
		_, err := parseAsJson(reader)
		if a.expected.MatchString(err.Error()) {
			t.Errorf("parse error: %v", err)
		}
	}
}

// YAML parser tests

func TestParseYaml(t *testing.T) {
	asserts := []ParseTest{
		ParseTest{
			`flag: value`,
			map[string]interface{}{"flag": "value"},
		},
		ParseTest{
			"flag1: value1\nflag2: value2\n",
			map[string]interface{}{"flag1": "value1", "flag2": "value2"},
		},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)
		actual, err := parseAsYaml(reader)

		if err != nil {
			t.Errorf("parse error: %v %v", err, a.configString)
		}

		if !reflect.DeepEqual(a.expected, actual.asMap()) {
			t.Errorf("not match: %#v %#v", a.expected, actual.asMap())
		}
	}
}

func TestParseYaml_ParseError(t *testing.T) {
	asserts := []ParseErrorTest{
		ParseErrorTest{`flag - value`, regexp.MustCompile("yaml: unmarshal errors:")},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)
		_, err := parseAsYaml(reader)
		if !a.expected.MatchString(err.Error()) {
			t.Errorf("expected error but")
		}
	}
}<commit_msg>Fix error messages<commit_after>package conflag

import (
	"reflect"
	"regexp"
	"strings"
	"testing"
)

type ParseTest struct {
	configString string
	expected     map[string]interface{}
}

type ParseErrorTest struct {
	configString string
	expected     *regexp.Regexp
}

// helper
func (c conf) asMap() map[string]interface{} {
	return c
}

// JSON parser tests

func TestParseJson(t *testing.T) {
	asserts := []ParseTest{
		ParseTest{
			`{"flag":"value"}`,
			map[string]interface{}{"flag": "value"},
		},
		ParseTest{
			`{"flag1":"value1", "flag2":"value2"}`,
			map[string]interface{}{"flag1": "value1", "flag2": "value2"},
		},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)
		actual, err := parseAsJson(reader)
		if err != nil {
			t.Errorf("Unexpected parse error: %v, for input %#v", err, a.configString)
		}

		if !reflect.DeepEqual(a.expected, actual.asMap()) {
			t.Errorf("Parsed result should be %#v, but %#v", a.expected, actual.asMap())
		}
	}
}

func TestParseJson_ParseError(t *testing.T) {
	asserts := []ParseErrorTest{
		ParseErrorTest{`{'flag':'value'}`, regexp.MustCompile("invalid character '\\'' looking for beginning of object key string")},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)
		_, err := parseAsJson(reader)
		if a.expected.MatchString(err.Error()) {
			t.Errorf("Unexpected parse error: %v", err)
		}
	}
}

// YAML parser tests

func TestParseYaml(t *testing.T) {
	asserts := []ParseTest{
		ParseTest{
			`flag: value`,
			map[string]interface{}{"flag": "value"},
		},
		ParseTest{
			"flag1: value1\nflag2: value2\n",
			map[string]interface{}{"flag1": "value1", "flag2": "value2"},
		},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)
		actual, err := parseAsYaml(reader)
		if err != nil {
			t.Errorf("Unexpected parse error: %v, for input %#v", err, a.configString)
		}

		if !reflect.DeepEqual(a.expected, actual.asMap()) {
			t.Errorf("Parsed result should be %#v, but %#v", a.expected, actual.asMap())
		}
	}
}

func TestParseYaml_ParseError(t *testing.T) {
	asserts := []ParseErrorTest{
		ParseErrorTest{`flag - value`, regexp.MustCompile("yaml: unmarshal errors:")},
	}

	for _, a := range asserts {
		reader := strings.NewReader(a.configString)
		_, err := parseAsYaml(reader)
		if !a.expected.MatchString(err.Error()) {
			t.Errorf("Unexpected parse error: %v", err)
		}
	}
}
{'fraction_non_alphanumeric': 0.1759474910014821, 'fraction_numerical': 0.0067753546474698285, 'mean_word_length': 4.168490153172867, 'pattern_counts': {'":': 18, '<': 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': '1623824', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1689, 'n_words': 365}