resource_test.go (2279B)
1 package resource 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 ) 8 9 func codeGet(ctx context.Context, nodeSym string) ([]byte, error) { 10 switch nodeSym { 11 case "bar": 12 return []byte("deafbeef"), nil 13 } 14 return nil, errors.New("unknown code") 15 } 16 17 func menuGet(ctx context.Context, menuSym string) (string, error) { 18 switch menuSym { 19 case "baz": 20 return "xyzzy", nil 21 } 22 return "", errors.New("unknown code") 23 24 } 25 26 func templateGet(ctx context.Context, nodeSym string) (string, error) { 27 switch nodeSym { 28 case "tinkywinky": 29 return "inky pinky {{.foo}} blinky clyde", nil 30 } 31 return "", errors.New("unknown code") 32 } 33 34 func entryFunc(ctx context.Context, nodeSym string, input []byte) (Result, error) { 35 return Result{ 36 Content: "42", 37 }, nil 38 } 39 40 func funcGet(ctx context.Context, loadSym string) (EntryFunc, error) { 41 switch loadSym { 42 case "dipsy": 43 return entryFunc, nil 44 } 45 return nil, errors.New("unknown code") 46 } 47 48 49 50 func TestMenuResourceSetters(t *testing.T) { 51 var err error 52 ctx := context.Background() 53 rs := NewMenuResource() 54 _, err = rs.GetCode(ctx, "foo") 55 if err == nil { 56 errors.New("expected error") 57 } 58 59 rs.WithCodeGetter(codeGet) 60 _, err = rs.GetCode(ctx, "foo") 61 if err == nil { 62 errors.New("expected error") 63 } 64 65 rs.WithCodeGetter(codeGet) 66 _, err = rs.GetCode(ctx, "bar") 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 _, err = rs.GetMenu(ctx, "bar") 72 if err == nil { 73 errors.New("expected error") 74 } 75 76 rs.WithMenuGetter(menuGet) 77 _, err = rs.GetMenu(ctx, "bar") 78 if err == nil { 79 errors.New("expected error") 80 } 81 82 rs.WithMenuGetter(menuGet) 83 _, err = rs.GetMenu(ctx, "baz") 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 _, err = rs.GetTemplate(ctx, "baz") 89 if err == nil { 90 errors.New("expected error") 91 } 92 93 rs.WithTemplateGetter(templateGet) 94 _, err = rs.GetTemplate(ctx, "baz") 95 if err == nil { 96 errors.New("expected error") 97 } 98 99 rs.WithTemplateGetter(templateGet) 100 _, err = rs.GetTemplate(ctx, "tinkywinky") 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 _, err = rs.FuncFor(ctx, "tinkywinky") 106 if err == nil { 107 errors.New("expected error") 108 } 109 110 rs.WithEntryFuncGetter(funcGet) 111 _, err = rs.FuncFor(ctx, "tinkywinky") 112 if err == nil { 113 errors.New("expected error") 114 } 115 116 rs.WithEntryFuncGetter(funcGet) 117 _, err = rs.FuncFor(ctx, "dipsy") 118 if err != nil { 119 t.Fatal(err) 120 } 121 }