Skip to content

Commit

Permalink
Added And predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
mymmrac committed Nov 8, 2023
1 parent aecb493 commit 1c5bff5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func main() {
m := tu.Message(tu.ID(msg.Chat.ID), "Run usage:\n```/run test```\n```/run update```")
m.ParseMode = telego.ModeMarkdownV2
_, _ = bot.SendMessage(m)
}, th.Union(
}, th.Or(
th.CommandEqualArgc("run", 0),
th.CommandEqualArgv("help", "run"),
))
Expand Down
23 changes: 21 additions & 2 deletions telegohandler/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ import (
"github.com/mymmrac/telego"
)

// Union is true if at least one of the predicates is true
func Union(predicates ...Predicate) Predicate {
// And is true if all the predicates are true
func And(predicates ...Predicate) Predicate {
return func(update telego.Update) bool {
for _, p := range predicates {
if !p(update) {
return false
}
}
return true
}
}

// Or is true if at least one of the predicates is true
func Or(predicates ...Predicate) Predicate {
return func(update telego.Update) bool {
for _, p := range predicates {
if p(update) {
Expand All @@ -19,6 +31,13 @@ func Union(predicates ...Predicate) Predicate {
}
}

// Union is true if at least one of the predicates is true
//
// Deprecated: Use [Or] predicate instead
func Union(predicates ...Predicate) Predicate {
return Or(predicates...)
}

// Not is true if predicate is false
func Not(predicate Predicate) Predicate {
return func(update telego.Update) bool {
Expand Down
36 changes: 36 additions & 0 deletions telegohandler/predicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@ func TestPredicates(t *testing.T) {
update telego.Update
matches bool
}{
{
name: "and_matches",
predicate: And(
func(update telego.Update) bool { return true },
func(update telego.Update) bool { return true },
),
update: telego.Update{},
matches: true,
},
{
name: "and_not_matches",
predicate: And(
func(update telego.Update) bool { return true },
func(update telego.Update) bool { return false },
),
update: telego.Update{},
matches: false,
},
{
name: "or_matches",
predicate: Or(
func(update telego.Update) bool { return true },
func(update telego.Update) bool { return false },
),
update: telego.Update{},
matches: true,
},
{
name: "or_not_matches",
predicate: Or(
func(update telego.Update) bool { return false },
func(update telego.Update) bool { return false },
),
update: telego.Update{},
matches: false,
},
{
name: "union_matches",
predicate: Union(
Expand Down

0 comments on commit 1c5bff5

Please sign in to comment.