[#XX] Added basic coverage functionality to PutNumber()

It probably doesn't work properly.

Signed-off-by: Lebedeva Ekaterina <ekaterina.lebedeva@yadro.com>
This commit is contained in:
Ekaterina Lebedeva 2023-07-26 17:15:56 +03:00
parent 3e8e119a22
commit 0f7bbb95dc
3 changed files with 118 additions and 13 deletions

View file

@ -18,12 +18,7 @@ func GetNumber(key []byte) int {
if len(key) != keySize { if len(key) != keySize {
panic("Invalid key size") panic("Invalid key size")
} }
// var num int
ctx := storage.GetContext() ctx := storage.GetContext()
// it := storage.Find(ctx, 123, storage.ValuesOnly)
// for iterator.Next(it) {
// num = iterator.Value(it).(int)
// }
num := storage.Get(ctx, append([]byte{prefixNumber}, key...)) num := storage.Get(ctx, append([]byte{prefixNumber}, key...))
if num == nil { if num == nil {
panic("Cannot get number") panic("Cannot get number")

View file

@ -4,9 +4,11 @@ go 1.20
require ( require (
github.com/nspcc-dev/neo-go v0.101.3 github.com/nspcc-dev/neo-go v0.101.3
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20221202075445-cb5c18dc73eb //github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20221202075445-cb5c18dc73eb
) )
//replace github.com/nspcc-dev/neo-go => /Users/ekaterina.lebedeva/neo-go-0.101.3/
require ( require (
github.com/benbjohnson/clock v1.1.0 // indirect github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
@ -49,5 +51,3 @@ require (
google.golang.org/protobuf v1.28.1 // indirect google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
replace github.com/nspcc-dev/neo-go => /Users/ekaterina.lebedeva/neo-go-0.101.3/

View file

@ -4,14 +4,17 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"strings"
"testing" "testing"
_ "github.com/nspcc-dev/neo-go/pkg/compiler" "github.com/davecgh/go-spew/spew"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/neotest" "github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/neotest/chain" "github.com/nspcc-dev/neo-go/pkg/neotest/chain"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require"
) )
const ctrPath = "../contract" const ctrPath = "../contract"
@ -35,14 +38,121 @@ func TestContract_GetNumber(t *testing.T) {
inv.InvokeFail(t, "Cannot get number", "getNumber", validKey) inv.InvokeFail(t, "Cannot get number", "getNumber", validKey)
} }
type Coverage struct {
Document string
MethodID string
StartLine int
StartCol int
EndLine int
EndCol int
Counter int
}
func TestContract_PutNumber(t *testing.T) { func TestContract_PutNumber(t *testing.T) {
e := newExecutor(t) e := newExecutor(t)
ctrPutGetNum := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "neo-go.yml")) ctrPutGetNum := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "neo-go.yml"))
e.DeployContract(t, ctrPutGetNum, nil) e.DeployContract(t, ctrPutGetNum, nil)
inv := e.CommitteeInvoker(ctrPutGetNum.Hash) di := ctrPutGetNum.DebugInfo
inv.Invoke(t, stackitem.Null{}, "putNumber", validKey, 42)
inv.Invoke(t, 42, "getNumber", validKey) // Find contract.go doc
iDoc := len(di.Documents)
for i, cDoc := range di.Documents {
if strings.Contains(cDoc, "contract.go") {
iDoc = i
}
}
if iDoc == len(di.Documents) {
fmt.Fprintf(os.Stderr, "No such file\n")
t.FailNow()
}
// Get sequence points for current method
iPutMethod := len(di.Methods)
for i := 0; i < len(di.Methods); i++ {
if di.Methods[i].ID == "PutNumber" {
iPutMethod = i
break
}
}
if iPutMethod == len(di.Methods) {
fmt.Fprintf(os.Stderr, "No \"PutNumber\" method in %s contract\n", ctrPutGetNum.Manifest.Name)
t.FailNow()
}
//seqPoints := make([]compiler.DebugSeqPoint, len(di.Methods[iPutMethod].SeqPoints))
//copy(seqPoints, di.Methods[iPutMethod].SeqPoints)
var seqPoints []compiler.DebugSeqPoint
for _, curPoint := range di.Methods[iPutMethod].SeqPoints {
if curPoint.Document == iDoc {
seqPoints = append(seqPoints, curPoint)
}
}
spew.Dump(seqPoints)
ic, err := e.Chain.GetTestVM(trigger.Application, nil, nil)
require.NoError(t, err)
fmt.Println(ic.VM.SyscallHandler == nil)
ic.VM.LoadNEFMethod(ctrPutGetNum.NEF, ctrPutGetNum.Hash, ctrPutGetNum.Hash, callflag.All, false, 101, -1, nil)
ic.VM.Context().Estack().PushVal(2)
ic.VM.Context().Estack().PushVal(validKey)
fmt.Println("\nPrinting debug info for PutNumber:")
for !ic.VM.HasStopped() {
_, curInstr := ic.VM.Context().NextInstr()
for i := 0; i < len(seqPoints); i++ {
if int(curInstr) == seqPoints[i].Opcode {
fmt.Printf("%s: %d.%d, %d.%d hooray!!\n", di.Documents[iDoc], seqPoints[i].StartLine, seqPoints[i].StartCol, seqPoints[i].EndLine, seqPoints[i].EndCol)
}
}
//fmt.Println(nStr, " ", curInstr)
ic.VM.Step()
}
if ic.VM.HasFailed() {
//govm.SyscallHandler()
fmt.Fprintln(os.Stderr, "Something went wrong")
t.FailNow()
}
fmt.Println("\n Done printing!")
// e.DeployContract(t, ctrPutGetNum, nil)
// // inv := e.CommitteeInvoker(ctrPutGetNum.Hash)
// // inv.Invoke(t, stackitem.Null{}, "putNumber", validKey, 42)
// // inv.Invoke(t, 42, "getNumber", validKey)
// fmt.Println("\n I'm printing debug info for compiled file!")
// spew.Dump(ctrPutGetNum.DebugInfo)
// fmt.Println()
// //fmt.Println(json.MarshalIndent(ctrPutGetNum.Manifest, "", " "))
// govm := vm.New()
// govm.LoadNEFMethod(ctrPutGetNum.NEF, ctrPutGetNum.Hash, ctrPutGetNum.Hash, callflag.All, false, 101, -1, nil)
// fmt.Println("PrintOps:")
// govm.PrintOps(os.Stdout)
// govm.Context().Estack().PushVal(2)
// govm.Context().Estack().PushVal(validKey)
// fmt.Println("\nPrinting opcodes for PutNumber:")
// for !govm.HasStopped() {
// nStr, curInstr := govm.Context().NextInstr()
// fmt.Println(nStr, " ", curInstr)
// govm.Step()
// }
// if govm.HasFailed() {
// //govm.SyscallHandler()
// fmt.Fprintln(os.Stderr, "Something went wrong")
// t.FailNow()
// }
// govm.LoadNEFMethod(ctrPutGetNum.NEF, ctrPutGetNum.Hash, ctrPutGetNum.Hash, callflag.All, true, 0, -1, nil)
// govm.Context().Estack().PushVal(validKey)
// fmt.Println("\nPrinting opcodes for GetNumber:")
// for !govm.HasStopped() {
// nStr, curInstr := govm.Context().NextInstr()
// fmt.Println(nStr, " ", curInstr)
// govm.Step()
// }
// res := govm.Estack().Pop().Value()
// fmt.Println("\nresult = ", res)
// fmt.Println("\n Done printing!")
} }
func TestContract_InvalidKey(t *testing.T) { func TestContract_InvalidKey(t *testing.T) {