params_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package tgbotapi
  2. import (
  3. "testing"
  4. )
  5. func assertLen(t *testing.T, params Params, l int) {
  6. actual := len(params)
  7. if actual != l {
  8. t.Fatalf("Incorrect number of params, expected %d but found %d\n", l, actual)
  9. }
  10. }
  11. func assertEq(t *testing.T, a interface{}, b interface{}) {
  12. if a != b {
  13. t.Fatalf("Values did not match, a: %v, b: %v\n", a, b)
  14. }
  15. }
  16. func TestAddNonEmpty(t *testing.T) {
  17. params := make(Params)
  18. params.AddNonEmpty("value", "value")
  19. assertLen(t, params, 1)
  20. assertEq(t, params["value"], "value")
  21. params.AddNonEmpty("test", "")
  22. assertLen(t, params, 1)
  23. assertEq(t, params["test"], "")
  24. }
  25. func TestAddNonZero(t *testing.T) {
  26. params := make(Params)
  27. params.AddNonZero("value", 1)
  28. assertLen(t, params, 1)
  29. assertEq(t, params["value"], "1")
  30. params.AddNonZero("test", 0)
  31. assertLen(t, params, 1)
  32. assertEq(t, params["test"], "")
  33. }
  34. func TestAddNonZero64(t *testing.T) {
  35. params := make(Params)
  36. params.AddNonZero64("value", 1)
  37. assertLen(t, params, 1)
  38. assertEq(t, params["value"], "1")
  39. params.AddNonZero64("test", 0)
  40. assertLen(t, params, 1)
  41. assertEq(t, params["test"], "")
  42. }
  43. func TestAddBool(t *testing.T) {
  44. params := make(Params)
  45. params.AddBool("value", true)
  46. assertLen(t, params, 1)
  47. assertEq(t, params["value"], "true")
  48. params.AddBool("test", false)
  49. assertLen(t, params, 1)
  50. assertEq(t, params["test"], "")
  51. }
  52. func TestAddNonZeroFloat(t *testing.T) {
  53. params := make(Params)
  54. params.AddNonZeroFloat("value", 1)
  55. assertLen(t, params, 1)
  56. assertEq(t, params["value"], "1.000000")
  57. params.AddNonZeroFloat("test", 0)
  58. assertLen(t, params, 1)
  59. assertEq(t, params["test"], "")
  60. }
  61. func TestAddInterface(t *testing.T) {
  62. params := make(Params)
  63. data := struct {
  64. Name string `json:"name"`
  65. }{
  66. Name: "test",
  67. }
  68. params.AddInterface("value", data)
  69. assertLen(t, params, 1)
  70. assertEq(t, params["value"], `{"name":"test"}`)
  71. params.AddInterface("test", nil)
  72. assertLen(t, params, 1)
  73. assertEq(t, params["test"], "")
  74. }
  75. func TestAddFirstValid(t *testing.T) {
  76. params := make(Params)
  77. params.AddFirstValid("value", 0, "", "test")
  78. assertLen(t, params, 1)
  79. assertEq(t, params["value"], "test")
  80. params.AddFirstValid("value2", 3, "test")
  81. assertLen(t, params, 2)
  82. assertEq(t, params["value2"], "3")
  83. }