Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config/supermicro.go: Add tests and resolve nested menu bug in FindOrCreateSetting #19

Merged
merged 1 commit into from
Aug 6, 2024
Merged
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
3 changes: 1 addition & 2 deletions config/supermicro.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ func (cm *supermicroVendorConfig) FindOrCreateSetting(path []string, value strin
return (*currentMenus)[0].Settings[len((*currentMenus)[0].Settings)-1]
} else {
// Intermediate part, find or create the menu
currentMenu := cm.FindOrCreateMenu(currentMenus, part)
currentMenus = &currentMenu.Menus
_ = cm.FindOrCreateMenu(currentMenus, part)
}
}

Expand Down
87 changes: 87 additions & 0 deletions config/supermicro_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package config

import (
"testing"
)

func TestSupermicroVendorConfig_FindOrCreateMenu(t *testing.T) {
// Create a new instance of supermicroVendorConfig
cm := &supermicroVendorConfig{}

// Create a slice of menus
menus := []*supermicroBiosCfgMenu{
{Name: "Menu1"},
{Name: "Menu2", Menus: []*supermicroBiosCfgMenu{{Name: "SubMenu1"}}},
}

// Call the FindOrCreateMenu function
menu := cm.FindOrCreateMenu(&menus, "Menu3")

// Check if the menu was created and added to the slice
if len(menus) != 3 {
t.Errorf("Expected 3 menus, got: %d", len(menus))
}

// Check the returned menu is the same as the last menu in the slice
if menu != menus[2] {
t.Errorf("Expected menu: %v, got: %v", menus[2], menu)
}

// Call the FindOrCreateMenu function with an existing menu name
existingMenu := cm.FindOrCreateMenu(&menus, "Menu1")

// Check if the existing menu was found and returned
if existingMenu != menus[0] {
t.Errorf("Expected menu: %v, got: %v", menus[0], existingMenu)
}

// Call the FindOrCreateMenu function with an existing submenu name
existingSubMenu := cm.FindOrCreateMenu(&(menus[1].Menus), "SubMenu1")

if existingSubMenu != menus[1].Menus[0] {
t.Errorf("Expected menu: %v, got: %v", menus[1].Menus[0], existingSubMenu)
}
}

func TestSupermicroVendorConfig_FindOrCreateSetting(t *testing.T) {
// Create a new instance of supermicroVendorConfig
cm := &supermicroVendorConfig{
ConfigData: &supermicroConfig{
BiosCfg: &supermicroBiosCfg{
Menus: []*supermicroBiosCfgMenu{
{
Name: "Menu1",
Settings: []*supermicroBiosCfgSetting{
{Name: "Setting1", SelectedOption: "Option1"},
},
},
},
},
},
}

// Define the path and value for the setting
path := []string{"Menu1", "Setting2"}
value := "Option2"

// Call the FindOrCreateSetting function
setting := cm.FindOrCreateSetting(path, value)

// Check if the setting was created and added to the menu
if len(cm.ConfigData.BiosCfg.Menus[0].Settings) != 2 {
t.Errorf("Expected 2 settings, got: %d", len(cm.ConfigData.BiosCfg.Menus[0].Settings))
}

// Check the returned setting is the same as the last setting in the menu
if setting != cm.ConfigData.BiosCfg.Menus[0].Settings[1] {
t.Errorf("Expected setting: %v, got: %v", cm.ConfigData.BiosCfg.Menus[0].Settings[1], setting)
}

// Call the FindOrCreateSetting function with an existing setting name
existingSetting := cm.FindOrCreateSetting([]string{"Menu1", "Setting1"}, "Option1")

// Check if the existing setting was found and returned
if existingSetting != cm.ConfigData.BiosCfg.Menus[0].Settings[0] {
t.Errorf("Expected setting: %v, got: %v", cm.ConfigData.BiosCfg.Menus[0].Settings[0], existingSetting)
}
}
Loading