Skip to content

Commit

Permalink
Implemented event.IsAny(mask, ...), IsAll(mask, ...), Is(mask) to mak…
Browse files Browse the repository at this point in the history
…e it easier for the end customer
  • Loading branch information
illarion committed Aug 21, 2024
1 parent 85059bf commit 2c7002a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ func (i InotifyEvent) String() string {
return fmt.Sprintf("{Wd=%d, Name=%s, Cookie=%d, Mask=%s}", i.Wd, i.Name, i.Cookie, InMaskToString(i.Mask))
}

// IsAny returns true if any of the in_mask is set in the event
func (i InotifyEvent) IsAny(in_mask ...uint32) bool {
for _, mask := range in_mask {
if i.Mask&mask == mask {
return true
}
}
return false
}

// IsAll returns true if all the in_masks is set in the event
func (i InotifyEvent) IsAll(in_mask ...uint32) bool {
for _, mask := range in_mask {
if i.Mask&mask != mask {
return false
}
}
return true
}

func (i InotifyEvent) Is(in_mask uint32) bool {
return i.Mask&in_mask == in_mask
}

// FileEvent is the wrapper around InotifyEvent with additional Eof marker. Reading from
// FileEvents from DirWatcher.C or FileWatcher.C may end with Eof when underlying inotify is closed
type FileEvent struct {
Expand Down
48 changes: 48 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gonotify

import (
"strings"
"testing"
)

// TestInotifyEvent_Is tests the Is function of InotifyEvent
func TestInotifyEvent_Is(t *testing.T) {
var i InotifyEvent
i.Mask = IN_ACCESS
if !i.Is(IN_ACCESS) {
t.Fail()
}
}

// TestInotifyEvent_IsAny tests the IsAny function of InotifyEvent
func TestInotifyEvent_IsAny(t *testing.T) {
var i InotifyEvent
i.Mask = IN_ACCESS
if !i.IsAny(IN_ACCESS, IN_ATTRIB) {
t.Fail()
}
}

// TestInotifyEvent_IsAll tests the IsAll function of InotifyEvent
func TestInotifyEvent_IsAll(t *testing.T) {
var i InotifyEvent
i.Mask = IN_ACCESS | IN_ATTRIB
if !i.IsAll(IN_ACCESS, IN_ATTRIB) {
t.Fail()
}
}

// TestInotifyEvent_String tests the String function of InotifyEvent
func TestInotifyEvent_String(t *testing.T) {
var i InotifyEvent
i.Mask = IN_ACCESS | IN_ATTRIB | IN_CLOSE_WRITE
if !strings.Contains(i.String(), "IN_ACCESS") {
t.Fail()
}
if !strings.Contains(i.String(), "IN_ATTRIB") {
t.Fail()
}
if !strings.Contains(i.String(), "IN_CLOSE_WRITE") {
t.Fail()
}
}

0 comments on commit 2c7002a

Please sign in to comment.