// Doing a unified test for Fields and SetFields func TestFields(t *testing.T) { fl := bagins.NewTagFieldList() test_len := func(l int) { // DRY! if len(fl.Fields()) != l { t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!") } } test_len(0) newFields := []bagins.TagField{ *bagins.NewTagField("label1", "value1"), *bagins.NewTagField("label2", "value2"), *bagins.NewTagField("label3", "value3"), } fl.SetFields(newFields) test_len(3) for i := 0; i < 3; i++ { exp := fmt.Sprintf("label%d", i+1) act := fl.Fields()[i].Label() if exp != act { t.Error("Expected", exp, "but returned", act) } } }
func TestRemoveField(t *testing.T) { fl := bagins.NewTagFieldList() test_len := func(l int) { // DRY again! if len(fl.Fields()) != l { t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!") } } for i := 0; i < 100; i++ { tmp := strconv.Itoa(i) fl.AddField(*bagins.NewTagField(tmp, tmp)) } test_len(100) // Should error if removing out of range. if err := fl.RemoveField(-6); err == nil { t.Error("Trying to remove negative index does not produce expected error!") } if err := fl.RemoveField(100); err == nil { t.Error("Trying to remove out of bound index does not produce expected error!") } test_len(100) // Remove every other one of the first 25 and test for i := 0; i < 50; i++ { if i%2 == 0 { fl.RemoveField(i) } } test_len(75) }
func TestAddField(t *testing.T) { fl := bagins.NewTagFieldList() exp_len := 100 for i := 0; i < exp_len; i++ { tmp := strconv.Itoa(i) fl.AddField(*bagins.NewTagField(tmp, tmp)) } if len(fl.Fields()) != exp_len { t.Error("Expected", exp_len, "fields but returned", len(fl.Fields()), "!") } for i, f := range fl.Fields() { if f.Value() != strconv.Itoa(i) { t.Error("Expected field value of", strconv.Itoa(i), "but returned", f.Value(), "!") } } }
func TestNewTagFieldList(t *testing.T) { var tfl interface{} = bagins.NewTagFieldList() if _, ok := tfl.(*bagins.TagFieldList); !ok { t.Error("TagFieldList not returned!") } }