Skip to content

Commit

Permalink
(wip) not migrated packages yet
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Aug 28, 2024
1 parent b46df8b commit de41b57
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 637 deletions.
12 changes: 6 additions & 6 deletions registry/backward/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (

func TestFail(t *testing.T) {

var tc = []pesticide.MustTestCase{
{TestCase: pesticide.TestCase{Input: `{{fail "This is an error"}}`}, ExpectedErr: "This is an error"},
var tc = []pesticide.TestCase{
{Input: `{{fail "This is an error"}}`, ExpectedErr: "This is an error"},
}

pesticide.RunMustTestCases(t, backward.NewRegistry(), tc)
pesticide.RunTestCases(t, backward.NewRegistry(), tc)
}

func TestUrlParse(t *testing.T) {

var tc = []pesticide.TestCase{
{Input: `{{ urlParse "https://example.com" | urlJoin }}`, Expected: "https://example.com"},
{Input: `{{ urlParse "https://example.com/path" | urlJoin }}`, Expected: "https://example.com/path"},
{Input: `{{ urlParse "https://user:pass@example.com/path?query=1" | urlJoin }}`, Expected: "https://user:pass@example.com/path?query=1"},
{Input: `{{ urlParse "https://example.com" | urlJoin }}`, ExpectedOutput: "https://example.com"},
{Input: `{{ urlParse "https://example.com/path" | urlJoin }}`, ExpectedOutput: "https://example.com/path"},
{Input: `{{ urlParse "https://user:pass@example.com/path?query=1" | urlJoin }}`, ExpectedOutput: "https://user:pass@example.com/path?query=1"},
}

pesticide.RunTestCases(t, backward.NewRegistry(), tc)
Expand Down
30 changes: 15 additions & 15 deletions registry/checksum/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func TestSha1Sum(t *testing.T) {
multiHash := sha1.Sum([]byte("hello world"))

var tc = []pesticide.TestCase{
{Name: "TestEmptyInput", Input: `{{sha1Sum ""}}`, Expected: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{sha1Sum "a"}}`, Expected: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{sha1Sum "hello world"}}`, Expected: hex.EncodeToString(multiHash[:])},
{Name: "TestEmptyInput", Input: `{{sha1Sum ""}}`, ExpectedOutput: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{sha1Sum "a"}}`, ExpectedOutput: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{sha1Sum "hello world"}}`, ExpectedOutput: hex.EncodeToString(multiHash[:])},
}
pesticide.RunTestCases(t, checksum.NewRegistry(), tc)
}
Expand All @@ -33,9 +33,9 @@ func TestSha256Sum(t *testing.T) {
multiHash := sha256.Sum256([]byte("hello world"))

var tc = []pesticide.TestCase{
{Name: "TestEmptyInput", Input: `{{sha256Sum ""}}`, Expected: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{sha256Sum "a"}}`, Expected: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{sha256Sum "hello world"}}`, Expected: hex.EncodeToString(multiHash[:])},
{Name: "TestEmptyInput", Input: `{{sha256Sum ""}}`, ExpectedOutput: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{sha256Sum "a"}}`, ExpectedOutput: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{sha256Sum "hello world"}}`, ExpectedOutput: hex.EncodeToString(multiHash[:])},
}
pesticide.RunTestCases(t, checksum.NewRegistry(), tc)
}
Expand All @@ -46,9 +46,9 @@ func TestSha512sum(t *testing.T) {
multiHash := sha512.Sum512([]byte("hello world"))

var tc = []pesticide.TestCase{
{Name: "TestEmptyInput", Input: `{{sha512Sum ""}}`, Expected: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{sha512Sum "a"}}`, Expected: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{sha512Sum "hello world"}}`, Expected: hex.EncodeToString(multiHash[:])},
{Name: "TestEmptyInput", Input: `{{sha512Sum ""}}`, ExpectedOutput: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{sha512Sum "a"}}`, ExpectedOutput: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{sha512Sum "hello world"}}`, ExpectedOutput: hex.EncodeToString(multiHash[:])},
}
pesticide.RunTestCases(t, checksum.NewRegistry(), tc)
}
Expand All @@ -59,9 +59,9 @@ func TestAdler32Sum(t *testing.T) {
multiHash := adler32.Checksum([]byte("hello world"))

var tc = []pesticide.TestCase{
{Name: "TestEmptyInput", Input: `{{adler32Sum ""}}`, Expected: fmt.Sprint(noHash)},
{Name: "TestSingleByteInput", Input: `{{adler32Sum "a"}}`, Expected: fmt.Sprint(soloHash)},
{Name: "TestMultiByteInput", Input: `{{adler32Sum "hello world"}}`, Expected: fmt.Sprint(multiHash)},
{Name: "TestEmptyInput", Input: `{{adler32Sum ""}}`, ExpectedOutput: fmt.Sprint(noHash)},
{Name: "TestSingleByteInput", Input: `{{adler32Sum "a"}}`, ExpectedOutput: fmt.Sprint(soloHash)},
{Name: "TestMultiByteInput", Input: `{{adler32Sum "hello world"}}`, ExpectedOutput: fmt.Sprint(multiHash)},
}
pesticide.RunTestCases(t, checksum.NewRegistry(), tc)
}
Expand All @@ -72,9 +72,9 @@ func TestMD5Sum(t *testing.T) {
multiHash := md5.Sum([]byte("hello world"))

var tc = []pesticide.TestCase{
{Name: "TestEmptyInput", Input: `{{md5Sum ""}}`, Expected: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{md5Sum "a"}}`, Expected: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{md5Sum "hello world"}}`, Expected: hex.EncodeToString(multiHash[:])},
{Name: "TestEmptyInput", Input: `{{md5Sum ""}}`, ExpectedOutput: hex.EncodeToString(noHash[:])},
{Name: "TestSingleByteInput", Input: `{{md5Sum "a"}}`, ExpectedOutput: hex.EncodeToString(soloHash[:])},
{Name: "TestMultiByteInput", Input: `{{md5Sum "hello world"}}`, ExpectedOutput: hex.EncodeToString(multiHash[:])},
}
pesticide.RunTestCases(t, checksum.NewRegistry(), tc)
}
183 changes: 31 additions & 152 deletions registry/regexp/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,127 +4,6 @@ import (
"regexp"
)

