Compare commits

..

2 commits

Author SHA1 Message Date
889df318db
test: method invocation. 2024-11-07 22:26:53 +01:00
8257acbfbb
fix! method invocation if no arguments are supplied. 2024-11-07 22:26:33 +01:00
2 changed files with 19 additions and 2 deletions

View file

@ -182,13 +182,21 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp {
private static Expression _invoke(IEnumerable<Expression> args) {
Object o = (Object) args.First();
String s = (String) args.Skip(1).First();
Cons l = (Cons) args.Skip(2).First();
IEnumerable<Expression> l;
if (args.Skip(2).First() is Boolean lb && lb == Boolean.FALSE) {
l = new List<Expression>();
} else if (args.Skip(2).First() is Cons lc) {
l = lc.ToList();
} else {
throw new ApplicationException($"Expected a list of arguments, got {args.Skip(2).First()}");
}
IList<Expression> r = new List<Expression>();
MethodInfo? mi = o.Value().GetType().GetMethod(s.Value());
if (mi == null) {
throw new ApplicationException($"{o.Value()} has not method {s.Value()}");
}
object?[]? l_ = l.ToList().Select<Expression, object?>(x => {
object?[]? l_ = l.Select<Expression, object?>(x => {
switch (x) {
case Integer s:
return s.Value();

View file

@ -16,6 +16,9 @@ namespace Tests
}
public int i { get => _i; }
public bool b { get => _b; }
public int I() {
return _i;
}
}
public class Test {
@ -200,6 +203,8 @@ namespace Tests
Assert.Equal(((Lisp_Boolean)r).Value(), true);
r = e.eval("""(getitems o "i" "b")""");
Assert.Equal(string.Format("{0}", r), "(5 nil)");
r = e.eval("""(invoke o "I" nil)""");
Assert.Equal(string.Format("{0}", r), "5");
}
[Fact]
@ -215,6 +220,10 @@ namespace Tests
Assert.Equal("t", e.eval("(and (quote (1 2 3 4)))").ToString());
Assert.Equal("t", e.eval("(or (quote (nil nil 1 nil)))").ToString());
Assert.Equal("nil", e.eval("(or (quote (nil nil nil nil)))").ToString());
Assert.Equal("t", e.eval("(any (lambda (x) (= x 2)) (list 1 2 3 4 5 6))").ToString());
Assert.Equal("nil", e.eval("(any (lambda (x) (= x 2)) (list 1 3 4 5 6))").ToString());
Assert.Equal("t", e.eval("(all (lambda (x) (= 1 (% x 2))) (list 1 3 5))").ToString());
Assert.Equal("nil", e.eval("(all (lambda (x) (= 1 (% x 2))) (list 1 3 4 5))").ToString());
}
}
}