File: tablewriter_test.go

package info (click to toggle)
golang-github-olekukonko-tablewriter 1.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 1,380 kB
  • sloc: makefile: 4
file content (696 lines) | stat: -rw-r--r-- 24,839 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package tablewriter

import (
	"bytes"
	"database/sql"
	"fmt"
	"github.com/olekukonko/ll"
	"github.com/olekukonko/tablewriter/tw"
	"os"
	"reflect"
	"sync"
	"testing"
)

// TestBuildPaddingLineContents tests the buildPaddingLineContents function with various configurations.
// It verifies padding line construction with different widths and merge states.
func TestBuildPaddingLineContents(t *testing.T) {
	table := NewTable(os.Stdout)
	widths := tw.NewMapper[int, int]()
	widths.Set(0, 5)
	widths.Set(1, 10)
	merges := map[int]tw.MergeState{
		1: {Horizontal: tw.MergeStateOption{Present: true, Start: false}},
	}
	t.Run("Basic Padding", func(t *testing.T) {
		line := table.buildPaddingLineContents("-", widths, 2, nil)
		expected := []string{"-----", "----------"}
		if !reflect.DeepEqual(line, expected) {
			t.Errorf("Expected %v, got %v", expected, line)
		}
	})
	t.Run("With Merge", func(t *testing.T) {
		line := table.buildPaddingLineContents("-", widths, 2, merges)
		expected := []string{"-----", ""}
		if !reflect.DeepEqual(line, expected) {
			t.Errorf("Expected %v, got %v", expected, line)
		}
	})
	t.Run("Zero Width", func(t *testing.T) {
		widths.Set(0, 0)
		line := table.buildPaddingLineContents("-", widths, 2, nil)
		expected := []string{"", "----------"}
		if !reflect.DeepEqual(line, expected) {
			t.Errorf("Expected %v, got %v", expected, line)
		}
	})
}

// TestCalculateContentMaxWidth tests the calculateContentMaxWidth function in batch and streaming modes.
// It verifies width calculations with various constraints and padding settings.
func TestCalculateContentMaxWidth(t *testing.T) {
	table := NewTable(os.Stdout)
	config := tw.CellConfig{
		Formatting: tw.CellFormatting{
			AutoWrap:   tw.WrapTruncate,
			Alignment:  tw.AlignLeft,
			AutoFormat: tw.Off,
		},
		ColMaxWidths: tw.CellWidth{Global: 10},
		Padding: tw.CellPadding{
			Global: tw.Padding{Left: " ", Right: " "},
		},
	}
	t.Run("Batch Mode with MaxWidth", func(t *testing.T) {
		got := table.calculateContentMaxWidth(0, config, 1, 1, false)
		if got != 8 { // 10 - 1 (left) - 1 (right)
			t.Errorf("Expected width 8, got %d", got)
		}
	})
	t.Run("Streaming Mode", func(t *testing.T) {
		table.streamWidths = map[int]int{0: 12}
		table.config.Stream.Enable = true
		table.hasPrinted = true
		got := table.calculateContentMaxWidth(0, config, 1, 1, true)
		if got != 10 { // 12 - 1 (left) - 1 (right)
			t.Errorf("Expected width 10, got %d", got)
		}
	})
	t.Run("No Constraint in Batch", func(t *testing.T) {
		config.ColMaxWidths.Global = 0
		got := table.calculateContentMaxWidth(0, config, 1, 1, false)
		if got != 0 {
			t.Errorf("Expected width 0, got %d", got)
		}
	})
}

