aboutsummaryrefslogtreecommitdiff
path: root/errors_test.go
diff options
context:
space:
mode:
authorTomas Aschan <[email protected]>2025-04-22 17:47:47 +0200
committerGitHub <[email protected]>2025-04-22 17:47:47 +0200
commitd661846b4df0d6611ad95577ddfb240474d21b7c (patch)
tree319f3221c52fb10c2e870d50cdfdc531c22eb4e4 /errors_test.go
parent19c9c4072e41218b18b93dbfc3798c18835d2fd5 (diff)
parent6ca66b16cbe1b365ce9a6c56faf9b04acb8d8035 (diff)
Merge pull request #425 from eth-p/error-structs
feat: Use structs for errors returned by pflag.
Diffstat (limited to 'errors_test.go')
-rw-r--r--errors_test.go67
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())
+ }
+}