File: wildcard_test.go

package info (click to toggle)
golang-github-iglou-eu-go-wildcard 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100 kB
  • sloc: makefile: 2
file content (100 lines) | stat: -rw-r--r-- 4,345 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (c) 2023 Iglou.eu <contact@iglou.eu>
 * Copyright (c) 2023 Adrien Kara <adrien@iglou.eu>
 *
 * Licensed under the BSD 3-Clause License,
 * see LICENSE.md for more details.
 */

package wildcard_test

import (
	"testing"

	"github.com/IGLOU-EU/go-wildcard/v2"
)

// TestMatch - Tests validate the logic of wild card matching.
// `Match` supports '*' and '?' wildcards.
// Sample usage: In resource matching for bucket policy validation.
func TestMatch(t *testing.T) {
	cases := []struct {
		s       string
		pattern string
		result  bool
	}{
		{"", "", true},
		{"", "*", true},
		{"", "**", true},
		{"", "?", true},
		{"", "??", true},
		{"", "?*", true},
		{"", "*?", true},
		{"", ".", false},
		{"", ".?", false},
		{"", "?.", false},
		{"", ".*", false},
		{"", "*.", false},
		{"", "*.?", false},
		{"", "?.*", false},

		{"a", "", false},
		{"a", "a", true},
		{"a", "*", true},
		{"a", "**", true},
		{"a", "?", true},
		{"a", "??", true},
		{"a", ".", true},
		{"a", ".?", true},
		{"a", "?.", false},
		{"a", ".*", true},
		{"a", "*.", true},
		{"a", "*.?", true},
		{"a", "?.*", false},

		{"match the exact string", "match the exact string", true},
		{"do not match a different string", "this is a different string", false},
		{"Match The Exact String WITH DIFFERENT CASE", "Match The Exact String WITH DIFFERENT CASE", true},
		{"do not match a different string WITH DIFFERENT CASE", "this is a different string WITH DIFFERENT CASE", false},
		{"Do Not Match The Exact String With Different Case", "do not match the exact string with different case", false},
		{"match an emoji πŸ˜ƒ", "match an emoji πŸ˜ƒ", true},
		{"do not match because of different emoji πŸ˜ƒ", "do not match because of different emoji πŸ˜„", false},
		{"πŸŒ…β˜•οΈπŸ“°πŸ‘¨β€πŸ’ΌπŸ‘©β€πŸ’ΌπŸ’πŸ–₯οΈπŸ’ΌπŸ’»πŸ“ŠπŸ“ˆπŸ“‰πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦πŸπŸ•°οΈπŸ’ͺπŸ‹οΈβ€β™‚οΈπŸ‹οΈβ€β™€οΈπŸ‹οΈβ€β™‚οΈπŸ’ΌπŸš΄β€β™‚οΈπŸš΄β€β™€οΈπŸš΄β€β™‚οΈπŸ›€πŸ’€πŸŒƒ", "πŸŒ…β˜•οΈπŸ“°πŸ‘¨β€πŸ’ΌπŸ‘©β€πŸ’ΌπŸ’πŸ–₯οΈπŸ’ΌπŸ’»πŸ“ŠπŸ“ˆπŸ“‰πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦πŸπŸ•°οΈπŸ’ͺπŸ‹οΈβ€β™‚οΈπŸ‹οΈβ€β™€οΈπŸ‹οΈβ€β™‚οΈπŸ’ΌπŸš΄β€β™‚οΈπŸš΄β€β™€οΈπŸš΄β€β™‚οΈπŸ›€πŸ’€πŸŒƒ", true},
		{"πŸŒ…β˜•οΈπŸ“°πŸ‘¨β€πŸ’ΌπŸ‘©β€πŸ’ΌπŸ’πŸ–₯οΈπŸ’ΌπŸ’»πŸ“ŠπŸ“ˆπŸ“‰πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦πŸπŸ•°οΈπŸ’ͺπŸ‹οΈβ€β™‚οΈπŸ‹οΈβ€β™€οΈπŸ‹οΈβ€β™‚οΈπŸ’ΌπŸš΄β€β™‚οΈπŸš΄β€β™€οΈπŸš΄β€β™‚οΈπŸ›€πŸ’€πŸŒƒ", "πŸ¦ŒπŸ‡πŸ¦‘πŸΏοΈπŸŒ²πŸŒ³πŸ°πŸŒ³πŸŒ²πŸŒžπŸŒ§οΈβ„οΈπŸŒ¬οΈβ›ˆοΈπŸ”₯πŸŽ„πŸŽ…πŸŽπŸŽ‰πŸŽŠπŸ₯³πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦πŸ’πŸ‘ͺπŸ’–πŸ‘©β€πŸ’ΌπŸ›€", false},

		{"match a string with a *", "match a string *", true},
		{"match a string with a * at the beginning", "* at the beginning", true},
		{"match a string with two *", "match * with *", true},
		{"do not match a string with extra and a *", "do not match a string * with more", false},

		{"match a string with a ?", "match ? string with a ?", true},
		{"match a string with a ? at the beginning", "?atch a string with a ? at the beginning", true},
		{"match a string with two ?", "match a string with two ??", true},
		{"match a optional char with a ?", "match a optional? char with a ?", true},
		{"match a optional   char with a ?", "match a optional?   char with a ?", true},
		{"do not match a string with extra and a ?", "do not match ? string with extra and a ? like this", false},

		{"match a string with a .", "match . string with a .", true},
		{"match a string with a . at the beginning", ".atch a string with a . at the beginning", true},
		{"match a string with two .", "match a ..ring with two .", true},
		{"do not match a string with extra .", "do not match a string with extra ..", false},

		{"A big brown fox jumps over the lazy dog, with all there wildcards friends", ". big?brown fox jumps over * wildcard. friend??", true},
		{"A big brown fox fails to jump over the lazy dog, with all there wildcards friends", ". big?brown fox jumps over * wildcard. friend??", false},
	}

	for i, c := range cases {
		result := wildcard.Match(c.pattern, c.s)
		if c.result != result {
			t.Errorf("Test %d: Expected `%v`, found `%v`; With Pattern: `%s` and String: `%s`", i+1, c.result, result, c.pattern, c.s)
		}
	}
}

func FuzzMatch(f *testing.F) {
	f.Fuzz(func(t *testing.T, s string) {
		if !wildcard.Match(s, s) {
			t.Fatalf("%s does not match %s", s, s)
		}
	})
}