go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

mem_test.go (1327B)


      1 package resource
      2 
      3 import (
      4 	"bytes"
      5 	"context"
      6 	"fmt"
      7 	"testing"
      8 )
      9 
     10 func testEntry(ctx context.Context, sym string, input []byte) (Result, error) {
     11 	return Result{
     12 		Content: fmt.Sprintf("%sbar", input),
     13 	}, nil
     14 }
     15 
     16 func TestMemResourceTemplate(t *testing.T) {
     17 	rs := NewMemResource()
     18 	rs.AddTemplate("foo", "bar")
     19 
     20 	ctx := context.TODO()
     21 	r, err := rs.GetTemplate(ctx, "foo")
     22 	if err != nil {
     23 		t.Fatal(err)
     24 	}
     25 	if r != "bar" {
     26 		fmt.Errorf("expected 'bar', got %s", r)
     27 	}
     28 
     29 	_, err = rs.GetTemplate(ctx, "bar")
     30 	if err == nil {
     31 		t.Fatalf("expected error")
     32 	}
     33 }
     34 
     35 func TestMemResourceCode(t *testing.T) {
     36 	rs := NewMemResource()
     37 	rs.AddBytecode("foo", []byte("bar"))
     38 
     39 	r, err := rs.GetCode("foo")
     40 	if err != nil {
     41 		t.Fatal(err)
     42 	}
     43 	if !bytes.Equal(r, []byte("bar")) {
     44 		fmt.Errorf("expected 'bar', got %x", r)
     45 	}
     46 
     47 	_, err = rs.GetCode("bar")
     48 	if err == nil {
     49 		t.Fatalf("expected error")
     50 	}
     51 }
     52 
     53 func TestMemResourceEntry(t *testing.T) {
     54 	rs := NewMemResource()
     55 	rs.AddEntryFunc("foo", testEntry)
     56 
     57 	fn, err := rs.FuncFor("foo")
     58 	if err != nil {
     59 		t.Fatal(err)
     60 	}
     61 
     62 	ctx := context.TODO()
     63 	r, err := fn(ctx, "foo", []byte("xyzzy"))
     64 	if err != nil {
     65 		t.Fatal(err)
     66 	}
     67 	if r.Content != "foobar" {
     68 		fmt.Errorf("expected 'foobar', got %x", r.Content)
     69 	}
     70 
     71 	_, err = rs.FuncFor("bar")
     72 	if err == nil {
     73 		t.Fatalf("expected error")
     74 	}
     75 }