mock_hand_sub_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package mock_hand_sub_local
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. type tester struct {
  7. t *testing.T
  8. hand *MockHandlerSub
  9. }
  10. func TestMockHandleServe(t *testing.T) {
  11. sf := &tester{
  12. t: t,
  13. }
  14. sf.new()
  15. sf.back()
  16. }
  17. // Проверка обратного вызова
  18. func (sf *tester) back() {
  19. sf.t.Log("back")
  20. sf.backGood1()
  21. }
  22. func (sf *tester) backGood1() {
  23. sf.t.Log("backGood1")
  24. sf.hand.FnBack([]byte("test_msg"))
  25. if sf.hand.Msg() != "test_msg" {
  26. sf.t.Fatalf("backGood1(): binMsg(%v)!='test_msg'", string(sf.hand.Msg_))
  27. }
  28. }
  29. // Создание мок-обработчика запросов
  30. func (sf *tester) new() {
  31. sf.t.Log("new")
  32. sf.newBad1()
  33. sf.newGood1()
  34. }
  35. func (sf *tester) newGood1() {
  36. sf.t.Log("newGood1")
  37. sf.hand = NewMockHandlerSub("test_topic", "test_name")
  38. if sf.hand == nil {
  39. sf.t.Fatalf("newGood1(): handler==nil")
  40. }
  41. if name := sf.hand.Name(); !strings.Contains(string(name), "test_name_") {
  42. sf.t.Fatalf("newGood1(): name(%v)!='test_name_'", name)
  43. }
  44. if topic := sf.hand.Topic(); topic != "test_topic" {
  45. sf.t.Fatalf("newGood1(): topic(%v)!='test_topic'", topic)
  46. }
  47. if msg := sf.hand.Msg(); msg != "" {
  48. sf.t.Fatalf("newGood1(): msg not empty")
  49. }
  50. }
  51. // Нет топика для создания
  52. func (sf *tester) newBad1() {
  53. sf.t.Log("newBad1")
  54. defer func() {
  55. if _panic := recover(); _panic == nil {
  56. sf.t.Fatalf("")
  57. }
  58. }()
  59. _ = NewMockHandlerSub("", "test_name")
  60. }