| 1 | package network |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func buildExampleMeta() *Meta { |
| 9 | return NewMeta() |
| 10 | } |
| 11 | |
| 12 | func TestNewMeta(t *testing.T) { |
| 13 | exp := len(Meta{}.m) |
| 14 | got := len(NewMeta().m) |
| 15 | if got != exp { |
| 16 | t.Fatalf("expected '%v', got '%v'", exp, got) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestMetaMarshalJSON(t *testing.T) { |
| 21 | _, err := buildExampleMeta().MarshalJSON() |
| 22 | if err != nil { |
| 23 | t.Error("unable to marshal JSON from meta struct") |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestMetaSet(t *testing.T) { |
| 28 | example := buildExampleMeta() |
| 29 | example.Set("picat", "<3") |
| 30 | if example.m["picat"] != "<3" { |
| 31 | t.Error("unable to set meta data in struct") |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // TODO document what this does, not too clear, |
| 36 | // at least for me today lolololol |
| 37 | func TestMetaGetIntsWith(t *testing.T) { |
| 38 | example := buildExampleMeta() |
| 39 | example.m["picat"] = "3," |
| 40 | |
| 41 | exp := []int{4, 3} |
| 42 | got := example.GetIntsWith("picat", 4, false) |
| 43 | |
| 44 | if len(exp) != len(got) { |
| 45 | t.Fatalf("expected '%v', got '%v'", exp, got) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestMetaSetInts(t *testing.T) { |
| 50 | example := buildExampleMeta() |
| 51 | example.SetInts("picat", []int{0, 1}) |
| 52 | |
| 53 | exp := strings.Join([]string{"0", "1"}, ",") |
| 54 | got := example.m["picat"] |
| 55 | |
| 56 | if exp != got { |
| 57 | t.Fatalf("expected '%v', got '%v'", exp, got) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestMetaGetOr(t *testing.T) { |
| 62 | example := buildExampleMeta() |
| 63 | dflt := "picat" |
| 64 | exp := dflt |
| 65 | got := example.GetOr("evilsocket", dflt) |
| 66 | if exp != got { |
| 67 | t.Fatalf("expected '%v', got '%v'", exp, got) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestMetaEach(t *testing.T) { |
| 72 | example := buildExampleMeta() |
| 73 | example.m["picat"] = true |
| 74 | example.m["evilsocket"] = true |
| 75 | |
| 76 | count := 0 |
| 77 | exampleCB := func(name string, value interface{}) { |
| 78 | count++ |
| 79 | } |
| 80 | example.Each(exampleCB) |
| 81 | |
| 82 | exp := 2 |
| 83 | got := count |
| 84 | if exp != got { |
| 85 | t.Fatalf("expected '%v', got '%v'", exp, got) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestMetaEmpty(t *testing.T) { |
| 90 | example := buildExampleMeta() |
| 91 | |
| 92 | if !example.Empty() { |
| 93 | t.Error("unable to check if filled struct is empty") |
| 94 | } |
| 95 | |
| 96 | example.m["picat"] = true //fill struct so not empty |
| 97 | |
| 98 | if example.Empty() { |
| 99 | t.Error("unable to check if filled struct is empty") |
| 100 | } |
| 101 | |
| 102 | } |