<commit_before>package proto

import (
	"bytes"
	"bufio"
	"os"
	"io"
	"net/textproto"

	"testing"
	"junta/assert"
)


// == Testing =============================

type ErroneousWriter struct {
	// No exported fields
	io.Writer
	which int
}

func (ew *ErroneousWriter) Write(bytes []byte) (int, os.Error) {
	if ew.which == 0 {
		return 0, os.NewError("BOOM!")
	}
	ew.which--
	return ew.Writer.Write(bytes)
}

type encTest struct {
	data interface{}
	encoding string
}

var encTests = []encTest{
	encTest{int(0), ":0\r\n"},
	encTest{[]byte{'a'}, "$1\r\na\r\n"},
	encTest{Line("hi"), "+hi\r\n"},
	encTest{os.NewError("hi"), "-hi\r\n"},
	encTest{nil, "$-1\r\n"},
	encTest{[]interface{}{[]byte{'a'}, []byte{'b'}}, "*2\r\n$1\r\na\r\n$1\r\nb\r\n"},
	encTest{[]interface{}{"GET", "FOO"}, "*2\r\n$3\r\nGET\r\n$3\r\nFOO\r\n"},
	encTest{[]interface{}{1, []interface{}{1}}, "*2\r\n:1\r\n*1\r\n:1\r\n"},
}

func TestProtoEncode(t *testing.T) {
	for _, e := range encTests {
		b := new(bytes.Buffer)
		w := bufio.NewWriter(b)
		ww := textproto.NewWriter(w)

		err := encode(ww, e.data)
		assert.Equal(t, nil, err)
		assert.Equal(t, e.encoding, string(b.Bytes()))
	}
}

func TestProtoEncodeHeaderError(t *testing.T) {
	b := new(bytes.Buffer)
	ew := &ErroneousWriter{b, 0}
	w := bufio.NewWriter(ew)<commit_msg>if a test fails, keep going<commit_after>package proto

import (
	"bytes"
	"bufio"
	"os"
	"io"
	"net/textproto"

	"testing"
	"junta/assert"
)


// == Testing =============================

type ErroneousWriter struct {
	// No exported fields
	io.Writer
	which int
}

func (ew *ErroneousWriter) Write(bytes []byte) (int, os.Error) {
	if ew.which == 0 {
		return 0, os.NewError("BOOM!")
	}
	ew.which--
	return ew.Writer.Write(bytes)
}

type encTest struct {
	data interface{}
	encoding string
}

var encTests = []encTest{
	encTest{int(0), ":0\r\n"},
	encTest{[]byte{'a'}, "$1\r\na\r\n"},
	encTest{Line("hi"), "+hi\r\n"},
	encTest{os.NewError("hi"), "-hi\r\n"},
	encTest{nil, "$-1\r\n"},
	encTest{[]interface{}{[]byte{'a'}, []byte{'b'}}, "*2\r\n$1\r\na\r\n$1\r\nb\r\n"},
	encTest{[]interface{}{"GET", "FOO"}, "*2\r\n$3\r\nGET\r\n$3\r\nFOO\r\n"},
	encTest{[]interface{}{1, []interface{}{1}}, "*2\r\n:1\r\n*1\r\n:1\r\n"},
}

func TestProtoEncode(t *testing.T) {
	for _, e := range encTests {
		b := new(bytes.Buffer)
		w := bufio.NewWriter(b)
		ww := textproto.NewWriter(w)

		err := encode(ww, e.data)
		if err != nil {
			t.Error("unexpected err:", err)
			continue
		}
		if e.encoding != string(b.Bytes()) {
			t.Error("expected %q", e.encoding)
			t.Error("     got %q", string(b.Bytes()))
		}
	}
}

func TestProtoEncodeHeaderError(t *testing.T) {
	b := new(bytes.Buffer)
	ew := &ErroneousWriter{b, 0}
	w := bufio.NewWriter(ew)
{'fraction_non_alphanumeric': 0.25441176470588234, 'fraction_numerical': 0.013970588235294118, 'mean_word_length': 3.9653284671532845, 'pattern_counts': {'":': 2, '<': 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': '6779392', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1186, 'n_words': 209}