validator_test.go 1.5 KB

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