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/05/12 14:02:54 UTC

[plc4x] branch develop updated: test(plc4go/spi): add test for buffer commons

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 b71a670a2c test(plc4go/spi): add test for buffer commons
b71a670a2c is described below

commit b71a670a2cf03452721ad14c99d67cb7a1c6ee47
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri May 12 16:02:45 2023 +0200

    test(plc4go/spi): add test for buffer commons
---
 plc4go/spi/utils/bufferCommons.go      |   3 -
 plc4go/spi/utils/bufferCommons_test.go | 298 +++++++++++++++++++++++++++++++++
 2 files changed, 298 insertions(+), 3 deletions(-)

diff --git a/plc4go/spi/utils/bufferCommons.go b/plc4go/spi/utils/bufferCommons.go
index f5bd4c8827..7f39d90b3d 100644
--- a/plc4go/spi/utils/bufferCommons.go
+++ b/plc4go/spi/utils/bufferCommons.go
@@ -81,9 +81,6 @@ func (s *Stack) Push(value any) any {
 }
 
 func (s *Stack) Pop() any {
-	if s.Len() <= 0 {
-		return nil
-	}
 	element := s.Back()
 	if element == nil {
 		return nil
diff --git a/plc4go/spi/utils/bufferCommons_test.go b/plc4go/spi/utils/bufferCommons_test.go
new file mode 100644
index 0000000000..f319d1c812
--- /dev/null
+++ b/plc4go/spi/utils/bufferCommons_test.go
@@ -0,0 +1,298 @@
+/*
+ * 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 (
+	"container/list"
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestBufferCommons_ExtractAdditionalStringRepresentation(t *testing.T) {
+	type args struct {
+		readerWriterArgs []WithReaderWriterArgs
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{
+			name: "extract nothing",
+		},
+		{
+			name: "extract the argument",
+			args: args{
+				readerWriterArgs: []WithReaderWriterArgs{
+					withAdditionalStringRepresentation{},
+				},
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			b := BufferCommons{}
+			assert.Equalf(t, tt.want, b.ExtractAdditionalStringRepresentation(tt.args.readerWriterArgs...), "ExtractAdditionalStringRepresentation(%v)", tt.args.readerWriterArgs)
+		})
+	}
+}
+
+func TestBufferCommons_IsToBeRenderedAsList(t *testing.T) {
+	type args struct {
+		readerWriterArgs []WithReaderWriterArgs
+	}
+	tests := []struct {
+		name string
+		args args
+		want bool
+	}{
+		{
+			name: "nope",
+		},
+		{
+			name: "it is not",
+			args: args{
+				readerWriterArgs: []WithReaderWriterArgs{
+					withRenderAsList{},
+				},
+			},
+		},
+		{
+			name: "it is",
+			args: args{
+				readerWriterArgs: []WithReaderWriterArgs{
+					withRenderAsList{renderAsList: true},
+				},
+			},
+			want: true,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			b := BufferCommons{}
+			assert.Equalf(t, tt.want, b.IsToBeRenderedAsList(tt.args.readerWriterArgs...), "IsToBeRenderedAsList(%v)", tt.args.readerWriterArgs)
+		})
+	}
+}
+
+func TestBufferCommons_SanitizeLogicalName(t *testing.T) {
+	type args struct {
+		logicalName string
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{
+			name: "nothing get's a value",
+			want: "value",
+		},
+		{
+			name: "something stays something",
+			args: args{
+				logicalName: "something",
+			},
+			want: "something",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			b := BufferCommons{}
+			assert.Equalf(t, tt.want, b.SanitizeLogicalName(tt.args.logicalName), "SanitizeLogicalName(%v)", tt.args.logicalName)
+		})
+	}
+}
+
+func TestStack_Empty(t *testing.T) {
+	type fields struct {
+		List list.List
+	}
+	tests := []struct {
+		name        string
+		fields      fields
+		want        bool
+		stackAssert func(t *testing.T, stack *Stack)
+	}{
+		{
+			name: "it is empty",
+			want: true,
+		},
+		{
+			name: "it is not empty",
+			fields: fields{
+				List: func() list.List {
+					l := list.List{}
+					l.PushBack("boink")
+					return l
+				}(),
+			},
+			want: false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			s := &Stack{
+				List: tt.fields.List,
+			}
+			assert.Equalf(t, tt.want, s.Empty(), "Empty()")
+			if tt.stackAssert != nil {
+				tt.stackAssert(t, s)
+			}
+		})
+	}
+}
+
+func TestStack_Peek(t *testing.T) {
+	type fields struct {
+		List list.List
+	}
+	tests := []struct {
+		name        string
+		fields      fields
+		want        any
+		stackAssert func(t *testing.T, stack *Stack)
+	}{
+		{
+			name: "empty",
+		},
+		{
+			name: "not empty",
+			fields: fields{
+				List: func() list.List {
+					l := list.List{}
+					l.PushBack("boink")
+					return l
+				}(),
+			},
+			want: "boink",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			s := &Stack{
+				List: tt.fields.List,
+			}
+			assert.Equalf(t, tt.want, s.Peek(), "Peek()")
+			if tt.stackAssert != nil {
+				tt.stackAssert(t, s)
+			}
+		})
+	}
+}
+
+func TestStack_Pop(t *testing.T) {
+	type fields struct {
+		List list.List
+	}
+	tests := []struct {
+		name        string
+		fields      fields
+		setupAssert func(t *testing.T, stack *Stack)
+		want        any
+		stackAssert func(t *testing.T, stack *Stack)
+	}{
+		{
+			name: "nothing to pop",
+			stackAssert: func(t *testing.T, stack *Stack) {
+				assert.Equal(t, stack.Len(), 0)
+			},
+		},
+		{
+			name: "something to pop",
+			setupAssert: func(t *testing.T, stack *Stack) {
+				stack.PushBack("boink")
+			},
+			want: "boink",
+			stackAssert: func(t *testing.T, stack *Stack) {
+				assert.Equal(t, stack.Len(), 0)
+			},
+		},
+		{
+			name: "something to pop with more to pop",
+			setupAssert: func(t *testing.T, stack *Stack) {
+				stack.PushBack("boink")
+				stack.PushBack("boink")
+			},
+			want: "boink",
+			stackAssert: func(t *testing.T, stack *Stack) {
+				assert.Equal(t, stack.Len(), 1)
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			s := &Stack{
+				List: tt.fields.List,
+			}
+			if tt.setupAssert != nil {
+				tt.setupAssert(t, s)
+			}
+			assert.Equalf(t, tt.want, s.Pop(), "Pop()")
+			if tt.stackAssert != nil {
+				tt.stackAssert(t, s)
+			}
+		})
+	}
+}
+
+func TestStack_Push(t *testing.T) {
+	type fields struct {
+		List list.List
+	}
+	type args struct {
+		value any
+	}
+	tests := []struct {
+		name        string
+		fields      fields
+		args        args
+		want        any
+		stackAssert func(t *testing.T, stack *Stack)
+	}{
+		{
+			name: "push nothing",
+			stackAssert: func(t *testing.T, stack *Stack) {
+				assert.Equal(t, stack.Len(), 1)
+			},
+		},
+		{
+			name: "push something",
+			args: args{
+				value: "boink",
+			},
+			want: "boink",
+			stackAssert: func(t *testing.T, stack *Stack) {
+				assert.Equal(t, stack.Len(), 1)
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			s := &Stack{
+				List: tt.fields.List,
+			}
+			assert.Equalf(t, tt.want, s.Push(tt.args.value), "Push(%v)", tt.args.value)
+			if tt.stackAssert != nil {
+				tt.stackAssert(t, s)
+			}
+		})
+	}
+}