private static void ShowHelp(ScribuntoConsole sc) { var commands = typeof(Program).GetMethods(BindingFlags.Static | BindingFlags.NonPublic) .Select(m => (method: m, attr: m.GetCustomAttribute <ConsoleCommandAttribute>())) .Where(t => t.attr != null) .Select(t => (command: t.attr.Command, desc: t.attr.Description, method: t.method)) .OrderBy(t => t.command); foreach (var t in commands) { Console.WriteLine(".{0,-15} {1}", t.command, t.desc); } }
private static async Task ExecuteCommandAsync(string command, ScribuntoConsole sc) { var method = typeof(Program).GetMethods(BindingFlags.Static | BindingFlags.NonPublic) .FirstOrDefault(m => string.Equals(m.GetCustomAttribute <ConsoleCommandAttribute>()?.Command, command, StringComparison.OrdinalIgnoreCase)); if (method == null) { WriteError("Invalid command: " + command + "."); return; } var result = method.Invoke(null, new object[] { sc }); if (result is Task t) { await t; } }
internal static async Task Main(string[] args) { var endPoint = args.Length > 0 ? args[0] : "https://test2.wikipedia.org/w/api.php"; using var client = new WikiClient { ClientUserAgent = "ScribuntoConsoleTestApplication1/0.1" }; var site = new WikiSite(client, endPoint); await site.Initialization; var sc = new ScribuntoConsole(site); await ResetSessionAsync(sc); var eofShortcut = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Ctrl + Z" : "Ctrl + D"; Console.WriteLine("* Enter any Lua expression to evaluate. EOF ({0}) to exit.", eofShortcut); Console.WriteLine("* Precede a line with '=' to evaluate it as an expression or use \x1b[36mprint()\x1b[0m. Use \x1b[36mmw.logObject()\x1b[0m for tables."); Console.WriteLine("* Use \x1b[36mmw.log()\x1b[0m and \x1b[36mmw.logObject()\x1b[0m in module code to send messages to this console."); Console.WriteLine("* Enter .help for a list of local commands."); while (true) { Console.Write("> "); var l = Console.ReadLine(); if (l == null) { break; } if (string.IsNullOrWhiteSpace(l)) { continue; } if (l.StartsWith(".")) { if (string.Equals(l, ".exit", StringComparison.OrdinalIgnoreCase)) { break; } await ExecuteCommandAsync(l[1..], sc);
public async Task TestConsoleAsync(string siteName) { const string ModuleContent = @" -- Test module for unit test local p = {} p.name = 'WCL test module' function p.foo() return 'Hello, world!' end function p.bar(arg) return arg[1] end return p "; var site = await WikiSiteFromNameAsync(siteName); var console = new ScribuntoConsole(site); await console.ResetAsync(ModuleContent); async Task <ScribuntoEvaluationResult> TestEvaluation(string expr, string returnValue = "", string output = "") { var id = console.SessionId; var r = await console.EvaluateAsync(expr); Assert.Equal(ScribuntoEvaluationResultType.Normal, r.Type); Assert.Equal(returnValue, r.ReturnValue); Assert.Equal(output, r.Output); Assert.Equal(id, r.SessionId); Assert.False(r.IsNewSession); return(r); } Assert.NotEqual(0, console.SessionId); Assert.NotEqual(0, console.SessionSize); Assert.NotEqual(0, console.SessionMaxSize); Assert.True(console.SessionSize < console.SessionMaxSize); await TestEvaluation("=1+1", "2"); await TestEvaluation("=[[test]]", "test"); await TestEvaluation("mw.log(2+1)", "", "3\n"); await TestEvaluation("local x = 100"); await TestEvaluation("=x", "100"); await TestEvaluation("=p", "table"); await TestEvaluation("=p.name", "WCL test module"); await TestEvaluation("=p.foo()", "Hello, world!"); var ex = await Assert.ThrowsAsync <ScribuntoConsoleException>(() => console.EvaluateAsync("=p.bar(nil)")); Assert.Equal("scribunto-lua-error-location", ex.ErrorCode); var sessionId = console.SessionId; var sessionSize = console.SessionSize; await console.ResetAsync(); Assert.Equal(sessionId, console.SessionId); Assert.True(sessionSize > console.SessionSize, "SessionSize after ResetAsync should reduce."); await TestEvaluation("=x", "nil"); }
private static void Exit(ScribuntoConsole sc) { // Stub }
private static void ShowMemory(ScribuntoConsole sc) { Console.WriteLine("Memory used / maximum allowed: {0}/{1}", sc.SessionSize, sc.SessionMaxSize); }
internal static async Task Main(string[] args) { var endPoint = args.Length > 0 ? args[0] : "https://test2.wikipedia.org/w/api.php"; using (var client = new WikiClient { ClientUserAgent = "ScribuntoConsoleTestApplication1/0.1" }) { var site = new WikiSite(client, endPoint); await site.Initialization; var sc = new ScribuntoConsole(site); await ResetSessionAsync(sc); var eofShortcut = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Ctrl + Z" : "Ctrl + D"; Console.WriteLine("* Enter any Lua expression to evaluate. EOF ({0}) to exit.", eofShortcut); Console.WriteLine("* Precede a line with '=' to evaluate it as an expression or use \x1b[36mprint()\x1b[0m. Use \x1b[36mmw.logObject()\x1b[0m for tables."); Console.WriteLine("* Use \x1b[36mmw.log()\x1b[0m and \x1b[36mmw.logObject()\x1b[0m in module code to send messages to this console."); Console.WriteLine("* Enter .help for a list of local commands.", eofShortcut); while (true) { Console.Write("> "); var l = Console.ReadLine(); if (l == null) { break; } if (string.IsNullOrWhiteSpace(l)) { continue; } if (l.StartsWith(".")) { if (string.Equals(l, ".exit", StringComparison.OrdinalIgnoreCase)) { break; } await ExecuteCommandAsync(l.Substring(1), sc); continue; } try { var result = await sc.EvaluateAsync(l); if (result.IsNewSession) { Console.WriteLine("---------- Session Cleared ----------"); } if (!string.IsNullOrEmpty(result.Output)) { Console.WriteLine(result.Output); } if (!string.IsNullOrEmpty(result.ReturnValue)) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(result.ReturnValue); Console.ResetColor(); } } catch (ScribuntoConsoleException ex) { if (!string.IsNullOrEmpty(ex.EvaluationResult.Output)) { Console.WriteLine(ex.EvaluationResult.Output); } WriteError(string.Format("{0}: {1}", ex.ErrorCode, ex.ErrorMessage)); } } } }
private static async Task ResetSessionAsync(ScribuntoConsole sc) { await sc.ResetAsync(); Console.WriteLine("Initialized Scribunto console on {0} with session ID {1}.", sc.Site.SiteInfo.SiteName, sc.SessionId); }