File: delta_test.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (367 lines) | stat: -rw-r--r-- 9,516 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package delta_test

import (
	"fmt"
	"reflect"
	"testing"

	"github.com/Azure/azure-sdk-for-go/eng/tools/internal/delta"
	"github.com/Azure/azure-sdk-for-go/eng/tools/internal/exports"
)

var oContent exports.Content
var nContent exports.Content
var oBreaking exports.Content
var nBreaking exports.Content

func init() {
	oContent, _ = exports.Get("./testdata/nonbreaking/old")
	nContent, _ = exports.Get("./testdata/nonbreaking/new")
	oBreaking, _ = exports.Get("./testdata/breaking/old")
	nBreaking, _ = exports.Get("./testdata/breaking/new")
}

func Test_GetAddedExports(t *testing.T) {
	aContent := delta.GetExports(oContent, nContent)

	// const

	if l := len(aContent.Consts); l != 4 {
		t.Logf("wrong number of consts added, have %v, want %v", l, 4)
		t.Fail()
	}

	cAdded := map[string]exports.Const{
		"Blue":    {Type: "Color", Value: "Blue"},
		"Green":   {Type: "Color", Value: "Green"},
		"Red":     {Type: "Color", Value: "Red"},
		"Holiday": {Type: "DayOfWeek", Value: "Holiday"},
	}

	for k, v := range cAdded {
		t.Run(fmt.Sprintf("const %s", k), func(t *testing.T) {
			if c, ok := aContent.Consts[k]; !ok {
				t.Log("missing")
				t.Fail()
			} else if c.Type != v.Type {
				t.Logf("mismatched const type, have %s, want %s", aContent.Consts[k].Type, v.Type)
				t.Fail()
			}
		})
	}

	// func

	if l := len(aContent.Funcs); l != 6 {
		t.Logf("wrong number of funcs added, have %v, want %v", l, 6)
		t.Fail()
	}

	fAdded := map[string]exports.Func{
		"DoNothing2":                 {},
		"Client.ExportData":          {Params: strPtr("context.Context, string, string, ExportRDBParameters"), Returns: strPtr("ExportDataFuture, error")},
		"Client.ExportDataPreparer":  {Params: strPtr("context.Context, string, string, ExportRDBParameters"), Returns: strPtr("*http.Request, error")},
		"Client.ExportDataSender":    {Params: strPtr("*http.Request"), Returns: strPtr("ExportDataFuture, error")},
		"Client.ExportDataResponder": {Params: strPtr("*http.Response"), Returns: strPtr("autorest.Response, error")},
		"ExportDataFuture.Result":    {Params: strPtr("Client"), Returns: strPtr("autorest.Response, error")},
	}

	for k, v := range fAdded {
		t.Run(fmt.Sprintf("func %s", k), func(t *testing.T) {
			if f, ok := aContent.Funcs[k]; !ok {
				t.Log("missing")
				t.Fail()
			} else if !reflect.DeepEqual(f, v) {
				t.Logf("mismatched func type, have %+v, want %+v", v, f)
				t.Fail()
			}
		})
	}

	// interface

	if l := len(aContent.Interfaces); l != 2 {
		t.Logf("wrong number of interfaces added, have %v, want %v", l, 2)
		t.Fail()
	}

	iAdded := map[string]exports.Interface{
		"NewInterface": {Methods: map[string]exports.Func{
			"One": {Params: strPtr("int")},
			"Two": {Returns: strPtr("error")},
		}},
		"SomeInterface": {Methods: map[string]exports.Func{
			"NewMethod": {Params: strPtr("string"), Returns: strPtr("bool, error")},
		}},
	}

	for k, v := range iAdded {
		t.Run(fmt.Sprintf("interface %s", k), func(t *testing.T) {
			if i, ok := aContent.Interfaces[k]; !ok {
				t.Log("missing")
				t.Fail()
			} else if !reflect.DeepEqual(i, v) {
				t.Logf("mismatched interface type, have %+v, want %+v", i, v)
				t.Fail()
			}
		})
	}

	// struct

	if l := len(aContent.Structs); l != 4 {
		t.Logf("wrong number of structs added, have %v, want %v", l, 4)
		t.Fail()
	}

	sAdded := map[string]exports.Struct{
		"ExportDataFuture": {
			AnonymousFields: []string{"azure.Future"},
			Fields:          map[string]string{"NewField": "string"},
		},
		"ExportRDBParameters": {
			Fields: map[string]string{
				"Format":    "*string",
				"Prefix":    "*string",
				"Container": "*string",
			},
		},
		"CreateProperties": {
			Fields: map[string]string{
				"NewField": "*float64",
			},
		},
		"DeleteFuture": {
			Fields: map[string]string{
				"NewField": "string",
			},
		},
	}

	for k, v := range sAdded {
		t.Run(fmt.Sprintf("struct %s", k), func(t *testing.T) {
			if s, ok := aContent.Structs[k]; !ok {
				t.Log("missing")
				t.Fail()
			} else if !reflect.DeepEqual(s, v) {
				t.Logf("mismatched struct type, have %+v, want %+v", v, s)
				t.Fail()
			}
		})
	}
}

