You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ne...@apache.org on 2017/01/13 23:36:14 UTC

[18/29] incubator-trafficcontrol git commit: Add some helper functions to make it easier to write tests

Add some helper functions to make it easier to write tests


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/b678410c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/b678410c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/b678410c

Branch: refs/heads/master
Commit: b678410cccc6c93940a597b797b8363b66eda66a
Parents: 4563a04
Author: sbogacz <sb...@zvelo.com>
Authored: Wed Jan 11 22:06:53 2017 -0700
Committer: David Neuman <da...@gmail.com>
Committed: Fri Jan 13 23:33:56 2017 +0000

----------------------------------------------------------------------
 traffic_stats/assert/testhelpers.go | 109 +++++++++++++++++++++++++++++++
 1 file changed, 109 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/b678410c/traffic_stats/assert/testhelpers.go
----------------------------------------------------------------------
diff --git a/traffic_stats/assert/testhelpers.go b/traffic_stats/assert/testhelpers.go
new file mode 100644
index 0000000..cc5f1e1
--- /dev/null
+++ b/traffic_stats/assert/testhelpers.go
@@ -0,0 +1,109 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+package assert
+
+import (
+	"reflect"
+	"testing"
+)
+
+// NotNil takes a value, and checks whether that value is not nil, failing the test
+// if not
+func NotNil(t *testing.T, a interface{}) {
+	if a == nil {
+		t.Errorf("expected %v to be non-nil, but it is", a)
+	}
+}
+
+// Nil takes a value, and checks whether that value is nil, failing the test
+// if not
+func Nil(t *testing.T, a interface{}) {
+	if a != nil {
+		t.Errorf("expected %v to be nil, but it's not", a)
+	}
+}
+
+// Equal takes two values, checks whether they are equal (using a deep equal
+// check) and fails the test if not
+func Equal(t *testing.T, a, b interface{}) {
+	if !reflect.DeepEqual(a, b) {
+		t.Errorf("expected %v to equal %v, but it does not", a, b)
+	}
+}
+
+// Empty takes a value and checks whether it is empty for the given type
+// supports slices, arrays, channels, strings, and maps
+func Empty(t *testing.T, a interface{}) {
+	val := reflect.ValueOf(a)
+	switch val.Kind() {
+	case reflect.Slice:
+		if val.Len() != 0 {
+			t.Errorf("expected %v to be empty, but it is not", a)
+		}
+	case reflect.Array:
+		if val.Len() != 0 {
+			t.Errorf("expected %v to be empty, but it is not", a)
+		}
+	case reflect.Chan:
+		if val.Len() != 0 {
+			t.Errorf("expected %v to be empty, but it is not", a)
+		}
+	case reflect.String:
+		if val.Len() != 0 {
+			t.Errorf("expected %v to be empty, but it is not", a)
+		}
+	case reflect.Map:
+		if val.Len() != 0 {
+			t.Errorf("expected %v to be empty, but it is not", a)
+		}
+	default:
+		t.Errorf("can't check that %v of type %T is empty", a, a)
+	}
+}
+
+// NotEmpty takes a value and checks whether it isvnot empty for the given type
+// supports slices, arrays, channels, strings, and maps
+func NotEmpty(t *testing.T, a interface{}) {
+	val := reflect.ValueOf(a)
+	switch val.Kind() {
+	case reflect.Slice:
+		if val.Len() == 0 {
+			t.Errorf("expected %v to not be empty, but it is", a)
+		}
+	case reflect.Array:
+		if val.Len() == 0 {
+			t.Errorf("expected %v to not be empty, but it is", a)
+		}
+	case reflect.Chan:
+		if val.Len() == 0 {
+			t.Errorf("expected %v to not be empty, but it is", a)
+		}
+	case reflect.String:
+		if val.Len() == 0 {
+			t.Errorf("expected %v to not be empty, but it is", a)
+		}
+	case reflect.Map:
+		if val.Len() == 0 {
+			t.Errorf("expected %v to not be empty, but it is", a)
+		}
+	default:
+		t.Errorf("can't check that %v of type %T is not empty", a, a)
+	}
+}