diff options
| author | Tomas Aschan <[email protected]> | 2025-08-29 21:59:56 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-29 21:59:56 +0200 |
| commit | fd649b20e66a30631ee35bba78164180d02730d4 (patch) | |
| tree | 321b27604470eed0b00615ea41f0f32f9bf8104a /errors_test.go | |
| parent | d66f0ce2a7f887f0ff5f5af48649f3a01b3f0566 (diff) | |
| parent | 1db553c27f5b9952dbb6484ed9999547b9e7dfb2 (diff) | |
Merge branch 'master' into fix-errhelp
Diffstat (limited to 'errors_test.go')
| -rw-r--r-- | errors_test.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/errors_test.go b/errors_test.go new file mode 100644 index 0000000..7b4c7a4 --- /dev/null +++ b/errors_test.go @@ -0,0 +1,67 @@ +package pflag + +import ( + "errors" + "testing" +) + +func TestNotExistError(t *testing.T) { + err := &NotExistError{ + name: "foo", + specifiedShorthands: "bar", + } + + if err.GetSpecifiedName() != "foo" { + t.Errorf("Expected GetSpecifiedName to return %q, got %q", "foo", err.GetSpecifiedName()) + } + if err.GetSpecifiedShortnames() != "bar" { + t.Errorf("Expected GetSpecifiedShortnames to return %q, got %q", "bar", err.GetSpecifiedShortnames()) + } +} + +func TestValueRequiredError(t *testing.T) { + err := &ValueRequiredError{ + flag: &Flag{}, + specifiedName: "foo", + specifiedShorthands: "bar", + } + + if err.GetFlag() == nil { + t.Error("Expected GetSpecifiedName to return its flag field, but got nil") + } + if err.GetSpecifiedName() != "foo" { + t.Errorf("Expected GetSpecifiedName to return %q, got %q", "foo", err.GetSpecifiedName()) + } + if err.GetSpecifiedShortnames() != "bar" { + t.Errorf("Expected GetSpecifiedShortnames to return %q, got %q", "bar", err.GetSpecifiedShortnames()) + } +} + +func TestInvalidValueError(t *testing.T) { + expectedCause := errors.New("error") + err := &InvalidValueError{ + flag: &Flag{}, + value: "foo", + cause: expectedCause, + } + + if err.GetFlag() == nil { + t.Error("Expected GetSpecifiedName to return its flag field, but got nil") + } + if err.GetValue() != "foo" { + t.Errorf("Expected GetValue to return %q, got %q", "foo", err.GetValue()) + } + if err.Unwrap() != expectedCause { + t.Errorf("Expected Unwrwap to return %q, got %q", expectedCause, err.Unwrap()) + } +} + +func TestInvalidSyntaxError(t *testing.T) { + err := &InvalidSyntaxError{ + specifiedFlag: "--=", + } + + if err.GetSpecifiedFlag() != "--=" { + t.Errorf("Expected GetSpecifiedFlag to return %q, got %q", "--=", err.GetSpecifiedFlag()) + } +} |