// TestCallStringer tests the convertToStringer function with caching enabled and disabled.
// It verifies stringer invocation and cache behavior for custom types.
func TestCallStringer(t *testing.T) {
	table := &Table{
		logger:               ll.New("test"),
		stringer:             func(s interface{}) []string { return []string{fmt.Sprintf("%v", s)} },
		stringerCache:        map[reflect.Type]reflect.Value{},
		stringerCacheEnabled: true,
	}
	input := struct{ Name string }{Name: "test"}
	cells, err := table.convertToStringer(input)
	if err != nil {
		t.Errorf("convertToStringer failed: %v", err)
	}
	if len(cells) != 1 || cells[0] != "{test}" {
		t.Errorf("convertToStringer returned unexpected cells: %v", cells)
	}

	// Test cache hit
	cells, err = table.convertToStringer(input)
	if err != nil {
		t.Errorf("convertToStringer failed on cache hit: %v", err)
	}
	if len(cells) != 1 || cells[0] != "{test}" {
		t.Errorf("convertToStringer returned unexpected cells on cache hit: %v", cells)
	}

	// Test disabled cache
	table.stringerCacheEnabled = false
	cells, err = table.convertToStringer(input)
	if err != nil {
		t.Errorf("convertToStringer failed without cache: %v", err)
	}
	if len(cells) != 1 || cells[0] != "{test}" {
		t.Errorf("convertToStringer returned unexpected cells without cache: %v", cells)
	}
}

// TestCallStringerConcurrent tests the convertToStringer function under concurrent access.
// It verifies thread-safety of the stringer cache with multiple goroutines.
func TestCallStringerConcurrent(t *testing.T) {
	table := &Table{
		logger:               ll.New("test"),
		stringer:             func(s interface{}) []string { return []string{fmt.Sprintf("%v", s)} },
		stringerCacheEnabled: true,
		stringerCache:        map[reflect.Type]reflect.Value{},
	}
	var wg sync.WaitGroup
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			input := struct{ ID int }{ID: i}
			cells, err := table.convertToStringer(input)
			if err != nil {
				t.Errorf("convertToStringer failed for ID %d: %v", i, err)
			}
			expected := fmt.Sprintf("{%d}", i)
			if len(cells) != 1 || cells[0] != expected {
				t.Errorf("convertToStringer returned unexpected cells for ID %d: got %v, want [%s]", i, cells, expected)
			}
		}(i)
	}
	wg.Wait()
}

// TestCallbacks tests the CellCallbacks functionality with the hybrid configuration approach.
// It verifies callbacks are triggered during rendering using WithConfig, Configure, and ConfigBuilder.
func TestCallbacks(t *testing.T) {
	tests := []struct {
		name         string
		setup        func(*Table) // How to configure the table
		expectedGlob int          // Expected global callback count
		expectedCol0 int          // Expected column 0 callback count
	}{
		// Test case: Using WithConfig Option
		{
			name: "WithConfig",
			setup: func(t *Table) {
				t.Header([]string{"Name", "Email", "Age"})
				t.Append([]string{"Alice", "alice@example.com", "25"})
			},
			expectedGlob: 1, // One header line
			expectedCol0: 1, // One callback for column 0
		},
		// Test case: Using Configure method
		{
			name: "Configure",
			setup: func(t *Table) {
				t.Configure(func(cfg *Config) {
					cfg.Header.Callbacks = tw.CellCallbacks{
						Global: t.config.Header.Callbacks.Global, // Preserve from base
						PerColumn: []func(){
							t.config.Header.Callbacks.PerColumn[0], // Preserve column 0
							nil,
							nil,
						},
					}
				})
				t.Header([]string{"Name", "Email", "Age"})
				t.Append([]string{"Bob", "bob@example.com", "30"})
			},
			expectedGlob: 1,
			expectedCol0: 1,
		},
		// Test case: Using ConfigBuilder
		{
			name: "ConfigBuilder",
			setup: func(t *Table) {
				config := NewConfigBuilder().
					Header().
					Build().
					Build()
				t.config = mergeConfig(t.config, config) // Apply builder config
				t.Header([]string{"Name", "Email", "Age"})
				t.Append([]string{"Charlie", "charlie@example.com", "35"})
			},
			expectedGlob: 1,
			expectedCol0: 1,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var buf bytes.Buffer
			globalCount := 0
			col0Count := 0

			// Base configuration with callbacks
			baseConfig := Config{
				Header: tw.CellConfig{
					Callbacks: tw.CellCallbacks{
						Global: func() { globalCount++ },
						PerColumn: []func(){
							func() { col0Count++ }, // Callback for column 0
							nil,
							nil,
						},
					},
				},
			}

			// Create table with base config
			table := NewTable(&buf, WithConfig(baseConfig))

			// Apply test-specific setup
			tt.setup(table)

			// Render to trigger callbacks
			table.Render()

			// Verify callback counts
			if globalCount != tt.expectedGlob {
				t.Errorf("%s: Expected global callback to run %d time(s), got %d", tt.name, tt.expectedGlob, globalCount)
			}
			if col0Count != tt.expectedCol0 {
				t.Errorf("%s: Expected column 0 callback to run %d time(s), got %d", tt.name, tt.expectedCol0, col0Count)
			}
		})
	}
}

