Repository: SimonBaeumer/commander
Pull request #16 (2019-03-07 20:54:31)

Add validator tests


Patch:
diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go
index 9192041..cb09f69 100644
--- a/pkg/runtime/runtime.go
+++ b/pkg/runtime/runtime.go
@@ -88,19 +88,18 @@ func Start(tests []TestCase) <-chan TestResult {
     var wg sync.WaitGroup
     for i := 0; i < workerCount; i++ {
         wg.Add(1)
-        go func() {
+        go func(tests chan TestCase) {
             defer wg.Done()
-            for t := range in {
+            for t := range tests {
                 out <- runTest(t)
             }
-
-        }()
+        }(in)
     }
 
-    go func() {
+    go func(results chan TestResult) {
         wg.Wait()
-        close(out)
-    }()
+        close(results)
+    }(out)
 
     return out
 }
diff --git a/pkg/runtime/validator.go b/pkg/runtime/validator.go
index 121fe97..c34a356 100644
--- a/pkg/runtime/validator.go
+++ b/pkg/runtime/validator.go
@@ -10,7 +10,7 @@ type ValidationResult struct {
 	Diff    string
 }
 
-func NewValidationResult(m matcher.MatcherResult) ValidationResult {
+func newValidationResult(m matcher.MatcherResult) ValidationResult {
 	return ValidationResult{
 		Success: m.Success,
 		Diff:    m.Diff,
@@ -23,7 +23,7 @@ func Validate(test TestCase) TestResult {
     matcherResult := validateExpectedOut(test.Result.Stdout, test.Expected.Stdout)
     if !matcherResult.Success {
         return TestResult{
-            ValidationResult: NewValidationResult(matcherResult),
+            ValidationResult: newValidationResult(matcherResult),
             TestCase:         test,
             FailedProperty:   Stdout,
         }
@@ -32,7 +32,7 @@ func Validate(test TestCase) TestResult {
     matcherResult = validateExpectedOut(test.Result.Stderr, test.Expected.Stderr)
     if !matcherResult.Success {
         return TestResult{
-            ValidationResult: NewValidationResult(matcherResult),
+            ValidationResult: newValidationResult(matcherResult),
             TestCase:         test,
             FailedProperty:   Stderr,
         }
@@ -41,14 +41,14 @@ func Validate(test TestCase) TestResult {
     matcherResult = equalMatcher.Match(test.Result.ExitCode, test.Expected.ExitCode)
     if !matcherResult.Success {
         return TestResult{
-            ValidationResult: NewValidationResult(matcherResult),
+            ValidationResult: newValidationResult(matcherResult),
             TestCase:         test,
             FailedProperty:   ExitCode,
         }
     }
 
     return TestResult{
-        ValidationResult: NewValidationResult(matcherResult),
+        ValidationResult: newValidationResult(matcherResult),
         TestCase:         test,
     }
 }


Test patch:
diff --git a/pkg/runtime/runtime_test.go b/pkg/runtime/runtime_test.go
index 6db2fb7..6e10000 100644
--- a/pkg/runtime/runtime_test.go
+++ b/pkg/runtime/runtime_test.go
@@ -10,6 +10,14 @@ func TestRuntime_Start(t *testing.T) {
     got := Start(s)
 
     assert.IsType(t, make(<-chan TestResult), got)
+
+    count := 0
+    for r := range got {
+        assert.Equal(t, "Output hello", r.TestCase.Title)
+        assert.True(t, r.ValidationResult.Success)
+        count++
+    }
+    assert.Equal(t, 1, count)
 }
 
 func getExampleTestSuite() []TestCase {
@@ -30,3 +38,4 @@ func getExampleTestSuite() []TestCase {
     return tests
 }
 
+
diff --git a/pkg/runtime/validator_test.go b/pkg/runtime/validator_test.go
new file mode 100644
index 0000000..9f48f88
--- /dev/null
+++ b/pkg/runtime/validator_test.go
@@ -0,0 +1,62 @@
+package runtime
+
+import (
+    "github.com/SimonBaeumer/commander/pkg/matcher"
+    "github.com/stretchr/testify/assert"
+    "testing"
+)
+
+func TestNewValidationResult(t *testing.T) {
+    mr := matcher.MatcherResult{}
+    got := newValidationResult(mr)
+    assert.IsType(t, ValidationResult{}, got)
+}
+
+func Test_Validate(t *testing.T) {
+    test := getExampleTest()
+
+    got := Validate(test)
+
+    assert.True(t, got.ValidationResult.Success)
+    assert.Equal(t, test, got.TestCase)
+}
+
+func Test_ValidateStdoutShouldFail(t *testing.T) {
+    test := getExampleTest()
+    test.Result = CommandResult{
+        Stdout: "hello\nline2",
+        Stderr: "error",
+        ExitCode: 0,
+    }
+
+    got := Validate(test)
+
+    assert.False(t, got.ValidationResult.Success)
+    assert.Equal(t, "Stdout", got.FailedProperty)
+}
+
+func getExampleTest() TestCase {
+    test := TestCase{
+        Expected: Expected{
+            Stdout: ExpectedOut{
+                Lines:     map[int]string{0: "hello"},
+                LineCount: 1,
+                Exactly:   "hello",
+                Contains:  []string{"hello"},
+            },
+            Stderr: ExpectedOut{
+                Lines:     map[int]string{0: "error"},
+                LineCount: 1,
+                Exactly:   "error",
+                Contains:  []string{"error"},
+            },
+            LineCount: 1,
+        },
+        Result: CommandResult{
+            Stdout:   "hello",
+            Stderr:   "error",
+            ExitCode: 0,
+        },
+    }
+    return test
+}
\ No newline at end of file