github.com/mizzy/docker@v1.5.0/pkg/parsers/filters/parse_test.go (about)

     1  package filters
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  )
     7  
     8  func TestParseArgs(t *testing.T) {
     9  	// equivalent of `docker ps -f 'created=today' -f 'image.name=ubuntu*' -f 'image.name=*untu'`
    10  	flagArgs := []string{
    11  		"created=today",
    12  		"image.name=ubuntu*",
    13  		"image.name=*untu",
    14  	}
    15  	var (
    16  		args = Args{}
    17  		err  error
    18  	)
    19  	for i := range flagArgs {
    20  		args, err = ParseFlag(flagArgs[i], args)
    21  		if err != nil {
    22  			t.Errorf("failed to parse %s: %s", flagArgs[i], err)
    23  		}
    24  	}
    25  	if len(args["created"]) != 1 {
    26  		t.Errorf("failed to set this arg")
    27  	}
    28  	if len(args["image.name"]) != 2 {
    29  		t.Errorf("the args should have collapsed")
    30  	}
    31  }
    32  
    33  func TestParam(t *testing.T) {
    34  	a := Args{
    35  		"created":    []string{"today"},
    36  		"image.name": []string{"ubuntu*", "*untu"},
    37  	}
    38  
    39  	v, err := ToParam(a)
    40  	if err != nil {
    41  		t.Errorf("failed to marshal the filters: %s", err)
    42  	}
    43  	v1, err := FromParam(v)
    44  	if err != nil {
    45  		t.Errorf("%s", err)
    46  	}
    47  	for key, vals := range v1 {
    48  		if _, ok := a[key]; !ok {
    49  			t.Errorf("could not find key %s in original set", key)
    50  		}
    51  		sort.Strings(vals)
    52  		sort.Strings(a[key])
    53  		if len(vals) != len(a[key]) {
    54  			t.Errorf("value lengths ought to match")
    55  			continue
    56  		}
    57  		for i := range vals {
    58  			if vals[i] != a[key][i] {
    59  				t.Errorf("expected %s, but got %s", a[key][i], vals[i])
    60  			}
    61  		}
    62  	}
    63  }
    64  
    65  func TestEmpty(t *testing.T) {
    66  	a := Args{}
    67  	v, err := ToParam(a)
    68  	if err != nil {
    69  		t.Errorf("failed to marshal the filters: %s", err)
    70  	}
    71  	v1, err := FromParam(v)
    72  	if err != nil {
    73  		t.Errorf("%s", err)
    74  	}
    75  	if len(a) != len(v1) {
    76  		t.Errorf("these should both be empty sets")
    77  	}
    78  }