// RegexFind returns the first match of the regex pattern in the string.
//
// Parameters:
//
// regex string - the regular expression pattern to search for.
// s string - the string to search.
//
// Returns:
//
// string - the first matching string.
//
// Example:
//
// {{ regexFind "a(b+)" "aaabbb" }} // Output: "abbb"
func (rr *RegexpRegistry) RegexFind(regex string, s string) string {
result, _ := rr.MustRegexFind(regex, s)
return result
}

// RegexFindAll returns all matches of the regex pattern in the string up to n
// matches.
//
// Parameters:
//
// regex string - the regular expression pattern to search for.
// s string - the string to search.
// n int - the maximum number of matches to return.
//
// Returns:
//
// []string - a slice of all matches.
//
// Example:
//
// {{ regexFindAll "a(b+)" "aaabbb" 2 }} // Output: ["abbb"]
func (rr *RegexpRegistry) RegexFindAll(regex string, s string, n int) []string {
result, _ := rr.MustRegexFindAll(regex, s, n)
return result
}

// RegexMatch checks if the string matches the regex pattern.
//
// Parameters:
//
// regex string - the regular expression pattern to match against.
// s string - the string to check.
//
// Returns:
//
// bool - true if the string matches the regex pattern, otherwise false.
//
// Example:
//
// {{ regexMatch "^[a-zA-Z]+$" "Hello" }} // Output: true
func (rr *RegexpRegistry) RegexMatch(regex string, s string) bool {
result, _ := rr.MustRegexMatch(regex, s)
return result
}

// RegexSplit splits the string by the regex pattern up to n times.
//
// Parameters:
//
// regex string - the regular expression pattern to split by.
// s string - the string to split.
// n int - the number of times to split.
//
// Returns:
//
// []string - a slice of the substrings split by the regex.
//
// Example:
//
// {{regexSplit "\\s+" "hello world" -1 }} // Output: ["hello", "world"]
func (rr *RegexpRegistry) RegexSplit(regex string, s string, n int) []string {
result, _ := rr.MustRegexSplit(regex, s, n)
return result
}

// RegexReplaceAll replaces all occurrences of the regex pattern in the string
// with the replacement string.
//
// Parameters:
//
// regex string - the regular expression pattern to replace.
// s string - the string to perform replacements on.
// repl string - the replacement string.
//
// Returns:
//
// string - the string with all replacements made.
//
// Example:
//
// {{ regexReplaceAll "[aeiou]" "hello" "i" }} // Output: "hillo"
func (rr *RegexpRegistry) RegexReplaceAll(regex string, s string, repl string) string {
result, _ := rr.MustRegexReplaceAll(regex, s, repl)
return result
}

// RegexReplaceAllLiteral replaces all occurrences of the regex pattern in the
// string with the literal replacement string.
//
// Parameters:
//
// regex string - the regular expression pattern to replace.
// s string - the string to perform replacements on.
// repl string - the replacement string, inserted literally.
//
// Returns:
//
// string - the string with all replacements made, without treating the replacement string as a regex replacement pattern.
//
// Example:
//
// {{ regexReplaceAllLiteral "[aeiou]" "hello" "$&" }} // Output: "h$&ll$&"
func (rr *RegexpRegistry) RegexReplaceAllLiteral(regex string, s string, repl string) string {
result, _ := rr.MustRegexReplaceAllLiteral(regex, s, repl)
return result
}

// RegexQuoteMeta returns a literal pattern string for the provided string.
//
// Parameters:
Expand All @@ -142,13 +21,13 @@ func (rr *RegexpRegistry) RegexQuoteMeta(str string) string {
return regexp.QuoteMeta(str)
}

