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 2024/10/01 12:09:13 UTC
(plc4x) branch develop updated (e1741cda8b -> 6ba7dfa574)
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 e1741cda8b feat(plc4py): Made the reading of the data types more robust
new 7aea3ab444 fix(plc4go/spi): fix footer being lost on name change
new 12ecb9ed97 feat(plc4go/spi): add footers for serializable
new 09439fb773 feat(plc4go/spi): add color support to ascii box
new 6ba7dfa574 test(plc4go): enable color in tests
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/go.mod | 1 +
plc4go/go.sum | 2 +
plc4go/internal/cbus/Configuration_test.go | 68 ----------
plc4go/internal/cbus/Connection_test.go | 62 ---------
plc4go/internal/cbus/MessageCodec_test.go | 70 -----------
plc4go/internal/cbus/Tag_test.go | 181 ---------------------------
plc4go/internal/s7/s7Io_test.go | 36 +++---
plc4go/spi/default/DefaultCodec_test.go | 66 ----------
plc4go/spi/default/DefaultConnection_test.go | 45 -------
plc4go/spi/testutils/TestUtils.go | 12 +-
plc4go/spi/utils/WriteBufferBoxBased.go | 24 +++-
plc4go/spi/utils/asciiBox.go | 151 +++++++++++++++++-----
plc4go/spi/utils/asciiBox_test.go | 57 ++++++++-
13 files changed, 226 insertions(+), 549 deletions(-)
(plc4x) 04/04: test(plc4go): enable color in tests
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 6ba7dfa5745f6c4e4722aeb6d592bb11ba4dfb0a
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Oct 1 14:09:00 2024 +0200
test(plc4go): enable color in tests
---
plc4go/internal/cbus/Configuration_test.go | 68 ----------
plc4go/internal/cbus/Connection_test.go | 62 ---------
plc4go/internal/cbus/MessageCodec_test.go | 70 -----------
plc4go/internal/cbus/Tag_test.go | 181 ---------------------------
plc4go/spi/default/DefaultCodec_test.go | 66 ----------
plc4go/spi/default/DefaultConnection_test.go | 45 -------
plc4go/spi/testutils/TestUtils.go | 12 +-
7 files changed, 10 insertions(+), 494 deletions(-)
diff --git a/plc4go/internal/cbus/Configuration_test.go b/plc4go/internal/cbus/Configuration_test.go
index bc96feb1d7..1a84f095bc 100644
--- a/plc4go/internal/cbus/Configuration_test.go
+++ b/plc4go/internal/cbus/Configuration_test.go
@@ -203,71 +203,3 @@ func Test_getFromOptions(t *testing.T) {
})
}
}
-
-func TestConfiguration_String(t *testing.T) {
- type fields struct {
- Srchk bool
- Exstat bool
- Pun bool
- LocalSal bool
- Pcn bool
- Idmon bool
- Monitor bool
- Smart bool
- XonXoff bool
- Connect bool
- MonitoredApplication1 byte
- MonitoredApplication2 byte
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "string it",
- fields: fields{
- Srchk: true,
- Exstat: true,
- Pun: true,
- LocalSal: true,
- Pcn: true,
- Idmon: true,
- Monitor: true,
- Smart: true,
- XonXoff: true,
- Connect: true,
- MonitoredApplication1: 2,
- MonitoredApplication2: 3,
- },
- want: `
-╔═Configuration═════════════════════════════════════════════════════════════════════════════════╗
-║╔═srchk═╗╔═exstat╗╔═pun═══╗╔═localSal╗╔═pcn═══╗╔═idmon═╗╔═monitor╗╔═smart═╗╔═xonXoff╗╔═connect╗║
-║║b1 true║║b1 true║║b1 true║║ b1 true ║║b1 true║║b1 true║║b1 true ║║b1 true║║b1 true ║║b1 true ║║
-║╚═══════╝╚═══════╝╚═══════╝╚═════════╝╚═══════╝╚═══════╝╚════════╝╚═══════╝╚════════╝╚════════╝║
-║╔═monitoredApplication1╗╔═monitoredApplication2╗ ║
-║║ 0x02 '.' ║║ 0x03 '.' ║ ║
-║╚══════════════════════╝╚══════════════════════╝ ║
-╚═══════════════════════════════════════════════════════════════════════════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := Configuration{
- Srchk: tt.fields.Srchk,
- Exstat: tt.fields.Exstat,
- Pun: tt.fields.Pun,
- LocalSal: tt.fields.LocalSal,
- Pcn: tt.fields.Pcn,
- Idmon: tt.fields.Idmon,
- Monitor: tt.fields.Monitor,
- Smart: tt.fields.Smart,
- XonXoff: tt.fields.XonXoff,
- Connect: tt.fields.Connect,
- MonitoredApplication1: tt.fields.MonitoredApplication1,
- MonitoredApplication2: tt.fields.MonitoredApplication2,
- }
- assert.Equalf(t, tt.want, c.String(), "String()")
- })
- }
-}
diff --git a/plc4go/internal/cbus/Connection_test.go b/plc4go/internal/cbus/Connection_test.go
index df4e61ccb5..8696ba22f2 100644
--- a/plc4go/internal/cbus/Connection_test.go
+++ b/plc4go/internal/cbus/Connection_test.go
@@ -487,68 +487,6 @@ func TestConnection_ReadRequestBuilder(t *testing.T) {
}
}
-func TestConnection_String(t *testing.T) {
- type fields struct {
- messageCodec *MessageCodec
- subscribers []*Subscriber
- tm transactions.RequestTransactionManager
- configuration Configuration
- connectionId string
- tracer tracer.Tracer
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "a string",
- want: `
-╔═Connection══════════════════════════════════════════════════════════════════════════════════════════════╗
-║╔═defaultConnection═══════╗╔═alphaGenerator═══════════════╗ ║
-║║╔═defaultTtl╗╔═connected╗║║╔═AlphaGenerator/currentAlpha╗║ ║
-║║║ 10s ║║ b0 false ║║║║ 0x67 'g' ║║ ║
-║║╚═══════════╝╚══════════╝║║╚════════════════════════════╝║ ║
-║╚═════════════════════════╝╚══════════════════════════════╝ ║
-║╔═configuration═════════════════════════════════════════════════════════════════════════════════════════╗║
-║║╔═Configuration═══════════════════════════════════════════════════════════════════════════════════════╗║║
-║║║╔═srchk══╗╔═exstat═╗╔═pun════╗╔═localSal╗╔═pcn════╗╔═idmon══╗╔═monitor╗╔═smart══╗╔═xonXoff╗╔═connect╗║║║
-║║║║b0 false║║b0 false║║b0 false║║b0 false ║║b0 false║║b0 false║║b0 false║║b0 false║║b0 false║║b0 false║║║║
-║║║╚════════╝╚════════╝╚════════╝╚═════════╝╚════════╝╚════════╝╚════════╝╚════════╝╚════════╝╚════════╝║║║
-║║║╔═monitoredApplication1╗╔═monitoredApplication2╗ ║║║
-║║║║ 0x00 '.' ║║ 0x00 '.' ║ ║║║
-║║║╚══════════════════════╝╚══════════════════════╝ ║║║
-║║╚═════════════════════════════════════════════════════════════════════════════════════════════════════╝║║
-║╚═══════════════════════════════════════════════════════════════════════════════════════════════════════╝║
-║╔═driverContext═══════════════════════════════════╗ ║
-║║╔═DriverContext═════════════════════════════════╗║ ║
-║║║╔═awaitSetupComplete╗╔═awaitDisconnectComplete╗║║ ║
-║║║║ b1 true ║║ b1 true ║║║ ║
-║║║╚═══════════════════╝╚════════════════════════╝║║ ║
-║║╚═══════════════════════════════════════════════╝║ ║
-║╚═════════════════════════════════════════════════╝ ║
-╚═════════════════════════════════════════════════════════════════════════════════════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &Connection{
- alphaGenerator: AlphaGenerator{currentAlpha: 'g'},
- messageCodec: tt.fields.messageCodec,
- subscribers: tt.fields.subscribers,
- tm: tt.fields.tm,
- configuration: tt.fields.configuration,
- driverContext: driverContextForTesting(),
- connectionId: tt.fields.connectionId,
- tracer: tt.fields.tracer,
- log: testutils.ProduceTestingLogger(t),
- }
- c.DefaultConnection = _default.NewDefaultConnection(c, testutils.EnrichOptionsWithOptionsForTesting(t)...)
- assert.Equalf(t, tt.want, c.String(), "String()")
- })
- }
-}
-
func TestConnection_SubscriptionRequestBuilder(t *testing.T) {
type fields struct {
messageCodec *MessageCodec
diff --git a/plc4go/internal/cbus/MessageCodec_test.go b/plc4go/internal/cbus/MessageCodec_test.go
index 54e849be97..699c789484 100644
--- a/plc4go/internal/cbus/MessageCodec_test.go
+++ b/plc4go/internal/cbus/MessageCodec_test.go
@@ -826,73 +826,3 @@ func Test_extractMMIAndSAL(t *testing.T) {
})
}
}
-
-func TestMessageCodec_String(t *testing.T) {
- type fields struct {
- DefaultCodec _default.DefaultCodec
- requestContext readWriteModel.RequestContext
- cbusOptions readWriteModel.CBusOptions
- monitoredMMIs chan readWriteModel.CALReply
- monitoredSALs chan readWriteModel.MonitoredSAL
- }
- tests := []struct {
- name string
- fields fields
- manipulator func(t *testing.T, messageCodec *MessageCodec)
- want string
- }{
- {
- name: "string it",
- fields: fields{
- DefaultCodec: _default.NewDefaultCodec(nil, test.NewTransportInstance(test.NewTransport())),
- requestContext: readWriteModel.NewRequestContext(true),
- cbusOptions: readWriteModel.NewCBusOptions(true, true, true, true, true, true, true, true, true),
- monitoredMMIs: nil,
- monitoredSALs: nil,
- },
- manipulator: func(t *testing.T, messageCodec *MessageCodec) {
- messageCodec.lastPackageHash.Store(2)
- messageCodec.hashEncountered.Store(3)
- messageCodec.currentlyReportedServerErrors.Store(4)
- },
- want: `
-╔═MessageCodec════════════════════════════════════════════════════════════════════════════════════════════════════════╗
-║╔═defaultCodec══════════════════════════════════════════════════════════════════════════════════════════╗ ║
-║║╔═transportInstance╗╔═defaultIncomingMessageChannel╗╔═customMessageHandling╗╔═running╗╔═receiveTimeout╗║ ║
-║║║ test ║║ 0 element(s) ║║ b0 false ║║b0 false║║ 10s ║║ ║
-║║╚══════════════════╝╚══════════════════════════════╝╚══════════════════════╝╚════════╝╚═══════════════╝║ ║
-║║╔═traceDefaultMessageCodecWorker╗ ║ ║
-║║║ b0 false ║ ║ ║
-║║╚═══════════════════════════════╝ ║ ║
-║╚═══════════════════════════════════════════════════════════════════════════════════════════════════════╝ ║
-║╔═requestContext/RequestContext/sendIdentifyRequestBefore╗ ║
-║║ b1 true ║ ║
-║╚════════════════════════════════════════════════════════╝ ║
-║╔═cbusOptions/CBusOptions═══════════════════════════════════════════════════════════╗╔═monitoredMMIs╗╔═monitoredSALs╗║
-║║╔═connect╗╔═smart═╗╔═idmon═╗╔═exstat╗╔═monitor╗╔═monall╗╔═pun═══╗╔═pcn═══╗╔═srchk═╗║║ 0 element(s) ║║ 0 element(s) ║║
-║║║b1 true ║║b1 true║║b1 true║║b1 true║║b1 true ║║b1 true║║b1 true║║b1 true║║b1 true║║╚══════════════╝╚══════════════╝║
-║║╚════════╝╚═══════╝╚═══════╝╚═══════╝╚════════╝╚═══════╝╚═══════╝╚═══════╝╚═══════╝║ ║
-║╚═══════════════════════════════════════════════════════════════════════════════════╝ ║
-║╔═lastPackageHash╗╔═hashEncountered════╗╔═currentlyReportedServerErrors╗ ║
-║║ 0x00000002 2 ║║0x0000000000000003 3║║ 0x0000000000000004 4 ║ ║
-║╚════════════════╝╚════════════════════╝╚══════════════════════════════╝ ║
-╚═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- m := &MessageCodec{
- DefaultCodec: tt.fields.DefaultCodec,
- requestContext: tt.fields.requestContext,
- cbusOptions: tt.fields.cbusOptions,
- monitoredMMIs: tt.fields.monitoredMMIs,
- monitoredSALs: tt.fields.monitoredSALs,
- log: testutils.ProduceTestingLogger(t),
- }
- if tt.manipulator != nil {
- tt.manipulator(t, m)
- }
- assert.Equalf(t, tt.want, m.String(), "String()")
- })
- }
-}
diff --git a/plc4go/internal/cbus/Tag_test.go b/plc4go/internal/cbus/Tag_test.go
index e5b6fd5224..9c35a00f1b 100644
--- a/plc4go/internal/cbus/Tag_test.go
+++ b/plc4go/internal/cbus/Tag_test.go
@@ -521,43 +521,6 @@ func Test_calGetStatusTag_SerializeWithWriteBuffer(t *testing.T) {
}
}
-func Test_calGetStatusTag_String(t *testing.T) {
- type fields struct {
- calTag calTag
- tagType TagType
- parameter readWriteModel.Parameter
- count uint8
- numElements uint16
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "default to string",
- want: `
-╔═STATUS════════════════════╗
-║╔═Parameter═══════╗╔═count╗║
-║║0x00 0 UNKNOWN_01║║0x00 0║║
-║╚═════════════════╝╚══════╝║
-╚═══════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := calGetStatusTag{
- calTag: tt.fields.calTag,
- tagType: tt.fields.tagType,
- parameter: tt.fields.parameter,
- count: tt.fields.count,
- numElements: tt.fields.numElements,
- }
- assert.Equalf(t, tt.want, c.String(), "String()")
- })
- }
-}
-
func Test_calIdentifyTag_GetAddressString(t *testing.T) {
type fields struct {
calTag calTag
@@ -795,39 +758,6 @@ func Test_calIdentifyTag_SerializeWithWriteBuffer(t *testing.T) {
}
}
-func Test_calIdentifyTag_String(t *testing.T) {
- type fields struct {
- calTag calTag
- tagType TagType
- attribute readWriteModel.Attribute
- numElements uint16
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "default to string",
- want: `
-╔═STATUS/Attribute══╗
-║0x00 0 Manufacturer║
-╚═══════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := calIdentifyTag{
- calTag: tt.fields.calTag,
- tagType: tt.fields.tagType,
- attribute: tt.fields.attribute,
- numElements: tt.fields.numElements,
- }
- assert.Equalf(t, tt.want, c.String(), "String()")
- })
- }
-}
-
func Test_calRecallTag_GetAddressString(t *testing.T) {
type fields struct {
calTag calTag
@@ -1110,43 +1040,6 @@ func Test_calRecallTag_SerializeWithWriteBuffer(t *testing.T) {
}
}
-func Test_calRecallTag_String(t *testing.T) {
- type fields struct {
- calTag calTag
- tagType TagType
- parameter readWriteModel.Parameter
- count uint8
- numElements uint16
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "default to string",
- want: `
-╔═STATUS════════════════════╗
-║╔═Parameter═══════╗╔═count╗║
-║║0x00 0 UNKNOWN_01║║0x00 0║║
-║╚═════════════════╝╚══════╝║
-╚═══════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := calRecallTag{
- calTag: tt.fields.calTag,
- tagType: tt.fields.tagType,
- parameter: tt.fields.parameter,
- count: tt.fields.count,
- numElements: tt.fields.numElements,
- }
- assert.Equalf(t, tt.want, c.String(), "String()")
- })
- }
-}
-
func Test_calTag_GetBridgeAddresses(t *testing.T) {
type fields struct {
bridgeAddresses []readWriteModel.BridgeAddress
@@ -2262,41 +2155,6 @@ func Test_salTag_SerializeWithWriteBuffer(t *testing.T) {
}
}
-func Test_salTag_String(t *testing.T) {
- type fields struct {
- bridgeAddresses []readWriteModel.BridgeAddress
- tagType TagType
- application readWriteModel.ApplicationIdContainer
- salCommand string
- numElements uint16
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "default to string",
- want: `
-╔═STATUS/ApplicationIdContainer╗
-║ 0x00 0 RESERVED_00 ║
-╚══════════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := salTag{
- bridgeAddresses: tt.fields.bridgeAddresses,
- tagType: tt.fields.tagType,
- application: tt.fields.application,
- salCommand: tt.fields.salCommand,
- numElements: tt.fields.numElements,
- }
- assert.Equalf(t, tt.want, s.String(), "String()")
- })
- }
-}
-
func Test_statusTag_GetAddressString(t *testing.T) {
type fields struct {
bridgeAddresses []readWriteModel.BridgeAddress
@@ -2715,42 +2573,3 @@ func Test_statusTag_SerializeWithWriteBuffer(t *testing.T) {
})
}
}
-
-func Test_statusTag_String(t *testing.T) {
- type fields struct {
- bridgeAddresses []readWriteModel.BridgeAddress
- tagType TagType
- statusRequestType StatusRequestType
- startingGroupAddressLabel *byte
- application readWriteModel.ApplicationIdContainer
- numElements uint16
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "default to string",
- want: `
-╔═STATUS══════════════════════════════════════════════════╗
-║╔═statusRequestType═════════════════╗╔═application══════╗║
-║║0x00 0 StatusRequestTypeBinaryState║║0x00 0 RESERVED_00║║
-║╚═══════════════════════════════════╝╚══════════════════╝║
-╚═════════════════════════════════════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := statusTag{
- bridgeAddresses: tt.fields.bridgeAddresses,
- tagType: tt.fields.tagType,
- statusRequestType: tt.fields.statusRequestType,
- startingGroupAddressLabel: tt.fields.startingGroupAddressLabel,
- application: tt.fields.application,
- numElements: tt.fields.numElements,
- }
- assert.Equalf(t, tt.want, s.String(), "String()")
- })
- }
-}
diff --git a/plc4go/spi/default/DefaultCodec_test.go b/plc4go/spi/default/DefaultCodec_test.go
index db2acebdcb..f7437fa7f9 100644
--- a/plc4go/spi/default/DefaultCodec_test.go
+++ b/plc4go/spi/default/DefaultCodec_test.go
@@ -37,7 +37,6 @@ import (
"github.com/apache/plc4x/plc4go/spi/options"
"github.com/apache/plc4x/plc4go/spi/testutils"
"github.com/apache/plc4x/plc4go/spi/transports"
- "github.com/apache/plc4x/plc4go/spi/transports/test"
)
func TestDefaultExpectation_GetAcceptsMessage(t *testing.T) {
@@ -1453,71 +1452,6 @@ func Test_defaultCodec_Work(t *testing.T) {
}
}
-func Test_defaultCodec_String(t *testing.T) {
- type fields struct {
- DefaultCodecRequirements DefaultCodecRequirements
- transportInstance transports.TransportInstance
- defaultIncomingMessageChannel chan spi.Message
- expectations []spi.Expectation
- running bool
- customMessageHandling func(codec DefaultCodecRequirements, message spi.Message) bool
- }
- tests := []struct {
- name string
- fields fields
- want string
- }{
- {
- name: "string it",
- fields: fields{
- transportInstance: test.NewTransportInstance(test.NewTransport()),
- defaultIncomingMessageChannel: func() chan spi.Message {
- messages := make(chan spi.Message, 1)
- messages <- NewMockMessage(t)
- return messages
- }(),
- expectations: []spi.Expectation{
- func() spi.Expectation {
- expectation := NewMockExpectation(t)
- expectation.EXPECT().String().Return("yoink1")
- return expectation
- }(),
- func() spi.Expectation {
- expectation := NewMockExpectation(t)
- expectation.EXPECT().String().Return("yoink2")
- return expectation
- }(),
- },
- customMessageHandling: nil,
- },
- want: `
-╔═defaultCodec═══════════════════════════════════════════════════════════════════════════════════════════╗
-║╔═transportInstance╗╔═expectations═══╗╔═defaultIncomingMessageChannel╗╔═customMessageHandling╗╔═running╗║
-║║ test ║║╔═value╗╔═value╗║║ 1 element(s) ║║ b0 false ║║b0 false║║
-║╚══════════════════╝║║yoink1║║yoink2║║╚══════════════════════════════╝╚══════════════════════╝╚════════╝║
-║ ║╚══════╝╚══════╝║ ║
-║ ╚════════════════╝ ║
-║╔═receiveTimeout╗╔═traceDefaultMessageCodecWorker╗ ║
-║║ 0s ║║ b0 false ║ ║
-║╚═══════════════╝╚═══════════════════════════════╝ ║
-╚════════════════════════════════════════════════════════════════════════════════════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- m := &defaultCodec{
- DefaultCodecRequirements: tt.fields.DefaultCodecRequirements,
- transportInstance: tt.fields.transportInstance,
- defaultIncomingMessageChannel: tt.fields.defaultIncomingMessageChannel,
- expectations: tt.fields.expectations,
- customMessageHandling: tt.fields.customMessageHandling,
- log: testutils.ProduceTestingLogger(t),
- }
- assert.Equalf(t, tt.want, m.String(), "String()")
- })
- }
-}
-
func Test_defaultCodec_startWorker(t *testing.T) {
type fields struct {
DefaultCodecRequirements DefaultCodecRequirements
diff --git a/plc4go/spi/default/DefaultConnection_test.go b/plc4go/spi/default/DefaultConnection_test.go
index 64873d64bc..289043a208 100644
--- a/plc4go/spi/default/DefaultConnection_test.go
+++ b/plc4go/spi/default/DefaultConnection_test.go
@@ -33,7 +33,6 @@ import (
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/options"
- "github.com/apache/plc4x/plc4go/spi/testutils"
"github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/apache/plc4x/plc4go/spi/transports"
"github.com/apache/plc4x/plc4go/spi/utils"
@@ -1295,47 +1294,3 @@ func Test_plcConnectionPingResult_GetErr(t *testing.T) {
})
}
}
-
-func Test_defaultConnection_String(t *testing.T) {
- type fields struct {
- DefaultConnectionRequirements DefaultConnectionRequirements
- defaultTtl time.Duration
- tagHandler spi.PlcTagHandler
- valueHandler spi.PlcValueHandler
- }
- tests := []struct {
- name string
- fields fields
- connected bool
- want string
- }{
- {
- name: "string it",
- fields: fields{
- defaultTtl: 20 * time.Hour,
- },
- connected: true,
- want: `
-╔═defaultConnection═══════╗
-║╔═defaultTtl╗╔═connected╗║
-║║ 20h0m0s ║║ b1 true ║║
-║╚═══════════╝╚══════════╝║
-╚═════════════════════════╝`[1:],
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- d := &defaultConnection{
- DefaultConnectionRequirements: tt.fields.DefaultConnectionRequirements,
- defaultTtl: tt.fields.defaultTtl,
- tagHandler: tt.fields.tagHandler,
- valueHandler: tt.fields.valueHandler,
- log: testutils.ProduceTestingLogger(t),
- }
- if tt.connected {
- d.connected.Store(true)
- }
- assert.Equalf(t, tt.want, d.String(), "String()")
- })
- }
-}
diff --git a/plc4go/spi/testutils/TestUtils.go b/plc4go/spi/testutils/TestUtils.go
index 80512b974d..d4c8cc276e 100644
--- a/plc4go/spi/testutils/TestUtils.go
+++ b/plc4go/spi/testutils/TestUtils.go
@@ -34,6 +34,7 @@ import (
"github.com/ajankovic/xdiff"
"github.com/ajankovic/xdiff/parser"
+ "github.com/fatih/color"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@@ -169,16 +170,23 @@ func getOrLeaveDuration(key string, setting *time.Duration) {
}
func shouldNoColor() bool {
+ if _, forceColorEnv := os.LookupEnv("FORCE_COLOR"); forceColorEnv {
+ color.NoColor = false // Apparently the color.NoColor is a bit to eager
+ return false
+ }
noColor := false
{
- // TODO: this is really an issue with go-junit-report not sanitizing output before dumping into xml...
+ _, noColorEnv := os.LookupEnv("NO_COLOR")
onJenkins := os.Getenv("JENKINS_URL") != ""
onGithubAction := os.Getenv("GITHUB_ACTIONS") != ""
onCI := os.Getenv("CI") != ""
- if onJenkins || onGithubAction || onCI {
+ if noColorEnv || onJenkins || onGithubAction || onCI {
noColor = true
}
}
+ if !noColor {
+ color.NoColor = false // Apparently the color.NoColor is a bit to eager
+ }
return noColor
}
(plc4x) 02/04: feat(plc4go/spi): add footers for serializable
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 12ecb9ed97185000f1eb3dd97656fdb24121717d
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Oct 1 09:59:50 2024 +0200
feat(plc4go/spi): add footers for serializable
---
plc4go/internal/s7/s7Io_test.go | 36 ++++++++++---------
plc4go/spi/utils/WriteBufferBoxBased.go | 24 +++++++++++--
plc4go/spi/utils/asciiBox.go | 61 ++++++++++++++++++++++++++-------
plc4go/spi/utils/asciiBox_test.go | 16 ++++++++-
4 files changed, 105 insertions(+), 32 deletions(-)
diff --git a/plc4go/internal/s7/s7Io_test.go b/plc4go/internal/s7/s7Io_test.go
index 9d925143ed..03576e92e2 100644
--- a/plc4go/internal/s7/s7Io_test.go
+++ b/plc4go/internal/s7/s7Io_test.go
@@ -22,6 +22,7 @@ package s7
import (
"context"
"fmt"
+ "github.com/stretchr/testify/require"
"strings"
"testing"
@@ -155,7 +156,7 @@ func TestS7MessageBytes(t *testing.T) {
║║║║╔═errorClass╗╔═errorCode╗║║╔═parameterType╗╔═S7ParameterReadVarResponse/numItems╗║ ║║║
║║║║║ 0x00 0 ║║ 0x00 0 ║║║║ 0x04 4 ║║ 0x01 1 ║║ ║║║
║║║║╚══════20/1═╝╚═════21/1═╝║║╚═════════22/1═╝╚═══════════════════════════════23/1═╝║ ║║║
-║║║╚═════════════════════════╝╚══════════════════════════════════════════════════════╝ ║║║
+║║║╚═════════════════════════╝╚═════════════════════════════════════════════════22/2═╝ ║║║
║║║╔═payload/S7Payload/S7PayloadReadVarResponse/items/S7VarPayloadDataItem═════════════╗ ║║║
║║║║╔═returnCode/DataTransportErrorCode╗╔═transportSize/DataTransportSize╗╔═dataLength╗║ ║║║
║║║║║ 0xff 255 OK ║║ 0x03 3 BIT ║║ 0x0001 1 ║║ ║║║
@@ -163,10 +164,10 @@ func TestS7MessageBytes(t *testing.T) {
║║║║╔═data═══════════════════════════════════════╗ ║ ║║║
║║║║║0|01 '. '║ ║ ║║║
║║║║╚═══════════════════════════════════════28/1═╝ ║ ║║║
-║║║╚═══════════════════════════════════════════════════════════════════════════════════╝ ║║║
-║║╚════════════════════════════════════════════════════════════════════════════════════════╝║║
-║╚══════════════════════════════════════════════════════════════════════════════════════════╝║
-╚════════════════════════════════════════════════════════════════════════════════════════════╝
+║║║╚══════════════════════════════════════════════════════════════════════════════24/5═╝ ║║║
+║║╚══════════════════════════════════════════════════════════════════════════════════10/19═╝║║
+║╚═════════════════════════════════════════════════════════════════════════════════════4/25═╝║
+╚═══════════════════════════════════════════════════════════════════════════════════════0/29═╝
`,
wantStringXml: `
<TPKTPacket>
@@ -562,11 +563,11 @@ func TestS7MessageBytes(t *testing.T) {
║║║║║║║║║║ 0x0000 0 ║║ 0x0 0 ║ ║║║║║║║║║
║║║║║║║║║╚═════31.5/2═╝╚══33.5/0.3═╝ ║║║║║║║║║
║║║║║║║║╚═════════════════════════════════════════════════════════════════════════════════════════╝║║║║║║║║
-║║║║║║║╚═══════════════════════════════════════════════════════════════════════════════════════════╝║║║║║║║
+║║║║║║║╚═════════════════════════════════════════════════════════════════════════════════════24/10═╝║║║║║║║
║║║║║║╚═════════════════════════════════════════════════════════════════════════════════════════════╝║║║║║║
║║║║║╚═══════════════════════════════════════════════════════════════════════════════════════════════╝║║║║║
║║║║╚═════════════════════════════════════════════════════════════════════════════════════════════════╝║║║║
-║║║╚═══════════════════════════════════════════════════════════════════════════════════════════════════╝║║║
+║║║╚═════════════════════════════════════════════════════════════════════════════════════════════20/14═╝║║║
║║║╔═payload/S7Payload/S7PayloadWriteVarRequest/items/S7VarPayloadDataItem═════════════╗ ║║║
║║║║╔═returnCode/DataTransportErrorCode╗╔═transportSize/DataTransportSize╗╔═dataLength╗║ ║║║
║║║║║ 0xff 255 OK ║║ 0x04 4 BYTE_WORD_DWORD ║║0x0200 512 ║║ ║║║
@@ -580,10 +581,10 @@ func TestS7MessageBytes(t *testing.T) {
║║║║║50|af fe af fe af fe af fe af fe '..........'║ ║ ║║║
║║║║║60|af fe af fe '.... '║ ║ ║║║
║║║║╚═══════════════════════════════════════38/64═╝ ║ ║║║
-║║║╚═══════════════════════════════════════════════════════════════════════════════════╝ ║║║
-║║╚═════════════════════════════════════════════════════════════════════════════════════════════════════╝║║
-║╚═══════════════════════════════════════════════════════════════════════════════════════════════════════╝║
-╚═════════════════════════════════════════════════════════════════════════════════════════════════════════╝`,
+║║║╚═════════════════════════════════════════════════════════════════════════════34/68═╝ ║║║
+║║╚═══════════════════════════════════════════════════════════════════════════════════════════════10/92═╝║║
+║╚══════════════════════════════════════════════════════════════════════════════════════════════════4/98═╝║
+╚═══════════════════════════════════════════════════════════════════════════════════════════════════0/102═╝`,
wantStringXml: `
<TPKTPacket>
<protocolId dataType="uint" bitLength="8">3</protocolId>
@@ -879,12 +880,15 @@ func TestS7MessageBytes(t *testing.T) {
}
})
t.Run("Simple 2 Compact Box", func(t *testing.T) {
- boxWriter := utils.NewWriteBufferBoxBased(utils.WithWriteBufferBoxBasedMergeSingleBoxes(), utils.WithWriteBufferBoxBasedOmitEmptyBoxes(), utils.WithWriteBufferBoxBasedPrintPosLengthFooter())
- if err := tt.args.debuggable.SerializeWithWriteBuffer(context.Background(), boxWriter); err != nil {
- t.Error(err)
- }
+ wb := utils.NewWriteBufferBoxBased(
+ utils.WithWriteBufferBoxBasedMergeSingleBoxes(),
+ utils.WithWriteBufferBoxBasedOmitEmptyBoxes(),
+ utils.WithWriteBufferBoxBasedPrintPosLengthFooter(),
+ )
+ err := wb.WriteSerializable(context.Background(), tt.args.debuggable)
+ require.NoError(t, err)
tt.wantStringSerializedCompact = strings.Trim(tt.wantStringSerializedCompact, "\n")
- if got := boxWriter.GetBox().String(); got != tt.wantStringSerializedCompact {
+ if got := wb.GetBox().String(); got != tt.wantStringSerializedCompact {
t.Errorf("Serialize BoxedCompact() = '\n%v\n', want '\n%v\n'", got, tt.wantStringSerializedCompact)
}
})
diff --git a/plc4go/spi/utils/WriteBufferBoxBased.go b/plc4go/spi/utils/WriteBufferBoxBased.go
index 60a555b002..32ba6d32ae 100644
--- a/plc4go/spi/utils/WriteBufferBoxBased.go
+++ b/plc4go/spi/utils/WriteBufferBoxBased.go
@@ -311,7 +311,21 @@ func (b *boxedWriteBuffer) WriteSerializable(ctx context.Context, serializable S
if serializable == nil {
return nil
}
- return serializable.SerializeWithWriteBuffer(ctx, b)
+ currentPos := int(b.pos) // used for footer so we remember that before we advance
+ if err := serializable.SerializeWithWriteBuffer(ctx, b); err != nil {
+ return err
+ }
+ back := b.Back()
+ if back == nil {
+ return nil
+ }
+ if ab, ok := back.Value.(AsciiBox); ok {
+ if la, ok := serializable.(LengthAware); ok {
+ bitLength := int(la.GetLengthInBits(ctx))
+ back.Value = ab.ChangeBoxFooter(b.getPosFooterWithCurrentPost(currentPos, bitLength))
+ }
+ }
+ return nil
}
func (b *boxedWriteBuffer) PopContext(logicalName string, _ ...WithWriterArgs) error {
@@ -366,11 +380,15 @@ func (b *boxedWriteBuffer) move(bits uint) {
}
func (b *boxedWriteBuffer) getPosFooter(bitLength int) string {
+ return b.getPosFooterWithCurrentPost(int(b.pos), bitLength)
+}
+
+func (b *boxedWriteBuffer) getPosFooterWithCurrentPost(currentPos, bitLength int) string {
if !b.printPosLengthFooter {
return ""
}
- bytePos := int(b.pos) / 8
- bitRemainder := int(b.pos) % 8
+ bytePos := currentPos / 8
+ bitRemainder := currentPos % 8
pos := strconv.Itoa(bytePos)
if bitRemainder != 0 {
pos += "." + strconv.Itoa(bitRemainder)
diff --git a/plc4go/spi/utils/asciiBox.go b/plc4go/spi/utils/asciiBox.go
index 839f6c0b1c..c5a37ce460 100644
--- a/plc4go/spi/utils/asciiBox.go
+++ b/plc4go/spi/utils/asciiBox.go
@@ -320,24 +320,53 @@ func (a *asciiBoxWriter) getBoxFooter(box AsciiBox) string {
}
func (a *asciiBoxWriter) changeBoxName(box AsciiBox, newName string) AsciiBox {
- if !a.hasBorders(box) {
- return a.boxString(box.String(), WithAsciiBoxName(newName))
- }
+ return a.changeBoxAttributes(box, &newName, nil, nil)
+}
+
+func (a *asciiBoxWriter) changeBoxHeader(box AsciiBox, newHeader string) AsciiBox {
+ return a.changeBoxAttributes(box, nil, &newHeader, nil)
+
+}
+
+func (a *asciiBoxWriter) changeBoxFooter(box AsciiBox, newFooter string) AsciiBox {
+ return a.changeBoxAttributes(box, nil, nil, &newFooter)
+}
+
+func (a *asciiBoxWriter) changeBoxAttributes(box AsciiBox, newName, newHeader, newFooter *string) AsciiBox {
+ // Current data
+ name := box.asciiBoxWriter.getBoxName(box)
header := box.asciiBoxWriter.getBoxHeader(box)
footer := box.asciiBoxWriter.getBoxFooter(box)
- minimumWidth := countChars(a.defaultBoxSet.UpperLeftCorner + a.defaultBoxSet.HorizontalLine + newName + a.defaultBoxSet.UpperRightCorner)
- if header != "" {
+ // set new metadata
+ if newName != nil {
+ name = *newName
+ }
+ if newHeader != nil {
+ header = *newHeader
+ }
+ if newFooter != nil {
+ footer = *newFooter
+ }
+ var newOptions = []func(options *BoxOptions){
+ WithAsciiBoxName(name),
+ WithAsciiBoxHeader(header),
+ WithAsciiBoxFooter(footer),
+ }
+
+ if !a.hasBorders(box) { // this means that this is a naked box.
+ return a.boxString(box.String(), newOptions...)
+ }
+ minimumWidth := countChars(a.defaultBoxSet.UpperLeftCorner + a.defaultBoxSet.HorizontalLine + name + a.defaultBoxSet.UpperRightCorner)
+ if header != "" { // if we have a header we need to extend that minimum width to make space for the header
minimumWidth += countChars(a.defaultBoxSet.HorizontalLine + header)
}
- boxContent := a.unwrap(box)
- rawWidth := boxContent.Width()
- minimumWidth = max(minimumWidth, rawWidth+2)
+ boxContent := a.unwrap(box) // get the content itself ...
+ rawWidth := boxContent.Width() // ... and look at the width.
+ minimumWidth = max(minimumWidth, rawWidth+2) // check that we have enough space for the content.
+ minimumWidth = max(minimumWidth, countChars(footer)+2) // check that we have enough space for the footer.
newBox := a.BoxString(
boxContent.String(),
- WithAsciiBoxName(newName),
- WithAsciiBoxHeader(header),
- WithAsciiBoxFooter(footer),
- WithAsciiBoxCharWidth(minimumWidth),
+ append(newOptions, WithAsciiBoxCharWidth(minimumWidth))...,
)
newBox.compressedBoxSet = a.defaultBoxSet.contributeToCompressedBoxSet(box)
return newBox
@@ -452,6 +481,14 @@ func (m AsciiBox) ChangeBoxName(newName string) AsciiBox {
return m.asciiBoxWriter.changeBoxName(m, newName)
}
+func (m AsciiBox) ChangeBoxHeader(newHeader string) AsciiBox {
+ return m.asciiBoxWriter.changeBoxHeader(m, newHeader)
+}
+
+func (m AsciiBox) ChangeBoxFooter(newFooter string) AsciiBox {
+ return m.asciiBoxWriter.changeBoxFooter(m, newFooter)
+}
+
func (m AsciiBox) IsEmpty() bool {
if m.asciiBoxWriter.hasBorders(m) {
return m.asciiBoxWriter.unwrap(m).String() == ""
diff --git a/plc4go/spi/utils/asciiBox_test.go b/plc4go/spi/utils/asciiBox_test.go
index b55ed94d80..b828471f55 100644
--- a/plc4go/spi/utils/asciiBox_test.go
+++ b/plc4go/spi/utils/asciiBox_test.go
@@ -1610,7 +1610,21 @@ func Test_asciiBoxWriter_changeBoxName(t *testing.T) {
}{
{
name: "change a name",
- want: AsciiBox{data: "\x00\x00"},
+ args: args{box: AsciiBox{asciiBoxWriter: NewAsciiBoxWriter().(*asciiBoxWriter)}},
+ fields: fields{
+ newLine: '\n',
+ emptyPadding: " ",
+ extraNameCharIndent: 1,
+ borderWidth: 1,
+ newLineCharWidth: 1,
+ defaultBoxSet: DefaultBoxSet(),
+ boxHeaderRegex: regexp.MustCompile(`^` + DefaultBoxSet().UpperLeftCorner + DefaultBoxSet().HorizontalLine + `(?P<name>[\w /]+)` + DefaultBoxSet().HorizontalLine + `*` + `(?P<header>[\w /]+)?` + DefaultBoxSet().HorizontalLine + `*` + DefaultBoxSet().UpperRightCorner),
+ boxFooterRegex: regexp.MustCompile(`(?m)^` + DefaultBoxSet().LowerLeftCorner + DefaultBoxSet().HorizontalLine + `*` + `(?P<footer>[\w /]+)` + DefaultBoxSet().HorizontalLine + `*` + DefaultBoxSet().LowerRightCorner),
+ },
+ want: AsciiBox{
+ data: "╔═╗\n║ ║\n╚═╝",
+ compressedBoxSet: "╔╗═║╚╝",
+ },
},
{
name: "full box name",
(plc4x) 03/04: feat(plc4go/spi): add color support to ascii box
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 09439fb77335780f9bf9e8962432281bf4198cfa
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Oct 1 13:53:31 2024 +0200
feat(plc4go/spi): add color support to ascii box
---
plc4go/go.mod | 1 +
plc4go/go.sum | 2 +
plc4go/internal/s7/s7Io_test.go | 2 +-
plc4go/spi/utils/asciiBox.go | 91 ++++++++++++++++++++++++++++++---------
plc4go/spi/utils/asciiBox_test.go | 37 +++++++++++++++-
5 files changed, 110 insertions(+), 23 deletions(-)
diff --git a/plc4go/go.mod b/plc4go/go.mod
index ed80b79ac2..72567f99d8 100644
--- a/plc4go/go.mod
+++ b/plc4go/go.mod
@@ -25,6 +25,7 @@ require (
github.com/IBM/netaddr v1.5.0
github.com/ajankovic/xdiff v0.0.1
github.com/cstockton/go-conv v1.0.0
+ github.com/fatih/color v1.17.0
github.com/google/uuid v1.6.0
github.com/gopacket/gopacket v1.3.0
github.com/icza/bitio v1.1.0
diff --git a/plc4go/go.sum b/plc4go/go.sum
index c6c06e007f..dc413a6090 100644
--- a/plc4go/go.sum
+++ b/plc4go/go.sum
@@ -11,6 +11,8 @@ github.com/cstockton/go-conv v1.0.0/go.mod h1:HuiHkkRgOA0IoBNPC7ysG7kNpjDYlgM7Kj
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
+github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
diff --git a/plc4go/internal/s7/s7Io_test.go b/plc4go/internal/s7/s7Io_test.go
index 03576e92e2..1024692f3b 100644
--- a/plc4go/internal/s7/s7Io_test.go
+++ b/plc4go/internal/s7/s7Io_test.go
@@ -22,11 +22,11 @@ package s7
import (
"context"
"fmt"
- "github.com/stretchr/testify/require"
"strings"
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"github.com/apache/plc4x/plc4go/protocols/s7/readwrite/model"
"github.com/apache/plc4x/plc4go/spi/utils"
diff --git a/plc4go/spi/utils/asciiBox.go b/plc4go/spi/utils/asciiBox.go
index c5a37ce460..dc2e3edde6 100644
--- a/plc4go/spi/utils/asciiBox.go
+++ b/plc4go/spi/utils/asciiBox.go
@@ -20,10 +20,12 @@
package utils
import (
+ "fmt"
"math"
"regexp"
"strings"
+ "github.com/fatih/color"
"github.com/rs/zerolog"
)
@@ -141,22 +143,7 @@ type AsciiBoxWriter interface {
}
func NewAsciiBoxWriter(opts ...func(writer *asciiBoxWriter)) AsciiBoxWriter {
- a := &asciiBoxWriter{
- newLine: '\n',
- emptyPadding: " ",
- // the name gets prefixed with an extra symbol for indent
- extraNameCharIndent: 1,
- borderWidth: 1,
- newLineCharWidth: 1,
- defaultBoxSet: DefaultBoxSet(),
- }
- for _, opt := range opts {
- opt(a)
- }
- hl := a.defaultBoxSet.HorizontalLine
- a.boxHeaderRegex = regexp.MustCompile(`^` + a.defaultBoxSet.UpperLeftCorner + hl + `(?P<name>[^` + hl + `]+)` + hl + `*` + `(?P<header>[^` + hl + `]+)?` + hl + `*` + a.defaultBoxSet.UpperRightCorner)
- a.boxFooterRegex = regexp.MustCompile(`(?m)^` + a.defaultBoxSet.LowerLeftCorner + hl + `*` + `(?P<footer>[^` + hl + `]+)` + hl + `*` + a.defaultBoxSet.LowerRightCorner)
- return a
+ return newAsciiBoxWriter(opts...)
}
func WithAsciiBoxWriterDefaultBoxSet(boxSet BoxSet) func(*asciiBoxWriter) {
@@ -165,6 +152,34 @@ func WithAsciiBoxWriterDefaultBoxSet(boxSet BoxSet) func(*asciiBoxWriter) {
}
}
+func WithAsciiBoxWriterDefaultColoredBoxes(nameColor, headerColor, footerColor *color.Color) func(*asciiBoxWriter) {
+ return func(a *asciiBoxWriter) {
+ if nameColor != nil {
+ a.namePrinter = nameColor.Sprint
+ } else {
+ a.namePrinter = fmt.Sprint
+ }
+ if headerColor != nil {
+ a.headerPrinter = headerColor.Sprint
+ } else {
+ a.headerPrinter = fmt.Sprint
+ }
+ if footerColor != nil {
+ a.footerPrinter = footerColor.Sprint
+ } else {
+ a.footerPrinter = fmt.Sprint
+ }
+ }
+}
+
+func WithAsciiBoxWriterDisableColoredBoxes() func(*asciiBoxWriter) {
+ return func(a *asciiBoxWriter) {
+ a.namePrinter = fmt.Sprint
+ a.headerPrinter = fmt.Sprint
+ a.footerPrinter = fmt.Sprint
+ }
+}
+
///////////////////////////////////////
///////////////////////////////////////
//
@@ -217,10 +232,35 @@ type asciiBoxWriter struct {
defaultBoxSet BoxSet
boxHeaderRegex *regexp.Regexp
boxFooterRegex *regexp.Regexp
+ namePrinter func(a ...any) string
+ headerPrinter func(a ...any) string
+ footerPrinter func(a ...any) string
}
var _ AsciiBoxWriter = (*asciiBoxWriter)(nil)
+func newAsciiBoxWriter(opts ...func(writer *asciiBoxWriter)) *asciiBoxWriter {
+ a := &asciiBoxWriter{
+ newLine: '\n',
+ emptyPadding: " ",
+ // the name gets prefixed with an extra symbol for indent
+ extraNameCharIndent: 1,
+ borderWidth: 1,
+ newLineCharWidth: 1,
+ defaultBoxSet: DefaultBoxSet(),
+ namePrinter: color.New(color.FgGreen, color.Bold).Sprint,
+ headerPrinter: color.New(color.FgBlue).Sprint,
+ footerPrinter: color.New(color.FgRed, color.Italic).Sprint,
+ }
+ for _, opt := range opts {
+ opt(a)
+ }
+ hl := a.defaultBoxSet.HorizontalLine
+ a.boxHeaderRegex = regexp.MustCompile(`^` + a.defaultBoxSet.UpperLeftCorner + hl + `(?P<name>[^` + hl + `]+)` + hl + `*` + `(?P<header>[^` + hl + `]+)?` + hl + `*` + a.defaultBoxSet.UpperRightCorner)
+ a.boxFooterRegex = regexp.MustCompile(`(?m)^` + a.defaultBoxSet.LowerLeftCorner + hl + `*` + `(?P<footer>[^` + hl + `]+)` + hl + `*` + a.defaultBoxSet.LowerRightCorner)
+ return a
+}
+
func (a *asciiBoxWriter) boxString(data string, options ...func(*BoxOptions)) AsciiBox {
var opts BoxOptions
opts.BoxSet = a.defaultBoxSet
@@ -229,16 +269,19 @@ func (a *asciiBoxWriter) boxString(data string, options ...func(*BoxOptions)) As
}
name := opts.Name
nameLength := countChars(name)
+ if name != "" {
+ name = a.namePrinter(name)
+ }
header := opts.Header
if name != "" && header != "" {
- header = opts.BoxSet.HorizontalLine + opts.BoxSet.HorizontalLine + header + opts.BoxSet.HorizontalLine // Lazy manipulation to trick calculation below (adds a spacing between name and header)
+ header = opts.BoxSet.HorizontalLine + opts.BoxSet.HorizontalLine + a.headerPrinter(header) + opts.BoxSet.HorizontalLine // Lazy manipulation to trick calculation below (adds a spacing between name and header)
}
headerLength := countChars(header)
footer := opts.Footer
if footer != "" {
- footer = footer + opts.BoxSet.HorizontalLine
+ footer = a.footerPrinter(footer) + opts.BoxSet.HorizontalLine
}
footerLength := countChars(footer)
@@ -292,7 +335,7 @@ func (a *asciiBoxWriter) getBoxName(box AsciiBox) string {
if index < 0 {
return ""
}
- return subMatch[index]
+ return cleanString(subMatch[index])
}
func (a *asciiBoxWriter) getBoxHeader(box AsciiBox) string {
@@ -304,7 +347,7 @@ func (a *asciiBoxWriter) getBoxHeader(box AsciiBox) string {
if index < 0 {
return ""
}
- return subMatch[index]
+ return cleanString(subMatch[index])
}
func (a *asciiBoxWriter) getBoxFooter(box AsciiBox) string {
@@ -316,7 +359,7 @@ func (a *asciiBoxWriter) getBoxFooter(box AsciiBox) string {
if index < 0 {
return ""
}
- return subMatch[index]
+ return cleanString(subMatch[index])
}
func (a *asciiBoxWriter) changeBoxName(box AsciiBox, newName string) AsciiBox {
@@ -445,6 +488,12 @@ func countChars(s string) int {
return len([]rune(ANSI_PATTERN.ReplaceAllString(s, "")))
}
+// cleanString returns the strings minus the control sequences
+func cleanString(s string) string {
+ regex, _ := regexp.Compile(`\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])`)
+ return regex.ReplaceAllString(s, "")
+}
+
//
// Internal section
//
diff --git a/plc4go/spi/utils/asciiBox_test.go b/plc4go/spi/utils/asciiBox_test.go
index b828471f55..bbbe99a6ab 100644
--- a/plc4go/spi/utils/asciiBox_test.go
+++ b/plc4go/spi/utils/asciiBox_test.go
@@ -26,6 +26,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func init() {
@@ -1029,7 +1030,17 @@ func TestNewAsciiBoxWriter(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- assert.Equalf(t, tt.want, NewAsciiBoxWriter(), "NewAsciiBoxWriter()")
+ got := NewAsciiBoxWriter()
+ require.NotNil(t, got)
+ require.IsType(t, new(asciiBoxWriter), got)
+ boxWriter := got.(*asciiBoxWriter)
+ assert.NotNil(t, boxWriter.namePrinter)
+ boxWriter.namePrinter = nil
+ assert.NotNil(t, boxWriter.headerPrinter)
+ boxWriter.headerPrinter = nil
+ assert.NotNil(t, boxWriter.footerPrinter)
+ boxWriter.footerPrinter = nil
+ assert.Equalf(t, tt.want, got, "NewAsciiBoxWriter()")
})
}
}
@@ -1363,6 +1374,9 @@ func Test_asciiBoxWriter_boxString(t *testing.T) {
borderWidth int
newLineCharWidth int
boxNameRegex *regexp.Regexp
+ namePrinter func(a ...any) string
+ headerPrinter func(a ...any) string
+ footerPrinter func(a ...any) string
}
type args struct {
data string
@@ -1580,6 +1594,18 @@ func Test_asciiBoxWriter_boxString(t *testing.T) {
borderWidth: tt.fields.borderWidth,
newLineCharWidth: tt.fields.newLineCharWidth,
boxHeaderRegex: tt.fields.boxNameRegex,
+ namePrinter: tt.fields.namePrinter,
+ headerPrinter: tt.fields.headerPrinter,
+ footerPrinter: tt.fields.footerPrinter,
+ }
+ if a.namePrinter == nil {
+ a.namePrinter = fmt.Sprint
+ }
+ if a.headerPrinter == nil {
+ a.headerPrinter = fmt.Sprint
+ }
+ if a.footerPrinter == nil {
+ a.footerPrinter = fmt.Sprint
}
tt.want.asciiBoxWriter = a
assert.Equalf(t, tt.want, a.boxString(tt.args.data, tt.args.options...), "boxString(%v)", tt.args.data)
@@ -1671,6 +1697,15 @@ func Test_asciiBoxWriter_changeBoxName(t *testing.T) {
boxHeaderRegex: tt.fields.boxHeaderRegex,
boxFooterRegex: tt.fields.boxFooterRegex,
}
+ if a.namePrinter == nil {
+ a.namePrinter = fmt.Sprint
+ }
+ if a.headerPrinter == nil {
+ a.headerPrinter = fmt.Sprint
+ }
+ if a.footerPrinter == nil {
+ a.footerPrinter = fmt.Sprint
+ }
tt.want.asciiBoxWriter = a
assert.Equalf(t, tt.want, a.changeBoxName(tt.args.box, tt.args.newName), "changeBoxName(%v, %v)", tt.args.box, tt.args.newName)
})
(plc4x) 01/04: fix(plc4go/spi): fix footer being lost on name change
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 7aea3ab444327cb6523d55f21ff371463ecbfef8
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Oct 1 08:54:24 2024 +0200
fix(plc4go/spi): fix footer being lost on name change
---
plc4go/spi/utils/asciiBox.go | 5 +++--
plc4go/spi/utils/asciiBox_test.go | 4 ++--
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/plc4go/spi/utils/asciiBox.go b/plc4go/spi/utils/asciiBox.go
index 4197d7a990..839f6c0b1c 100644
--- a/plc4go/spi/utils/asciiBox.go
+++ b/plc4go/spi/utils/asciiBox.go
@@ -153,8 +153,9 @@ func NewAsciiBoxWriter(opts ...func(writer *asciiBoxWriter)) AsciiBoxWriter {
for _, opt := range opts {
opt(a)
}
- a.boxHeaderRegex = regexp.MustCompile(`^` + a.defaultBoxSet.UpperLeftCorner + a.defaultBoxSet.HorizontalLine + `(?P<name>[\w /]+)` + a.defaultBoxSet.HorizontalLine + `*` + `(?P<header>[\w /]+)?` + a.defaultBoxSet.HorizontalLine + `*` + a.defaultBoxSet.UpperRightCorner)
- a.boxFooterRegex = regexp.MustCompile(`(?m)^` + a.defaultBoxSet.LowerLeftCorner + a.defaultBoxSet.HorizontalLine + `*` + `(?P<footer>[\w /]+)` + a.defaultBoxSet.HorizontalLine + `*` + a.defaultBoxSet.LowerRightCorner)
+ hl := a.defaultBoxSet.HorizontalLine
+ a.boxHeaderRegex = regexp.MustCompile(`^` + a.defaultBoxSet.UpperLeftCorner + hl + `(?P<name>[^` + hl + `]+)` + hl + `*` + `(?P<header>[^` + hl + `]+)?` + hl + `*` + a.defaultBoxSet.UpperRightCorner)
+ a.boxFooterRegex = regexp.MustCompile(`(?m)^` + a.defaultBoxSet.LowerLeftCorner + hl + `*` + `(?P<footer>[^` + hl + `]+)` + hl + `*` + a.defaultBoxSet.LowerRightCorner)
return a
}
diff --git a/plc4go/spi/utils/asciiBox_test.go b/plc4go/spi/utils/asciiBox_test.go
index 79e8ba4b32..b55ed94d80 100644
--- a/plc4go/spi/utils/asciiBox_test.go
+++ b/plc4go/spi/utils/asciiBox_test.go
@@ -1022,8 +1022,8 @@ func TestNewAsciiBoxWriter(t *testing.T) {
extraNameCharIndent: 1,
borderWidth: 1,
newLineCharWidth: 1,
- boxHeaderRegex: regexp.MustCompile(`^` + upperLeftCorner + horizontalLine + `(?P<name>[\w /]+)` + horizontalLine + `*` + `(?P<header>[\w /]+)?` + horizontalLine + `*` + upperRightCorner),
- boxFooterRegex: regexp.MustCompile(`(?m)^` + lowerLeftCorner + horizontalLine + `*` + `(?P<footer>[\w /]+)` + horizontalLine + `*` + lowerRightCorner),
+ boxHeaderRegex: regexp.MustCompile(`^` + upperLeftCorner + horizontalLine + `(?P<name>[^` + horizontalLine + `]+)` + horizontalLine + `*` + `(?P<header>[^` + horizontalLine + `]+)?` + horizontalLine + `*` + upperRightCorner),
+ boxFooterRegex: regexp.MustCompile(`(?m)^` + lowerLeftCorner + horizontalLine + `*` + `(?P<footer>[^` + horizontalLine + `]+)` + horizontalLine + `*` + lowerRightCorner),
},
},
}