@@ -120,47 +120,47 @@ func TestEncode(t *testing.T) {
120
120
metric * dto.MetricFamily
121
121
format Format
122
122
withUnit bool
123
- out string
123
+ expOut string
124
124
}{
125
- // // 1: Untyped ProtoDelim
126
- // {
127
- // metric: metric1,
128
- // format: FmtProtoDelim,
129
- // out: ,
130
- // },
131
- // // 2: Untyped FmtProtoCompact
132
- // {
133
- // metric: metric1,
134
- // format: FmtProtoCompact,
135
- // out: ,
136
- // },
137
- // // 3: Untyped FmtProtoText
138
- // {
139
- // metric: metric1,
140
- // format: FmtProtoText,
141
- // out: ,
142
- // },
125
+ // 1: Untyped ProtoDelim
126
+ {
127
+ metric : metric1 ,
128
+ format : FmtProtoDelim ,
129
+ // expOut: `\x1b\n\nfoo_metric\x18\x03\"\v*\t\tX9\xb4\xc8v\xbe\xf3?`, // does this look like the desired output?
130
+ },
131
+ // 2: Untyped FmtProtoCompact
132
+ {
133
+ metric : metric1 ,
134
+ format : FmtProtoCompact ,
135
+ // expOut: `name:\"foo_metric\" type:UNTYPED metric:{untyped:{value:1.234}}\n`, // does this look like the desired output?
136
+ },
137
+ // 3: Untyped FmtProtoText
138
+ {
139
+ metric : metric1 ,
140
+ format : FmtProtoText ,
141
+ // expOut: `name: \"foo_metric\"\ntype: UNTYPED\nmetric: {\n untyped: {\n value: 1.234\n }\n}\n\n`, // does this look like the desired output?
142
+ },
143
143
// 4: Untyped FmtText
144
144
{
145
145
metric : metric1 ,
146
146
format : FmtText ,
147
- out : `# TYPE foo_metric untyped
147
+ expOut : `# TYPE foo_metric untyped
148
148
foo_metric 1.234
149
149
` ,
150
150
},
151
151
// 5: Untyped FmtOpenMetrics_0_0_1
152
152
{
153
153
metric : metric1 ,
154
154
format : FmtOpenMetrics_0_0_1 ,
155
- out : `# TYPE foo_metric unknown
155
+ expOut : `# TYPE foo_metric unknown
156
156
foo_metric 1.234
157
157
` ,
158
158
},
159
159
// 6: Untyped FmtOpenMetrics_1_0_0
160
160
{
161
161
metric : metric1 ,
162
162
format : FmtOpenMetrics_1_0_0 ,
163
- out : `# TYPE foo_metric unknown
163
+ expOut : `# TYPE foo_metric unknown
164
164
foo_metric 1.234
165
165
` ,
166
166
},
@@ -181,7 +181,7 @@ foo_metric 1.234
181
181
},
182
182
format : FmtOpenMetrics_0_0_1 ,
183
183
withUnit : true ,
184
- out : `# HELP foos_duration_seconds Duration of foos in seconds.
184
+ expOut : `# HELP foos_duration_seconds Duration of foos in seconds.
185
185
# TYPE foos_duration_seconds counter
186
186
# UNIT foos_duration_seconds seconds
187
187
foos_duration_seconds_total 420.0
@@ -202,7 +202,7 @@ foos_duration_seconds_total 420.0
202
202
},
203
203
},
204
204
format : FmtOpenMetrics_1_0_0 ,
205
- out : `# HELP foos_duration_seconds Duration of foos in seconds.
205
+ expOut : `# HELP foos_duration_seconds Duration of foos in seconds.
206
206
# TYPE foos_duration_seconds counter
207
207
foos_duration_seconds_total 420.0
208
208
` ,
@@ -213,24 +213,33 @@ foos_duration_seconds_total 420.0
213
213
if scenario .withUnit {
214
214
opts = append (opts , ToOpenMetricsWithUnit ())
215
215
}
216
- out := bytes .NewBuffer (make ([]byte , 0 , len (scenario .out )))
216
+ out := bytes .NewBuffer (make ([]byte , 0 , len (scenario .expOut )))
217
217
enc := NewEncoder (out , scenario .format , opts ... )
218
218
err := enc .Encode (scenario .metric )
219
219
if err != nil {
220
220
t .Errorf ("%d. error: %s" , i , err )
221
221
continue
222
222
}
223
-
224
- if expected , got := len (scenario .out ), len (out .Bytes ()); expected != got {
225
- t .Errorf (
226
- "%d. expected %d bytes written, got %d" ,
227
- i , expected , got ,
228
- )
223
+ // N.B. For the first three cases, I'm maintaining the same checks as they were before, i. e. the expected output is
224
+ // not declared ergo no comparison with the actual output is ever performed, expect for its length being bigger than zero.
225
+ if i > 2 {
226
+ if expected , got := len (scenario .expOut ), len (out .Bytes ()); expected != got {
227
+ t .Errorf (
228
+ "%d. expected %d bytes written, got %d" ,
229
+ i , expected , got ,
230
+ )
231
+ }
232
+ if expected , got := scenario .expOut , out .String (); expected != got {
233
+ t .Errorf (
234
+ "%d. expected out=%q, got %q" ,
235
+ i , expected , got ,
236
+ )
237
+ }
229
238
}
230
- if expected , got := scenario . out , out . String (); expected != got {
239
+ if len ( out . Bytes ()) == 0 {
231
240
t .Errorf (
232
- "%d. expected out=%q, got %q " ,
233
- i , expected , got ,
241
+ "%d. expected output not to be empty " ,
242
+ i ,
234
243
)
235
244
}
236
245
}
0 commit comments