github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/parsers/parsers_test.go (about)

     1  package parsers // import "github.com/demonoid81/moby/pkg/parsers"
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestParseKeyValueOpt(t *testing.T) {
     9  	invalids := map[string]string{
    10  		"":    "Unable to parse key/value option: ",
    11  		"key": "Unable to parse key/value option: key",
    12  	}
    13  	for invalid, expectedError := range invalids {
    14  		if _, _, err := ParseKeyValueOpt(invalid); err == nil || err.Error() != expectedError {
    15  			t.Fatalf("Expected error %v for %v, got %v", expectedError, invalid, err)
    16  		}
    17  	}
    18  	valids := map[string][]string{
    19  		"key=value":               {"key", "value"},
    20  		" key = value ":           {"key", "value"},
    21  		"key=value1=value2":       {"key", "value1=value2"},
    22  		" key = value1 = value2 ": {"key", "value1 = value2"},
    23  	}
    24  	for valid, expectedKeyValue := range valids {
    25  		key, value, err := ParseKeyValueOpt(valid)
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  		if key != expectedKeyValue[0] || value != expectedKeyValue[1] {
    30  			t.Fatalf("Expected {%v: %v} got {%v: %v}", expectedKeyValue[0], expectedKeyValue[1], key, value)
    31  		}
    32  	}
    33  }
    34  
    35  func TestParseUintList(t *testing.T) {
    36  	valids := map[string]map[int]bool{
    37  		"":             {},
    38  		"7":            {7: true},
    39  		"1-6":          {1: true, 2: true, 3: true, 4: true, 5: true, 6: true},
    40  		"0-7":          {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true},
    41  		"0,3-4,7,8-10": {0: true, 3: true, 4: true, 7: true, 8: true, 9: true, 10: true},
    42  		"0-0,0,1-4":    {0: true, 1: true, 2: true, 3: true, 4: true},
    43  		"03,1-3":       {1: true, 2: true, 3: true},
    44  		"3,2,1":        {1: true, 2: true, 3: true},
    45  		"0-2,3,1":      {0: true, 1: true, 2: true, 3: true},
    46  	}
    47  	for k, v := range valids {
    48  		out, err := ParseUintList(k)
    49  		if err != nil {
    50  			t.Fatalf("Expected not to fail, got %v", err)
    51  		}
    52  		if !reflect.DeepEqual(out, v) {
    53  			t.Fatalf("Expected %v, got %v", v, out)
    54  		}
    55  	}
    56  
    57  	invalids := []string{
    58  		"this",
    59  		"1--",
    60  		"1-10,,10",
    61  		"10-1",
    62  		"-1",
    63  		"-1,0",
    64  	}
    65  	for _, v := range invalids {
    66  		if out, err := ParseUintList(v); err == nil {
    67  			t.Fatalf("Expected failure with %s but got %v", v, out)
    68  		}
    69  	}
    70  }
    71  
    72  func TestParseUintListMaximumLimits(t *testing.T) {
    73  	v := "10,1000"
    74  	if _, err := ParseUintListMaximum(v, 0); err != nil {
    75  		t.Fatalf("Expected not to fail, got %v", err)
    76  	}
    77  	if _, err := ParseUintListMaximum(v, 1000); err != nil {
    78  		t.Fatalf("Expected not to fail, got %v", err)
    79  	}
    80  	if out, err := ParseUintListMaximum(v, 100); err == nil {
    81  		t.Fatalf("Expected failure with %s but got %v", v, out)
    82  	}
    83  }