Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit18424a4

Browse files
dashpoleflc1125MrAlias
authored
Add tests for attribute JSON marshalling (#7268)
Forked from#7175 (comment)This adds a test for JSON marshaling of attribute sets.---------Co-authored-by: Flc゛ <four_leaf_clover@foxmail.com>Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
1 parent9798759 commit18424a4

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

‎attribute/set_test.go‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,133 @@ func args(m reflect.Method) []reflect.Value {
348348
returnout
349349
}
350350

351+
funcTestMarshalJSON(t*testing.T) {
352+
for_,tc:=range []struct {
353+
descstring
354+
kvs []attribute.KeyValue
355+
wantJSONstring
356+
}{
357+
{
358+
desc:"empty",
359+
kvs: []attribute.KeyValue{},
360+
wantJSON:`[]`,
361+
},
362+
{
363+
desc:"single string attribute",
364+
kvs: []attribute.KeyValue{attribute.String("A","a")},
365+
wantJSON:`[{"Key":"A","Value":{"Type":"STRING","Value":"a"}}]`,
366+
},
367+
{
368+
desc:"many mixed attributes",
369+
kvs: []attribute.KeyValue{
370+
attribute.Bool("A",true),
371+
attribute.BoolSlice("B", []bool{true,false}),
372+
attribute.Int("C",1),
373+
attribute.IntSlice("D", []int{2,3}),
374+
attribute.Int64("E",22),
375+
attribute.Int64Slice("F", []int64{33,44}),
376+
attribute.Float64("G",1.1),
377+
attribute.Float64Slice("H", []float64{2.2,3.3}),
378+
attribute.String("I","Z"),
379+
attribute.StringSlice("J", []string{"X","Y"}),
380+
attribute.Stringer("K",&simpleStringer{val:"foo"}),
381+
},
382+
wantJSON:`[
383+
{
384+
"Key": "A",
385+
"Value": {
386+
"Type": "BOOL",
387+
"Value": true
388+
}
389+
},
390+
{
391+
"Key": "B",
392+
"Value": {
393+
"Type": "BOOLSLICE",
394+
"Value": [true, false]
395+
}
396+
},
397+
{
398+
"Key": "C",
399+
"Value": {
400+
"Type": "INT64",
401+
"Value": 1
402+
}
403+
},
404+
{
405+
"Key": "D",
406+
"Value": {
407+
"Type": "INT64SLICE",
408+
"Value": [2, 3]
409+
}
410+
},
411+
{
412+
"Key": "E",
413+
"Value": {
414+
"Type": "INT64",
415+
"Value": 22
416+
}
417+
},
418+
{
419+
"Key": "F",
420+
"Value": {
421+
"Type": "INT64SLICE",
422+
"Value": [33, 44]
423+
}
424+
},
425+
{
426+
"Key": "G",
427+
"Value": {
428+
"Type": "FLOAT64",
429+
"Value": 1.1
430+
}
431+
},
432+
{
433+
"Key": "H",
434+
"Value": {
435+
"Type": "FLOAT64SLICE",
436+
"Value": [2.2, 3.3]
437+
}
438+
},
439+
{
440+
"Key": "I",
441+
"Value": {
442+
"Type": "STRING",
443+
"Value": "Z"
444+
}
445+
},
446+
{
447+
"Key": "J",
448+
"Value": {
449+
"Type": "STRINGSLICE",
450+
"Value": ["X", "Y"]
451+
}
452+
},
453+
{
454+
"Key": "K",
455+
"Value": {
456+
"Type": "STRING",
457+
"Value": "foo"
458+
}
459+
}
460+
]`,
461+
},
462+
} {
463+
t.Run(tc.desc,func(t*testing.T) {
464+
set:=attribute.NewSet(tc.kvs...)
465+
by,err:=set.MarshalJSON()
466+
require.NoError(t,err)
467+
assert.JSONEq(t,tc.wantJSON,string(by))
468+
})
469+
}
470+
}
471+
472+
typesimpleStringerstruct {
473+
valstring
474+
}
475+
476+
func (s*simpleStringer)String()string {returns.val }
477+
351478
funcBenchmarkFiltering(b*testing.B) {
352479
varkvs [26]attribute.KeyValue
353480
buf:= [1]byte{'A'-1}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp