Repository navigation
Expand file tree
/
Copy pathbytecode_version_test.go
More file actions
62 lines (51 loc) · 2.13 KB
/
Copy pathbytecode_version_test.go
File metadata and controls
62 lines (51 loc) · 2.13 KB
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
package numscript_test
import (
"bytes"
"encoding/binary"
"testing"
"github.com/formancehq/numscript"
"github.com/stretchr/testify/require"
)
// The public surface a host needs to keep stored bytecode and the executing
// build in step: the current version, the version stamped on what Compile and
// Encode produce, a peek that reads it off raw bytes, and the typed rejection.
func TestBytecodeVersionPublicAPI(t *testing.T) {
varsEncoder, program, err := numscript.Compile(`vars {
monetary $amt
}
send $amt (
source = @src
destination = @dst
)`)
require.NoError(t, err)
require.Equal(t, numscript.CurrentBytecodeVersion, program.Version)
programBytes := program.Encode()
peeked, err := numscript.PeekCompiledProgramVersion(programBytes)
require.NoError(t, err)
require.Equal(t, numscript.CurrentBytecodeVersion, peeked)
decoded, err := numscript.DecodeCompiledProgram(programBytes)
require.NoError(t, err)
require.Equal(t, numscript.CurrentBytecodeVersion, decoded.Version)
vars, err := varsEncoder.Encode(map[string]string{"amt": "USD/2 100"})
require.NoError(t, err)
require.Equal(t, numscript.CurrentBytecodeVersion, vars.Version)
varsBytes := vars.Encode()
peeked, err = numscript.PeekVarsVersion(varsBytes)
require.NoError(t, err)
require.Equal(t, numscript.CurrentBytecodeVersion, peeked)
// A blob from the next major is reported, not misread: the peek still
// tells which version it is, the decoders reject it with the typed error.
next := numscript.BytecodeVersion{Major: numscript.CurrentBytecodeVersion.Major + 1}
foreign := bytes.Clone(programBytes)
binary.LittleEndian.PutUint16(foreign[4:], next.Major)
binary.LittleEndian.PutUint16(foreign[6:], next.Minor)
peeked, err = numscript.PeekCompiledProgramVersion(foreign)
require.NoError(t, err)
require.Equal(t, next, peeked)
require.False(t, numscript.CurrentBytecodeVersion.CanRead(peeked))
_, err = numscript.DecodeCompiledProgram(foreign)
var unsupported numscript.UnsupportedBytecodeVersionError
require.ErrorAs(t, err, &unsupported)
require.Equal(t, next, unsupported.Encoded)
require.Equal(t, numscript.CurrentBytecodeVersion, unsupported.Supported)
}