// TestConvertToString tests the convertToString function with various input types.
// It verifies correct string conversion for nil, strings, SQL null types, and errors.
func TestConvertToString(t *testing.T) {
	table := &Table{logger: ll.New("test")}
	tests := []struct {
		input    interface{}
		expected string
	}{
		{nil, ""},
		{"test", "test"},
		{[]byte("bytes"), "bytes"},
		{fmt.Errorf("err"), "err"},
		{sql.NullString{String: "valid", Valid: true}, "valid"},
		{sql.NullString{Valid: false}, ""},
		// Add more cases
	}
	for _, tt := range tests {
		result := table.convertToString(tt.input)
		if tt.expected != result {
			t.Errorf("ConvertToString: expected %v, got '%v'", tt.expected, result)
		}
	}
}

// TestEnsureStreamWidthsCalculated tests the ensureStreamWidthsCalculated function.
// It verifies stream width initialization in streaming mode with various inputs.
func TestEnsureStreamWidthsCalculated(t *testing.T) {
	table := NewTable(os.Stdout, WithStreaming(tw.StreamConfig{Enable: true}))
	sampleData := []string{"A", "B"}
	config := tw.CellConfig{}
	t.Run("Already Initialized", func(t *testing.T) {
		table.streamWidths = tw.NewMapper[int, int]().Set(0, 5).Set(1, 5)
		table.streamNumCols = 2
		err := table.ensureStreamWidthsCalculated(sampleData, config)
		if err != nil {
			t.Errorf("Expected nil, got %v", err)
		}
	})
	t.Run("Initialize New", func(t *testing.T) {
		table.streamWidths = nil
		table.streamNumCols = 0
		err := table.ensureStreamWidthsCalculated(sampleData, config)
		if err != nil {
			t.Errorf("Expected nil, got %v", err)
		}
		if table.streamNumCols != 2 {
			t.Errorf("Expected streamNumCols=2, got %d", table.streamNumCols)
		}
		if table.streamWidths.Len() < 2 {
			t.Errorf("Expected at least 2 widths, got %d", table.streamWidths.Len())
		}
	})
	t.Run("Zero Columns", func(t *testing.T) {
		table.streamWidths = nil
		table.streamNumCols = 0
		err := table.ensureStreamWidthsCalculated([]string{}, config)
		if err == nil || err.Error() != "failed to determine column count for streaming" {
			t.Errorf("Expected error for zero columns, got %v", err)
		}
	})
}

// Helper to get a fresh default CellConfig for a section.
// This uses the defaultConfig() function from the tablewriter package.
func getTestSectionDefaultConfig(section string) tw.CellConfig {
	fullDefaultCfg := defaultConfig()
	switch section {
	case "header":
		return fullDefaultCfg.Header
	case "row":
		return fullDefaultCfg.Row
	case "footer":
		return fullDefaultCfg.Footer
	default:
		return fullDefaultCfg.Row
	}
}

