github.com/informationsea/shellflow@v0.1.3/flowscript/flowscript_stringset_test.go (about)

     1  package flowscript
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestStringSet(t *testing.T) {
    10  	stringSet := NewStringSet()
    11  	if s := stringSet.Size(); s != 0 {
    12  		t.Fatalf("size of empty set should 0 / actual: %d", s)
    13  	}
    14  
    15  	stringSet.Add("hoge")
    16  	stringSet.Add("hoge")
    17  	stringSet.Add("foo")
    18  	if s := stringSet.Size(); s != 2 {
    19  		t.Fatalf("size of set should 2 / actual: %d", s)
    20  	}
    21  
    22  	if !stringSet.Contains("hoge") {
    23  		t.Fatal("hoge should be contained")
    24  	}
    25  
    26  	if !stringSet.Contains("foo") {
    27  		t.Fatal("foo should be contained")
    28  	}
    29  
    30  	if stringSet.Contains("bar") {
    31  		t.Fatal("bar should not be contained")
    32  	}
    33  
    34  	if x := stringSet.Array(); !reflect.DeepEqual(x, []string{"foo", "hoge"}) {
    35  		t.Fatalf("bad array: %s", x)
    36  	}
    37  
    38  	if x, e := json.Marshal(stringSet); e != nil || string(x) != "[\"foo\",\"hoge\"]" {
    39  		t.Fatalf("bad json: %s / error: %s", x, e)
    40  	}
    41  
    42  	stringSet.Remove("hoge")
    43  
    44  	if stringSet.Contains("hoge") {
    45  		t.Fatal("hoge should be contained")
    46  	}
    47  
    48  	if !stringSet.Contains("foo") {
    49  		t.Fatal("foo should be contained")
    50  	}
    51  
    52  	if stringSet.Contains("bar") {
    53  		t.Fatal("bar should not be contained")
    54  	}
    55  
    56  	stringSet2 := NewStringSet()
    57  	stringSet2.Add("1")
    58  	stringSet2.Add("2")
    59  	stringSet2.Add("foo")
    60  
    61  	stringSet.AddAll(stringSet2)
    62  
    63  	if stringSet.Contains("hoge") {
    64  		t.Fatal("hoge should not be contained")
    65  	}
    66  
    67  	if !stringSet.Contains("foo") {
    68  		t.Fatal("foo should be contained")
    69  	}
    70  
    71  	if stringSet.Contains("bar") {
    72  		t.Fatal("bar should not be contained")
    73  	}
    74  
    75  	if !stringSet.Contains("1") {
    76  		t.Fatal("1 should be contained")
    77  	}
    78  
    79  	if !stringSet.Contains("2") {
    80  		t.Fatal("2 should be contained")
    81  	}
    82  
    83  	if a := stringSet.Array(); !reflect.DeepEqual([]string{"1", "2", "foo"}, a) {
    84  		t.Fatalf("bad array result: %s", a)
    85  	}
    86  
    87  	stringSet3 := NewStringSetWithValues("hoge", "1", "3", "4")
    88  	if s := stringSet3.Size(); s != 4 {
    89  		t.Fatalf("bad size: %d", s)
    90  	}
    91  	if v := stringSet3.Array(); !reflect.DeepEqual(v, []string{"1", "3", "4", "hoge"}) {
    92  		t.Fatalf("bad array: %s", v)
    93  	}
    94  	if v := stringSet3.Intersect(stringSet).Array(); !reflect.DeepEqual(v, []string{"1"}) {
    95  		t.Fatalf("bad array: %s", v)
    96  	}
    97  }
    98  
    99  func TestStringSetUnmarshal(t *testing.T) {
   100  	var stringSet StringSet
   101  	if e := json.Unmarshal([]byte("[\"a\", \"b\", \"c\"]"), &stringSet); e != nil {
   102  		t.Fatalf("bad unmarshal: %s", e)
   103  	}
   104  	if v := stringSet.Array(); !reflect.DeepEqual(v, []string{"a", "b", "c"}) {
   105  		t.Fatalf("bad unmarshal result: %s", v)
   106  	}
   107  }