MCPcopy Create free account
hub / github.com/btcsuite/btcd / TestScriptBuilderAddOp

Function TestScriptBuilderAddOp

txscript/scriptbuilder_test.go:44–109  ·  view source on GitHub ↗

TestScriptBuilderAddOp tests that pushing opcodes to a script via the ScriptBuilder API works as expected.

(t *testing.T)

Source from the content-addressed store, hash-verified

42// TestScriptBuilderAddOp tests that pushing opcodes to a script via the
43// ScriptBuilder API works as expected.
44func TestScriptBuilderAddOp(t *testing.T) {
45 t.Parallel()
46
47 tests := []struct {
48 name string
49 opcodes []byte
50 expected []byte
51 }{
52 {
53 name: "push OP_0",
54 opcodes: []byte{OP_0},
55 expected: []byte{OP_0},
56 },
57 {
58 name: "push OP_1 OP_2",
59 opcodes: []byte{OP_1, OP_2},
60 expected: []byte{OP_1, OP_2},
61 },
62 {
63 name: "push OP_HASH160 OP_EQUAL",
64 opcodes: []byte{OP_HASH160, OP_EQUAL},
65 expected: []byte{OP_HASH160, OP_EQUAL},
66 },
67 }
68
69 // Run tests and individually add each op via AddOp.
70 builder := NewScriptBuilder()
71 t.Logf("Running %d tests", len(tests))
72 for i, test := range tests {
73 builder.Reset()
74 for _, opcode := range test.opcodes {
75 builder.AddOp(opcode)
76 }
77 result, err := builder.Script()
78 if err != nil {
79 t.Errorf("ScriptBuilder.AddOp #%d (%s) unexpected "+
80 "error: %v", i, test.name, err)
81 continue
82 }
83 if !bytes.Equal(result, test.expected) {
84 t.Errorf("ScriptBuilder.AddOp #%d (%s) wrong result\n"+
85 "got: %x\nwant: %x", i, test.name, result,
86 test.expected)
87 continue
88 }
89 }
90
91 // Run tests and bulk add ops via AddOps.
92 t.Logf("Running %d tests", len(tests))
93 for i, test := range tests {
94 builder.Reset()
95 result, err := builder.AddOps(test.opcodes).Script()
96 if err != nil {
97 t.Errorf("ScriptBuilder.AddOps #%d (%s) unexpected "+
98 "error: %v", i, test.name, err)
99 continue
100 }
101 if !bytes.Equal(result, test.expected) {

Callers

nothing calls this directly

Calls 6

ResetMethod · 0.95
AddOpMethod · 0.95
ScriptMethod · 0.95
AddOpsMethod · 0.95
NewScriptBuilderFunction · 0.85
ScriptMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…