github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/parameters/flags_test.go (about)

     1  package parameters_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/lang/parameters"
     7  	"github.com/lmorg/murex/lang/types"
     8  	"github.com/lmorg/murex/test/count"
     9  	"github.com/lmorg/murex/utils/json"
    10  )
    11  
    12  type flagTest struct {
    13  	Parameters []string
    14  	Arguments  parameters.Arguments
    15  
    16  	ExpFlags      map[string]string
    17  	ExpAdditional []string
    18  
    19  	Error bool
    20  }
    21  
    22  func TestFlags(t *testing.T) {
    23  	tests := []flagTest{
    24  		{
    25  			Parameters: []string{"--string", "--test", "--number", "-5"},
    26  			Arguments: parameters.Arguments{
    27  				//AllowAdditional: true,
    28  				Flags: map[string]string{
    29  					"--string": types.String,
    30  					"--number": types.Number,
    31  				},
    32  			},
    33  			ExpFlags: map[string]string{
    34  				"--string": "--test",
    35  				"--number": "-5",
    36  			},
    37  			ExpAdditional: nil,
    38  			Error:         false,
    39  		},
    40  	}
    41  
    42  	count.Tests(t, len(tests))
    43  
    44  	for i, test := range tests {
    45  		if test.ExpFlags == nil {
    46  			test.ExpFlags = make(map[string]string)
    47  		}
    48  		if test.ExpAdditional == nil {
    49  			test.ExpAdditional = make([]string, 0)
    50  		}
    51  
    52  		flags, additional, err := parameters.ParseFlags(test.Parameters, &test.Arguments)
    53  
    54  		if flags == nil {
    55  			flags = make(map[string]string)
    56  		}
    57  		if additional == nil {
    58  			additional = make([]string, 0)
    59  		}
    60  
    61  		var failed bool
    62  
    63  		if (err != nil) != test.Error {
    64  			t.Errorf("Unexpected error in test %d:", i)
    65  			failed = true
    66  		}
    67  
    68  		if json.LazyLogging(test.ExpFlags) != json.LazyLogging(flags) {
    69  			t.Errorf("Flags doesn't match expected in test %d:", i)
    70  			failed = true
    71  		}
    72  
    73  		if json.LazyLogging(test.ExpAdditional) != json.LazyLogging(additional) {
    74  			t.Errorf("Additional doesn't match expected in test %d:", i)
    75  			failed = true
    76  		}
    77  
    78  		if failed {
    79  			t.Logf("  Parameters: %s", json.LazyLogging(test.Parameters))
    80  			t.Logf("  Arguments:  %s", json.LazyLogging(test.Arguments))
    81  			t.Logf("  exp flags:  %s", json.LazyLogging(test.ExpFlags))
    82  			t.Logf("  act flags:  %s", json.LazyLogging(flags))
    83  			t.Logf("  exp aditnl: %s", json.LazyLogging(test.ExpAdditional))
    84  			t.Logf("  act aditnl: %s", json.LazyLogging(additional))
    85  			t.Logf("  exp error:  %v", test.Error)
    86  			t.Logf("  act error:  %v", err)
    87  		}
    88  	}
    89  }