From e8321441afb7491172919704278059f7c2991920 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Wed, 6 Nov 2013 00:10:16 +0000 Subject: [PATCH] add a stringer for ScriptClass --- script.go | 18 ++++++++++++++++++ script_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ test_coverage.txt | 21 +++++++++++---------- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/script.go b/script.go index 9aa814bb..a275aef4 100644 --- a/script.go +++ b/script.go @@ -146,6 +146,24 @@ const ( NonStandardTy // None of the above. ) +var scriptClassToName = []string{ + PubKeyTy: "pubkey", + PubKeyHashTy: "pubkeyhash", + ScriptHashTy: "scripthash", + MultiSigTy: "multisig", + NonStandardTy: "nonstandard", +} + +// String implements the Stringer interface by returning the name of +// the enum script class. If the enum is invalid then "Invalid" will be +// returned. +func (t ScriptClass) String() string { + if int(t) > len(scriptClassToName) || int(t) < 0 { + return "Invalid" + } + return scriptClassToName[t] +} + // Script is the virtual machine that executes btcscripts. type Script struct { scripts [][]parsedOpcode diff --git a/script_test.go b/script_test.go index 3962650b..7371761c 100644 --- a/script_test.go +++ b/script_test.go @@ -2664,3 +2664,50 @@ nexttest: } } } + +var classStringifyTests = []struct{ + name string + scriptclass btcscript.ScriptClass + stringed string +}{ + { + name: "pubkey", + scriptclass: btcscript.PubKeyTy, + stringed: "pubkey", + }, + { + name: "pubkeyhash", + scriptclass: btcscript.PubKeyHashTy, + stringed: "pubkeyhash", + }, + { + name: "scripthash", + scriptclass: btcscript.ScriptHashTy, + stringed: "scripthash", + }, + { + name: "multisigty", + scriptclass: btcscript.MultiSigTy, + stringed: "multisig", + }, + { + name: "nonstandard", + scriptclass: btcscript.NonStandardTy, + stringed: "nonstandard", + }, + { + name: "broken", + scriptclass: btcscript.ScriptClass(255), + stringed: "Invalid", + }, +} + +func TestStringifyClass(t *testing.T) { + for _, test := range classStringifyTests { + typeString := test.scriptclass.String() + if typeString != test.stringed { + t.Errorf("%s: got \"%s\" expected \"%s\"", test.name, + typeString, test.stringed) + } + } +} diff --git a/test_coverage.txt b/test_coverage.txt index 0ac26ed4..f0709775 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -84,24 +84,24 @@ github.com/conformal/btcscript/script.go Script.DisasmPC 100.00% (4/4) github.com/conformal/btcscript/stack.go Stack.PeekBool 100.00% (4/4) github.com/conformal/btcscript/script.go getStack 100.00% (4/4) github.com/conformal/btcscript/script.go IsPayToScriptHash 100.00% (4/4) -github.com/conformal/btcscript/address.go ScriptType.String 100.00% (3/3) -github.com/conformal/btcscript/script.go setStack 100.00% (3/3) -github.com/conformal/btcscript/script.go scriptUInt16 100.00% (3/3) -github.com/conformal/btcscript/stack.go fromBool 100.00% (3/3) github.com/conformal/btcscript/script.go scriptUInt8 100.00% (3/3) +github.com/conformal/btcscript/script.go setStack 100.00% (3/3) +github.com/conformal/btcscript/stack.go fromBool 100.00% (3/3) +github.com/conformal/btcscript/script.go scriptUInt16 100.00% (3/3) github.com/conformal/btcscript/script.go scriptUInt32 100.00% (3/3) -github.com/conformal/btcscript/opcode.go calcHash 100.00% (2/2) +github.com/conformal/btcscript/address.go ScriptType.String 100.00% (3/3) +github.com/conformal/btcscript/script.go ScriptClass.String 100.00% (3/3) +github.com/conformal/btcscript/stack.go Stack.NipN 100.00% (2/2) +github.com/conformal/btcscript/address.go ScriptToAddrHash 100.00% (2/2) github.com/conformal/btcscript/script.go GetSigOpCount 100.00% (2/2) +github.com/conformal/btcscript/opcode.go calcHash 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeCodeSeparator 100.00% (2/2) +github.com/conformal/btcscript/script.go PayToPubKeyHashScript 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeDepth 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeN 100.00% (2/2) -github.com/conformal/btcscript/script.go PayToPubKeyHashScript 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcode1Negate 100.00% (2/2) github.com/conformal/btcscript/stack.go Stack.Depth 100.00% (2/2) github.com/conformal/btcscript/opcode.go opcodeFalse 100.00% (2/2) -github.com/conformal/btcscript/stack.go Stack.NipN 100.00% (2/2) -github.com/conformal/btcscript/address.go ScriptToAddrHash 100.00% (2/2) -github.com/conformal/btcscript/script.go isPubkeyHash 100.00% (1/1) github.com/conformal/btcscript/script.go isScriptHash 100.00% (1/1) github.com/conformal/btcscript/script.go SignatureScript 100.00% (1/1) github.com/conformal/btcscript/script.go parseScript 100.00% (1/1) @@ -131,6 +131,7 @@ github.com/conformal/btcscript/opcode.go opcode2Drop 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeReturn 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeNop 100.00% (1/1) github.com/conformal/btcscript/script.go isPubkey 100.00% (1/1) +github.com/conformal/btcscript/script.go isPubkeyHash 100.00% (1/1) github.com/conformal/btcscript/opcode.go opcodeOver 100.00% (1/1) github.com/conformal/btcscript/script.go Script.GetStack 100.00% (1/1) github.com/conformal/btcscript/opcode.go calcHash160 100.00% (1/1) @@ -149,5 +150,5 @@ github.com/conformal/btcscript/script.go Script.Execute 44.44% (8/18) github.com/conformal/btcscript/log.go SetLogWriter 0.00% (0/7) github.com/conformal/btcscript/script.go IsPushOnlyScript 0.00% (0/4) github.com/conformal/btcscript/log.go logClosure.String 0.00% (0/1) -github.com/conformal/btcscript --------------------------- 96.58% (989/1024) +github.com/conformal/btcscript --------------------------- 96.59% (992/1027)