|
4 | 4 | package pprofile |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "errors" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | func TestFunctionEqual(t *testing.T) { |
@@ -63,6 +65,193 @@ func TestFunctionEqual(t *testing.T) { |
63 | 65 | } |
64 | 66 | } |
65 | 67 |
|
| 68 | +func TestFunctionSwitchDictionary(t *testing.T) { |
| 69 | + for _, tt := range []struct { |
| 70 | + name string |
| 71 | + function Function |
| 72 | + |
| 73 | + src ProfilesDictionary |
| 74 | + dst ProfilesDictionary |
| 75 | + |
| 76 | + wantFunction Function |
| 77 | + wantDictionary ProfilesDictionary |
| 78 | + wantErr error |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "with an empty key value and unit", |
| 82 | + function: NewFunction(), |
| 83 | + |
| 84 | + src: NewProfilesDictionary(), |
| 85 | + dst: NewProfilesDictionary(), |
| 86 | + |
| 87 | + wantFunction: NewFunction(), |
| 88 | + wantDictionary: NewProfilesDictionary(), |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "with an existing name", |
| 92 | + function: func() Function { |
| 93 | + fn := NewFunction() |
| 94 | + fn.SetNameStrindex(1) |
| 95 | + return fn |
| 96 | + }(), |
| 97 | + |
| 98 | + src: func() ProfilesDictionary { |
| 99 | + d := NewProfilesDictionary() |
| 100 | + d.StringTable().Append("", "test") |
| 101 | + return d |
| 102 | + }(), |
| 103 | + dst: func() ProfilesDictionary { |
| 104 | + d := NewProfilesDictionary() |
| 105 | + d.StringTable().Append("", "foo") |
| 106 | + return d |
| 107 | + }(), |
| 108 | + |
| 109 | + wantFunction: func() Function { |
| 110 | + fn := NewFunction() |
| 111 | + fn.SetNameStrindex(2) |
| 112 | + return fn |
| 113 | + }(), |
| 114 | + wantDictionary: func() ProfilesDictionary { |
| 115 | + d := NewProfilesDictionary() |
| 116 | + d.StringTable().Append("", "foo", "test") |
| 117 | + return d |
| 118 | + }(), |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "with a name index that does not match anything", |
| 122 | + function: func() Function { |
| 123 | + fn := NewFunction() |
| 124 | + fn.SetNameStrindex(1) |
| 125 | + return fn |
| 126 | + }(), |
| 127 | + |
| 128 | + src: NewProfilesDictionary(), |
| 129 | + dst: NewProfilesDictionary(), |
| 130 | + |
| 131 | + wantFunction: func() Function { |
| 132 | + fn := NewFunction() |
| 133 | + fn.SetNameStrindex(1) |
| 134 | + return fn |
| 135 | + }(), |
| 136 | + wantDictionary: NewProfilesDictionary(), |
| 137 | + wantErr: errors.New("invalid name index 1"), |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "with an existing system name", |
| 141 | + function: func() Function { |
| 142 | + fn := NewFunction() |
| 143 | + fn.SetSystemNameStrindex(1) |
| 144 | + return fn |
| 145 | + }(), |
| 146 | + |
| 147 | + src: func() ProfilesDictionary { |
| 148 | + d := NewProfilesDictionary() |
| 149 | + d.StringTable().Append("", "test") |
| 150 | + return d |
| 151 | + }(), |
| 152 | + dst: func() ProfilesDictionary { |
| 153 | + d := NewProfilesDictionary() |
| 154 | + d.StringTable().Append("", "foo") |
| 155 | + return d |
| 156 | + }(), |
| 157 | + |
| 158 | + wantFunction: func() Function { |
| 159 | + fn := NewFunction() |
| 160 | + fn.SetSystemNameStrindex(2) |
| 161 | + return fn |
| 162 | + }(), |
| 163 | + wantDictionary: func() ProfilesDictionary { |
| 164 | + d := NewProfilesDictionary() |
| 165 | + d.StringTable().Append("", "foo", "test") |
| 166 | + return d |
| 167 | + }(), |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "with a system name index that does not match anything", |
| 171 | + function: func() Function { |
| 172 | + fn := NewFunction() |
| 173 | + fn.SetSystemNameStrindex(1) |
| 174 | + return fn |
| 175 | + }(), |
| 176 | + |
| 177 | + src: NewProfilesDictionary(), |
| 178 | + dst: NewProfilesDictionary(), |
| 179 | + |
| 180 | + wantFunction: func() Function { |
| 181 | + fn := NewFunction() |
| 182 | + fn.SetSystemNameStrindex(1) |
| 183 | + return fn |
| 184 | + }(), |
| 185 | + wantDictionary: NewProfilesDictionary(), |
| 186 | + wantErr: errors.New("invalid system name index 1"), |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "with an existing filename", |
| 190 | + function: func() Function { |
| 191 | + fn := NewFunction() |
| 192 | + fn.SetFilenameStrindex(1) |
| 193 | + return fn |
| 194 | + }(), |
| 195 | + |
| 196 | + src: func() ProfilesDictionary { |
| 197 | + d := NewProfilesDictionary() |
| 198 | + d.StringTable().Append("", "test") |
| 199 | + return d |
| 200 | + }(), |
| 201 | + dst: func() ProfilesDictionary { |
| 202 | + d := NewProfilesDictionary() |
| 203 | + d.StringTable().Append("", "foo") |
| 204 | + return d |
| 205 | + }(), |
| 206 | + |
| 207 | + wantFunction: func() Function { |
| 208 | + fn := NewFunction() |
| 209 | + fn.SetFilenameStrindex(2) |
| 210 | + return fn |
| 211 | + }(), |
| 212 | + wantDictionary: func() ProfilesDictionary { |
| 213 | + d := NewProfilesDictionary() |
| 214 | + d.StringTable().Append("", "foo", "test") |
| 215 | + return d |
| 216 | + }(), |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "with a filename index that does not match anything", |
| 220 | + function: func() Function { |
| 221 | + fn := NewFunction() |
| 222 | + fn.SetFilenameStrindex(1) |
| 223 | + return fn |
| 224 | + }(), |
| 225 | + |
| 226 | + src: NewProfilesDictionary(), |
| 227 | + dst: NewProfilesDictionary(), |
| 228 | + |
| 229 | + wantFunction: func() Function { |
| 230 | + fn := NewFunction() |
| 231 | + fn.SetFilenameStrindex(1) |
| 232 | + return fn |
| 233 | + }(), |
| 234 | + wantDictionary: NewProfilesDictionary(), |
| 235 | + wantErr: errors.New("invalid filename index 1"), |
| 236 | + }, |
| 237 | + } { |
| 238 | + t.Run(tt.name, func(t *testing.T) { |
| 239 | + fn := tt.function |
| 240 | + dst := tt.dst |
| 241 | + err := fn.switchDictionary(tt.src, dst) |
| 242 | + |
| 243 | + if tt.wantErr == nil { |
| 244 | + require.NoError(t, err) |
| 245 | + } else { |
| 246 | + require.Equal(t, tt.wantErr, err) |
| 247 | + } |
| 248 | + |
| 249 | + assert.Equal(t, tt.wantFunction, fn) |
| 250 | + assert.Equal(t, tt.wantDictionary, dst) |
| 251 | + }) |
| 252 | + } |
| 253 | +} |
| 254 | + |
66 | 255 | func buildFunction(name, sName, fileName int32, startLine int64) Function { |
67 | 256 | f := NewFunction() |
68 | 257 | f.SetNameStrindex(name) |
|
0 commit comments