// MustRegexFind searches for the first match of a regex pattern in a string
// RegexFind searches for the first match of a regex pattern in a string
// and returns it, with error handling.
//
// Parameters:
//
// regex string - the regular expression to search with.
// s string - the string to search within.
// str string - the string to search within.
//
// Returns:
//
Expand All @@ -157,22 +36,22 @@ func (rr *RegexpRegistry) RegexQuoteMeta(str string) string {
//
// Example:
//
// {{ "hello world" | mustRegexFind "hello" }} // Output: "hello", nil
func (rr *RegexpRegistry) MustRegexFind(regex string, s string) (string, error) {
// {{ "hello world" | RegexFind "hello" }} // Output: "hello", nil
func (rr *RegexpRegistry) RegexFind(regex string, str string) (string, error) {
r, err := regexp.Compile(regex)
if err != nil {
return "", err
}
return r.FindString(s), nil
return r.FindString(str), nil
}

// MustRegexFindAll finds all matches of a regex pattern in a string up to a
// RegexFindAll finds all matches of a regex pattern in a string up to a
// specified limit, with error handling.
//
// Parameters:
//
// regex string - the regular expression to search with.
// s string - the string to search within.
// str string - the string to search within.
// n int - the maximum number of matches to return; use -1 for no limit.
//
// Returns:
Expand All @@ -182,21 +61,21 @@ func (rr *RegexpRegistry) MustRegexFind(regex string, s string) (string, error)
//
// Example:
//
// {{ mustRegexFindAll "a.", "aba acada afa", 3 }} // Output: ["ab", "ac", "af"], nil
func (rr *RegexpRegistry) MustRegexFindAll(regex string, s string, n int) ([]string, error) {
// {{ RegexFindAll "a.", "aba acada afa", 3 }} // Output: ["ab", "ac", "af"], nil
func (rr *RegexpRegistry) RegexFindAll(regex string, str string, n int) ([]string, error) {
r, err := regexp.Compile(regex)
if err != nil {
return []string{}, err
}
return r.FindAllString(s, n), nil
return r.FindAllString(str, n), nil
}

// MustRegexMatch checks if a string matches a regex pattern, with error handling.
// RegexMatch checks if a string matches a regex pattern, with error handling.
//
// Parameters:
//
// regex string - the regular expression to match against.
// s string - the string to check.
// str string - the string to check.
//
// Returns:
//
Expand All @@ -205,18 +84,18 @@ func (rr *RegexpRegistry) MustRegexFindAll(regex string, s string, n int) ([]str
//
// Example:
//
// {{ mustRegexMatch "^[a-zA-Z]+$", "Hello" }} // Output: true, nil
func (rr *RegexpRegistry) MustRegexMatch(regex string, s string) (bool, error) {
return regexp.MatchString(regex, s)
// {{ RegexMatch "^[a-zA-Z]+$", "Hello" }} // Output: true, nil
func (rr *RegexpRegistry) RegexMatch(regex string, str string) (bool, error) {
return regexp.MatchString(regex, str)
}

// MustRegexSplit splits a string by a regex pattern up to a specified number of
// RegexSplit splits a string by a regex pattern up to a specified number of
// substrings, with error handling.
//
// Parameters:
//
// regex string - the regular expression to split by.
// s string - the string to split.
// str string - the string to split.
// n int - the maximum number of substrings to return; use -1 for no limit.
//
// Returns:
Expand All @@ -226,23 +105,23 @@ func (rr *RegexpRegistry) MustRegexMatch(regex string, s string) (bool, error) {
//
// Example:
//
// {{ mustRegexSplit "\\s+", "hello world from Go", 2 }} // Output: ["hello", "world from Go"], nil
func (rr *RegexpRegistry) MustRegexSplit(regex string, s string, n int) ([]string, error) {
// {{ RegexSplit "\\s+", "hello world from Go", 2 }} // Output: ["hello", "world from Go"], nil
func (rr *RegexpRegistry) RegexSplit(regex string, str string, n int) ([]string, error) {
r, err := regexp.Compile(regex)
if err != nil {
return []string{}, err
}
return r.Split(s, n), nil
return r.Split(str, n), nil
}

// MustRegexReplaceAll replaces all occurrences of a regex pattern in a string
// RegexReplaceAll replaces all occurrences of a regex pattern in a string
// with a replacement string, with error handling.
//
// Parameters:
//
// regex string - the regular expression to replace.
// s string - the string containing the original text.
// repl string - the replacement text.
// str string - the string containing the original text.
// replacedBy string - the replacement text.
//
// Returns:
//
Expand All @@ -251,23 +130,23 @@ func (rr *RegexpRegistry) MustRegexSplit(regex string, s string, n int) ([]strin
//
// Example:
//
// {{ mustRegexReplaceAll "\\d", "R2D2 C3PO", "X" }} // Output: "RXDX CXPO", nil
func (rr *RegexpRegistry) MustRegexReplaceAll(regex string, s string, repl string) (string, error) {
// {{ RegexReplaceAll "\\d", "R2D2 C3PO", "X" }} // Output: "RXDX CXPO", nil
func (rr *RegexpRegistry) RegexReplaceAll(regex string, str string, replacedBy string) (string, error) {
r, err := regexp.Compile(regex)
if err != nil {
return "", err
}
return r.ReplaceAllString(s, repl), nil
return r.ReplaceAllString(str, replacedBy), nil
}

// MustRegexReplaceAllLiteral replaces all occurrences of a regex pattern in a
// RegexReplaceAllLiteral replaces all occurrences of a regex pattern in a
// string with a literal replacement string, with error handling.
//
// Parameters:
//
// regex string - the regular expression to replace.
// s string - the string containing the original text.
// repl string - the literal replacement text.
// replacedBy string - the literal replacement text.
//
// Returns:
//
Expand All @@ -276,11 +155,11 @@ func (rr *RegexpRegistry) MustRegexReplaceAll(regex string, s string, repl strin
//
// Example:
//
// {{ mustRegexReplaceAllLiteral "world", "hello world", "$1" }} // Output: "hello $1", nil
func (rr *RegexpRegistry) MustRegexReplaceAllLiteral(regex string, s string, repl string) (string, error) {
// {{ RegexReplaceAllLiteral "world", "hello world", "$1" }} // Output: "hello $1", nil
func (rr *RegexpRegistry) RegexReplaceAllLiteral(regex string, s string, replacedBy string) (string, error) {
r, err := regexp.Compile(regex)
if err != nil {
return "", err
}
return r.ReplaceAllLiteralString(s, repl), nil
return r.ReplaceAllLiteralString(s, replacedBy), nil
}
Loading

0 comments on commit de41b57

Please sign in to comment.