|
4 | 4 | package model |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
7 | 8 | "github.com/pb33f/libopenapi/index" |
8 | 9 | "testing" |
9 | 10 |
|
10 | 11 | "github.com/pb33f/libopenapi/datamodel/low" |
| 12 | + "github.com/pb33f/libopenapi/datamodel/low/base" |
11 | 13 | "github.com/pb33f/libopenapi/orderedmap" |
12 | 14 | "github.com/pb33f/libopenapi/utils" |
13 | 15 | "github.com/stretchr/testify/assert" |
@@ -237,6 +239,78 @@ func TestCheckMapForAdditionRemoval(t *testing.T) { |
237 | 239 | assert.Len(t, changes, 1) |
238 | 240 | } |
239 | 241 |
|
| 242 | +func buildTestExample(t *testing.T, raw string) *base.Example { |
| 243 | + t.Helper() |
| 244 | + |
| 245 | + var node yaml.Node |
| 246 | + assert.NoError(t, yaml.Unmarshal([]byte(raw), &node)) |
| 247 | + |
| 248 | + var example base.Example |
| 249 | + assert.NoError(t, low.BuildModel(node.Content[0], &example)) |
| 250 | + assert.NoError(t, example.Build(context.Background(), nil, node.Content[0], nil)) |
| 251 | + return &example |
| 252 | +} |
| 253 | + |
| 254 | +func TestOverrideChangeCollectionBreaking(t *testing.T) { |
| 255 | + overrideChangeCollectionBreaking(nil, true) |
| 256 | + overrideChangeCollectionBreaking("not-a-collection", true) |
| 257 | + |
| 258 | + changes := &ExampleChanges{ |
| 259 | + PropertyChanges: NewPropertyChanges([]*Change{ |
| 260 | + {Breaking: true}, |
| 261 | + {Breaking: false}, |
| 262 | + }), |
| 263 | + } |
| 264 | + |
| 265 | + overrideChangeCollectionBreaking(changes, false) |
| 266 | + assert.False(t, changes.Changes[0].Breaking) |
| 267 | + assert.False(t, changes.Changes[1].Breaking) |
| 268 | + |
| 269 | + overrideChangeCollectionBreaking(changes, true) |
| 270 | + assert.True(t, changes.Changes[0].Breaking) |
| 271 | + assert.True(t, changes.Changes[1].Breaking) |
| 272 | +} |
| 273 | + |
| 274 | +func TestCompareExamplesWithParentBreaking(t *testing.T) { |
| 275 | + ResetDefaultBreakingRules() |
| 276 | + ResetActiveBreakingRulesConfig() |
| 277 | + defer ResetDefaultBreakingRules() |
| 278 | + defer ResetActiveBreakingRulesConfig() |
| 279 | + |
| 280 | + config := GenerateDefaultBreakingRules() |
| 281 | + config.Example.Value = rule(false, true, false) |
| 282 | + config.Parameter.Examples = rule(false, false, false) |
| 283 | + config.MediaType.Examples = rule(true, false, true) |
| 284 | + SetActiveBreakingRulesConfig(config) |
| 285 | + |
| 286 | + modified := compareExamplesWithParentBreaking(CompParameter, PropExamples)( |
| 287 | + buildTestExample(t, "value: left"), |
| 288 | + buildTestExample(t, "value: right"), |
| 289 | + ) |
| 290 | + assert.NotNil(t, modified) |
| 291 | + assert.False(t, modified.GetAllChanges()[0].Breaking) |
| 292 | + |
| 293 | + added := compareExamplesWithParentBreaking(CompMediaType, PropExamples)( |
| 294 | + nil, |
| 295 | + buildTestExample(t, "value: right"), |
| 296 | + ) |
| 297 | + assert.NotNil(t, added) |
| 298 | + assert.True(t, added.GetAllChanges()[0].Breaking) |
| 299 | + |
| 300 | + removed := compareExamplesWithParentBreaking(CompMediaType, PropExamples)( |
| 301 | + buildTestExample(t, "value: left"), |
| 302 | + nil, |
| 303 | + ) |
| 304 | + assert.NotNil(t, removed) |
| 305 | + assert.True(t, removed.GetAllChanges()[0].Breaking) |
| 306 | + |
| 307 | + unchanged := compareExamplesWithParentBreaking(CompParameter, PropExamples)( |
| 308 | + buildTestExample(t, "value: same"), |
| 309 | + buildTestExample(t, "value: same"), |
| 310 | + ) |
| 311 | + assert.Nil(t, unchanged) |
| 312 | +} |
| 313 | + |
240 | 314 | type test_hasIndex struct { |
241 | 315 | index *index.SpecIndex |
242 | 316 | } |
|
0 commit comments