@ -624,81 +624,94 @@ func TestLabels_Map(t *testing.T) {
require . Equal ( t , map [ string ] string { "aaa" : "111" , "bbb" : "222" } , Labels { { "aaa" , "111" } , { "bbb" , "222" } } . Map ( ) )
require . Equal ( t , map [ string ] string { "aaa" : "111" , "bbb" : "222" } , Labels { { "aaa" , "111" } , { "bbb" , "222" } } . Map ( ) )
}
}
func TestLabels_WithLabels ( t * testing . T ) {
func TestLabels_BytesWithLabels ( t * testing . T ) {
require . Equal ( t , Labels { { "aaa" , "111" } , { "bbb" , "222" } } , Labels { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } . WithLabels ( "aaa" , "bbb" ) )
require . Equal ( t , Labels { { "aaa" , "111" } , { "bbb" , "222" } } . Bytes ( nil ) , Labels { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } . BytesWithLabels ( nil , "aaa" , "bbb" ) )
require . Equal ( t , Labels { } . Bytes ( nil ) , Labels { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } . BytesWithLabels ( nil ) )
}
}
func TestLabels_ WithoutLabels( t * testing . T ) {
func TestLabels_ Bytes WithoutLabels( t * testing . T ) {
require . Equal ( t , Labels { { "aaa" , "111" } } , Labels { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } . WithoutLabels( "bbb" , "ccc" ) )
require . Equal ( t , Labels { { "aaa" , "111" } } .Bytes ( nil ) , Labels { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } . Bytes WithoutLabels( nil , "bbb" , "ccc" ) )
require . Equal ( t , Labels { { "aaa" , "111" } } , Labels { { "aaa" , "111" } , { "bbb" , "222" } , { MetricName , "333" } } . WithoutLabels ( "bbb" ) )
require . Equal ( t , Labels { { "aaa" , "111" } } .Bytes ( nil ) , Labels { { MetricName , "333" } , { "aaa" , "111" } , { "bbb" , "222" } } . BytesWithoutLabels ( nil , MetricName , "bbb" ) )
}
}
func TestBulider_NewBulider ( t * testing . T ) {
func TestBuilder ( t * testing . T ) {
require . Equal (
for i , tcase := range [ ] struct {
t ,
base Labels
& Builder {
del [ ] string
base : Labels { { "aaa" , "111" } } ,
keep [ ] string
del : [ ] string { } ,
set [ ] Label
add : [ ] Label { } ,
want Labels
} {
{
base : FromStrings ( "aaa" , "111" ) ,
want : FromStrings ( "aaa" , "111" ) ,
} ,
} ,
NewBuilder ( Labels { { "aaa" , "111" } } ) ,
{
)
base : FromStrings ( "aaa" , "111" , "bbb" , "222" , "ccc" , "333" ) ,
}
func TestBuilder_Del ( t * testing . T ) {
require . Equal (
t ,
& Builder {
del : [ ] string { "bbb" } ,
del : [ ] string { "bbb" } ,
add: [ ] Label { { "aaa" , "111" } , { "ccc" , "333" }} ,
want : FromStrings ( "aaa" , "111" , "ccc" , "333" ) ,
} ,
} ,
( & Builder {
{
del : [ ] string { } ,
base : nil ,
add : [ ] Label { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } ,
set : [ ] Label { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } ,
} ) . Del ( "bbb" ) ,
del : [ ] string { "bbb" } ,
)
want : FromStrings ( "aaa" , "111" , "ccc" , "333" ) ,
}
} ,
{
func TestBuilder_Set ( t * testing . T ) {
base : FromStrings ( "aaa" , "111" ) ,
require . Equal (
set : [ ] Label { { "bbb" , "222" } } ,
t ,
want : FromStrings ( "aaa" , "111" , "bbb" , "222" ) ,
& Builder {
} ,
base : Labels { { "aaa" , "111" } } ,
{
del : [ ] string { } ,
base : FromStrings ( "aaa" , "111" ) ,
add : [ ] Label { { "bbb" , "222" } } ,
set : [ ] Label { { "bbb" , "222" } , { "bbb" , "333" } } ,
} ,
want : FromStrings ( "aaa" , "111" , "bbb" , "333" ) ,
( & Builder {
} ,
base : Labels { { "aaa" , "111" } } ,
{
del : [ ] string { } ,
base : FromStrings ( "aaa" , "111" , "bbb" , "222" , "ccc" , "333" ) ,
add : [ ] Label { } ,
} ) . Set ( "bbb" , "222" ) ,
)
require . Equal (
t ,
& Builder {
base : Labels { { "aaa" , "111" } } ,
del : [ ] string { } ,
add : [ ] Label { { "bbb" , "333" } } ,
} ,
( & Builder {
base : Labels { { "aaa" , "111" } } ,
del : [ ] string { } ,
add : [ ] Label { { "bbb" , "222" } } ,
} ) . Set ( "bbb" , "333" ) ,
)
}
func TestBuilder_Labels ( t * testing . T ) {
require . Equal (
t ,
Labels { { "aaa" , "111" } , { "ccc" , "333" } , { "ddd" , "444" } } ,
( & Builder {
base : Labels { { "aaa" , "111" } , { "bbb" , "222" } , { "ccc" , "333" } } ,
del : [ ] string { "bbb" } ,
del : [ ] string { "bbb" } ,
add : [ ] Label { { "ddd" , "444" } } ,
set : [ ] Label { { "ddd" , "444" } } ,
} ) . Labels ( ) ,
want : FromStrings ( "aaa" , "111" , "ccc" , "333" , "ddd" , "444" ) ,
)
} ,
{ // Blank value is interpreted as delete.
base : FromStrings ( "aaa" , "111" , "bbb" , "" , "ccc" , "333" ) ,
want : FromStrings ( "aaa" , "111" , "ccc" , "333" ) ,
} ,
{
base : FromStrings ( "aaa" , "111" , "bbb" , "222" , "ccc" , "333" ) ,
set : [ ] Label { { "bbb" , "" } } ,
want : FromStrings ( "aaa" , "111" , "ccc" , "333" ) ,
} ,
{
base : FromStrings ( "aaa" , "111" , "bbb" , "222" , "ccc" , "333" ) ,
keep : [ ] string { "bbb" } ,
want : FromStrings ( "bbb" , "222" ) ,
} ,
{
base : FromStrings ( "aaa" , "111" , "bbb" , "222" , "ccc" , "333" ) ,
keep : [ ] string { "aaa" , "ccc" } ,
want : FromStrings ( "aaa" , "111" , "ccc" , "333" ) ,
} ,
{
base : FromStrings ( "aaa" , "111" , "bbb" , "222" , "ccc" , "333" ) ,
del : [ ] string { "bbb" } ,
set : [ ] Label { { "ddd" , "444" } } ,
keep : [ ] string { "aaa" , "ddd" } ,
want : FromStrings ( "aaa" , "111" , "ddd" , "444" ) ,
} ,
} {
t . Run ( fmt . Sprint ( i ) , func ( t * testing . T ) {
b := NewBuilder ( tcase . base )
for _ , lbl := range tcase . set {
b . Set ( lbl . Name , lbl . Value )
}
if len ( tcase . keep ) > 0 {
b . Keep ( tcase . keep ... )
}
b . Del ( tcase . del ... )
require . Equal ( t , tcase . want , b . Labels ( ) )
} )
}
}
}
func TestLabels_Hash ( t * testing . T ) {
func TestLabels_Hash ( t * testing . T ) {