Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"testing"
)

func TestConvertCmd(t *testing.T) {
tests := []struct {
name string
test func(t *testing.T)
}{
{
name: "command configuration",
test: func(t *testing.T) {
if convertCmd.Use != "convert" {
t.Errorf("Expected Use to be 'convert', got '%s'", convertCmd.Use)
}

expectedAliases := []string{"conv"}
if len(convertCmd.Aliases) != len(expectedAliases) {
t.Errorf("Expected %d aliases, got %d", len(expectedAliases), len(convertCmd.Aliases))
}

for i, alias := range expectedAliases {
if i >= len(convertCmd.Aliases) || convertCmd.Aliases[i] != alias {
t.Errorf("Expected alias '%s' at index %d", alias, i)
}
}

expectedShort := "Convert geoip data from one format to another by using config file"
if convertCmd.Short != expectedShort {
t.Errorf("Expected Short to be '%s', got '%s'", expectedShort, convertCmd.Short)
}
},
},
{
name: "command flags",
test: func(t *testing.T) {
// Check config flag exists as persistent flag
configFlag := convertCmd.PersistentFlags().Lookup("config")
if configFlag == nil {
t.Error("Expected config flag to exist")
}

// Check that the flag has correct shorthand
if configFlag.Shorthand != "c" {
t.Errorf("Expected shorthand 'c', got '%s'", configFlag.Shorthand)
}

// Check default value
expectedDefault := "config.json"
if configFlag.DefValue != expectedDefault {
t.Errorf("Expected default value '%s', got '%s'", expectedDefault, configFlag.DefValue)
}
},
},
{
name: "command has run function",
test: func(t *testing.T) {
if convertCmd.Run == nil {
t.Error("Expected convert command to have a Run function")
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, tt.test)
}
}

func TestConvertCmdExecution(t *testing.T) {
// Test that the convert command structure is correct
// We can't easily test the full execution without mocking the lib package and file system
// But we can test that the command is properly configured

// Test that the command is added to root command
found := false
for _, cmd := range rootCmd.Commands() {
if cmd.Use == "convert" {
found = true
break
}
}

if !found {
t.Error("Convert command should be added to root command")
}
}

func TestConvertInit(t *testing.T) {
// Test that the init function properly sets up the command
// The init function should have:
// 1. Added the command to rootCmd
// 2. Set up the config flag

// Check that convertCmd is not nil (should be initialized)
if convertCmd == nil {
t.Error("convertCmd should be initialized")
}

// Check that the command has the config flag with persistent flag
configFlag := convertCmd.PersistentFlags().Lookup("config")
if configFlag == nil {
t.Error("Expected config flag to be a persistent flag")
}
}
103 changes: 103 additions & 0 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"testing"
)

func TestInitImports(t *testing.T) {
// The init.go file contains only side-effect imports
// We can't directly test import statements, but we can test that the imports
// had their intended effect by checking if the plugins are registered

// Since the imports are side-effect imports (with _), they should have
// executed their init() functions which register the plugins

// We can't directly test this without access to the plugin registry,
// but we can test that the file structure is correct and doesn't cause compilation errors

// The mere fact that this test compiles and runs means the imports are valid
t.Log("All plugin imports are valid and don't cause compilation errors")

// Test that this is a valid Go package
// If the imports were invalid, the package wouldn't compile

// List of expected plugins that should be imported
expectedPlugins := []string{
"maxmind",
"mihomo",
"plaintext",
"singbox",
"special",
"v2ray",
}

// We can't directly verify the imports without reflection or package introspection
// But we can log what we expect to be imported
for _, plugin := range expectedPlugins {
t.Logf("Expected plugin import: github.com/Loyalsoldier/geoip/plugin/%s", plugin)
}
}

func TestPackageStructure(t *testing.T) {
// Test that the main package structure is correct
// This test verifies that all the expected components exist

// Check that key variables are defined
if rootCmd == nil {
t.Error("rootCmd should be defined")
}

if convertCmd == nil {
t.Error("convertCmd should be defined")
}

if listCmd == nil {
t.Error("listCmd should be defined")
}

if lookupCmd == nil {
t.Error("lookupCmd should be defined")
}

if mergeCmd == nil {
t.Error("mergeCmd should be defined")
}

// Check that supportedInputFormats is defined and not empty
if supportedInputFormats == nil {
t.Error("supportedInputFormats should be defined")
}

if len(supportedInputFormats) == 0 {
t.Error("supportedInputFormats should not be empty")
}
}

func TestMainPackageInit(t *testing.T) {
// Test that the init functions have been called and set up the commands correctly

// All commands should be added to the root command
commands := rootCmd.Commands()
if len(commands) == 0 {
t.Error("Root command should have subcommands after init")
}

expectedCommands := map[string]bool{
"convert": false,
"list": false,
"lookup": false,
"merge": false,
}

for _, cmd := range commands {
if _, exists := expectedCommands[cmd.Use]; exists {
expectedCommands[cmd.Use] = true
}
}

for cmdName, found := range expectedCommands {
if !found {
t.Errorf("Expected command '%s' not found in root command", cmdName)
}
}
}
110 changes: 110 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package main

import (
"testing"
)

func TestListCmd(t *testing.T) {
tests := []struct {
name string
test func(t *testing.T)
}{
{
name: "command configuration",
test: func(t *testing.T) {
if listCmd.Use != "list" {
t.Errorf("Expected Use to be 'list', got '%s'", listCmd.Use)
}

expectedAliases := []string{"l", "ls"}
if len(listCmd.Aliases) != len(expectedAliases) {
t.Errorf("Expected %d aliases, got %d", len(expectedAliases), len(listCmd.Aliases))
}

for i, alias := range expectedAliases {
if i >= len(listCmd.Aliases) || listCmd.Aliases[i] != alias {
t.Errorf("Expected alias '%s' at index %d", alias, i)
}
}

expectedShort := "List all available input and output formats"
if listCmd.Short != expectedShort {
t.Errorf("Expected Short to be '%s', got '%s'", expectedShort, listCmd.Short)
}
},
},
{
name: "command has run function",
test: func(t *testing.T) {
if listCmd.Run == nil {
t.Error("Expected list command to have a Run function")
}
},
},
{
name: "command is added to root",
test: func(t *testing.T) {
// Test that the command is added to root command
found := false
for _, cmd := range rootCmd.Commands() {
if cmd.Use == "list" {
found = true
break
}
}

if !found {
t.Error("List command should be added to root command")
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, tt.test)
}
}

func TestListCmdExecution(t *testing.T) {
// Test that the list command can be executed without panicking
// We can't easily test the actual output without mocking the lib package
// But we can test that the Run function exists and is callable

if listCmd.Run == nil {
t.Error("Expected list command to have a Run function")
return
}

// The Run function calls lib.ListInputConverter() and lib.ListOutputConverter()
// We can't test the actual execution without mocking these functions or causing side effects
// But we can verify the command structure is correct

// Test that the command accepts no arguments (which is the default behavior)
// and doesn't have any required flags
if listCmd.Args != nil {
// If Args is set, it should allow any number of arguments or be nil for default behavior
// For the list command, it should accept no arguments, so Args being nil is correct
t.Logf("List command Args function is set: %v", listCmd.Args)
}
}

func TestListInit(t *testing.T) {
// Test that the init function properly sets up the command

// Check that listCmd is not nil (should be initialized)
if listCmd == nil {
t.Error("listCmd should be initialized")
}

// Check that the command has no flags (it shouldn't need any)
flags := listCmd.Flags()
if flags.NFlag() > 0 {
t.Errorf("Expected list command to have no flags, but it has %d", flags.NFlag())
}

// Check that the command has no persistent flags
persistentFlags := listCmd.PersistentFlags()
if persistentFlags.NFlag() > 0 {
t.Errorf("Expected list command to have no persistent flags, but it has %d", persistentFlags.NFlag())
}
}
Loading