-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support add generic type custom function (#365)
* feat: support add generic type custom function * fix: not support .NET 4.5.2 * fix: indeterminate sorting of GetImplicitRolesForUser
- Loading branch information
1 parent
6be2f80
commit fe66cd4
Showing
84 changed files
with
149 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using Casbin.Model; | ||
using Casbin.UnitTests.Util; | ||
using DynamicExpresso; | ||
using Xunit; | ||
|
||
namespace Casbin.UnitTests.GenericTests; | ||
|
||
public class GenericFunctionTest | ||
{ | ||
[Fact] | ||
public void TestGenericFunction() | ||
{ | ||
Interpreter interpreter = new(); | ||
RequestValues<string, int> r = Request.CreateValues("A", 1); | ||
PolicyValues<string, int> p = Policy.CreateValues("A", 1); | ||
interpreter.SetFunction("equal", new Func<string, string, bool>( | ||
(a, b) => a == b) | ||
); | ||
interpreter.SetFunction("equal", new Func<int, int, bool>( | ||
(a, b) => a == b) | ||
); | ||
|
||
Func<RequestValues<string, int>, PolicyValues<string, int>, bool> func1 = | ||
ExpressionUtil.Compile(interpreter, "equal(r.Value2, p.Value2) && equal(r.Value2, p.Value2)", | ||
nameof(r), in r, nameof(p), in p); | ||
|
||
Assert.True(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 1))); | ||
Assert.False(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 2))); | ||
Assert.False(func1(Request.CreateValues("B", 1), Policy.CreateValues("B", 2))); | ||
} | ||
|
||
#if !NET452 | ||
[Fact] | ||
public void TestGenericFunctionModel() | ||
{ | ||
Enforcer e = new Enforcer(DefaultModel.NewModelFromText( | ||
""" | ||
[request_definition] | ||
r = obj1, obj2 | ||
|
||
[policy_definition] | ||
p = _ | ||
|
||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
|
||
[matchers] | ||
m = max(r.obj1, r.obj2) > 2 | ||
""")); | ||
|
||
e.AddFunction("max", new Func<int, int, int>( | ||
// ReSharper disable once ConvertClosureToMethodGroup | ||
(a, b) => Math.Max(a, b) | ||
)); | ||
Assert.True(e.Enforce(1, 3)); | ||
Assert.False(e.Enforce(1, 2)); | ||
Assert.False(e.Enforce("1", "111")); | ||
|
||
e.AddFunction("max", new Func<string, string, int>( | ||
(a, b) => Math.Max(a.Length, b.Length) | ||
)); | ||
Assert.True(e.Enforce(1, 3)); | ||
Assert.False(e.Enforce(1, 2)); | ||
Assert.True(e.Enforce("1", "111")); | ||
} | ||
#endif | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.