CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package tgbot
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestUpdateNumericInput(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
value, key int
|
|
want int
|
|
}{
|
|
{name: "append digit", value: 12, key: 3, want: 123},
|
|
{name: "append zero", value: 12, key: 0, want: 120},
|
|
{name: "backspace", value: 123, key: -1, want: 12},
|
|
{name: "backspace zero", value: 0, key: -1, want: 0},
|
|
{name: "clear", value: 123, key: -2, want: 0},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := updateNumericInput(tt.value, tt.key); got != tt.want {
|
|
t.Fatalf("updateNumericInput(%d, %d) = %d, want %d", tt.value, tt.key, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNumericInputTransitionIsUsedByEveryKeypad(t *testing.T) {
|
|
entries, err := os.ReadDir(".")
|
|
if err != nil {
|
|
t.Fatalf("read tgbot package: %v", err)
|
|
}
|
|
fset := token.NewFileSet()
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
if entry.IsDir() || filepath.Ext(name) != ".go" || name == "numeric_input.go" || filepath.Ext(strings.TrimSuffix(name, "_test.go")) != ".go" {
|
|
continue
|
|
}
|
|
parsed, err := parser.ParseFile(fset, name, nil, 0)
|
|
if err != nil {
|
|
t.Fatalf("parse %s: %v", name, err)
|
|
}
|
|
ast.Inspect(parsed, func(node ast.Node) bool {
|
|
switchStmt, ok := node.(*ast.SwitchStmt)
|
|
if !ok {
|
|
return true
|
|
}
|
|
hasClear, hasBackspace, hasDefault := false, false, false
|
|
for _, stmt := range switchStmt.Body.List {
|
|
clause := stmt.(*ast.CaseClause)
|
|
if clause.List == nil {
|
|
hasDefault = true
|
|
}
|
|
for _, expr := range clause.List {
|
|
hasClear = hasClear || numericKeyLiteral(expr, "2")
|
|
hasBackspace = hasBackspace || numericKeyLiteral(expr, "1")
|
|
}
|
|
}
|
|
if hasClear && hasBackspace && hasDefault {
|
|
position := fset.Position(switchStmt.Pos())
|
|
t.Errorf("open-coded numeric keypad transition at %s; use updateNumericInput", position)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
|
|
func numericKeyLiteral(expr ast.Expr, magnitude string) bool {
|
|
unary, ok := expr.(*ast.UnaryExpr)
|
|
if !ok || unary.Op != token.SUB {
|
|
return false
|
|
}
|
|
literal, ok := unary.X.(*ast.BasicLit)
|
|
return ok && literal.Kind == token.INT && literal.Value == magnitude
|
|
}
|