validator_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2021 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package validator // import "miniflux.app/validator"
  5. import "testing"
  6. func TestIsValidURL(t *testing.T) {
  7. scenarios := map[string]bool{
  8. "https://www.example.org": true,
  9. "http://www.example.org/": true,
  10. "www.example.org": false,
  11. }
  12. for link, expected := range scenarios {
  13. result := IsValidURL(link)
  14. if result != expected {
  15. t.Errorf(`Unexpected result, got %v instead of %v`, result, expected)
  16. }
  17. }
  18. }
  19. func TestValidateRange(t *testing.T) {
  20. if err := ValidateRange(-1, 0); err == nil {
  21. t.Error(`An invalid offset should generate a error`)
  22. }
  23. if err := ValidateRange(0, -1); err == nil {
  24. t.Error(`An invalid limit should generate a error`)
  25. }
  26. if err := ValidateRange(42, 42); err != nil {
  27. t.Error(`A valid offset and limit should not generate any error`)
  28. }
  29. }
  30. func TestValidateDirection(t *testing.T) {
  31. for _, status := range []string{"asc", "desc"} {
  32. if err := ValidateDirection(status); err != nil {
  33. t.Error(`A valid direction should not generate any error`)
  34. }
  35. }
  36. if err := ValidateDirection("invalid"); err == nil {
  37. t.Error(`An invalid direction should generate a error`)
  38. }
  39. }
  40. func TestIsValidRegex(t *testing.T) {
  41. scenarios := map[string]bool{
  42. "(?i)miniflux": true,
  43. "[": false,
  44. }
  45. for expr, expected := range scenarios {
  46. result := IsValidRegex(expr)
  47. if result != expected {
  48. t.Errorf(`Unexpected result, got %v instead of %v`, result, expected)
  49. }
  50. }
  51. }