<commit_before>package jamon

import (
	"io/ioutil"
	"os"
	"reflect"
	"testing"
)

func TestJamon_Load(t *testing.T) {
	testSuite := []struct {
		contents string
		expected Config
	}{
		{
			contents: `
[defaults]

key=value
key2=value2`,
			expected: Config{
				"defaults": Category{
					"key":  "value",
					"key2": "value2",
				},
			},
		}, {
			contents: `
floating=pairs
of=keys # with a comment

# These are the defaults
[defaults]

key=value

# This key is important
key2=value2

[custom] # This is the best category
a=b
c=d`,
			expected: Config{
				"JAMON.NO_CATEGORY": Category{
					"floating": "pairs",
					"of":       "keys",
				},
				"custom": Category{
					"a": "b",
					"c": "d",
				},
				"defaults": Category{
					"key":  "value",
					"key2": "value2",
				},
			},
		},
	}

	for _, test := range testSuite {
		err := ioutil.WriteFile("testfile.tmp", []byte(test.contents), 0777)
		if err != nil {
			t.Errorf("Error creating temporary file: %s", err)
		}

		config, err := Load("testfile.tmp")
		if err != nil {
			t.Error("Error loading file: %s", err)
		}

		if !reflect.DeepEqual(config, test.expected) {
			t.Errorf("Expected %+v, got %+v", test.expected, config)
		}

		err = os.Remove("testfile.tmp")
		if err != nil {
			t.Errorf("Error removing file: %s", err)
		}
	}
}

func TestJamon_parseLine(t *testing.T) {
	testSuite := []struct {
		input      string
		isCategory bool
		value, key string
		hasError   bool
	}{
		// Categories
		{"[category]", true, "category", "", false},
		{"[category.name] # weird comment", true, "category.name", "", false},

		// Errors
		{"category", false, "", "", true},

		// Key / value pairs<commit_msg>Full coverage<commit_after>package jamon

import (
	"io/ioutil"
	"os"
	"reflect"
	"testing"
)

func TestJamon_Load(t *testing.T) {
	testSuite := []struct {
		contents string
		expected Config
	}{
		{
			contents: `
[defaults]

key=value
key2=value2`,
			expected: Config{
				"defaults": Category{
					"key":  "value",
					"key2": "value2",
				},
			},
		}, {
			contents: `
floating=pairs
of=keys # with a comment

# These are the defaults
[defaults]

key=value

# This key is important
key2=value2

[custom] # This is the best category
a=b
c=d`,
			expected: Config{
				"JAMON.NO_CATEGORY": Category{
					"floating": "pairs",
					"of":       "keys",
				},
				"custom": Category{
					"a": "b",
					"c": "d",
				},
				"defaults": Category{
					"key":  "value",
					"key2": "value2",
				},
			},
		},
	}

	for _, test := range testSuite {
		err := ioutil.WriteFile("testfile.tmp", []byte(test.contents), 0777)
		if err != nil {
			t.Errorf("Error creating temporary file: %s", err)
		}

		config, err := Load("testfile.tmp")
		if err != nil {
			t.Error("Error loading file: %s", err)
		}

		if !reflect.DeepEqual(config, test.expected) {
			t.Errorf("Expected %+v, got %+v", test.expected, config)
		}

		err = os.Remove("testfile.tmp")
		if err != nil {
			t.Errorf("Error removing file: %s", err)
		}
	}
}

func TestJamon_Load_Error(t *testing.T) {
	_, err := Load("no-way-this-file-is-there.123")
	if err == nil {
		t.Error("Was expecting an error when opening an odd file")
	}
}

func TestJamon_parseLine(t *testing.T) {
	testSuite := []struct {
		input      string
		isCategory bool
		value, key string
		hasError   bool
	}{
		// Categories
		{"[category]", true, "category", "", false},
		{"[category.name] # weird comment", true, "category.name", "", false},

		// Errors
		{"category", false, "", "", true},

		// Key / value pairs
{'fraction_non_alphanumeric': 0.17718652703085197, 'fraction_numerical': 0.007642230399094254, 'mean_word_length': 2.9618834080717487, 'pattern_counts': {'":': 24, '<': 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': '2301655', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1300, 'n_words': 321}