<commit_before>package chanson

import (
	"bytes"
	"encoding/json"
	"io"
	"strings"
	"testing"

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

func trim(s string) string {
	return strings.Map(func(r rune) rune {
		switch r {
		case ' ', '\n', '\t':
			return -1
		default:
			return r
		}
	}, s)
}

func TestObjectKeyVal(t *testing.T) {
	buf := bytes.NewBuffer(nil)
	New(buf).Object(func(obj Object) {
		obj.Set("foo", "bar")
		obj.Set("fun", func(w io.Writer) {
			w.Write([]byte(`"val"`))
		})
		obj.Set("a\nnewline", "baz")
	})

	assert.Equal(t, trim(`
	{
		"foo": "bar",
		"fun": "val",
		"a\nnewline": "baz"
	}`), trim(buf.String()))
}

func TestArrays(t *testing.T) {
	buf := bytes.NewBuffer(nil)
	New(buf).Array(func(a Array) {
		a.Push(func(w io.Writer) {
			w.Write([]byte("1"))
		})
		a.Push(2)<commit_msg>Extend and separate key encoding test<commit_after>package chanson

import (
	"bytes"
	"encoding/json"
	"io"
	"strings"
	"testing"

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

func trim(s string) string {
	return strings.Map(func(r rune) rune {
		switch r {
		case ' ', '\n', '\t':
			return -1
		default:
			return r
		}
	}, s)
}

func TestObjectKeyVal(t *testing.T) {
	buf := bytes.NewBuffer(nil)
	New(buf).Object(func(obj Object) {
		obj.Set("foo", "bar")
		obj.Set("fun", func(w io.Writer) {
			w.Write([]byte(`"val"`))
		})
	})

	assert.Equal(t, trim(`
	{
		"foo": "bar",
		"fun": "val"
	}`), trim(buf.String()))
}

func TestObjectKeyEncoding(t *testing.T) {

	for _, test := range []struct {
		key      string
		expected string
	}{
		{"key\n", `"key\n"`},
		{"key\t", `"key\t"`},
		{"key\b", `"key\b"`},
		{"key\f", `"key\f"`},
		{"key\\", `"key\\"`},
	} {
		buf := bytes.NewBuffer(nil)
		New(buf).Object(func(obj Object) {
			obj.Set(test.key, 0)
		})

		assert.Equal(t, "{"+test.expected+":0}", trim(buf.String()))
	}
}

func TestArrays(t *testing.T) {
	buf := bytes.NewBuffer(nil)
	New(buf).Array(func(a Array) {
		a.Push(func(w io.Writer) {
			w.Write([]byte("1"))
		})
		a.Push(2)
{'fraction_non_alphanumeric': 0.23023023023023023, 'fraction_numerical': 0.004004004004004004, 'mean_word_length': 3.4820627802690582, 'pattern_counts': {'":': 6, '<': 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': '3254438', 'n_tokens_mistral': 922, 'n_tokens_neox': 862, 'n_words': 156}