| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package config |
| 6 |
| 7 import ( |
| 8 "testing" |
| 9 |
| 10 "golang.org/x/net/context" |
| 11 |
| 12 . "github.com/smartystreets/goconvey/convey" |
| 13 ) |
| 14 |
| 15 // retainingBackend is a simple Backend implementation that retains the last set |
| 16 // of data that it received. |
| 17 type retainingBackend struct { |
| 18 Backend |
| 19 |
| 20 lastItems []*Item |
| 21 lastErr error |
| 22 } |
| 23 |
| 24 func (b *retainingBackend) Get(c context.Context, configSet, path string, p Para
ms) (*Item, error) { |
| 25 var item *Item |
| 26 item, b.lastErr = b.Backend.Get(c, configSet, path, p) |
| 27 b.lastItems = []*Item{item} |
| 28 return item, b.lastErr |
| 29 } |
| 30 |
| 31 // GetAll implements Backend. |
| 32 func (b *retainingBackend) GetAll(c context.Context, t GetAllType, path string,
p Params) ([]*Item, error) { |
| 33 b.lastItems, b.lastErr = b.Backend.GetAll(c, t, path, p) |
| 34 return b.lastItems, b.lastErr |
| 35 } |
| 36 |
| 37 type customFormatter string |
| 38 |
| 39 func (cf customFormatter) FormatItem(c string, fd string) (string, error) { |
| 40 return string(cf), nil |
| 41 } |
| 42 |
| 43 type retainingResolver struct { |
| 44 format string |
| 45 item *Item |
| 46 } |
| 47 |
| 48 func (rr *retainingResolver) Format() (string, string) { return rr.format, "" } |
| 49 func (rr *retainingResolver) Resolve(it *Item) error { |
| 50 rr.item = it |
| 51 return nil |
| 52 } |
| 53 |
| 54 type panicFormatter struct{} |
| 55 |
| 56 func (pf panicFormatter) FormatItem(string, string) (string, error) { panic("pan
ic") } |
| 57 |
| 58 func TestFormatBackend(t *testing.T) { |
| 59 t.Parallel() |
| 60 |
| 61 Convey(`A testing environment`, t, func() { |
| 62 tb := testingBackend{ |
| 63 items: []*Item{ |
| 64 {Meta: Meta{"projects/foo", "path", "####", "v1"
}, Content: "foo"}, |
| 65 {Meta: Meta{"projects/bar", "path", "####", "v1"
}, Content: "bar"}, |
| 66 }, |
| 67 } |
| 68 |
| 69 // Pass all things from the backend through the formatter. |
| 70 var fr FormatterRegistry |
| 71 fb := FormatBackend{ |
| 72 Backend: &tb, |
| 73 GetRegistry: func(context.Context) *FormatterRegistry {
return &fr }, |
| 74 } |
| 75 |
| 76 // Retain all raw Items that were returned for examination. |
| 77 rb := retainingBackend{ |
| 78 Backend: &fb, |
| 79 } |
| 80 |
| 81 c := context.Background() |
| 82 c = WithBackend(c, &rb) |
| 83 |
| 84 Convey(`Will panic if an attempt to register empty key is made.`
, func() { |
| 85 So(func() { fr.Register("", nil) }, ShouldPanic) |
| 86 }) |
| 87 |
| 88 Convey(`Will ignore items that have already been formatted.`, fu
nc() { |
| 89 rr := retainingResolver{"test", nil} |
| 90 fr.Register("test", panicFormatter{}) |
| 91 |
| 92 // Confirm that this setup correctly attempts to format
the item. |
| 93 So(func() { Get(c, AsService, "", "", &rr, nil) }, Shoul
dPanic) |
| 94 |
| 95 // Now, pretend the item is already formatter. We should
not get a panic. |
| 96 tb.items[0].Format = "something" |
| 97 So(Get(c, AsService, "", "", &rr, nil), ShouldBeNil) |
| 98 }) |
| 99 |
| 100 Convey(`Testing custom formatter`, func() { |
| 101 cf := customFormatter("content") |
| 102 fr.Register("test", cf) |
| 103 |
| 104 // Confirm that this setup correctly attempts to format
the item. |
| 105 rr := retainingResolver{"test", nil} |
| 106 So(Get(c, AsService, "", "", &rr, nil), ShouldBeNil) |
| 107 So(rr.item, ShouldNotBeNil) |
| 108 So(rr.item.Format, ShouldEqual, "test") |
| 109 So(rr.item.Content, ShouldEqual, "content") |
| 110 }) |
| 111 }) |
| 112 } |
| OLD | NEW |