good morning!!!!

Skip to content
Snippets Groups Projects
capturer.go 786 B
Newer Older
Garet Halliday's avatar
Garet Halliday committed
package test

import (
	"bytes"
	"fmt"

Garet Halliday's avatar
Garet Halliday committed
	"gfx.cafe/gfx/pggat/lib/fed"
	"gfx.cafe/gfx/pggat/lib/gsql"
Garet Halliday's avatar
Garet Halliday committed
)

type Capturer struct {
	Packets []fed.Packet
}

func (T *Capturer) WritePacket(packet fed.Packet) error {
	T.Packets = append(T.Packets, bytes.Clone(packet))
Garet Halliday's avatar
Garet Halliday committed
	return nil
}

func (T *Capturer) Check(other *Capturer) error {
	if len(T.Packets) != len(other.Packets) {
Garet Halliday's avatar
Garet Halliday committed
		return fmt.Errorf("wrong number of packets! got %d but expected %d", len(other.Packets), len(T.Packets))
Garet Halliday's avatar
Garet Halliday committed
	}

	for i := range T.Packets {
		expected := T.Packets[i]
		actual := other.Packets[i]

		if !bytes.Equal(expected.Bytes(), actual.Bytes()) {
			return fmt.Errorf("mismatched packet! expected %v but got %v", expected.Bytes(), actual.Bytes())
		}
	}

	return nil
}

var _ gsql.ResultWriter = (*Capturer)(nil)