github.com/jd-ly/tools@v0.5.7/internal/event/export/ocagent/wire/metrics_test.go (about)

     1  package wire
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestMarshalJSON(t *testing.T) {
     9  	tests := []struct {
    10  		name string
    11  		pt   *Point
    12  		want string
    13  	}{
    14  		{
    15  			"PointInt64",
    16  			&Point{
    17  				Value: PointInt64Value{
    18  					Int64Value: 5,
    19  				},
    20  			},
    21  			`{"int64Value":5}`,
    22  		},
    23  		{
    24  			"PointDouble",
    25  			&Point{
    26  				Value: PointDoubleValue{
    27  					DoubleValue: 3.14,
    28  				},
    29  			},
    30  			`{"doubleValue":3.14}`,
    31  		},
    32  		{
    33  			"PointDistribution",
    34  			&Point{
    35  				Value: PointDistributionValue{
    36  					DistributionValue: &DistributionValue{
    37  						Count: 3,
    38  						Sum:   10,
    39  						Buckets: []*Bucket{
    40  							{
    41  								Count: 1,
    42  							},
    43  							{
    44  								Count: 2,
    45  							},
    46  						},
    47  						BucketOptions: &BucketOptionsExplicit{
    48  							Bounds: []float64{
    49  								0, 5,
    50  							},
    51  						},
    52  					},
    53  				},
    54  			},
    55  			`{"distributionValue":{"count":3,"sum":10,"bucket_options":{"explicit":{"bounds":[0,5]}},"buckets":[{"count":1},{"count":2}]}}`,
    56  		},
    57  		{
    58  			"nil point",
    59  			nil,
    60  			`null`,
    61  		},
    62  	}
    63  
    64  	for _, tt := range tests {
    65  		t.Run(tt.name, func(t *testing.T) {
    66  			buf, err := tt.pt.MarshalJSON()
    67  			if err != nil {
    68  				t.Fatalf("Got:\n%v\nWant:\n%v", err, nil)
    69  			}
    70  			got := string(buf)
    71  			if !reflect.DeepEqual(got, tt.want) {
    72  				t.Fatalf("Got:\n%s\nWant:\n%s", got, tt.want)
    73  			}
    74  		})
    75  	}
    76  }