This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin.git
The following commit(s) were added to refs/heads/master by this push:
new df166f6 test: add key get wrapper coverage (#1730)
df166f6 is described below
commit df166f67a798dad9da970d8c3d3726d1df6a25d8
Author: legendpei <[email protected]>
AuthorDate: Tue Aug 4 15:53:42 2026 +0800
test: add key get wrapper coverage (#1730)
---
util/builtin_operators_test.go | 72 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/util/builtin_operators_test.go b/util/builtin_operators_test.go
index c358850..a4990bb 100644
--- a/util/builtin_operators_test.go
+++ b/util/builtin_operators_test.go
@@ -345,6 +345,20 @@ func testKeyMatchFunc(t *testing.T, res bool, err string,
args ...interface{}) {
}
}
+func testKeyGetFunc(t *testing.T, res interface{}, err string, args
...interface{}) {
+ t.Helper()
+ myRes, myErr := KeyGetFunc(args...)
+ myErrStr := ""
+
+ if myErr != nil {
+ myErrStr = myErr.Error()
+ }
+
+ if myRes != res || err != myErrStr {
+ t.Errorf("%v returns %v %v, supposed to be %v %v", args, myRes,
myErr, res, err)
+ }
+}
+
func testKeyMatch2Func(t *testing.T, res bool, err string, args
...interface{}) {
t.Helper()
myRes, myErr := KeyMatch2Func(args...)
@@ -359,6 +373,20 @@ func testKeyMatch2Func(t *testing.T, res bool, err string,
args ...interface{})
}
}
+func testKeyGet2Func(t *testing.T, res interface{}, err string, args
...interface{}) {
+ t.Helper()
+ myRes, myErr := KeyGet2Func(args...)
+ myErrStr := ""
+
+ if myErr != nil {
+ myErrStr = myErr.Error()
+ }
+
+ if myRes != res || err != myErrStr {
+ t.Errorf("%v returns %v %v, supposed to be %v %v", args, myRes,
myErr, res, err)
+ }
+}
+
func testKeyMatch3Func(t *testing.T, res bool, err string, args
...interface{}) {
t.Helper()
myRes, myErr := KeyMatch3Func(args...)
@@ -373,6 +401,20 @@ func testKeyMatch3Func(t *testing.T, res bool, err string,
args ...interface{})
}
}
+func testKeyGet3Func(t *testing.T, res interface{}, err string, args
...interface{}) {
+ t.Helper()
+ myRes, myErr := KeyGet3Func(args...)
+ myErrStr := ""
+
+ if myErr != nil {
+ myErrStr = myErr.Error()
+ }
+
+ if myRes != res || err != myErrStr {
+ t.Errorf("%v returns %v %v, supposed to be %v %v", args, myRes,
myErr, res, err)
+ }
+}
+
func testKeyMatch4Func(t *testing.T, res bool, err string, args
...interface{}) {
t.Helper()
myRes, myErr := KeyMatch4Func(args...)
@@ -431,6 +473,16 @@ func TestKeyMatchFunc(t *testing.T) {
testKeyMatchFunc(t, true, "", "/foo/bar", "/foo*")
}
+func TestKeyGetFunc(t *testing.T) {
+ testKeyGetFunc(t, false, "keyGet: expected 2 arguments, but got 1",
"/foo")
+ testKeyGetFunc(t, false, "keyGet: expected 2 arguments, but got 3",
"/foo/bar", "/foo/*", "bar")
+ testKeyGetFunc(t, false, "keyGet: argument must be a string",
"/foo/bar", true)
+
+ testKeyGetFunc(t, "", "", "/foo/bar", "/foo")
+ testKeyGetFunc(t, "bar", "", "/foo/bar", "/foo/*")
+ testKeyGetFunc(t, "/bar", "", "/foo/bar", "/foo*")
+}
+
func TestKeyMatch2Func(t *testing.T) {
testKeyMatch2Func(t, false, "keyMatch2: expected 2 arguments, but got
1", "/")
testKeyMatch2Func(t, false, "keyMatch2: expected 2 arguments, but got
3", "/foo/create/123", "/*", "/foo/update/123")
@@ -444,6 +496,16 @@ func TestKeyMatch2Func(t *testing.T) {
testKeyMatch2Func(t, false, "", "/foo", "/foo/*")
}
+func TestKeyGet2Func(t *testing.T) {
+ testKeyGet2Func(t, false, "keyGet2: expected 3 arguments, but got 2",
"/myid/using/myresid", "/:id/using/:resId")
+ testKeyGet2Func(t, false, "keyGet2: expected 3 arguments, but got 4",
"/myid/using/myresid", "/:id/using/:resId", "id", "resId")
+ testKeyGet2Func(t, false, "keyGet2: argument must be a string",
"/myid/using/myresid", "/:id/using/:resId", true)
+
+ testKeyGet2Func(t, "myid", "", "/myid/using/myresid",
"/:id/using/:resId", "id")
+ testKeyGet2Func(t, "myresid", "", "/myid/using/myresid",
"/:id/using/:resId", "resId")
+ testKeyGet2Func(t, "", "", "/myid/using/myresid", "/:id/using/:resId",
"missing")
+}
+
func TestKeyMatch3Func(t *testing.T) {
testKeyMatch3Func(t, false, "keyMatch3: expected 2 arguments, but got
1", "/")
testKeyMatch3Func(t, false, "keyMatch3: expected 2 arguments, but got
3", "/foo/create/123", "/*", "/foo/update/123")
@@ -472,6 +534,16 @@ func TestKeyMatch3Func(t *testing.T) {
testKeyMatch3Func(t, false, "", "/proxy/", "/proxy/{id}/*")
}
+func TestKeyGet3Func(t *testing.T) {
+ testKeyGet3Func(t, false, "keyGet3: expected 3 arguments, but got 2",
"/myid/using/myresid", "/{id}/using/{resId}")
+ testKeyGet3Func(t, false, "keyGet3: expected 3 arguments, but got 4",
"/myid/using/myresid", "/{id}/using/{resId}", "id", "resId")
+ testKeyGet3Func(t, false, "keyGet3: argument must be a string",
"/myid/using/myresid", "/{id}/using/{resId}", true)
+
+ testKeyGet3Func(t, "myid", "", "/myid/using/myresid",
"/{id}/using/{resId}", "id")
+ testKeyGet3Func(t, "group_name", "",
"/api/group1_group_name/project1_admin/info",
"/api/{g}_{gn}/{proj}_admin/info", "gn")
+ testKeyGet3Func(t, "", "", "/myid/using/myresid",
"/{id}/using/{resId}", "missing")
+}
+
func TestKeyMatch4Func(t *testing.T) {
testKeyMatch4Func(t, false, "keyMatch4: expected 2 arguments, but got
1", "/parent/123/child/123")
testKeyMatch4Func(t, false, "keyMatch4: expected 2 arguments, but got
3", "/parent/123/child/123", "/parent/{id}/child/{id}", true)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]