You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/03/09 06:21:35 UTC

[GitHub] [beam] youngoli commented on a change in pull request #17024: [BEAM-13904] Increase unit testing in the reflectx package

youngoli commented on a change in pull request #17024:
URL: https://github.com/apache/beam/pull/17024#discussion_r822270180



##########
File path: sdks/go/pkg/beam/core/util/reflectx/call_test.go
##########
@@ -0,0 +1,125 @@
+// 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 reflectx
+
+import (
+	"reflect"
+	"strings"
+	"testing"
+)
+
+type mapperString struct {
+	fn func(string) string
+}
+
+func mapperStringMaker(fn interface{}) Func {
+	f := fn.(func(string) string)
+	return &mapperString{fn: f}
+}
+
+func (c *mapperString) Name() string {
+	return "testMapperString"
+}
+
+func (c *mapperString) Type() reflect.Type {
+	return reflect.TypeOf(c.fn)
+}
+
+func (c *mapperString) Call(args []interface{}) []interface{} {
+	out := c.fn(args[0].(string))
+	return []interface{}{out}
+}
+
+func (c *mapperString) Call1x1(v interface{}) interface{} {
+	return c.fn(v.(string))
+}
+
+func TestMakeFunc(t *testing.T) {
+	RegisterFunc(reflect.TypeOf((*func(string) string)(nil)).Elem(), mapperStringMaker)
+	fn := func(str string) string {
+		return string(str)
+	}
+	madeFn := MakeFunc(fn)
+
+	if got, want := madeFn.Name(), "testMapperString"; got != want {
+		t.Fatalf("MakeFunc(fn).Name()=%v, want %v", got, want)
+	}
+}
+
+func TestCallNoPanic(t *testing.T) {
+	RegisterFunc(reflect.TypeOf((*func(string) string)(nil)).Elem(), mapperStringMaker)
+	fn := func(str string) string {
+		return string(str)
+	}
+	madeFn := MakeFunc(fn)
+
+	ret, err := CallNoPanic(madeFn, []interface{}{"tester"})
+	if err != nil {
+		t.Fatalf("CallNoPanic(madeFn, [\"tester\"]) errored %v, want nil", err)
+	}
+	if got, want := ret[0].(string), string("tester"); got != want {
+		t.Fatalf("CallNoPanic(madeFn, [\"tester\"]) got %v, want %v", got, want)
+	}
+}
+
+func TestCallNoPanic_Panic(t *testing.T) {
+	RegisterFunc(reflect.TypeOf((*func(string) string)(nil)).Elem(), mapperStringMaker)
+	fn := func(str string) string {
+		if str == "tester" {
+			panic("OH NO!")
+		}
+		return string(str)
+	}
+	madeFn := MakeFunc(fn)
+
+	_, err := CallNoPanic(madeFn, []interface{}{"tester"})
+	if err == nil {
+		t.Fatalf("CallNoPanic(madeFn, [\"tester\"]) didn't error when it should have")
+	}
+	if !strings.Contains(err.Error(), "OH NO!") {
+		t.Fatalf("CallNoPanic(madeFn, [\"tester\"]) errored %v, should have contained OH NO!", err)

Review comment:
       Same as above, if wrapping an error put it at the end.

##########
File path: sdks/go/pkg/beam/core/util/reflectx/call_test.go
##########
@@ -0,0 +1,125 @@
+// 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 reflectx
+
+import (
+	"reflect"
+	"strings"
+	"testing"
+)
+
+type mapperString struct {
+	fn func(string) string
+}
+
+func mapperStringMaker(fn interface{}) Func {
+	f := fn.(func(string) string)
+	return &mapperString{fn: f}
+}
+
+func (c *mapperString) Name() string {
+	return "testMapperString"
+}
+
+func (c *mapperString) Type() reflect.Type {
+	return reflect.TypeOf(c.fn)
+}
+
+func (c *mapperString) Call(args []interface{}) []interface{} {
+	out := c.fn(args[0].(string))
+	return []interface{}{out}
+}
+
+func (c *mapperString) Call1x1(v interface{}) interface{} {
+	return c.fn(v.(string))
+}
+
+func TestMakeFunc(t *testing.T) {
+	RegisterFunc(reflect.TypeOf((*func(string) string)(nil)).Elem(), mapperStringMaker)
+	fn := func(str string) string {
+		return string(str)
+	}
+	madeFn := MakeFunc(fn)
+
+	if got, want := madeFn.Name(), "testMapperString"; got != want {
+		t.Fatalf("MakeFunc(fn).Name()=%v, want %v", got, want)
+	}
+}
+
+func TestCallNoPanic(t *testing.T) {
+	RegisterFunc(reflect.TypeOf((*func(string) string)(nil)).Elem(), mapperStringMaker)
+	fn := func(str string) string {
+		return string(str)
+	}
+	madeFn := MakeFunc(fn)
+
+	ret, err := CallNoPanic(madeFn, []interface{}{"tester"})
+	if err != nil {
+		t.Fatalf("CallNoPanic(madeFn, [\"tester\"]) errored %v, want nil", err)

Review comment:
       You probably remember from the other PR, but put errors at the end of format strings, after a colon.
   
   I forgot to mention why in the other PR. It's Go convention so that you can see the source errors in sequence and easily be able to read where one ends and another begins.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org