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 2021/05/03 11:43:35 UTC
[plc4x] branch develop updated (fc680ca -> 6f03788)
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git.
from fc680ca plc4go: implemented write support for SingleItemRequestInterceptor.go
new 8271aa4 plc4go: small cleanup
new bb4484c plc4go: added the possibility to get the name and change the name of a asciibox
new 5730d30 plc4go: fixed a bug in unwrap of AsciiBox where boxes within boxes got wrongfully trimmed
new 6f03788 plc4go: added an option to WriteBufferBoxBased.go to print boxes in a compact form
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../plc4go/ads/readwrite/model/AdsStaticHelper.go | 6 +-
.../plc4go/s7/readwrite/model/S7StaticHelper.go | 6 +-
plc4go/internal/plc4go/s7/s7Io_test.go | 63 ++++++-
.../plc4go/spi/utils/WriteBufferBoxBased.go | 21 ++-
plc4go/internal/plc4go/spi/utils/asciiBox.go | 189 ++++++++++++++++-----
plc4go/internal/plc4go/spi/utils/asciiBox_test.go | 106 ++++++++++++
6 files changed, 326 insertions(+), 65 deletions(-)
[plc4x] 03/04: plc4go: fixed a bug in unwrap of AsciiBox where
boxes within boxes got wrongfully trimmed
Posted by sr...@apache.org.
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
commit 5730d30a08264e4b06be30fffdbb674c9ac92276
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon May 3 13:42:10 2021 +0200
plc4go: fixed a bug in unwrap of AsciiBox where boxes within boxes got wrongfully trimmed
---
plc4go/internal/plc4go/spi/utils/asciiBox.go | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/plc4go/internal/plc4go/spi/utils/asciiBox.go b/plc4go/internal/plc4go/spi/utils/asciiBox.go
index 5b86899..7ddc2a3 100644
--- a/plc4go/internal/plc4go/spi/utils/asciiBox.go
+++ b/plc4go/internal/plc4go/spi/utils/asciiBox.go
@@ -308,7 +308,12 @@ func unwrap(box AsciiBox) AsciiBox {
}
runes := []rune(line)
// Strip the vertical Lines and trim the padding
- newLines[i-1] = strings.Trim(string(runes[1:len(runes)-1]), emptyPadding)
+ unwrappedLine := string(runes[1 : len(runes)-1])
+ if !strings.ContainsAny(unwrappedLine, verticalLine+horizontalLine) {
+ // only trim boxes witch don't contain other boxes
+ unwrappedLine = strings.Trim(unwrappedLine, emptyPadding)
+ }
+ newLines[i-1] = unwrappedLine
}
return AsciiBox(strings.Join(newLines, string(newLine)))
}
[plc4x] 04/04: plc4go: added an option to WriteBufferBoxBased.go to
print boxes in a compact form
Posted by sr...@apache.org.
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
commit 6f03788195589ae6e1e6e139a73f44921717e916
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon May 3 13:43:21 2021 +0200
plc4go: added an option to WriteBufferBoxBased.go to print boxes in a compact form
---
plc4go/internal/plc4go/s7/s7Io_test.go | 63 +++++++++++++++++++---
.../plc4go/spi/utils/WriteBufferBoxBased.go | 21 +++++++-
2 files changed, 75 insertions(+), 9 deletions(-)
diff --git a/plc4go/internal/plc4go/s7/s7Io_test.go b/plc4go/internal/plc4go/s7/s7Io_test.go
index ad8a63b..65eb2ed 100644
--- a/plc4go/internal/plc4go/s7/s7Io_test.go
+++ b/plc4go/internal/plc4go/s7/s7Io_test.go
@@ -38,13 +38,14 @@ func TestS7MessageBytes(t *testing.T) {
debuggable debuggable
}
tests := []struct {
- name string
- args args
- wantString string
- wantStringSerialized string
- wantStringXml string
- wantStringJson string
- wantDump string
+ name string
+ args args
+ wantString string
+ wantStringSerialized string
+ wantStringSerializedCompact string
+ wantStringXml string
+ wantStringJson string
+ wantDump string
}{
{
name: "TPKT Packet with Read var response data",
@@ -156,6 +157,43 @@ func TestS7MessageBytes(t *testing.T) {
║╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝║
╚══════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
`,
+ wantStringSerializedCompact: `
+╔═TPKTPacket══════════════════════════════════════════════════════════════════════════════════════════════════╗
+║╔═protocolId╗╔═reserved╗╔═len═════╗ ║
+║║ 0x03 3 ║║ 0x00 0 ║║0x001d 29║ ║
+║╚═══════════╝╚═════════╝╚═════════╝ ║
+║╔═payload/COTPPacket════════════════════════════════════════════════════════════════════════════════════════╗║
+║║╔═headerLength╗╔═tpduCode╗╔═COTPPacketData═════╗ ║║
+║║║ 0x05 5 ║║0xf0 240 ║║╔═eot════╗╔═tpduRef╗║ ║║
+║║╚═════════════╝╚═════════╝║║b0 false║║ 0xd 13 ║║ ║║
+║║ ║╚════════╝╚════════╝║ ║║
+║║ ╚════════════════════╝ ║║
+║║╔═parameters/COTPParameter════════════════════════════════════════════════════════════╗ ║║
+║║║╔═parameterType╗╔═parameterLength╗╔═COTPParameterTpduSize/tpduSize/COTPTpduSize═════╗║ ║║
+║║║║ 0xc0 192 ║║ 0x01 1 ║║ 0x0c 12 SIZE_4096 ║║ ║║
+║║║╚══════════════╝╚════════════════╝╚═════════════════════════════════════════════════╝║ ║║
+║║╚═════════════════════════════════════════════════════════════════════════════════════╝ ║║
+║║╔═S7Message═══════════════════════════════════════════════════════════════════════════════════════════════╗║║
+║║║╔═protocolId╗╔═messageType╗╔═reserved╗╔═tpduReference╗╔═parameterLength╗╔═payloadLength╗ ║║║
+║║║║ 0x32 50 ║║ 0x03 3 ║║0x0000 0 ║║ 0x000b 11 ║║ 0x0002 2 ║║ 0x0005 5 ║ ║║║
+║║║╚═══════════╝╚════════════╝╚═════════╝╚══════════════╝╚════════════════╝╚══════════════╝ ║║║
+║║║╔═S7MessageResponseData═══╗╔═S7Parameter═════════════════════════════════════════════╗ ║║║
+║║║║╔═errorClass╗╔═errorCode╗║║╔═parameterType╗╔═S7ParameterReadVarResponse/numItems═══╗║ ║║║
+║║║║║ 0x00 0 ║║ 0x00 0 ║║║║ 0x04 4 ║║ 0x01 1 ║║ ║║║
+║║║║╚═══════════╝╚══════════╝║║╚══════════════╝╚═══════════════════════════════════════╝║ ║║║
+║║║╚═════════════════════════╝╚═════════════════════════════════════════════════════════╝ ║║║
+║║║╔═S7Payload/S7PayloadReadVarResponse/items/S7VarPayloadDataItem═════════════════════════════════════════╗║║║
+║║║║╔═returnCode/DataTransportErrorCode════════════╗╔═transportSize/DataTransportSize════════╗╔═dataLength╗║║║║
+║║║║║ 0xff 255 OK ║║ 0x03 3 BIT ║║ 0x0001 1 ║║║║║
+║║║║╚══════════════════════════════════════════════╝╚════════════════════════════════════════╝╚═══════════╝║║║║
+║║║║╔═data/╗╔═padding╗ ║║║║
+║║║║║0x01 1║║ ║ ║║║║
+║║║║╚══════╝╚════════╝ ║║║║
+║║║╚═══════════════════════════════════════════════════════════════════════════════════════════════════════╝║║║
+║║╚═════════════════════════════════════════════════════════════════════════════════════════════════════════╝║║
+║╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════╝║
+╚═════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
+`,
wantStringXml: `
<TPKTPacket>
<protocolId dataType="uint" bitLength="8">3</protocolId>
@@ -373,6 +411,17 @@ func TestS7MessageBytes(t *testing.T) {
}
}
{
+ // Simple 2 Compact Box
+ boxWriter := utils.NewBoxedWriteBufferWithOptions(true)
+ if err := tt.args.debuggable.Serialize(boxWriter); err != nil {
+ t.Error(err)
+ }
+ tt.wantStringSerializedCompact = strings.Trim(tt.wantStringSerializedCompact, "\n")
+ if got := string(boxWriter.GetBox()); got != tt.wantStringSerializedCompact {
+ t.Errorf("Serialize Boxed() = '\n%v\n', want '\n%v\n'", got, tt.wantStringSerializedCompact)
+ }
+ }
+ {
// Simple 2 Xml
xmlWriteBuffer := utils.NewXmlWriteBuffer()
if err := tt.args.debuggable.Serialize(xmlWriteBuffer); err != nil {
diff --git a/plc4go/internal/plc4go/spi/utils/WriteBufferBoxBased.go b/plc4go/internal/plc4go/spi/utils/WriteBufferBoxBased.go
index 57815a0..abcebb9 100644
--- a/plc4go/internal/plc4go/spi/utils/WriteBufferBoxBased.go
+++ b/plc4go/internal/plc4go/spi/utils/WriteBufferBoxBased.go
@@ -38,6 +38,15 @@ func NewBoxedWriteBuffer() WriteBufferBoxBased {
}
}
+func NewBoxedWriteBufferWithOptions(mergeSingleBoxes bool) WriteBufferBoxBased {
+ return &boxedWriteBuffer{
+ List: list.New(),
+ desiredWidth: 120,
+ currentWidth: 118,
+ mergeSingleBoxes: mergeSingleBoxes,
+ }
+}
+
///////////////////////////////////////
///////////////////////////////////////
//
@@ -47,8 +56,9 @@ func NewBoxedWriteBuffer() WriteBufferBoxBased {
type boxedWriteBuffer struct {
bufferCommons
*list.List
- desiredWidth int
- currentWidth int
+ desiredWidth int
+ currentWidth int
+ mergeSingleBoxes bool
}
//
@@ -174,6 +184,13 @@ findTheBox:
panic("We should never reach this point")
}
}
+ if b.mergeSingleBoxes && len(finalBoxes) == 1 {
+ onlyChild := finalBoxes[0]
+ childName := onlyChild.GetBoxName()
+ onlyChild = onlyChild.ChangeBoxName(logicalName + "/" + childName)
+ b.PushBack(onlyChild)
+ return nil
+ }
asciiBox := BoxBox(logicalName, AlignBoxes(finalBoxes, b.currentWidth), 0)
b.PushBack(asciiBox)
return nil
[plc4x] 01/04: plc4go: small cleanup
Posted by sr...@apache.org.
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
commit 8271aa451f84ea5d0ac4a7ce801c0b026fefaa37
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon May 3 11:04:58 2021 +0200
plc4go: small cleanup
---
plc4go/internal/plc4go/ads/readwrite/model/AdsStaticHelper.go | 6 +-----
plc4go/internal/plc4go/s7/readwrite/model/S7StaticHelper.go | 6 +-----
2 files changed, 2 insertions(+), 10 deletions(-)
diff --git a/plc4go/internal/plc4go/ads/readwrite/model/AdsStaticHelper.go b/plc4go/internal/plc4go/ads/readwrite/model/AdsStaticHelper.go
index 99a28c0..4a71694 100644
--- a/plc4go/internal/plc4go/ads/readwrite/model/AdsStaticHelper.go
+++ b/plc4go/internal/plc4go/ads/readwrite/model/AdsStaticHelper.go
@@ -32,11 +32,7 @@ func StaticHelperParseAmsString(io utils.ReadBuffer, stringLength int32, encodin
case "UTF-16":
multiplier = 16
}
- readString, err := io.ReadString("", uint32(stringLength*multiplier))
- if err != nil {
- return "", err
- }
- return readString, nil
+ return io.ReadString("", uint32(stringLength*multiplier))
}
func StaticHelperSerializeAmsString(io utils.WriteBuffer, value values.PlcValue, stringLength int32, encoding string) error {
diff --git a/plc4go/internal/plc4go/s7/readwrite/model/S7StaticHelper.go b/plc4go/internal/plc4go/s7/readwrite/model/S7StaticHelper.go
index 580d6f3..2b72fe9 100644
--- a/plc4go/internal/plc4go/s7/readwrite/model/S7StaticHelper.go
+++ b/plc4go/internal/plc4go/s7/readwrite/model/S7StaticHelper.go
@@ -130,11 +130,7 @@ func StaticHelperParseS7String(io utils.ReadBuffer, stringLength int32, encoding
case "UTF-16":
multiplier = 16
}
- readString, err := io.ReadString("", uint32(stringLength*multiplier))
- if err != nil {
- return "", err
- }
- return readString, nil
+ return io.ReadString("", uint32(stringLength*multiplier))
}
func StaticHelperSerializeS7String(io utils.WriteBuffer, value values.PlcValue, stringLength int32, encoding string) error {
[plc4x] 02/04: plc4go: added the possibility to get the name and
change the name of a asciibox
Posted by sr...@apache.org.
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
commit bb4484c23215cd2d335355e3828f1765eb9702a6
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon May 3 13:25:02 2021 +0200
plc4go: added the possibility to get the name and change the name of a asciibox
---
plc4go/internal/plc4go/spi/utils/asciiBox.go | 184 ++++++++++++++++------
plc4go/internal/plc4go/spi/utils/asciiBox_test.go | 106 +++++++++++++
2 files changed, 244 insertions(+), 46 deletions(-)
diff --git a/plc4go/internal/plc4go/spi/utils/asciiBox.go b/plc4go/internal/plc4go/spi/utils/asciiBox.go
index c7b8801..5b86899 100644
--- a/plc4go/internal/plc4go/spi/utils/asciiBox.go
+++ b/plc4go/internal/plc4go/spi/utils/asciiBox.go
@@ -22,6 +22,7 @@ package utils
import (
"github.com/rs/zerolog/log"
"math"
+ "regexp"
"strings"
)
@@ -59,6 +60,14 @@ func (m AsciiBox) Lines() []string {
return strings.Split(string(m), "\n")
}
+func (m AsciiBox) GetBoxName() string {
+ return getBoxName(m)
+}
+
+func (m AsciiBox) ChangeBoxName(newName string) AsciiBox {
+ return changeBoxName(m, newName)
+}
+
// String returns the string of the box
func (m AsciiBox) String() string {
return string(m)
@@ -71,36 +80,7 @@ func BoxBox(name string, box AsciiBox, charWidth int) AsciiBox {
// BoxString boxes a newline separated string into a beautiful box
func BoxString(name string, data string, charWidth int) AsciiBox {
- // the name gets prefixed with a extra symbol for indent
- const extraNameCharWidth = 1
- const borderWidth = 1
- const newLineCharWidth = 1
- rawBox := AsciiBox(data)
- longestLine := rawBox.Width()
- if charWidth < longestLine {
- if DebugAsciiBox {
- log.Debug().Msgf("Overflow by %d chars", longestLine-charWidth)
- }
- charWidth = longestLine + borderWidth + borderWidth
- }
- var boxedString strings.Builder
- boxedString.Grow((borderWidth + longestLine + borderWidth + newLineCharWidth) * rawBox.Height())
- namePadding := int(math.Max(float64(charWidth-countChars(name)-borderWidth-extraNameCharWidth-borderWidth), 0))
- boxedString.WriteString("╔═" + name + strings.Repeat("═", namePadding) + "╗\n")
- // Name of the header stretches the box so we align to that
- charWidth = borderWidth + extraNameCharWidth + countChars(name) + namePadding + borderWidth
- for _, line := range rawBox.Lines() {
- linePadding := float64(charWidth - boxLineOverheat - countChars(line))
- if linePadding < 0 {
- linePadding = 0
- }
- frontPadding := math.Floor(linePadding / 2.0)
- backPadding := math.Ceil(linePadding / 2.0)
- boxedString.WriteString("║" + strings.Repeat(" ", int(frontPadding)) + line + strings.Repeat(" ", int(backPadding)) + "║\n")
- }
- bottomPadding := namePadding + countChars(name) + extraNameCharWidth
- boxedString.WriteString("╚" + strings.Repeat("═", bottomPadding) + "╝")
- return AsciiBox(boxedString.String())
+ return boxString(name, data, charWidth)
}
// AlignBoxes aligns all boxes to a desiredWidth and orders them from left to right and top to bottom (size will be at min the size of the biggest box)
@@ -150,19 +130,6 @@ func AlignBoxes(boxes []AsciiBox, desiredWidth int) AsciiBox {
return bigBox
}
-func mergeHorizontal(boxes []AsciiBox) AsciiBox {
- switch len(boxes) {
- case 0:
- return ""
- case 1:
- return boxes[0]
- case 2:
- return BoxSideBySide(boxes[0], boxes[1])
- default:
- return BoxSideBySide(boxes[0], mergeHorizontal(boxes[1:]))
- }
-}
-
// BoxSideBySide renders two boxes side by side
func BoxSideBySide(box1, box2 AsciiBox) AsciiBox {
const newLineCharWidth = 1
@@ -213,12 +180,101 @@ func BoxBelowBox(box1, box2 AsciiBox) AsciiBox {
return AsciiBox(box1.String() + "\n" + box2.String())
}
+///////////////////////////////////////
+///////////////////////////////////////
+//
+// Internal section
+//
+
+const (
+ upperLeftCorner = "╔"
+ upperRightCorner = "╗"
+ horizontalLine = "═"
+ verticalLine = "║"
+ lowerLeftCorner = "╚"
+ lowerRightCorner = "╝"
+ newLine = '\n'
+ emptyPadding = " "
+ // the name gets prefixed with a extra symbol for indent
+ extraNameCharIndent = 1
+ borderWidth = 1
+ newLineCharWidth = 1
+)
+
+var boxNameRegex *regexp.Regexp
+
+func init() {
+ boxNameRegex = regexp.MustCompile(`^` + upperLeftCorner + horizontalLine + `(?P<name>[\w /]+)` + horizontalLine + `*` + upperRightCorner)
+}
+
+func boxString(name string, data string, charWidth int) AsciiBox {
+ rawBox := AsciiBox(data)
+ longestLine := rawBox.Width()
+ if charWidth < longestLine {
+ if DebugAsciiBox {
+ log.Debug().Msgf("Overflow by %d chars", longestLine-charWidth)
+ }
+ charWidth = longestLine + borderWidth + borderWidth
+ }
+ var boxedString strings.Builder
+ boxedString.Grow((borderWidth + longestLine + borderWidth + newLineCharWidth) * rawBox.Height())
+ namePadding := int(math.Max(float64(charWidth-countChars(name)-borderWidth-extraNameCharIndent-borderWidth), 0))
+ boxedString.WriteString(upperLeftCorner + horizontalLine + name + strings.Repeat(horizontalLine, namePadding) + upperRightCorner)
+ boxedString.WriteRune(newLine)
+ // Name of the header stretches the box so we align to that
+ charWidth = borderWidth + extraNameCharIndent + countChars(name) + namePadding + borderWidth
+ for _, line := range rawBox.Lines() {
+ linePadding := float64(charWidth - boxLineOverheat - countChars(line))
+ if linePadding < 0 {
+ linePadding = 0
+ }
+ frontPadding := math.Floor(linePadding / 2.0)
+ backPadding := math.Ceil(linePadding / 2.0)
+ boxedString.WriteString(verticalLine + strings.Repeat(emptyPadding, int(frontPadding)) + line + strings.Repeat(emptyPadding, int(backPadding)) + verticalLine)
+ boxedString.WriteRune(newLine)
+ }
+ bottomPadding := namePadding + countChars(name) + extraNameCharIndent
+ boxedString.WriteString(lowerLeftCorner + strings.Repeat(horizontalLine, bottomPadding) + lowerRightCorner)
+ return AsciiBox(boxedString.String())
+}
+
+func getBoxName(box AsciiBox) string {
+ subMatch := boxNameRegex.FindStringSubmatch(box.String())
+ if subMatch == nil {
+ return ""
+ }
+ if len(subMatch) != 2 {
+ panic("should never occur as we only have one named group")
+ }
+ return subMatch[1]
+}
+
+func changeBoxName(box AsciiBox, newName string) AsciiBox {
+ if !hasBorders(box) {
+ return boxString(newName, box.String(), 0)
+ }
+ minimumWidthWithNewName := countChars(upperLeftCorner + horizontalLine + newName + upperRightCorner)
+ nameLengthDifference := minimumWidthWithNewName - (unwrap(box).Width() + borderWidth + borderWidth)
+ return BoxString(newName, unwrap(box).String(), box.Width()+nameLengthDifference)
+}
+
+func mergeHorizontal(boxes []AsciiBox) AsciiBox {
+ switch len(boxes) {
+ case 0:
+ return ""
+ case 1:
+ return boxes[0]
+ case 2:
+ return BoxSideBySide(boxes[0], boxes[1])
+ default:
+ return BoxSideBySide(boxes[0], mergeHorizontal(boxes[1:]))
+ }
+}
+
func expandBox(box AsciiBox, desiredWidth int) AsciiBox {
- const newLineCharWidth = 1
if box.Width() >= desiredWidth {
return box
}
- // TODO: should we expand the borders?
boxLines := box.Lines()
numberOfLine := len(boxLines)
boxWidth := box.Width()
@@ -229,12 +285,48 @@ func expandBox(box AsciiBox, desiredWidth int) AsciiBox {
newBox.WriteString(line)
newBox.WriteString(padding)
if i < numberOfLine-1 {
- newBox.WriteRune('\n')
+ newBox.WriteRune(newLine)
}
}
return AsciiBox(newBox.String())
}
+func unwrap(box AsciiBox) AsciiBox {
+ if !hasBorders(box) {
+ return box
+ }
+ originalLines := box.Lines()
+ newLines := make([]string, len(originalLines)-2)
+ for i, line := range originalLines {
+ if i == 0 {
+ // we ignore the first line
+ continue
+ }
+ if i == len(originalLines)-1 {
+ // we ignore the last line
+ break
+ }
+ runes := []rune(line)
+ // Strip the vertical Lines and trim the padding
+ newLines[i-1] = strings.Trim(string(runes[1:len(runes)-1]), emptyPadding)
+ }
+ return AsciiBox(strings.Join(newLines, string(newLine)))
+}
+
+func hasBorders(box AsciiBox) bool {
+ if len(box.String()) == 0 {
+ return false
+ }
+ // Check if the first char is the upper left corner
+ return []rune(box.String())[0] == []rune(upperLeftCorner)[0]
+}
+
func countChars(s string) int {
return len([]rune(s))
}
+
+//
+// Internal section
+//
+///////////////////////////////////////
+///////////////////////////////////////
diff --git a/plc4go/internal/plc4go/spi/utils/asciiBox_test.go b/plc4go/internal/plc4go/spi/utils/asciiBox_test.go
index ff62a64..f743a89 100644
--- a/plc4go/internal/plc4go/spi/utils/asciiBox_test.go
+++ b/plc4go/internal/plc4go/spi/utils/asciiBox_test.go
@@ -29,6 +29,98 @@ func init() {
DebugAsciiBox = true
}
+func TestAsciiBox_GetBoxName(t *testing.T) {
+ type args struct {
+ box AsciiBox
+ }
+ tests := []struct {
+ name string
+ args args
+ want string
+ }{
+ {
+ name: "simple name",
+ args: args{
+ box: BoxString("someName", "some content", 0),
+ },
+ want: "someName",
+ },
+ {
+ name: "no name",
+ args: args{
+ box: BoxString("", "some content", 0),
+ },
+ want: "",
+ },
+ {
+ name: "long name",
+ args: args{
+ box: BoxString("veryLongName12_13", "some content", 0),
+ },
+ want: "veryLongName12_13",
+ },
+ {
+ name: "name with spaces and slashes",
+ args: args{
+ box: BoxString("payload / Message / Concrete Message", "some content", 0),
+ },
+ want: "payload / Message / Concrete Message",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := tt.args.box.GetBoxName(); got != tt.want {
+ t.Errorf("AsciiBox_GetBoxName() = '\n%v\n', want '\n%v\n'", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestAsciiBox_ChangeBoxName(t *testing.T) {
+ type args struct {
+ box AsciiBox
+ newName string
+ }
+ tests := []struct {
+ name string
+ args args
+ want AsciiBox
+ }{
+ {
+ name: "box with simple name",
+ args: args{
+ box: BoxString("simpleName", "some content", 0),
+ newName: "newSimpleName",
+ },
+ want: BoxString("newSimpleName", "some content", 0),
+ },
+ {
+ name: "box with shorter name",
+ args: args{
+ box: BoxString("veryLongName", "some content", 0),
+ newName: "name",
+ },
+ want: BoxString("name", "some content", 0),
+ },
+ {
+ name: "box getting dressed",
+ args: args{
+ box: `some content`,
+ newName: "name",
+ },
+ want: BoxString("name", "some content", 0),
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ tt.want = trimBox(tt.want)
+ if got := tt.args.box.ChangeBoxName(tt.args.newName); got != tt.want {
+ t.Errorf("BoxSideBySide() = '\n%v\n', want '\n%v\n'", got, tt.want)
+ }
+ })
+ }
+}
+
func TestBoxSideBySide(t *testing.T) {
type args struct {
box1 AsciiBox
@@ -293,6 +385,20 @@ func TestBoxString(t *testing.T) {
╚════════════════════════╝
`,
},
+ {
+ name: "simplebox with too long name",
+ args: args{
+ name: "sampleFieldsampleFieldsampleFieldsampleField",
+ data: "123123123123\n123123123123123123123123",
+ charWidth: 1,
+ },
+ want: `
+╔═sampleFieldsampleFieldsampleFieldsampleField╗
+║ 123123123123 ║
+║ 123123123123123123123123 ║
+╚═════════════════════════════════════════════╝
+`,
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {