diff --git a/examples/custom_rule_test.go b/examples/custom_rule_test.go new file mode 100644 index 0000000..dbcf208 --- /dev/null +++ b/examples/custom_rule_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "testing" + "log" +) + + +// Test generated using Keploy +func TestErrInvalidPalindromeError(t *testing.T) { + err := &ErrInvalidPalindrome{Value: "not-a-palindrome"} + expected := "invalid palindrome: not-a-palindrome" + if err.Error() != expected { + t.Errorf("Expected %v, got %v", expected, err.Error()) + } +} + +// Test generated using Keploy +func TestPalindromeRuleName(t *testing.T) { + rule := &PalindromeRule{} + expected := "palindrome" + if rule.Name() != expected { + t.Errorf("Expected %v, got %v", expected, rule.Name()) + } +} + + +// Test generated using Keploy +func TestCustomRuleValidation(t *testing.T) { + // Mocking log output + var logOutput string + log.SetOutput(&mockWriter{func(p []byte) (n int, err error) { + logOutput += string(p) + return len(p), nil + }}) + + CustomRuleValidation() + + if logOutput == "" { + t.Error("Expected log output, got none") + } +} + +type mockWriter struct { + writeFunc func(p []byte) (n int, err error) +} + +func (m *mockWriter) Write(p []byte) (n int, err error) { + return m.writeFunc(p) +} + diff --git a/examples/http_api_test.go b/examples/http_api_test.go new file mode 100644 index 0000000..b0371f7 --- /dev/null +++ b/examples/http_api_test.go @@ -0,0 +1,65 @@ +package main + +import ( + "testing" +) + + +// Test generated using Keploy +func TestErrIsAdultErrorMessage(t *testing.T) { + err := &ErrIsAdult{} + expected := "The client isn't a adult then it isn't allowed buy" + if err.Error() != expected { + t.Errorf("Expected error message '%s', got '%s'", expected, err.Error()) + } +} + +// Test generated using Keploy +func TestIsAdultRuleName(t *testing.T) { + rule := &IsAdultRule{} + expected := "is_adult" + if rule.Name() != expected { + t.Errorf("Expected rule name '%s', got '%s'", expected, rule.Name()) + } +} + + +// Test generated using Keploy +func TestIsAdultRuleValidateEmptyValue(t *testing.T) { + rule := &IsAdultRule{adultAge: 21} + valid, err := rule.Validate("", "", "") + if !valid || err == nil { + t.Errorf("Expected validation to fail with ErrIsAdult, got valid=%v, err=%v", valid, err) + } +} + + +// Test generated using Keploy +func TestIsAdultRuleValidateNonIntegerValue(t *testing.T) { + rule := &IsAdultRule{adultAge: 21} + valid, err := rule.Validate("", "notanumber", "") + if valid || err == nil { + t.Errorf("Expected validation to fail with an error, got valid=%v, err=%v", valid, err) + } +} + + +// Test generated using Keploy +func TestIsAdultRuleValidateUnderage(t *testing.T) { + rule := &IsAdultRule{adultAge: 21} + valid, err := rule.Validate("", "18", "") + if !valid || err == nil { + t.Errorf("Expected validation to fail with ErrIsAdult, got valid=%v, err=%v", valid, err) + } +} + + +// Test generated using Keploy +func TestIsAdultRuleValidateAdult(t *testing.T) { + rule := &IsAdultRule{adultAge: 21} + valid, err := rule.Validate("", "21", "") + if !valid || err != nil { + t.Errorf("Expected validation to pass, got valid=%v, err=%v", valid, err) + } +} + diff --git a/examples/simple.go b/examples/simple.go index 911317c..c7d22d7 100644 --- a/examples/simple.go +++ b/examples/simple.go @@ -1,75 +1,81 @@ package main import ( - "log" + "log" - gody "github.com/guiferpa/gody/v2" - "github.com/guiferpa/gody/v2/rule" + gody "github.com/guiferpa/gody/v2" + "github.com/guiferpa/gody/v2/rule" ) -func SimpleDefaultValidation() { - b := struct { - Text string `json:"text" validate:"not_empty"` - }{} - - valid, err := gody.DefaultValidate(b, nil) - if err != nil { - if !valid { - log.Println("body do not validated:", err) - } - - switch err.(type) { - case *rule.ErrNotEmpty: - log.Println("not empty error:", err) - - } - } +func SimpleDefaultValidation(text string) error { + b := struct { + Text string `json:"text" validate:"not_empty"` + }{ + Text: text, + } + + valid, err := gody.DefaultValidate(b, nil) + if err != nil { + if !valid { + log.Println("body do not validated:", err) + } + + switch err.(type) { + case *rule.ErrNotEmpty: + log.Println("not empty error:", err) + } + } + return err } -func SimplePureValidation() { - b := struct { - Text string `json:"text" validate:"not_empty"` - }{} - - rules := []gody.Rule{ - rule.NotEmpty, - } - valid, err := gody.Validate(b, rules) - if err != nil { - if !valid { - log.Println("body do not validated:", err) - } - - switch err.(type) { - case *rule.ErrNotEmpty: - log.Println("not empty error:", err) - - } - } +func SimplePureValidation(text string) error { + b := struct { + Text string `json:"text" validate:"not_empty"` + }{ + Text: text, + } + + rules := []gody.Rule{ + rule.NotEmpty, + } + valid, err := gody.Validate(b, rules) + if err != nil { + if !valid { + log.Println("body do not validated:", err) + } + + switch err.(type) { + case *rule.ErrNotEmpty: + log.Println("not empty error:", err) + } + } + return err } -func SimpleValidationFromValidator() { - b := struct { - Text string `json:"text" validate:"not_empty"` - }{} - - validator := gody.NewValidator() - - if err := validator.AddRules(rule.NotEmpty); err != nil { - log.Println(err) - return - } - - valid, err := validator.Validate(b) - if err != nil { - if !valid { - log.Println("body do not validated:", err) - } - - switch err.(type) { - case *rule.ErrNotEmpty: - log.Println("not empty error:", err) - - } - } +func SimpleValidationFromValidator(text string) error { + b := struct { + Text string `json:"text" validate:"not_empty"` + }{ + Text: text, + } + + validator := gody.NewValidator() + + if err := validator.AddRules(rule.NotEmpty); err != nil { + log.Println(err) + return err + } + + valid, err := validator.Validate(b) + if err != nil { + if !valid { + log.Println("body do not validated:", err) + } + + switch err.(type) { + case *rule.ErrNotEmpty: + log.Println("not empty error:", err) + } + } + return err } diff --git a/examples/simple_test.go b/examples/simple_test.go new file mode 100644 index 0000000..6b127d3 --- /dev/null +++ b/examples/simple_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "testing" + "github.com/guiferpa/gody/v2/rule" +) + + +// Test generated using Keploy +func TestSimpleDefaultValidation_EmptyText(t *testing.T) { + err := SimpleDefaultValidation("") + if err == nil { + t.Errorf("Expected error for empty Text, but got nil") + } + if _, ok := err.(*rule.ErrNotEmpty); !ok { + t.Errorf("Expected ErrNotEmpty, but got %v", err) + } +} + +// Test generated using Keploy +func TestSimplePureValidation_EmptyText(t *testing.T) { + err := SimplePureValidation("") + if err == nil { + t.Errorf("Expected error for empty Text, but got nil") + } + if _, ok := err.(*rule.ErrNotEmpty); !ok { + t.Errorf("Expected ErrNotEmpty, but got %v", err) + } +} + + +// Test generated using Keploy +func TestSimpleValidationFromValidator_EmptyText(t *testing.T) { + err := SimpleValidationFromValidator("") + if err == nil { + t.Errorf("Expected error for empty Text, but got nil") + } + if _, ok := err.(*rule.ErrNotEmpty); !ok { + t.Errorf("Expected ErrNotEmpty, but got %v", err) + } +} + diff --git a/validate_test.go b/validate_test.go index 4b66709..5c7ff51 100644 --- a/validate_test.go +++ b/validate_test.go @@ -1,10 +1,10 @@ package gody import ( - "reflect" - "testing" + "reflect" + "testing" - "github.com/guiferpa/gody/v2/rule/ruletest" + "github.com/guiferpa/gody/v2/rule/ruletest" ) type StructAForTest struct { @@ -108,3 +108,33 @@ func TestSetTagName(t *testing.T) { return } } + +// Test generated using Keploy +func TestDefaultValidate_CallsRawDefaultValidate(t *testing.T) { + payload := StructAForTest{A: "test-a", B: "test-b"} + customRules := []Rule{ruletest.NewRule("fake", true, nil)} + + validated, err := DefaultValidate(payload, customRules) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if !validated { + t.Error("Expected validation to pass, but it failed") + } +} + + +// Test generated using Keploy +func TestRawValidate_RawSerializeError(t *testing.T) { + payload := make(chan int) // Invalid type to trigger RawSerialize error + rules := []Rule{ruletest.NewRule("fake", true, nil)} + + validated, err := RawValidate(payload, "validate", rules) + if validated { + t.Error("Expected validation to fail, but it passed") + } + if err == nil { + t.Error("Expected an error, but got nil") + } +} +