Этот коммит содержится в:
Claudio Costa
2021-06-17 09:22:16 +02:00
коммит произвёл GitHub
родитель 4b04defea6
Коммит 6bb1cbca63
3 изменённых файлов: 57 добавлений и 0 удалений

Просмотреть файл

@@ -129,6 +129,8 @@ func BotFromJson(data io.Reader) *Bot {
}
// Patch modifies an existing bot with optional fields from the given patch.
// TODO 6.0: consider returning a boolean to indicate whether or not the patch
// applied any changes.
func (b *Bot) Patch(patch *BotPatch) {
if patch.Username != nil {
b.Username = *patch.Username
@@ -143,6 +145,23 @@ func (b *Bot) Patch(patch *BotPatch) {
}
}
// WouldPatch returns whether or not the given patch would be applied or not.
func (b *Bot) WouldPatch(patch *BotPatch) bool {
if patch == nil {
return false
}
if patch.Username != nil && *patch.Username != b.Username {
return true
}
if patch.DisplayName != nil && *patch.DisplayName != b.DisplayName {
return true
}
if patch.Description != nil && *patch.Description != b.Description {
return true
}
return false
}
// ToJson serializes the bot patch to json.
func (b *BotPatch) ToJson() []byte {
data, err := json.Marshal(b)

Просмотреть файл

@@ -491,6 +491,40 @@ func TestBotPatch(t *testing.T) {
}
}
func TestBotWouldPatch(t *testing.T) {
b := &Bot{
UserId: NewId(),
}
t.Run("nil patch", func(t *testing.T) {
ok := b.WouldPatch(nil)
require.False(t, ok)
})
t.Run("nil patch fields", func(t *testing.T) {
patch := &BotPatch{}
ok := b.WouldPatch(patch)
require.False(t, ok)
})
t.Run("patch", func(t *testing.T) {
patch := &BotPatch{
DisplayName: NewString("BotName"),
}
ok := b.WouldPatch(patch)
require.True(t, ok)
})
t.Run("no patch", func(t *testing.T) {
patch := &BotPatch{
DisplayName: NewString("BotName"),
}
b.Patch(patch)
ok := b.WouldPatch(patch)
require.False(t, ok)
})
}
func TestBotPatchToAndFromJson(t *testing.T) {
botPatch1 := &BotPatch{
Username: sToP("username"),