You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2023/04/27 14:15:36 UTC

[plc4x] branch develop updated: test(plc4go/spi): add tests for misc

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 96b338e621 test(plc4go/spi): add tests for misc
96b338e621 is described below

commit 96b338e62104e6ca262c6197c300dc04dc95855a
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Apr 27 16:15:26 2023 +0200

    test(plc4go/spi): add tests for misc
---
 plc4go/spi/utils/Misc.go      |   3 +
 plc4go/spi/utils/Misc_test.go | 136 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+)

diff --git a/plc4go/spi/utils/Misc.go b/plc4go/spi/utils/Misc.go
index 46971f2c53..732851d0fb 100644
--- a/plc4go/spi/utils/Misc.go
+++ b/plc4go/spi/utils/Misc.go
@@ -37,6 +37,9 @@ func InlineIf(test bool, a func() any, b func() any) any {
 //
 //	and is safe to call even if the channel has already been received
 func CleanupTimer(timer *time.Timer) {
+	if timer == nil {
+		return
+	}
 	if !timer.Stop() {
 		select {
 		case <-timer.C:
diff --git a/plc4go/spi/utils/Misc_test.go b/plc4go/spi/utils/Misc_test.go
new file mode 100644
index 0000000000..7bfa8be8a0
--- /dev/null
+++ b/plc4go/spi/utils/Misc_test.go
@@ -0,0 +1,136 @@
+/*
+ * 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
+ *
+ *   https://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 utils
+
+import (
+	"github.com/stretchr/testify/assert"
+	"golang.org/x/exp/constraints"
+	"testing"
+	"time"
+)
+
+func TestCleanupTimer(t *testing.T) {
+	type args struct {
+		timer *time.Timer
+	}
+	tests := []struct {
+		name string
+		args args
+	}{
+		{
+			name: "nil safety",
+		},
+		{
+			name: "running timer",
+			args: args{
+				timer: time.NewTimer(100000 * time.Hour),
+			},
+		},
+		{
+			name: "completed timer",
+			args: args{
+				timer: func() *time.Timer {
+					timer := time.NewTimer(0)
+					timer.Stop()
+					return timer
+				}(),
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			CleanupTimer(tt.args.timer)
+		})
+	}
+}
+
+func TestInlineIf(t *testing.T) {
+	type args struct {
+		test bool
+		a    func() any
+		b    func() any
+	}
+	tests := []struct {
+		name string
+		args args
+		want any
+	}{
+		{
+			name: "take a",
+			args: args{
+				test: true,
+				a: func() any {
+					t.Log("alright")
+					return "you got it"
+				},
+			},
+			want: "you got it",
+		},
+		{
+			name: "take b",
+			args: args{
+				test: false,
+				b: func() any {
+					t.Log("alright")
+					return "you got it"
+				},
+			},
+			want: "you got it",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			assert.Equalf(t, tt.want, InlineIf(tt.args.test, tt.args.a, tt.args.b), "InlineIf(%v, %v, %v)", tt.args.test, tt.args.a, tt.args.b)
+		})
+	}
+}
+
+func TestMin(t *testing.T) {
+	type args[T constraints.Ordered] struct {
+		left  T
+		right T
+	}
+	type testCase[T constraints.Ordered] struct {
+		name string
+		args args[T]
+		want T
+	}
+	tests := []testCase[string]{
+		{
+			args: args[string]{
+				"a",
+				"b",
+			},
+			want: "a",
+		},
+		{
+			args: args[string]{
+				"b",
+				"a",
+			},
+			want: "a",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			assert.Equalf(t, tt.want, Min(tt.args.left, tt.args.right), "Min(%v, %v)", tt.args.left, tt.args.right)
+		})
+	}
+}