Add comprehensive unit tests for lib package#303
Conversation
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
| func TestContainerAddMergeIgnoreIPv4(t *testing.T) { | ||
| c := NewContainer() | ||
|
|
||
| // First add normally | ||
| entry1 := NewEntry("us") | ||
| if err := entry1.AddPrefix("1.0.0.0/24"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := c.Add(entry1); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Add with IgnoreIPv4 to existing entry | ||
| entry2 := NewEntry("us") | ||
| if err := entry2.AddPrefix("2.0.0.0/24"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := entry2.AddPrefix("2001:db8::/32"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := c.Add(entry2, IgnoreIPv4); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func TestContainerAddMergeIgnoreIPv6(t *testing.T) { | ||
| c := NewContainer() | ||
|
|
||
| // First add normally | ||
| entry1 := NewEntry("us") | ||
| if err := entry1.AddPrefix("2001:db8::/32"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := c.Add(entry1); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Add with IgnoreIPv6 to existing entry | ||
| entry2 := NewEntry("us") | ||
| if err := entry2.AddPrefix("1.0.0.0/24"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := entry2.AddPrefix("2001:db9::/32"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := c.Add(entry2, IgnoreIPv6); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } |
There was a problem hiding this comment.
@copilot These two tests miss checking resulting CIDR prefixes at the end, please fix them.
There was a problem hiding this comment.
Fixed in d0d5153. Both tests now verify the resulting CIDR prefixes — TestContainerAddMergeIgnoreIPv4 checks that the original IPv4 (1.0.0.0/24) is preserved and only IPv6 (2001:db8::/32) is merged, and TestContainerAddMergeIgnoreIPv6 checks that the original IPv6 (2001:db8::/32) is preserved and only IPv4 (1.0.0.0/24) is merged.
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…upIPv6ErrorEntryMissingIPSet and reorder Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
|
@copilot Test |
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
Fixed in 19c8c8d. All five tests now verify resulting CIDR prefixes:
Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
| func TestListInputConverter(t *testing.T) { | ||
| origMap := inputConverterMap | ||
| inputConverterMap = make(map[string]InputConverter) | ||
| defer func() { inputConverterMap = origMap }() | ||
|
|
||
| mock := &mockInputConverter{typeName: "test-ic", action: ActionAdd, description: "Test Input"} | ||
| RegisterInputConverter("test-ic", mock) | ||
|
|
||
| // Capture stdout | ||
| old := os.Stdout | ||
| r, w, _ := os.Pipe() | ||
| os.Stdout = w | ||
|
|
||
| ListInputConverter() | ||
|
|
||
| w.Close() | ||
| os.Stdout = old | ||
|
|
||
| var buf bytes.Buffer | ||
| buf.ReadFrom(r) | ||
| output := buf.String() | ||
|
|
||
| if len(output) == 0 { | ||
| t.Error("ListInputConverter should produce output") | ||
| } | ||
| } | ||
|
|
||
| func TestListOutputConverter(t *testing.T) { | ||
| origMap := outputConverterMap | ||
| outputConverterMap = make(map[string]OutputConverter) | ||
| defer func() { outputConverterMap = origMap }() | ||
|
|
||
| mock := &mockOutputConverter{typeName: "test-oc", action: ActionOutput, description: "Test Output"} | ||
| RegisterOutputConverter("test-oc", mock) | ||
|
|
||
| // Capture stdout | ||
| old := os.Stdout | ||
| r, w, _ := os.Pipe() | ||
| os.Stdout = w | ||
|
|
||
| ListOutputConverter() | ||
|
|
||
| w.Close() | ||
| os.Stdout = old | ||
|
|
||
| var buf bytes.Buffer | ||
| buf.ReadFrom(r) | ||
| output := buf.String() | ||
|
|
||
| if len(output) == 0 { | ||
| t.Error("ListOutputConverter should produce output") | ||
| } | ||
| } |
There was a problem hiding this comment.
Fixed in 16b530c. Both tests now verify the exact output string — TestListInputConverter checks for "All available input formats:\n - test-ic (Test Input)\n" and TestListOutputConverter checks for "All available output formats:\n - test-oc (Test Output)\n".
…putConverter Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
libpackage to understand functions/methods to testlib/lib_test.go- tests for constants, types, IgnoreIPv4/IgnoreIPv6 functionslib/error_test.go- tests for error variableslib/entry_test.go- tests for Entry (NewEntry, AddPrefix, RemovePrefix, processPrefix, MarshalPrefix, MarshalIPRange, MarshalText, buildIPSet, GetIPv4Set, GetIPv6Set)lib/container_test.go- tests for Container (NewContainer, GetEntry, Add, Remove, Loop, Len, Lookup)lib/common_test.go- tests for GetIgnoreIPType, GetRemoteURLContent, GetRemoteURLReader, WantedListExtended.UnmarshalJSONlib/config_test.go- tests for RegisterInputConfigCreator, RegisterOutputConfigCreator, createInputConfig, createOutputConfig, inputConvConfig.UnmarshalJSON, outputConvConfig.UnmarshalJSONlib/converter_test.go- tests for RegisterInputConverter, RegisterOutputConverter, ListInputConverter, ListOutputConverterlib/instance_test.go- tests for NewInstance, InitConfig, InitConfigFromBytes, AddInput, AddOutput, ResetInput, ResetOutput, RunInput, RunOutput, Run🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.