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