func Test_GetAddedStructFields(t *testing.T) {
	nf := delta.GetStructFields(oContent, nContent)

	if l := len(nf); l != 2 {
		t.Logf("wrong number of structs with new fields, have %v, want %v", l, 2)
		t.Fail()
	}

	added := map[string]exports.Struct{
		"CreateProperties": {
			Fields: map[string]string{"NewField": "*float64"},
		},
		"DeleteFuture": {
			Fields: map[string]string{"NewField": "string"},
		},
	}

	if !reflect.DeepEqual(added, nf) {
		t.Logf("mismatched fields added, have %+v, want %+v", nf, added)
		t.Fail()
	}
}

func Test_GetAddedInterfaceMethods(t *testing.T) {
	ni := delta.GetInterfaceMethods(oContent, nContent)

	if l := len(ni); l != 1 {
		t.Logf("wrong number of interfaces with new methods, have %v, want %v", l, 1)
		t.Fail()
	}

	added := map[string]exports.Interface{
		"SomeInterface": {
			Methods: map[string]exports.Func{
				"NewMethod": {Params: strPtr("string"), Returns: strPtr("bool, error")},
			},
		},
	}

	if !reflect.DeepEqual(added, ni) {
		t.Logf("mismatched methods added, have %+v, want %+v", ni, added)
		t.Fail()
	}
}

func Test_GetNoChanges(t *testing.T) {
	nc := delta.GetExports(nContent, nContent)
	if !reflect.DeepEqual(nc, delta.NewContent()) {
		t.Log("expected empty exports")
		t.Fail()
	}

	ni := delta.GetInterfaceMethods(nContent, nContent)
	if !reflect.DeepEqual(ni, map[string]exports.Interface{}) {
		t.Log("expected no new interfaces")
		t.Fail()
	}

	nf := delta.GetStructFields(oContent, oContent)
	if !reflect.DeepEqual(nf, map[string]exports.Struct{}) {
		t.Log("expected no new struct fields")
		t.Fail()
	}
}

func Test_GetConstTypeChanges(t *testing.T) {
	cc := delta.GetConstTypeChanges(oBreaking, nBreaking)

	if l := len(cc); l != 8 {
		t.Logf("wrong number of const changed, have %v, want %v", l, 8)
		t.Fail()
	}

	change := delta.Signature{
		From: "DayOfWeek",
		To:   "Day",
	}
	changed := map[string]delta.Signature{
		"Friday":    change,
		"Monday":    change,
		"Saturday":  change,
		"Sunday":    change,
		"Thursday":  change,
		"Tuesday":   change,
		"Wednesday": change,
		"Weekend":   change,
	}

	if !reflect.DeepEqual(cc, changed) {
		t.Logf("mismatched changes, have %+v, want %+v", cc, changed)
		t.Fail()
	}
}

