diff --git a/event.go b/event.go index e2f4f29..45bf64d 100644 --- a/event.go +++ b/event.go @@ -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 { diff --git a/event_test.go b/event_test.go new file mode 100644 index 0000000..5aeb573 --- /dev/null +++ b/event_test.go @@ -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() + } +}