3826: Add a couple of easy unit tests.
authorTom Clegg <tom@curoverse.com>
Tue, 14 Oct 2014 14:06:11 +0000 (10:06 -0400)
committerTom Clegg <tom@curoverse.com>
Tue, 14 Oct 2014 14:06:11 +0000 (10:06 -0400)
services/crunchstat/crunchstat_test.go [new file with mode: 0644]

diff --git a/services/crunchstat/crunchstat_test.go b/services/crunchstat/crunchstat_test.go
new file mode 100644 (file)
index 0000000..00cd4d8
--- /dev/null
@@ -0,0 +1,39 @@
+package main
+
+import (
+       "regexp"
+       "testing"
+)
+
+func TestOpenAndReadAllFail(t *testing.T) {
+       log_chan := make(chan string)
+       go func() {
+               defer close(log_chan)
+               if x, err := OpenAndReadAll("/nonexistent/file", log_chan); err == nil {
+                       t.Fatalf("Expected error, got %v", x)
+               }
+       }()
+       if _, ok := <-log_chan; !ok {
+               t.Fatalf("Expected error message about nonexistent file")
+       }
+       if msg, ok := <-log_chan; ok {
+               t.Fatalf("Expected channel to close, got %s", msg)
+       }
+}
+
+func TestOpenAndReadAllSuccess(t *testing.T) {
+       log_chan := make(chan string)
+       go func() {
+               defer close(log_chan)
+               data, err := OpenAndReadAll("./crunchstat_test.go", log_chan)
+               if err != nil {
+                       t.Fatalf("got error %s", err)
+               }
+               if matched, err := regexp.MatchString("^package main\n", string(data)); err != nil || !matched {
+                       t.Fatalf("data failed regexp: %s", err)
+               }
+       }()
+       if msg, ok := <-log_chan; ok {
+               t.Fatalf("Expected channel to close, got %s", msg)
+       }
+}