go.ligato.io/vpp-agent/v3@v3.5.0/plugins/kvscheduler/internal/test/flag.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 // A set of node flags used for testing of the Graph. 18 19 // TestingFlag as a base for flags in the UTs. 20 type TestingFlag struct { 21 Name, Value string 22 Index int 23 } 24 25 // GetIndex returns the assigned index. 26 func (flag *TestingFlag) GetIndex() int { 27 return flag.Index 28 } 29 30 // GetName returns the assigned name. 31 func (flag *TestingFlag) GetName() string { 32 return flag.Name 33 } 34 35 // GetValue returns the assigned value. 36 func (flag *TestingFlag) GetValue() string { 37 return flag.Value 38 } 39 40 // Color is a property to be assigned to nodes for testing purposes. 41 type Color int 42 43 const ( 44 // Red color. 45 Red Color = iota 46 // Blue color. 47 Blue 48 // Green color. 49 Green 50 ) 51 52 // String converts color to string. 53 func (color Color) String() string { 54 switch color { 55 case Red: 56 return "red" 57 case Blue: 58 return "blue" 59 case Green: 60 return "green" 61 } 62 return "unknown" 63 } 64 65 // ColorFlagName is the name of the color flag. 66 const ColorFlagName = "color" 67 // ColorFlagIndex is the index of the color flag. 68 const ColorFlagIndex = 0 69 70 // ColorFlagImpl implements flag used in UTs to associate "color" with nodes. 71 type ColorFlagImpl struct { 72 TestingFlag 73 Color Color 74 } 75 76 // ColorFlag returns a new instance of color flag for testing. 77 func ColorFlag(color Color) *ColorFlagImpl { 78 return &ColorFlagImpl{ 79 TestingFlag: TestingFlag{ 80 Index: ColorFlagIndex, 81 Name: ColorFlagName, 82 Value: color.String(), 83 }, 84 Color: color, 85 } 86 } 87 88 // AnyColorFlag can be used to match nodes with any color assigned. 89 func AnyColorFlag() *ColorFlagImpl { 90 return &ColorFlagImpl{ 91 TestingFlag: TestingFlag{ 92 Index: ColorFlagIndex, 93 Name: ColorFlagName, 94 Value: "", 95 }, 96 } 97 } 98 99 // AbstractFlagName is the name of the abstract flag. 100 const AbstractFlagName = "is-abstract" 101 // AbstractFlagIndex is the index of the abstract flag. 102 const AbstractFlagIndex = 1 103 104 // AbstractFlagImpl is used in UTs to mark "abstract" key-value pairs. 105 type AbstractFlagImpl struct { 106 TestingFlag 107 } 108 109 // AbstractFlag returns a new instance of AbstractFlag for testing. 110 func AbstractFlag() *AbstractFlagImpl { 111 return &AbstractFlagImpl{ 112 TestingFlag: TestingFlag{ 113 Index: AbstractFlagIndex, 114 Name: AbstractFlagName, 115 // empty value -> it is a boolean flag 116 }, 117 } 118 } 119 120 // TemporaryFlagName is the name of the temporary flag. 121 const TemporaryFlagName = "is-temporary" 122 // TemporaryFlagIndex is the index of the temporary flag. 123 const TemporaryFlagIndex = 2 124 125 // TemporaryFlagImpl is used in UTs to mark "temporary" key-value pairs. 126 type TemporaryFlagImpl struct { 127 TestingFlag 128 } 129 130 // TemporaryFlag returns a new instance of TemporaryFlag for testing. 131 func TemporaryFlag() *TemporaryFlagImpl { 132 return &TemporaryFlagImpl{ 133 TestingFlag: TestingFlag{ 134 Index: TemporaryFlagIndex, 135 Name: TemporaryFlagName, 136 // empty value -> it is a boolean flag 137 }, 138 } 139 }