]> go.fuhry.dev Git - fsnotify.git/commitdiff
Add a String() func to Event.Op (#165)
authorSlawek Ligus <root@ooz.ie>
Sun, 2 Oct 2016 02:20:31 +0000 (19:20 -0700)
committerNathan Youngman <git@nathany.com>
Sun, 2 Oct 2016 02:20:31 +0000 (20:20 -0600)
* Add a String() method to Event.Op

closes #84

* Covering all 5 Op types by the test

fsnotify.go
fsnotify_test.go [new file with mode: 0644]

index d1d39a0ebdbde13814a0d43769a18cecfa8bbac3..e7f55fee7a145226a927fbab4ff197524c5e348f 100644 (file)
@@ -30,33 +30,33 @@ const (
        Chmod
 )
 
-// String returns a string representation of the event in the form
-// "file: REMOVE|WRITE|..."
-func (e Event) String() string {
+func (op Op) String() string {
        // Use a buffer for efficient string concatenation
        var buffer bytes.Buffer
 
-       if e.Op&Create == Create {
+       if op&Create == Create {
                buffer.WriteString("|CREATE")
        }
-       if e.Op&Remove == Remove {
+       if op&Remove == Remove {
                buffer.WriteString("|REMOVE")
        }
-       if e.Op&Write == Write {
+       if op&Write == Write {
                buffer.WriteString("|WRITE")
        }
-       if e.Op&Rename == Rename {
+       if op&Rename == Rename {
                buffer.WriteString("|RENAME")
        }
-       if e.Op&Chmod == Chmod {
+       if op&Chmod == Chmod {
                buffer.WriteString("|CHMOD")
        }
-
-       // If buffer remains empty, return no event names
        if buffer.Len() == 0 {
-               return fmt.Sprintf("%q: ", e.Name)
+               return ""
        }
+       return buffer.String()[1:] // Strip leading pipe
+}
 
-       // Return a list of event names, with leading pipe character stripped
-       return fmt.Sprintf("%q: %s", e.Name, buffer.String()[1:])
+// String returns a string representation of the event in the form
+// "file: REMOVE|WRITE|..."
+func (e Event) String() string {
+       return fmt.Sprintf("%q: %s", e.Name, e.Op.String())
 }
diff --git a/fsnotify_test.go b/fsnotify_test.go
new file mode 100644 (file)
index 0000000..9d6d72a
--- /dev/null
@@ -0,0 +1,40 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build !plan9
+
+package fsnotify
+
+import "testing"
+
+func TestEventStringWithValue(t *testing.T) {
+       for opMask, expectedString := range map[Op]string{
+               Chmod | Create: `"/usr/someFile": CREATE|CHMOD`,
+               Rename:         `"/usr/someFile": RENAME`,
+               Remove:         `"/usr/someFile": REMOVE`,
+               Write | Chmod:  `"/usr/someFile": WRITE|CHMOD`,
+       } {
+               event := Event{Name: "/usr/someFile", Op: opMask}
+               if event.String() != expectedString {
+                       t.Fatalf("Expected %s, got: %v", expectedString, event.String())
+               }
+
+       }
+}
+
+func TestEventOpStringWithValue(t *testing.T) {
+       expectedOpString := "WRITE|CHMOD"
+       event := Event{Name: "someFile", Op: Write | Chmod}
+       if event.Op.String() != expectedOpString {
+               t.Fatalf("Expected %s, got: %v", expectedOpString, event.Op.String())
+       }
+}
+
+func TestEventOpStringWithNoValue(t *testing.T) {
+       expectedOpString := ""
+       event := Event{Name: "testFile", Op: 0}
+       if event.Op.String() != expectedOpString {
+               t.Fatalf("Expected %s, got: %v", expectedOpString, event.Op.String())
+       }
+}