func Test_GetFuncSigChanges(t *testing.T) {
	fsc := delta.GetFuncSigChanges(oBreaking, nBreaking)

	if l := len(fsc); l != 6 {
		t.Logf("wrong number of func sigs changed, have %v want %v", l, 6)
		t.Fail()
	}

	changed := map[string]delta.FuncSig{
		"DoNothing": {
			Params: &delta.Signature{From: delta.None, To: "string"},
		},
		"DoNothingWithParam": {
			Params: &delta.Signature{From: "int", To: delta.None},
		},
		"Client.List": {
			Params:  &delta.Signature{From: "context.Context", To: "context.Context, string"},
			Returns: &delta.Signature{From: "ListResultPage, error", To: "ListResult, error"},
		},
		"Client.ListPreparer": {
			Params: &delta.Signature{From: "context.Context", To: "context.Context, string"},
		},
		"Client.Delete": {
			Params: &delta.Signature{From: "context.Context, string, string", To: "context.Context, string"},
		},
		"Client.DeletePreparer": {
			Params: &delta.Signature{From: "context.Context, string, string", To: "context.Context, string"},
		},
	}

	for k, v := range changed {
		t.Run(fmt.Sprintf("func %s", k), func(t *testing.T) {
			if f, ok := fsc[k]; !ok {
				t.Log("missing")
				t.Fail()
			} else {
				if !reflect.DeepEqual(v, f) {
					t.Logf("mismatched changes, have %+v, want %+v", f, v)
					t.Fail()
				}
			}
		})
	}
}

func Test_GetInterfaceMethodSigChanges(t *testing.T) {
	isc := delta.GetInterfaceMethodSigChanges(oBreaking, nBreaking)

	if l := len(isc); l != 1 {
		t.Logf("wrong number of interfaces with method sig changes, have %v, want %v", l, 1)
		t.Fail()
	}

	changed := map[string]delta.InterfaceDef{
		"SomeInterface": {
			MethodSigs: map[string]delta.FuncSig{
				"One": {Params: &delta.Signature{From: delta.None, To: "string"}},
				"Two": {Params: &delta.Signature{From: "bool", To: "bool, int"}},
			},
		},
	}

	for k, v := range changed {
		t.Run(fmt.Sprintf("interface %s", k), func(t *testing.T) {
			if i, ok := isc[k]; !ok {
				t.Log("missing")
				t.Fail()
			} else {
				if !reflect.DeepEqual(v, i) {
					t.Logf("mismatched changes, have %+v, want %+v", i, v)
					t.Fail()
				}
			}
		})
	}
}

func Test_GetStructFieldChanges(t *testing.T) {
	sfc := delta.GetStructFieldChanges(oBreaking, nBreaking)

	if l := len(sfc); l != 2 {
		t.Logf("wrong number of structs with field changes, have %v, want %v", l, 2)
		t.Fail()
	}

	changed := map[string]delta.StructDef{
		"CreateProperties": {
			Fields: map[string]delta.Signature{
				"SubnetID":           {From: "*string", To: "*int"},
				"RedisConfiguration": {From: "map[string]*string", To: "interface{}"},
			},
		},
		"ListResult": {
			Fields: map[string]delta.Signature{
				"NextLink": {From: "*string", To: "string"},
			},
		},
	}

	for k, v := range changed {
		t.Run(fmt.Sprintf("struct %s", k), func(t *testing.T) {
			if s, ok := sfc[k]; !ok {
				t.Log("missing")
				t.Fail()
			} else {
				if !reflect.DeepEqual(v, s) {
					t.Logf("mismatched changes, have %+v, want %+v", s, v)
					t.Fail()
				}
			}
		})
	}
}

func strPtr(s string) *string {
	return &s
}