ringio_test.go 1.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* See the file "LICENSE.txt" for the full license governing this code. */

package main

import (
	"bytes"
	"reflect"
	"testing"
)

type rioTestPair struct {
	params [2]int
	in     [][]byte
	out    []byte
}

func TestRingIO(t *testing.T) {
	tests := []rioTestPair{
		{
			[2]int{3, 12},
			[][]byte{
				[]byte("a string\n"),
				[]byte("another string\n"),
				[]byte("something\n"),
				[]byte("else\n"),
			},
			[]byte("another stri\nsomething\nelse\n"),
		},
		{
			[2]int{2, 10},
			[][]byte{
				[]byte("a string\n"),
				[]byte("another string\n"),
				[]byte("something\n"),
				[]byte("else\n"),
			},
			[]byte("something\nelse\n"),
		},
		{
			[2]int{2, 4},
			[][]byte{
				[]byte("a string"),
				[]byte("test1"),
				[]byte("test2"),
			},
			[]byte("test\ntest\n"),
		},
		{
			[2]int{100, 100},
			[][]byte{
				[]byte("a string"),
				[]byte("test1"),
				[]byte("test2"),
			},
			[]byte("a stringtest1test2"),
		},
	}
	var buf bytes.Buffer
	for _, tp := range tests {
		rio := newRingIO(&buf, tp.params[0], tp.params[1])
		for _, l := range tp.in {
			rio.Write(l)
		}
		got := rio.GetAsText()
		wanted := tp.out
		if !reflect.DeepEqual(got, wanted) {
			t.Errorf("wanted:\n>>>\n%s\n<<<\ngot:\n>>>\n%s\n<<<", wanted, got)
		}
	}
}