testutil

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package testutil provides shared test helpers for solana-go. It is intended for use in _test packages only; import it with

import "github.com/MevYu/solana-go/internal/testutil"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustHash

func MustHash(t testing.TB, s string) solana.Hash

MustHash decodes a base58 hash and calls t.Fatal on failure.

func MustNewKeypair

func MustNewKeypair(t testing.TB) (solana.PublicKey, ed25519.PrivateKey)

MustNewKeypair generates a fresh Ed25519 keypair for use in tests. It calls t.Fatal on failure.

func MustPublicKey

func MustPublicKey(t testing.TB, s string) solana.PublicKey

MustPublicKey decodes a base58 public key and calls t.Fatal on failure.

func NewMockRPCClient

func NewMockRPCClient(t testing.TB, handler RPCHandler) *rpc.Client

NewMockRPCClient creates a mock HTTP server using handler and returns an rpc.Client pointed at it.

func NewMockRPCServer

func NewMockRPCServer(t testing.TB, handler RPCHandler) *httptest.Server

NewMockRPCServer starts an httptest.Server that answers JSON-RPC 2.0 requests by dispatching to handler. It registers a cleanup function that stops the server when the test ends.

Example:

srv := testutil.NewMockRPCServer(t, func(method string, params json.RawMessage) (any, error) {
    if method == "getBalance" {
        return map[string]any{"context": map[string]any{"slot": 100}, "value": uint64(500)}, nil
    }
    return nil, fmt.Errorf("unexpected method %s", method)
})
client := rpc.NewClient(srv.URL, jsonrpc.Config{})

func ZeroHash

func ZeroHash() solana.Hash

ZeroHash returns a Hash with all bytes set to zero. Useful as a placeholder blockhash in unit tests that don't exercise blockhash validation.

Types

type RPCHandler

type RPCHandler func(method string, params json.RawMessage) (any, error)

RPCHandler is a function that handles a single JSON-RPC method call in a mock server. It receives the method name and the raw params JSON, and returns a result value to be serialised into the response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL