@@ -18,6 +18,8 @@ import (
1818 "net/http"
1919 "testing"
2020
21+ "github.com/stretchr/testify/assert"
22+ "github.com/stretchr/testify/require"
2123 "google.golang.org/protobuf/proto"
2224
2325 "github.com/prometheus/common/model"
@@ -309,8 +311,32 @@ foo_metric 1.234
309311}
310312
311313func TestEscapedEncode (t * testing.T ) {
312- var buff bytes.Buffer
313- delimEncoder := NewEncoder (& buff , FmtProtoDelim + "; escaping=underscores" )
314+ tests := []struct {
315+ name string
316+ format Format
317+ }{
318+ {
319+ name : "ProtoDelim" ,
320+ format : FmtProtoDelim ,
321+ },
322+ {
323+ name : "ProtoDelim with escaping underscores" ,
324+ format : FmtProtoDelim + "; escaping=underscores" ,
325+ },
326+ {
327+ name : "ProtoCompact" ,
328+ format : FmtProtoCompact ,
329+ },
330+ {
331+ name : "ProtoText" ,
332+ format : FmtProtoText ,
333+ },
334+ {
335+ name : "Text" ,
336+ format : FmtText ,
337+ },
338+ }
339+
314340 metric := & dto.MetricFamily {
315341 Name : proto .String ("foo.metric" ),
316342 Type : dto .MetricType_UNTYPED .Enum (),
@@ -334,61 +360,19 @@ func TestEscapedEncode(t *testing.T) {
334360 },
335361 }
336362
337- err := delimEncoder .Encode (metric )
338- if err != nil {
339- t .Errorf ("unexpected error during encode: %s" , err .Error ())
340- }
363+ for _ , tt := range tests {
364+ t .Run (tt .name , func (t * testing.T ) {
365+ var buff bytes.Buffer
366+ encoder := NewEncoder (& buff , tt .format )
367+ err := encoder .Encode (metric )
368+ require .NoError (t , err )
341369
342- out := buff .Bytes ()
343- if len (out ) == 0 {
344- t .Errorf ("expected the output bytes buffer to be non-empty" )
345- }
346-
347- buff .Reset ()
348-
349- compactEncoder := NewEncoder (& buff , FmtProtoCompact )
350- err = compactEncoder .Encode (metric )
351- if err != nil {
352- t .Errorf ("unexpected error during encode: %s" , err .Error ())
353- }
354-
355- out = buff .Bytes ()
356- if len (out ) == 0 {
357- t .Errorf ("expected the output bytes buffer to be non-empty" )
358- }
359-
360- buff .Reset ()
361-
362- protoTextEncoder := NewEncoder (& buff , FmtProtoText )
363- err = protoTextEncoder .Encode (metric )
364- if err != nil {
365- t .Errorf ("unexpected error during encode: %s" , err .Error ())
366- }
367-
368- out = buff .Bytes ()
369- if len (out ) == 0 {
370- t .Errorf ("expected the output bytes buffer to be non-empty" )
371- }
372-
373- buff .Reset ()
374-
375- textEncoder := NewEncoder (& buff , FmtText )
376- err = textEncoder .Encode (metric )
377- if err != nil {
378- t .Errorf ("unexpected error during encode: %s" , err .Error ())
379- }
380-
381- out = buff .Bytes ()
382- if len (out ) == 0 {
383- t .Errorf ("expected the output bytes buffer to be non-empty" )
384- }
385-
386- expected := `# TYPE foo_metric untyped
387- foo_metric 1.234
388- foo_metric{dotted_label_name="my.label.value"} 8
389- `
390-
391- if string (out ) != expected {
392- t .Errorf ("expected TextEncoder to return %s, but got %s instead" , expected , string (out ))
370+ s := buff .String ()
371+ assert .NotContains (t , s , "foo.metric" )
372+ assert .Contains (t , s , "foo_metric" )
373+ assert .NotContains (t , s , "dotted.label.name" )
374+ assert .Contains (t , s , "dotted_label_name" )
375+ assert .Contains (t , s , "my.label.value" )
376+ })
393377 }
394378}
0 commit comments