// TestMergeCellConfig comprehensively tests the mergeCellConfig function.
func TestMergeCellConfig(t *testing.T) {
	tests := []struct {
		name           string
		baseConfig     func() tw.CellConfig
		inputConfig    tw.CellConfig
		expectedConfig func() tw.CellConfig
	}{
		{
			name:        "EmptyInput_OnRowDefaultBase",
			baseConfig:  func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				// src.AutoFormat is 0 (Pending) from empty CellConfig.
				// mergeCellConfig assigns src.AutoFormat to dst.AutoFormat.
				cfg.Formatting.AutoFormat = tw.Pending // Should be 0
				return cfg
			},
		},
		{
			name:       "OverrideMergeModeNone_OnRowDefaultBase",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{MergeMode: tw.MergeNone},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.MergeMode = tw.MergeNone
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "OverrideMergeModeVertical_OnRowDefaultBase",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{MergeMode: tw.MergeVertical},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.MergeMode = tw.MergeVertical
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "OverrideMergeModeHorizontal_OnRowDefaultBase",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{MergeMode: tw.MergeHorizontal},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.MergeMode = tw.MergeHorizontal
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "OverrideMergeModeBoth_OnRowDefaultBase",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{MergeMode: tw.MergeBoth},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.MergeMode = tw.MergeBoth
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "OverrideMergeModeHierarchical_OnRowDefaultBase",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{MergeMode: tw.MergeHierarchical},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.MergeMode = tw.MergeHierarchical
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "ConfigBuilderOutput_MergingIntoRowDefault",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") }, // Base AutoFormat = tw.Off (-1)
			inputConfig: NewConfigBuilder().
				WithHeaderAlignment(tw.AlignCenter).
				WithHeaderMergeMode(tw.MergeHorizontal).
				Build().Header, // Src AutoFormat = tw.On (1) from defaultConfig().Header
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Alignment.Global = tw.AlignCenter
				cfg.Formatting.AutoWrap = tw.WrapTruncate // from defaultConfig().Header
				cfg.Formatting.AutoFormat = tw.On         // from src (Builder's Header)
				cfg.Formatting.MergeMode = tw.MergeHorizontal
				// Padding.Global is same in default row and header, so it's effectively overwritten by itself.
				return cfg
			},
		},
		{
			name:        "Row_EmptyInput_OnRowBase",
			baseConfig:  func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "Row_OverrideAlignment_OnRowBase",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{Alignment: tw.AlignCenter},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.Alignment = tw.AlignCenter
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "Row_OverrideColumnAligns_OnRowBase_WithSkip",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				ColumnAligns: []tw.Align{tw.AlignRight, tw.Skip, tw.AlignLeft},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.ColumnAligns = []tw.Align{tw.AlignRight, tw.Empty, tw.AlignLeft}
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "Row_OverrideAutoWrap_OnRowBase",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{AutoWrap: tw.WrapTruncate},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.AutoWrap = tw.WrapTruncate
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "Row_OverrideAutoFormat_InputOff_BaseOff",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") }, // Base AutoFormat = tw.Off (-1)
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{AutoFormat: tw.Off}, // Src AutoFormat = tw.Off (-1)
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.AutoFormat = tw.Off // Expected -1
				return cfg
			},
		},
		{
			name:       "Row_OverrideAutoFormat_InputOn_BaseOff",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") }, // Base AutoFormat = tw.Off (-1)
			inputConfig: tw.CellConfig{
				Formatting: tw.CellFormatting{AutoFormat: tw.On}, // Src AutoFormat = tw.On (1)
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Formatting.AutoFormat = tw.On // Expected 1
				return cfg
			},
		},
		{
			name:       "Header_ConfigBuilderOutput_MergingIntoHeaderDefault",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("header") }, // Base AutoFormat = tw.On (1)
			inputConfig: NewConfigBuilder().
				WithHeaderAlignment(tw.AlignCenter).
				WithHeaderMergeMode(tw.MergeHorizontal).
				Build().Header, // Src AutoFormat = tw.On (1)
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("header")
				cfg.Formatting.MergeMode = tw.MergeHorizontal
				cfg.Formatting.AutoFormat = tw.On
				return cfg
			},
		},
		{
			name:       "OverrideColMaxWidthGlobal_OnRowDefault",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				ColMaxWidths: tw.CellWidth{Global: 50},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.ColMaxWidths.Global = 50
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "MergeColMaxWidthPerColumn_NewEntries_OnRowDefault",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				ColMaxWidths: tw.CellWidth{PerColumn: map[int]int{0: 10, 2: 20}},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.ColMaxWidths.PerColumn = map[int]int{0: 10, 2: 20}
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name: "MergeColMaxWidthPerColumn_OverwriteAndAdd_OnExisting",
			baseConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.ColMaxWidths.PerColumn = map[int]int{0: 5, 1: 15}
				return cfg
			},
			inputConfig: tw.CellConfig{
				ColMaxWidths: tw.CellWidth{PerColumn: map[int]int{0: 10, 2: 20, 1: 0}},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.ColMaxWidths.PerColumn = map[int]int{0: 10, 1: 15, 2: 20}
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name:       "MergePaddingPerColumn_NewEntries_OnRowDefault",
			baseConfig: func() tw.CellConfig { return getTestSectionDefaultConfig("row") },
			inputConfig: tw.CellConfig{
				Padding: tw.CellPadding{PerColumn: []tw.Padding{
					{Left: "L0", Right: "R0"},
					{},
					{Left: "L2", Right: "R2"},
				}},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Padding.PerColumn = []tw.Padding{
					{Left: "L0", Right: "R0"},
					{},
					{Left: "L2", Right: "R2"},
				}
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name: "MergePaddingPerColumn_OverwriteExtendAndPreserve_OnExisting",
			baseConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Padding.PerColumn = []tw.Padding{
					{Left: "BASE_L0"}, {Left: "BASE_L1"}, {Left: "BASE_L2"},
				}
				return cfg
			},
			inputConfig: tw.CellConfig{
				Padding: tw.CellPadding{PerColumn: []tw.Padding{
					{Left: "SRC_L0"},
					{},
				}},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.Padding.PerColumn = []tw.Padding{
					{Left: "SRC_L0"}, {Left: "BASE_L1"}, {Left: "BASE_L2"},
				}
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
		{
			name: "MergeColumnAligns_SrcShorterThanDst_WithSkipAndEmpty",
			baseConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.ColumnAligns = []tw.Align{tw.AlignCenter, tw.AlignRight, tw.AlignCenter}
				return cfg
			},
			inputConfig: tw.CellConfig{
				ColumnAligns: []tw.Align{tw.AlignLeft, tw.Skip},
			},
			expectedConfig: func() tw.CellConfig {
				cfg := getTestSectionDefaultConfig("row")
				cfg.ColumnAligns = []tw.Align{tw.AlignLeft, tw.AlignRight, tw.AlignCenter}
				cfg.Formatting.AutoFormat = tw.Pending // src.AutoFormat is 0
				return cfg
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			baseForTest := tt.baseConfig()
			expected := tt.expectedConfig()

			got := mergeCellConfig(baseForTest, tt.inputConfig)

			// Use a more verbose comparison for Formatting if DeepEqual fails to pinpoint AutoFormat
			if !reflect.DeepEqual(got.Formatting, expected.Formatting) {
				if got.Formatting.Alignment != expected.Formatting.Alignment ||
					got.Formatting.AutoWrap != expected.Formatting.AutoWrap ||
					got.Formatting.MergeMode != expected.Formatting.MergeMode ||
					got.Formatting.AutoFormat != expected.Formatting.AutoFormat { // Key comparison
					t.Errorf("Formatting mismatch:\nexpected: Alignment:%v, AutoWrap:%d, MergeMode:%d, AutoFormat:%d (%s)\ngot:      Alignment:%v, AutoWrap:%d, MergeMode:%d, AutoFormat:%d (%s)",
						expected.Formatting.Alignment, expected.Formatting.AutoWrap, expected.Formatting.MergeMode, expected.Formatting.AutoFormat, expected.Formatting.AutoFormat.String(),
						got.Formatting.Alignment, got.Formatting.AutoWrap, got.Formatting.MergeMode, got.Formatting.AutoFormat, got.Formatting.AutoFormat.String())
				} else {
					// Fallback if the above doesn't catch it (shouldn't happen for simple fields)
					t.Errorf("Formatting mismatch (DeepEqual failed):\nexpected: %+v\ngot:      %+v", expected.Formatting, got.Formatting)
				}
			}

			if !reflect.DeepEqual(got.Padding.Global, expected.Padding.Global) {
				t.Errorf("Padding.Global mismatch\nexpected: %+v\ngot:      %+v", expected.Padding.Global, got.Padding.Global)
			}
			if !reflect.DeepEqual(got.Padding.PerColumn, expected.Padding.PerColumn) {
				t.Errorf("Padding.PerColumn mismatch\nexpected: %#v\ngot:      %#v", expected.Padding.PerColumn, got.Padding.PerColumn)
			}
			if !reflect.DeepEqual(got.ColMaxWidths.Global, expected.ColMaxWidths.Global) {
				t.Errorf("ColMaxWidths.Global mismatch\nexpected: %d\ngot:      %d", expected.ColMaxWidths.Global, got.ColMaxWidths.Global)
			}
			if !reflect.DeepEqual(got.ColMaxWidths.PerColumn, expected.ColMaxWidths.PerColumn) {
				t.Errorf("ColMaxWidths.PerColumn mismatch\nexpected: %#v\ngot:      %#v", expected.ColMaxWidths.PerColumn, got.ColMaxWidths.PerColumn)
			}
			if !reflect.DeepEqual(got.ColumnAligns, expected.ColumnAligns) {
				t.Errorf("ColumnAligns mismatch\nexpected: %#v\ngot:      %#v", expected.ColumnAligns, got.ColumnAligns)
			}

			if (got.Callbacks.Global == nil) != (expected.Callbacks.Global == nil) {
				t.Errorf("Callbacks.Global nilness mismatch\nexpected nil: %t, got nil: %t",
					expected.Callbacks.Global == nil, got.Callbacks.Global == nil)
			}
			if len(got.Callbacks.PerColumn) != len(expected.Callbacks.PerColumn) {
				t.Errorf("Callbacks.PerColumn length mismatch\nexpected: %d, got: %d",
					len(expected.Callbacks.PerColumn), len(got.Callbacks.PerColumn))
			} else {
				for i := range got.Callbacks.PerColumn {
					if (got.Callbacks.PerColumn[i] == nil) != (expected.Callbacks.PerColumn[i] == nil) {
						t.Errorf("Callbacks.PerColumn[%d] nilness mismatch\nexpected nil: %t, got nil: %t",
							i, expected.Callbacks.PerColumn[i] == nil, got.Callbacks.PerColumn[i] == nil)
						break
					}
				}
			}
			if (got.Filter.Global == nil) != (expected.Filter.Global == nil) {
				t.Errorf("Filter.Global nilness mismatch\nexpected nil: %t, got nil: %t",
					expected.Filter.Global == nil, got.Filter.Global == nil)
			}
			if len(got.Filter.PerColumn) != len(expected.Filter.PerColumn) {
				t.Errorf("Filter.PerColumn length mismatch\nexpected: %d, got: %d",
					len(expected.Filter.PerColumn), len(got.Filter.PerColumn))
			} else {
				for i := range got.Filter.PerColumn {
					if (got.Filter.PerColumn[i] == nil) != (expected.Filter.PerColumn[i] == nil) {
						t.Errorf("Filter.PerColumn[%d] nilness mismatch\nexpected nil: %t, got nil: %t",
							i, expected.Filter.PerColumn[i] == nil, got.Filter.PerColumn[i] == nil)
						break
					}
				}
			}
		})
	}
}