From 698c647f07873b1b648e5865c51f2c0d06bc5831 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 3 Feb 2020 17:50:25 +0300 Subject: [PATCH] emit: refactor tests Add structure and call Bytes() method on buffer once. --- pkg/vm/emit/emit_test.go | 50 +++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/pkg/vm/emit/emit_test.go b/pkg/vm/emit/emit_test.go index 487cbc35a..f56b37226 100644 --- a/pkg/vm/emit/emit_test.go +++ b/pkg/vm/emit/emit_test.go @@ -10,25 +10,37 @@ import ( ) func TestEmitInt(t *testing.T) { - buf := new(bytes.Buffer) - Int(buf, 10) - assert.Equal(t, opcode.Opcode(buf.Bytes()[0]), opcode.PUSH10) - buf.Reset() - Int(buf, 100) - assert.Equal(t, buf.Bytes()[0], uint8(1)) - assert.Equal(t, buf.Bytes()[1], uint8(100)) - buf.Reset() - Int(buf, 1000) - assert.Equal(t, buf.Bytes()[0], uint8(2)) - assert.Equal(t, buf.Bytes()[1:3], []byte{0xe8, 0x03}) + t.Run("1-byte int", func(t *testing.T) { + buf := new(bytes.Buffer) + Int(buf, 10) + result := buf.Bytes() + assert.EqualValues(t, opcode.PUSH10, result[0]) + }) + + t.Run("2-byte int", func(t *testing.T) { + buf := new(bytes.Buffer) + Int(buf, 100) + result := buf.Bytes() + assert.EqualValues(t, opcode.PUSHBYTES1, result[0]) + assert.EqualValues(t, 100, result[1]) + }) + + t.Run("4-byte int", func(t *testing.T) { + buf := new(bytes.Buffer) + Int(buf, 1000) + result := buf.Bytes() + assert.EqualValues(t, opcode.PUSHBYTES2, result[0]) + assert.EqualValues(t, 1000, binary.LittleEndian.Uint16(result[1:3])) + }) } func TestEmitBool(t *testing.T) { buf := new(bytes.Buffer) Bool(buf, true) Bool(buf, false) - assert.Equal(t, opcode.Opcode(buf.Bytes()[0]), opcode.PUSH1) - assert.Equal(t, opcode.Opcode(buf.Bytes()[1]), opcode.PUSH0) + result := buf.Bytes() + assert.Equal(t, opcode.Opcode(result[0]), opcode.PUSH1) + assert.Equal(t, opcode.Opcode(result[1]), opcode.PUSH0) } func TestEmitString(t *testing.T) { @@ -49,9 +61,10 @@ func TestEmitSyscall(t *testing.T) { buf := new(bytes.Buffer) for _, syscall := range syscalls { Syscall(buf, syscall) - assert.Equal(t, opcode.Opcode(buf.Bytes()[0]), opcode.SYSCALL) - assert.Equal(t, buf.Bytes()[1], uint8(len(syscall))) - assert.Equal(t, buf.Bytes()[2:], []byte(syscall)) + result := buf.Bytes() + assert.Equal(t, opcode.Opcode(result[0]), opcode.SYSCALL) + assert.Equal(t, result[1], uint8(len(syscall))) + assert.Equal(t, result[2:], []byte(syscall)) buf.Reset() } } @@ -59,7 +72,8 @@ func TestEmitSyscall(t *testing.T) { func TestEmitCall(t *testing.T) { buf := new(bytes.Buffer) Call(buf, opcode.JMP, 100) - assert.Equal(t, opcode.Opcode(buf.Bytes()[0]), opcode.JMP) - label := binary.LittleEndian.Uint16(buf.Bytes()[1:3]) + result := buf.Bytes() + assert.Equal(t, opcode.Opcode(result[0]), opcode.JMP) + label := binary.LittleEndian.Uint16(result[1:3]) assert.Equal(t, label, uint16(100)) }