1
0

numeric_input_test.go 1018 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package tgbot
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. func TestUpdateNumericInput(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. value, key int
  11. want int
  12. }{
  13. {name: "append digit", value: 12, key: 3, want: 123},
  14. {name: "append zero", value: 12, key: 0, want: 120},
  15. {name: "backspace", value: 123, key: -1, want: 12},
  16. {name: "backspace zero", value: 0, key: -1, want: 0},
  17. {name: "clear", value: 123, key: -2, want: 0},
  18. }
  19. for _, tt := range tests {
  20. t.Run(tt.name, func(t *testing.T) {
  21. if got := updateNumericInput(tt.value, tt.key); got != tt.want {
  22. t.Fatalf("updateNumericInput(%d, %d) = %d, want %d", tt.value, tt.key, got, tt.want)
  23. }
  24. })
  25. }
  26. }
  27. func TestNumericInputTransitionIsUsedByEveryKeypad(t *testing.T) {
  28. source, err := os.ReadFile("tgbot_router.go")
  29. if err != nil {
  30. t.Fatalf("read tgbot_router.go: %v", err)
  31. }
  32. if got := strings.Count(string(source), "updateNumericInput("); got != 6 {
  33. t.Fatalf("numeric keypad transition call sites = %d, want 6", got)
  34. }
  35. }