Skip to content

Commit

Permalink
🚑 ☔ Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Feb 20, 2025
1 parent 2bb61e9 commit 5dd8f6c
Show file tree
Hide file tree
Showing 8 changed files with 630 additions and 50 deletions.
19 changes: 7 additions & 12 deletions pkg/cmds/helpers/test-helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,13 @@ func NewTestParsedLayers(pls *layers.ParameterLayers, ls ...TestParsedLayer) *la

// compareValues handles comparison of values with special cases for slice types
func compareValues(t *testing.T, expected, actual interface{}, key string) {
// Check if we're dealing with a string slice and interface slice
if actualStrSlice, ok := actual.([]string); ok {
if expectedInterfaceSlice, ok := expected.([]interface{}); ok {
// Try to convert the expected interface slice to string slice
expectedStrSlice, err := cast.CastListToStringList(expectedInterfaceSlice)
require.NoError(t, err)
assert.Equal(t, expectedStrSlice, actualStrSlice, "mismatch for key %s", key)
return
}
}
// Default comparison for other types
assert.Equal(t, expected, actual, "mismatch for key %s", key)
normalizedExpected, err := cast.NormalizeValue(expected)
require.NoError(t, err, "failed to normalize expected value for key %s", key)

normalizedActual, err := cast.NormalizeValue(actual)
require.NoError(t, err, "failed to normalize actual value for key %s", key)

assert.Equal(t, normalizedExpected, normalizedActual, "mismatch for key %s", key)
}

func TestExpectedOutputs(t *testing.T, expectedLayers []TestExpectedLayer, parsedLayers *layers.ParsedLayers) {
Expand Down
40 changes: 30 additions & 10 deletions pkg/cmds/parameters/gather-arguments_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package parameters

import (
"testing"

"github.com/go-go-golems/glazed/pkg/helpers/cast"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

// Test no arguments are passed to the function
Expand Down Expand Up @@ -378,12 +379,12 @@ func TestObjectListFromFileParsing(t *testing.T) {
assert.NoError(t, err)
v1, present := result.Get("arg1")
assert.True(t, present)
assert.Equal(t, []interface{}{
map[string]interface{}{
assert.Equal(t, []map[string]interface{}{
{
"name": "objectList1",
"type": "object",
},
map[string]interface{}{
{
"name": "objectList2",
"type": "object",
},
Expand All @@ -406,12 +407,31 @@ func TestObjectListFromFilesParsing(t *testing.T) {
v1, present := result.Get("arg1")
assert.True(t, present)
assert.Equal(t,
[]interface{}{map[string]interface{}{"name": "objectList1", "type": "object"},
map[string]interface{}{"name": "objectList2", "type": "object"},
map[string]interface{}{"name": "objectList3", "type": "object"},
map[string]interface{}{"name": "objectList4", "type": "object"},
map[string]interface{}{"name": "objectList5", "type": "object"},
map[string]interface{}{"name": "objectList6", "type": "object"},
[]map[string]interface{}{
{
"name": "objectList1",
"type": "object",
},
{
"name": "objectList2",
"type": "object",
},
{
"name": "objectList3",
"type": "object",
},
{
"name": "objectList4",
"type": "object",
},
{
"name": "objectList5",
"type": "object",
},
{
"name": "objectList6",
"type": "object",
},
}, v1.Value)
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/cmds/parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ func (p *ParameterDefinition) SetValueFromInterface(value reflect.Value, v inter
func (pds *ParameterDefinitions) ParsedParametersFromDefaults() (*ParsedParameters, error) {
ret := NewParsedParameters()
err := pds.ForEachE(func(definition *ParameterDefinition) error {
err := ret.UpdateValue(definition.Name, definition, definition.Default,
if definition.Default == nil {
return nil
}
err := ret.UpdateValue(definition.Name, definition, *definition.Default,
WithParseStepSource("defaults"),
WithParseStepValue(definition.Default),
)
Expand Down Expand Up @@ -442,9 +445,9 @@ func (p *ParameterDefinition) CheckValueValidity(v interface{}) (interface{}, er
case ParameterTypeObjectListFromFile:
fallthrough
case ParameterTypeObjectListFromFiles:
l, ok := v.([]interface{})
l, ok := cast.CastList2[map[string]interface{}, interface{}](v)
if !ok {
return nil, errors.Errorf("Value for parameter %s is not a list of objects: %v", p.Name, v)
return nil, errors.Errorf("Value for parameter %s (type %T) is not a list of objects: %v", p.Name, v, v)
}
return l, nil

Expand Down
Loading

0 comments on commit 5dd8f6c

Please sign in to comment.