diff options
| author | copilot-swe-agent[bot] <[email protected]> | 2025-11-14 08:36:12 +0000 |
|---|---|---|
| committer | copilot-swe-agent[bot] <[email protected]> | 2025-11-14 08:36:12 +0000 |
| commit | ea4f75a68465992c68779fc0cc4ea3ef251af05e (patch) | |
| tree | fa8104a0377e3f7810953301803bfc0bfcba35be /lib/converter_test.go | |
| parent | be5f580e8bfb9169bb8b410d1e154057e9ac1ed5 (diff) | |
Add comprehensive unit tests for lib package with 90.3% coveragecopilot/add-unit-tests-for-lib-package
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'lib/converter_test.go')
| -rw-r--r-- | lib/converter_test.go | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/lib/converter_test.go b/lib/converter_test.go new file mode 100644 index 00000000..9910c211 --- /dev/null +++ b/lib/converter_test.go @@ -0,0 +1,177 @@ +package lib + +import ( + "strings" + "testing" +) + +func TestRegisterInputConverter(t *testing.T) { + // Save original state + originalMap := inputConverterMap + defer func() { inputConverterMap = originalMap }() + + // Reset map for testing + inputConverterMap = make(map[string]InputConverter) + + tests := []struct { + name string + converter string + wantErr bool + }{ + { + name: "register new converter", + converter: "test1", + wantErr: false, + }, + { + name: "register duplicate converter", + converter: "test1", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create a mock converter + mockConverter := &mockInputConverter{typ: tt.converter} + err := RegisterInputConverter(tt.converter, mockConverter) + if (err != nil) != tt.wantErr { + t.Errorf("RegisterInputConverter() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestRegisterOutputConverter(t *testing.T) { + // Save original state + originalMap := outputConverterMap + defer func() { outputConverterMap = originalMap }() + + // Reset map for testing + outputConverterMap = make(map[string]OutputConverter) + + tests := []struct { + name string + converter string + wantErr bool + }{ + { + name: "register new converter", + converter: "test1", + wantErr: false, + }, + { + name: "register duplicate converter", + converter: "test1", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create a mock converter + mockConverter := &mockOutputConverter{typ: tt.converter} + err := RegisterOutputConverter(tt.converter, mockConverter) + if (err != nil) != tt.wantErr { + t.Errorf("RegisterOutputConverter() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestListInputConverter(t *testing.T) { + // Save original state + originalMap := inputConverterMap + defer func() { inputConverterMap = originalMap }() + + // Reset and populate map + inputConverterMap = make(map[string]InputConverter) + inputConverterMap["test"] = &mockInputConverter{typ: "test"} + + // Just test that it doesn't panic + ListInputConverter() +} + +func TestListOutputConverter(t *testing.T) { + // Save original state + originalMap := outputConverterMap + defer func() { outputConverterMap = originalMap }() + + // Reset and populate map + outputConverterMap = make(map[string]OutputConverter) + outputConverterMap["test"] = &mockOutputConverter{typ: "test"} + + // Just test that it doesn't panic + ListOutputConverter() +} + +// Mock converters for testing +type mockInputConverter struct { + typ string +} + +func (m *mockInputConverter) GetType() string { + return m.typ +} + +func (m *mockInputConverter) GetAction() Action { + return ActionAdd +} + +func (m *mockInputConverter) GetDescription() string { + return "mock input converter" +} + +func (m *mockInputConverter) Input(c Container) (Container, error) { + return c, nil +} + +type mockOutputConverter struct { + typ string +} + +func (m *mockOutputConverter) GetType() string { + return m.typ +} + +func (m *mockOutputConverter) GetAction() Action { + return ActionOutput +} + +func (m *mockOutputConverter) GetDescription() string { + return "mock output converter" +} + +func (m *mockOutputConverter) Output(c Container) error { + return nil +} + +func TestRegisterConverterWithWhitespace(t *testing.T) { + // Save original state + originalMap := inputConverterMap + defer func() { inputConverterMap = originalMap }() + + // Reset map for testing + inputConverterMap = make(map[string]InputConverter) + + mockConverter := &mockInputConverter{typ: "test"} + err := RegisterInputConverter(" test ", mockConverter) + if err != nil { + t.Errorf("RegisterInputConverter() with whitespace should not error: %v", err) + } + + // Verify it was registered with trimmed name + if _, ok := inputConverterMap["test"]; !ok { + // Check without trim + found := false + for k := range inputConverterMap { + if strings.TrimSpace(k) == "test" { + found = true + break + } + } + if !found { + t.Error("Converter not registered properly with whitespace") + } + } +} |
