meta_test.go 2.0 KB

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