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 5501725b95520f2150fc6fe81c1ba4fa914297aa Author: Sebastian Rühl <[email protected]> AuthorDate: Sun Apr 18 11:17:06 2021 +0200 plc4go: moved convenience functions into dumpUtils.go --- plc4go/internal/plc4go/spi/utils/asciiBox.go | 51 ---- plc4go/internal/plc4go/spi/utils/asciiBox_test.go | 61 ----- plc4go/internal/plc4go/spi/utils/dumpUtils.go | 114 +++++++++ plc4go/internal/plc4go/spi/utils/dumpUtils_test.go | 278 +++++++++++++++++++++ plc4go/internal/plc4go/spi/utils/hex.go | 39 --- plc4go/internal/plc4go/spi/utils/hex_test.go | 149 ----------- 6 files changed, 392 insertions(+), 300 deletions(-) diff --git a/plc4go/internal/plc4go/spi/utils/asciiBox.go b/plc4go/internal/plc4go/spi/utils/asciiBox.go index 9e8fc3a..b4a685d 100644 --- a/plc4go/internal/plc4go/spi/utils/asciiBox.go +++ b/plc4go/internal/plc4go/spi/utils/asciiBox.go @@ -20,10 +20,8 @@ package utils import ( - "fmt" "github.com/rs/zerolog/log" "math" - "reflect" "strings" ) @@ -51,55 +49,6 @@ type AsciiBoxer interface { Box(string, int) AsciiBox } -func BoxAnything(name string, anything interface{}, charWidth int) AsciiBox { - switch anything.(type) { - case nil: - return "" - case AsciiBoxer: - if reflect.ValueOf(anything).IsNil() { - return "" - } - // A box usually has its own name - return anything.(AsciiBoxer).Box(name, charWidth) - case bool: - return BoxString(name, fmt.Sprintf("%t", anything), 0) - case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64: - // TODO: include hex later with this line - //return BoxString(name, fmt.Sprintf("%#0*x %d", unsafe.Sizeof(anything)/2, anything, anything), 0) - return BoxString(name, fmt.Sprintf("%d", anything), 0) - case []byte: - return AsciiBox(DumpFixedWidth(anything.([]byte), charWidth)) - case string: - return BoxString(name, anything.(string), charWidth) - case fmt.Stringer: - return BoxString(name, anything.(fmt.Stringer).String(), 0) - default: - valueOf := reflect.ValueOf(anything) - if valueOf.IsNil() { - return "" - } - switch valueOf.Kind() { - case reflect.Bool: - return BoxString(name, fmt.Sprintf("%t", anything), 0) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, - reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64: - // TODO: include hex here somehow. Seems that %x does print strange hex values here - return BoxString(name, fmt.Sprintf("%d", anything), 0) - case reflect.Slice, reflect.Array: - boxes := make([]AsciiBox, valueOf.Len()) - for i := 0; i < valueOf.Len(); i++ { - index := valueOf.Index(i) - boxes[i] = BoxAnything("", index.Interface(), charWidth-2) - } - return BoxBox(name, AlignBoxes(boxes, charWidth), 0) - case reflect.Ptr, reflect.Uintptr: - return BoxAnything(name, valueOf.Elem().Interface(), charWidth) - default: - return BoxString(name, fmt.Sprintf("0x%x", anything.(interface{})), charWidth) - } - } -} - // BoxBox boxes a box func BoxBox(name string, box AsciiBox, charWidth int) AsciiBox { return BoxString(name, string(box), charWidth) diff --git a/plc4go/internal/plc4go/spi/utils/asciiBox_test.go b/plc4go/internal/plc4go/spi/utils/asciiBox_test.go index 68a5857..ea7ed80 100644 --- a/plc4go/internal/plc4go/spi/utils/asciiBox_test.go +++ b/plc4go/internal/plc4go/spi/utils/asciiBox_test.go @@ -28,67 +28,6 @@ func init() { DebugAsciiBox = true } -func TestBoxAnything(t *testing.T) { - type args struct { - name string - anything interface{} - charWidth int - } - tests := []struct { - name string - args args - want AsciiBox - }{ - { - name: "test bool", - args: args{ - name: "exampleBool", - anything: true, - charWidth: 0, - }, - want: ` -╔═exampleBool╗ -║ true ║ -╚════════════╝ -`, - }, - { - name: "test int", - args: args{ - name: "exampleInt", - anything: 1, - charWidth: 0, - }, - want: ` -╔═exampleInt╗ -║ 1 ║ -╚═══════════╝ -`, - }, - { - name: "test int 123123123", - args: args{ - name: "exampleInt", - anything: 123123123, - charWidth: 0, - }, - want: ` -╔═exampleInt╗ -║ 123123123 ║ -╚═══════════╝ -`, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - tt.want = trimBox(tt.want) - if got := BoxAnything(tt.args.name, tt.args.anything, tt.args.charWidth); got != tt.want { - t.Errorf("BoxAnything() = '\n%v\n', want '\n%v\n'", got, tt.want) - } - }) - } -} - func TestBoxSideBySide(t *testing.T) { type args struct { box1 AsciiBox diff --git a/plc4go/internal/plc4go/spi/utils/dumpUtils.go b/plc4go/internal/plc4go/spi/utils/dumpUtils.go new file mode 100644 index 0000000..59a1bad --- /dev/null +++ b/plc4go/internal/plc4go/spi/utils/dumpUtils.go @@ -0,0 +1,114 @@ +// +// 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 utils + +import ( + "fmt" + "github.com/rs/zerolog/log" + "reflect" +) + +// BoxedDump dumps a 56+2 char wide hex string +func BoxedDump(name string, data []byte) AsciiBox { + return BoxString(name, DumpFixedWidth(data, DefaultWidth), DefaultWidth+boxLineOverheat) +} + +// BoxedDumpFixedWidth dumps a hex into a beautiful box +func BoxedDumpFixedWidth(name string, data []byte, charWidth int) AsciiBox { + // we substract the 2 lines at the side + dumpWidth := charWidth - 1 - 1 + return BoxString(name, DumpFixedWidth(data, dumpWidth), charWidth) +} + +// BoxedDumpAnything dumps anything as hex into a beautiful box +func BoxedDumpAnything(name string, anything interface{}) AsciiBox { + return BoxString(name, DumpAnything(anything), 0) +} + +// BoxedDumpAnythingFixedWidth dumps anything as hex into a beautiful box with a given width +func BoxedDumpAnythingFixedWidth(name string, anything interface{}, charWidth int) AsciiBox { + return BoxString(name, DumpAnythingFixedWidth(anything, charWidth), 0) +} + +// DumpAnything dumps anything as hex +func DumpAnything(anything interface{}) string { + return DumpAnythingFixedWidth(anything, DefaultWidth) +} + +// DumpAnythingFixedWidth dumps anything as hex +func DumpAnythingFixedWidth(anything interface{}, charWidth int) string { + convertedBytes, err := toBytes(anything) + if err != nil { + if DebugHex { + log.Error().Err(err).Msg("Error converting to bytes") + } + return "<undumpable>" + } + return DumpFixedWidth(convertedBytes, charWidth) +} + +func BoxAnything(name string, anything interface{}, charWidth int) AsciiBox { + switch anything.(type) { + case nil: + return "" + case AsciiBoxer: + if reflect.ValueOf(anything).IsNil() { + return "" + } + // A box usually has its own name + return anything.(AsciiBoxer).Box(name, charWidth) + case bool: + return BoxString(name, fmt.Sprintf("%t", anything), 0) + case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64: + // TODO: include hex later with this line + //return BoxString(name, fmt.Sprintf("%#0*x %d", unsafe.Sizeof(anything)/2, anything, anything), 0) + return BoxString(name, fmt.Sprintf("%d", anything), 0) + case []byte: + return AsciiBox(DumpFixedWidth(anything.([]byte), charWidth)) + case string: + return BoxString(name, anything.(string), charWidth) + case fmt.Stringer: + return BoxString(name, anything.(fmt.Stringer).String(), 0) + default: + valueOf := reflect.ValueOf(anything) + if valueOf.IsNil() { + return "" + } + switch valueOf.Kind() { + case reflect.Bool: + return BoxString(name, fmt.Sprintf("%t", anything), 0) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, + reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64: + // TODO: include hex here somehow. Seems that %x does print strange hex values here + return BoxString(name, fmt.Sprintf("%d", anything), 0) + case reflect.Slice, reflect.Array: + boxes := make([]AsciiBox, valueOf.Len()) + for i := 0; i < valueOf.Len(); i++ { + index := valueOf.Index(i) + boxes[i] = BoxAnything("", index.Interface(), charWidth-2) + } + return BoxBox(name, AlignBoxes(boxes, charWidth), 0) + case reflect.Ptr, reflect.Uintptr: + return BoxAnything(name, valueOf.Elem().Interface(), charWidth) + default: + return BoxString(name, fmt.Sprintf("0x%x", anything.(interface{})), charWidth) + } + } +} diff --git a/plc4go/internal/plc4go/spi/utils/dumpUtils_test.go b/plc4go/internal/plc4go/spi/utils/dumpUtils_test.go new file mode 100644 index 0000000..1c6216e --- /dev/null +++ b/plc4go/internal/plc4go/spi/utils/dumpUtils_test.go @@ -0,0 +1,278 @@ +// +// 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 utils + +import ( + "strings" + "testing" +) + +func TestBoxAnything(t *testing.T) { + type args struct { + name string + anything interface{} + charWidth int + } + tests := []struct { + name string + args args + want AsciiBox + }{ + { + name: "test bool", + args: args{ + name: "exampleBool", + anything: true, + charWidth: 0, + }, + want: ` +╔═exampleBool╗ +║ true ║ +╚════════════╝ +`, + }, + { + name: "test int", + args: args{ + name: "exampleInt", + anything: 1, + charWidth: 0, + }, + want: ` +╔═exampleInt╗ +║ 1 ║ +╚═══════════╝ +`, + }, + { + name: "test int 123123123", + args: args{ + name: "exampleInt", + anything: 123123123, + charWidth: 0, + }, + want: ` +╔═exampleInt╗ +║ 123123123 ║ +╚═══════════╝ +`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.want = trimBox(tt.want) + if got := BoxAnything(tt.args.name, tt.args.anything, tt.args.charWidth); got != tt.want { + t.Errorf("BoxAnything() = '\n%v\n', want '\n%v\n'", got, tt.want) + } + }) + } +} + +func TestBoxedDump(t *testing.T) { + type args struct { + name string + data []byte + } + tests := []struct { + name string + args args + want string + }{ + { + name: "Test Dump", + args: args{ + name: "super nice data", + data: []byte("1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aa1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aab"), + }, + want: ` +╔═super nice data══════════════════════════════╗ +║000|31 32 33 34 35 36 37 38 39 30 '1234567890'║ +║010|61 62 63 64 65 66 67 68 69 6a 'abcdefghij'║ +║020|6b 6c 6d 6e 6f 70 71 72 73 74 'klmnopqrst'║ +║030|75 76 77 78 79 7a d3 31 32 33 'uvwxyz.123'║ +║040|34 35 36 37 38 39 30 61 62 63 '4567890abc'║ +║050|64 65 66 67 68 69 6a 6b 6c 6d 'defghijklm'║ +║060|6e 6f 70 71 72 73 74 75 76 77 'nopqrstuvw'║ +║070|78 79 7a d3 61 61 31 32 33 34 'xyz.aa1234'║ +║080|35 36 37 38 39 30 61 62 63 64 '567890abcd'║ +║090|65 66 67 68 69 6a 6b 6c 6d 6e 'efghijklmn'║ +║100|6f 70 71 72 73 74 75 76 77 78 'opqrstuvwx'║ +║110|79 7a d3 31 32 33 34 35 36 37 'yz.1234567'║ +║120|38 39 30 61 62 63 64 65 66 67 '890abcdefg'║ +║130|68 69 6a 6b 6c 6d 6e 6f 70 71 'hijklmnopq'║ +║140|72 73 74 75 76 77 78 79 7a d3 'rstuvwxyz.'║ +║150|61 61 62 'aab '║ +╚══════════════════════════════════════════════╝ +`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := BoxedDump(tt.args.name, tt.args.data); got != AsciiBox(strings.Trim(tt.want, "\n")) { + t.Errorf("Dump() = \n%v\n, want \n%v\n", got, tt.want) + } + }) + } +} + +func TestDumpAnything(t *testing.T) { + type args struct { + anything interface{} + } + tests := []struct { + name string + args args + want string + }{ + { + name: "Random struct", + args: args{ + anything: struct { + A string + B string + C string + D struct { + E string + F string + } + }{A: "a", B: "b", C: "c", D: struct { + E string + F string + }{ + E: "e", + F: "f", + }}, + }, + want: ` +000|25 ff 81 03 01 02 ff 82 00 01 '%.........' +010|04 01 01 41 01 0c 00 01 01 42 '...A.....B' +020|01 0c 00 01 01 43 01 0c 00 01 '.....C....' +030|01 44 01 ff 84 00 00 00 37 ff '.D......7.' +040|83 03 01 01 1d 73 74 72 75 63 '.....struc' +050|74 20 7b 20 45 20 73 74 72 69 't { E stri' +060|6e 67 3b 20 46 20 73 74 72 69 'ng; F stri' +070|6e 67 20 7d 01 ff 84 00 01 02 'ng }......' +080|01 01 45 01 0c 00 01 01 46 01 '..E.....F.' +090|0c 00 00 00 14 ff 82 01 01 61 '.........a' +100|01 01 62 01 01 63 01 01 01 65 '..b..c...e' +110|01 01 66 00 00 '..f.. ' +`, + }, + { + name: "unexported struct gob error", + args: args{ + anything: struct { + a string + }{a: "a"}, + }, + want: "<undumpable>", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := DumpAnything(tt.args.anything); got != strings.Trim(tt.want, "\n") { + t.Errorf("Dump() = \n%v\n, want \n%v\n", got, tt.want) + } + }) + } +} + +func TestBoxedDumpFixedWidth(t *testing.T) { + type args struct { + name string + data []byte + charWidth int + } + tests := []struct { + name string + args args + want string + }{ + { + name: "Test Dump", + args: args{ + name: "super nice data", + data: []byte("1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aa1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aab"), + charWidth: 110, + }, + want: ` +╔═super nice data════════════════════════════════════════════════════════════════════════════════════════════╗ +║ 000|31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f '1234567890abcdefghijklmno' ║ +║ 025|70 71 72 73 74 75 76 77 78 79 7a d3 31 32 33 34 35 36 37 38 39 30 61 62 63 'pqrstuvwxyz.1234567890abc' ║ +║ 050|64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a d3 61 'defghijklmnopqrstuvwxyz.a' ║ +║ 075|61 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 'a1234567890abcdefghijklmn' ║ +║ 100|6f 70 71 72 73 74 75 76 77 78 79 7a d3 31 32 33 34 35 36 37 38 39 30 61 62 'opqrstuvwxyz.1234567890ab' ║ +║ 125|63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a d3 'cdefghijklmnopqrstuvwxyz.' ║ +║ 150|61 61 62 'aab ' ║ +╚════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ +`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := BoxedDumpFixedWidth(tt.args.name, tt.args.data, tt.args.charWidth); got != AsciiBox(strings.Trim(tt.want, "\n")) { + t.Errorf("Dump() = \n%v\n, want \n%v\n", got, tt.want) + } + }) + } +} + +func TestBoxedDumpAnythingFixedWidth(t *testing.T) { + type args struct { + name string + anything interface{} + charWidth int + } + tests := []struct { + name string + args args + want AsciiBox + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := BoxedDumpAnythingFixedWidth(tt.args.name, tt.args.anything, tt.args.charWidth); got != tt.want { + t.Errorf("BoxedDumpAnythingFixedWidth() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestDumpAnythingFixedWidth(t *testing.T) { + type args struct { + anything interface{} + charWidth int + } + tests := []struct { + name string + args args + want string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := DumpAnythingFixedWidth(tt.args.anything, tt.args.charWidth); got != tt.want { + t.Errorf("DumpAnythingFixedWidth() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/plc4go/internal/plc4go/spi/utils/hex.go b/plc4go/internal/plc4go/spi/utils/hex.go index 143cd56..a11a9e6 100644 --- a/plc4go/internal/plc4go/spi/utils/hex.go +++ b/plc4go/internal/plc4go/spi/utils/hex.go @@ -47,50 +47,11 @@ const pipeWidth = 1 // DebugHex set to true to get debug messages var DebugHex bool -// BoxedDump dumps a 56+2 char wide hex string -func BoxedDump(name string, data []byte) AsciiBox { - return BoxString(name, DumpFixedWidth(data, DefaultWidth), DefaultWidth+boxLineOverheat) -} - // Dump dumps a 56 char wide hex string func Dump(data []byte) string { return DumpFixedWidth(data, DefaultWidth) } -// BoxedDumpFixedWidth dumps a hex into a beautiful box -func BoxedDumpFixedWidth(name string, data []byte, charWidth int) AsciiBox { - // we substract the 2 lines at the side - dumpWidth := charWidth - 1 - 1 - return BoxString(name, DumpFixedWidth(data, dumpWidth), charWidth) -} - -// BoxedDumpAnything dumps anything as hex into a beautiful box -func BoxedDumpAnything(name string, anything interface{}) AsciiBox { - return BoxString(name, DumpAnything(anything), 0) -} - -// BoxedDumpAnythingFixedWidth dumps anything as hex into a beautiful box with a given width -func BoxedDumpAnythingFixedWidth(name string, anything interface{}, charWidth int) AsciiBox { - return BoxString(name, DumpAnythingFixedWidth(anything, charWidth), 0) -} - -// DumpAnything dumps anything as hex -func DumpAnything(anything interface{}) string { - return DumpAnythingFixedWidth(anything, DefaultWidth) -} - -// DumpAnythingFixedWidth dumps anything as hex -func DumpAnythingFixedWidth(anything interface{}, charWidth int) string { - convertedBytes, err := toBytes(anything) - if err != nil { - if DebugHex { - log.Error().Err(err).Msg("Error converting to bytes") - } - return "<undumpable>" - } - return DumpFixedWidth(convertedBytes, charWidth) -} - // DumpFixedWidth dumps hex as hex string. Min width of string returned is 18 up to supplied charWidth func DumpFixedWidth(data []byte, desiredCharWidth int) string { if data == nil || len(data) < 1 { diff --git a/plc4go/internal/plc4go/spi/utils/hex_test.go b/plc4go/internal/plc4go/spi/utils/hex_test.go index 8f1fdea..f22dfe5 100644 --- a/plc4go/internal/plc4go/spi/utils/hex_test.go +++ b/plc4go/internal/plc4go/spi/utils/hex_test.go @@ -212,155 +212,6 @@ func BenchmarkTestDump(b *testing.B) { DebugHex = true } -func TestBoxedDump(t *testing.T) { - type args struct { - name string - data []byte - } - tests := []struct { - name string - args args - want string - }{ - { - name: "Test Dump", - args: args{ - name: "super nice data", - data: []byte("1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aa1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aab"), - }, - want: ` -╔═super nice data══════════════════════════════╗ -║000|31 32 33 34 35 36 37 38 39 30 '1234567890'║ -║010|61 62 63 64 65 66 67 68 69 6a 'abcdefghij'║ -║020|6b 6c 6d 6e 6f 70 71 72 73 74 'klmnopqrst'║ -║030|75 76 77 78 79 7a d3 31 32 33 'uvwxyz.123'║ -║040|34 35 36 37 38 39 30 61 62 63 '4567890abc'║ -║050|64 65 66 67 68 69 6a 6b 6c 6d 'defghijklm'║ -║060|6e 6f 70 71 72 73 74 75 76 77 'nopqrstuvw'║ -║070|78 79 7a d3 61 61 31 32 33 34 'xyz.aa1234'║ -║080|35 36 37 38 39 30 61 62 63 64 '567890abcd'║ -║090|65 66 67 68 69 6a 6b 6c 6d 6e 'efghijklmn'║ -║100|6f 70 71 72 73 74 75 76 77 78 'opqrstuvwx'║ -║110|79 7a d3 31 32 33 34 35 36 37 'yz.1234567'║ -║120|38 39 30 61 62 63 64 65 66 67 '890abcdefg'║ -║130|68 69 6a 6b 6c 6d 6e 6f 70 71 'hijklmnopq'║ -║140|72 73 74 75 76 77 78 79 7a d3 'rstuvwxyz.'║ -║150|61 61 62 'aab '║ -╚══════════════════════════════════════════════╝ -`, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := BoxedDump(tt.args.name, tt.args.data); got != AsciiBox(strings.Trim(tt.want, "\n")) { - t.Errorf("Dump() = \n%v\n, want \n%v\n", got, tt.want) - } - }) - } -} - -func TestBoxedDumpFixedWidth(t *testing.T) { - type args struct { - name string - data []byte - charWidth int - } - tests := []struct { - name string - args args - want string - }{ - { - name: "Test Dump", - args: args{ - name: "super nice data", - data: []byte("1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aa1234567890abcdefghijklmnopqrstuvwxyz\3231234567890abcdefghijklmnopqrstuvwxyz\323aab"), - charWidth: 110, - }, - want: ` -╔═super nice data════════════════════════════════════════════════════════════════════════════════════════════╗ -║ 000|31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f '1234567890abcdefghijklmno' ║ -║ 025|70 71 72 73 74 75 76 77 78 79 7a d3 31 32 33 34 35 36 37 38 39 30 61 62 63 'pqrstuvwxyz.1234567890abc' ║ -║ 050|64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a d3 61 'defghijklmnopqrstuvwxyz.a' ║ -║ 075|61 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 'a1234567890abcdefghijklmn' ║ -║ 100|6f 70 71 72 73 74 75 76 77 78 79 7a d3 31 32 33 34 35 36 37 38 39 30 61 62 'opqrstuvwxyz.1234567890ab' ║ -║ 125|63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a d3 'cdefghijklmnopqrstuvwxyz.' ║ -║ 150|61 61 62 'aab ' ║ -╚════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ -`, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := BoxedDumpFixedWidth(tt.args.name, tt.args.data, tt.args.charWidth); got != AsciiBox(strings.Trim(tt.want, "\n")) { - t.Errorf("Dump() = \n%v\n, want \n%v\n", got, tt.want) - } - }) - } -} - -func TestDumpAnything(t *testing.T) { - type args struct { - anything interface{} - } - tests := []struct { - name string - args args - want string - }{ - { - name: "Random struct", - args: args{ - anything: struct { - A string - B string - C string - D struct { - E string - F string - } - }{A: "a", B: "b", C: "c", D: struct { - E string - F string - }{ - E: "e", - F: "f", - }}, - }, - want: ` -000|25 ff 81 03 01 02 ff 82 00 01 '%.........' -010|04 01 01 41 01 0c 00 01 01 42 '...A.....B' -020|01 0c 00 01 01 43 01 0c 00 01 '.....C....' -030|01 44 01 ff 84 00 00 00 37 ff '.D......7.' -040|83 03 01 01 1d 73 74 72 75 63 '.....struc' -050|74 20 7b 20 45 20 73 74 72 69 't { E stri' -060|6e 67 3b 20 46 20 73 74 72 69 'ng; F stri' -070|6e 67 20 7d 01 ff 84 00 01 02 'ng }......' -080|01 01 45 01 0c 00 01 01 46 01 '..E.....F.' -090|0c 00 00 00 14 ff 82 01 01 61 '.........a' -100|01 01 62 01 01 63 01 01 01 65 '..b..c...e' -110|01 01 66 00 00 '..f.. ' -`, - }, - { - name: "unexported struct gob error", - args: args{ - anything: struct { - a string - }{a: "a"}, - }, - want: "<undumpable>", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := DumpAnything(tt.args.anything); got != strings.Trim(tt.want, "\n") { - t.Errorf("Dump() = \n%v\n, want \n%v\n", got, tt.want) - } - }) - } -} - func TestDumpFixedWidth(t *testing.T) { type args struct { data []byte
