github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/utils/testing/rec.go (about)

     1  /*
     2  Copyright 2018 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testing
    18  
    19  import "strings"
    20  
    21  // Record denotes an item saved by Recorder's Rec method.
    22  type Record struct {
    23  	Name  string      `json:"name"`
    24  	Value interface{} `json:"value,omitempty"`
    25  }
    26  
    27  // Recorder is used to record various events for use in tests.
    28  type Recorder interface {
    29  	// Rec adds an item to Recorder using the specified name and data.
    30  	Rec(name string, data interface{})
    31  }
    32  
    33  type nullRecorder struct{}
    34  
    35  func (r *nullRecorder) Rec(name string, data interface{}) {}
    36  
    37  // NullRecorder ignores all the items passed to it via Rec.
    38  var NullRecorder = &nullRecorder{}
    39  
    40  // TopLevelRecorder records the items as-is, optionally
    41  // applying filters to them. It can also create child
    42  // recorders and give back its current contents.
    43  type TopLevelRecorder struct {
    44  	recs    []*Record
    45  	filters []string
    46  }
    47  
    48  // NewToplevelRecorder creates a new TopLevelRecorder.
    49  func NewToplevelRecorder() *TopLevelRecorder {
    50  	return &TopLevelRecorder{}
    51  }
    52  
    53  // Rec implements Rec method of Recorder interface.
    54  func (r *TopLevelRecorder) Rec(name string, value interface{}) {
    55  	if r.nameMatches(name) {
    56  		r.recs = append(r.recs, &Record{Name: name, Value: value})
    57  	}
    58  }
    59  
    60  // Content returns the current contents of the recorder.
    61  func (r *TopLevelRecorder) Content() []*Record {
    62  	return r.recs
    63  }
    64  
    65  // Child creates a Child recorder for this TopLevelRecorder that will
    66  // add the specified prefix (plus ": ") to the names of items it
    67  // records.
    68  func (r *TopLevelRecorder) Child(prefix string) *ChildRecorder {
    69  	return NewChildRecorder(r, prefix)
    70  }
    71  
    72  // AddFilter adds a new filter substring to the TopLevelRecorder.  If
    73  // any filter substrings are specified, Rec will ignore items with
    74  // names that don't include any of these substrings (i.e. the filters
    75  // are ORed)
    76  func (r *TopLevelRecorder) AddFilter(filter string) {
    77  	r.filters = append(r.filters, filter)
    78  }
    79  
    80  func (r *TopLevelRecorder) nameMatches(name string) bool {
    81  	if len(r.filters) == 0 {
    82  		return true
    83  	}
    84  	for _, f := range r.filters {
    85  		if strings.Contains(name, f) {
    86  			return true
    87  		}
    88  	}
    89  	return false
    90  }
    91  
    92  // ChildRecorder is a recorder that prefixes the names of its items
    93  // with the specified prefix and then passes them to its parent
    94  // recorder.
    95  type ChildRecorder struct {
    96  	parent Recorder
    97  	prefix string
    98  }
    99  
   100  // NewChildRecorder creates a new ChildRecorder.
   101  func NewChildRecorder(parent Recorder, prefix string) *ChildRecorder {
   102  	return &ChildRecorder{parent: parent, prefix: prefix}
   103  }
   104  
   105  // Rec implements Rec method of Recorder interface.
   106  func (r *ChildRecorder) Rec(name string, data interface{}) {
   107  	r.parent.Rec(r.prefix+": "+name, data)
   108  }
   109  
   110  // Child creates a Child recorder for this recorder.
   111  func (r *ChildRecorder) Child(prefix string) *ChildRecorder {
   112  	return NewChildRecorder(r, prefix)
   113  }