github.com/miqui/docker@v1.9.1/pkg/stringutils/strslice_test.go (about)

     1  package stringutils
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestStrSliceMarshalJSON(t *testing.T) {
    10  	strss := map[*StrSlice]string{
    11  		nil:         "",
    12  		&StrSlice{}: "null",
    13  		&StrSlice{[]string{"/bin/sh", "-c", "echo"}}: `["/bin/sh","-c","echo"]`,
    14  	}
    15  
    16  	for strs, expected := range strss {
    17  		data, err := strs.MarshalJSON()
    18  		if err != nil {
    19  			t.Fatal(err)
    20  		}
    21  		if string(data) != expected {
    22  			t.Fatalf("Expected %v, got %v", expected, string(data))
    23  		}
    24  	}
    25  }
    26  
    27  func TestStrSliceUnmarshalJSON(t *testing.T) {
    28  	parts := map[string][]string{
    29  		"":   {"default", "values"},
    30  		"[]": {},
    31  		`["/bin/sh","-c","echo"]`: {"/bin/sh", "-c", "echo"},
    32  	}
    33  	for json, expectedParts := range parts {
    34  		strs := &StrSlice{
    35  			[]string{"default", "values"},
    36  		}
    37  		if err := strs.UnmarshalJSON([]byte(json)); err != nil {
    38  			t.Fatal(err)
    39  		}
    40  
    41  		actualParts := strs.Slice()
    42  		if len(actualParts) != len(expectedParts) {
    43  			t.Fatalf("Expected %v parts, got %v (%v)", len(expectedParts), len(actualParts), expectedParts)
    44  		}
    45  		for index, part := range actualParts {
    46  			if part != expectedParts[index] {
    47  				t.Fatalf("Expected %v, got %v", expectedParts, actualParts)
    48  				break
    49  			}
    50  		}
    51  	}
    52  }
    53  
    54  func TestStrSliceUnmarshalString(t *testing.T) {
    55  	var e *StrSlice
    56  	echo, err := json.Marshal("echo")
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if err := json.Unmarshal(echo, &e); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	slice := e.Slice()
    65  	if len(slice) != 1 {
    66  		t.Fatalf("expected 1 element after unmarshal: %q", slice)
    67  	}
    68  
    69  	if slice[0] != "echo" {
    70  		t.Fatalf("expected `echo`, got: %q", slice[0])
    71  	}
    72  }
    73  
    74  func TestStrSliceUnmarshalSlice(t *testing.T) {
    75  	var e *StrSlice
    76  	echo, err := json.Marshal([]string{"echo"})
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if err := json.Unmarshal(echo, &e); err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	slice := e.Slice()
    85  	if len(slice) != 1 {
    86  		t.Fatalf("expected 1 element after unmarshal: %q", slice)
    87  	}
    88  
    89  	if slice[0] != "echo" {
    90  		t.Fatalf("expected `echo`, got: %q", slice[0])
    91  	}
    92  }
    93  
    94  func TestStrSliceToString(t *testing.T) {
    95  	slices := map[*StrSlice]string{
    96  		NewStrSlice(""):           "",
    97  		NewStrSlice("one"):        "one",
    98  		NewStrSlice("one", "two"): "one two",
    99  	}
   100  	for s, expected := range slices {
   101  		toString := s.ToString()
   102  		if toString != expected {
   103  			t.Fatalf("Expected %v, got %v", expected, toString)
   104  		}
   105  	}
   106  }
   107  
   108  func TestStrSliceLen(t *testing.T) {
   109  	var emptyStrSlice *StrSlice
   110  	slices := map[*StrSlice]int{
   111  		NewStrSlice(""):           1,
   112  		NewStrSlice("one"):        1,
   113  		NewStrSlice("one", "two"): 2,
   114  		emptyStrSlice:             0,
   115  	}
   116  	for s, expected := range slices {
   117  		if s.Len() != expected {
   118  			t.Fatalf("Expected %d, got %d", s.Len(), expected)
   119  		}
   120  	}
   121  }
   122  
   123  func TestStrSliceSlice(t *testing.T) {
   124  	var emptyStrSlice *StrSlice
   125  	slices := map[*StrSlice][]string{
   126  		NewStrSlice("one"):        {"one"},
   127  		NewStrSlice("one", "two"): {"one", "two"},
   128  		emptyStrSlice:             nil,
   129  	}
   130  	for s, expected := range slices {
   131  		if !reflect.DeepEqual(s.Slice(), expected) {
   132  			t.Fatalf("Expected %v, got %v", s.Slice(), expected)
   133  		}
   134  	}
   135  }