2024-06-27 01:47:44 +02:00
|
|
|
using Xunit;
|
|
|
|
using Lisp_Environment = Jellyfin.Plugin.SmartPlaylist.Lisp.Environment;
|
|
|
|
using Lisp_Boolean = Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler.Boolean;
|
2024-06-29 18:29:40 +02:00
|
|
|
using Lisp_Object = Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler.Object;
|
2024-06-27 01:47:44 +02:00
|
|
|
using Jellyfin.Plugin.SmartPlaylist.Lisp;
|
|
|
|
using Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler;
|
|
|
|
|
|
|
|
namespace Tests
|
|
|
|
{
|
2024-06-29 18:29:40 +02:00
|
|
|
public class O {
|
|
|
|
int _i;
|
|
|
|
bool _b;
|
|
|
|
public O(int i, bool b) {
|
|
|
|
_i = i;
|
|
|
|
_b = b;
|
|
|
|
}
|
|
|
|
public int i { get => _i; }
|
|
|
|
public bool b { get => _b; }
|
|
|
|
}
|
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
public class Test {
|
|
|
|
[Fact]
|
2024-10-25 02:18:38 +02:00
|
|
|
public static void TestTokenizer() {
|
2024-10-25 20:20:18 +02:00
|
|
|
StringTokenStream sts = StringTokenStream.generate("(\"some literal string\" def ghj +100 -+300 1 >= ++ !=)");
|
2024-06-27 01:47:44 +02:00
|
|
|
Assert.Equal(sts.get().value, "(");
|
|
|
|
Assert.Equal(sts.get().value, "\"");
|
|
|
|
Assert.Equal(sts.get().value, "some");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "literal");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "string");
|
|
|
|
Assert.Equal(sts.get().value, "\"");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "def");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "ghj");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "+");
|
|
|
|
Assert.Equal(sts.get().value, "100");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "-");
|
|
|
|
Assert.Equal(sts.get().value, "+");
|
|
|
|
Assert.Equal(sts.get().value, "300");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "1");
|
2024-10-25 02:18:38 +02:00
|
|
|
Assert.Equal(sts.get().value, " ");
|
2024-10-25 20:20:18 +02:00
|
|
|
Assert.Equal(sts.get().value, ">");
|
|
|
|
Assert.Equal(sts.get().value, "=");
|
2024-10-25 02:18:38 +02:00
|
|
|
Assert.Equal(sts.get().value, " ");
|
2024-10-25 20:20:18 +02:00
|
|
|
Assert.Equal(sts.get().value, "+");
|
|
|
|
Assert.Equal(sts.get().value, "+");
|
|
|
|
Assert.Equal(sts.get().value, " ");
|
|
|
|
Assert.Equal(sts.get().value, "!");
|
|
|
|
Assert.Equal(sts.get().value, "=");
|
2024-06-27 01:47:44 +02:00
|
|
|
Assert.Equal(sts.get().value, ")");
|
|
|
|
sts.commit();
|
|
|
|
Assert.Equal(sts.available(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2024-10-25 02:18:38 +02:00
|
|
|
public static void TestParser() {
|
|
|
|
string program = "(+ 1 (* 2 3))";
|
2024-06-27 01:47:44 +02:00
|
|
|
StringTokenStream sts = StringTokenStream.generate(program);
|
|
|
|
Parser p = new Parser(sts);
|
|
|
|
Assert.Equal(program, string.Format("{0}", p.parse()));
|
2024-06-29 18:29:40 +02:00
|
|
|
|
2024-10-25 02:18:38 +02:00
|
|
|
program = "(haskeys o \"i\" \"b\")";
|
2024-06-29 18:29:40 +02:00
|
|
|
sts = StringTokenStream.generate(program);
|
|
|
|
p = new Parser(sts);
|
|
|
|
Assert.Equal(program, string.Format("{0}", p.parse()));
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2024-10-25 02:18:38 +02:00
|
|
|
public static void TestFunctions() {
|
|
|
|
IList<Tuple<string, Expression>> cases = new List<Tuple<string, Expression>>();
|
|
|
|
Expression e = new Executor().eval("(+ 10 20)");
|
|
|
|
Assert.Equal(((Integer) e).value, 30);
|
2024-06-29 18:29:40 +02:00
|
|
|
|
|
|
|
e = new Executor().eval("(> 1 2)");
|
|
|
|
Assert.Equal(((Lisp_Boolean) e).value, false);
|
2024-06-27 01:47:44 +02:00
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
e = new Executor().eval("(if (> 1 2) 3 4)");
|
|
|
|
Assert.Equal(((Integer) e).value, 4);
|
2024-06-27 01:47:44 +02:00
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
e = new Executor().eval("(begin (define x 1) x)");
|
|
|
|
Assert.Equal(((Integer) e).value, 1);
|
2024-06-27 01:47:44 +02:00
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
e = new Executor().eval("(apply + (1 2))");
|
|
|
|
Assert.Equal(((Integer) e).value, 3);
|
2024-06-27 01:47:44 +02:00
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
e = new Executor().eval("(car (10 20 30))");
|
|
|
|
Assert.Equal(((Integer) e).value, 10);
|
2024-06-27 01:47:44 +02:00
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
e = new Executor().eval("(cdr (10 20 30))");
|
2024-10-25 20:20:18 +02:00
|
|
|
Assert.Equal(string.Format("{0}", e), "(20 30)");
|
2024-06-29 18:29:40 +02:00
|
|
|
|
|
|
|
e = new Executor().eval("(cons 1 3)");
|
2024-10-25 20:20:18 +02:00
|
|
|
Assert.Equal(string.Format("{0}", e), "(1 3)");
|
2024-06-29 18:29:40 +02:00
|
|
|
|
|
|
|
e = new Executor().eval("(cons 1 (2 3))");
|
2024-10-25 20:20:18 +02:00
|
|
|
Assert.Equal(string.Format("{0}", e), "(1 2 3)");
|
2024-06-29 18:29:40 +02:00
|
|
|
|
|
|
|
e = new Executor().eval("(length (cons 1 (2 3)))");
|
|
|
|
Assert.Equal(string.Format("{0}", e), "3");
|
2024-10-25 20:20:18 +02:00
|
|
|
|
|
|
|
e = new Executor().eval("(>= 2 2)");
|
|
|
|
Assert.Equal(string.Format("{0}", e), "t");
|
|
|
|
|
|
|
|
e = new Executor().eval("(> 2 2))");
|
|
|
|
Assert.Equal(string.Format("{0}", e), "nil");
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
|
|
|
[Fact]
|
|
|
|
public static void ObjectTest() {
|
|
|
|
Executor e = new Executor();
|
|
|
|
Expression r;
|
2024-10-26 03:49:52 +02:00
|
|
|
e.environment.Set("o", new Lisp_Object(new O(5, false)));
|
2024-06-29 18:29:40 +02:00
|
|
|
r = e.eval("(haskeys o 'i' 'b')");
|
|
|
|
Assert.Equal(((Lisp_Boolean)r).value, true);
|
|
|
|
r = e.eval("(getitems o 'i' 'b')");
|
2024-10-25 20:20:18 +02:00
|
|
|
Assert.Equal(string.Format("{0}", r), "(5 nil)");
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-10-26 03:49:52 +02:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void ProcedureTest() {
|
|
|
|
Executor e = new Executor();
|
|
|
|
Expression r;
|
|
|
|
r = e.eval("((lambda (a) (* a a)) 2)");
|
|
|
|
Assert.Equal(string.Format("{0}", r), "4");
|
|
|
|
|
|
|
|
r = e.eval("(begin (define mull (lambda (a) (* a a))) (mull 3))");
|
|
|
|
Assert.Equal(string.Format("{0}", r), "9");
|
|
|
|
|
|
|
|
//r = e.eval("""
|
|
|
|
//(begin (define pi 3.1415) 1)
|
|
|
|
//""");
|
|
|
|
//Assert.Equal(string.Format("{0}", r), "1");
|
|
|
|
}
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
}
|