public static JsTestCommand ParseCommand(string RelativeUrl) { var result = new JsTestCommand(); if (string.IsNullOrEmpty(RelativeUrl) || RelativeUrl == "/" || RelativeUrl == "\\") { result.Command = JsTestCommand.JsCommand.view; } else if (IsJsFile(RelativeUrl)) { result.IsJs = true; result.JsPath = RelativeUrl; } string querystring = RelativeUrl; if (querystring.IndexOf("?") > -1) { querystring = querystring.Substring(querystring.IndexOf("?") + 1); } var query = System.Web.HttpUtility.ParseQueryString(querystring); var keys = query.AllKeys; if (keys.Contains("command")) { string command = query["command"]; result.Command = (JsTestCommand.JsCommand)Enum.Parse(typeof(JsTestCommand.JsCommand), command); } if (keys.Contains("folder")) { result.Folder = query["folder"]; } if (keys.Contains("file")) { result.File = query["file"]; } if (keys.Contains("function")) { result.Function = query["function"]; } return(result); }
public static void RenderJs(RenderContext Context, JsTestOption option, string root, RenderRespnose response, JsTestCommand command) { response.ContentType = "application/javascript"; if (!string.IsNullOrEmpty(command.JsPath)) { string filename = command.JsPath.Replace("/", "\\"); if (filename.IndexOf("?") > -1) { filename = filename.Substring(0, filename.IndexOf("?")); } if (filename.StartsWith("\\")) { filename = filename.Substring(1); } string fullname = root; string prepath = option.FolderPath(Context); if (!string.IsNullOrEmpty(prepath)) { fullname = IOHelper.CombinePath(root, prepath); } fullname = IOHelper.CombinePath(fullname, filename); if (!System.IO.File.Exists(fullname)) { // This is to make sure the render of assert js... foreach (var item in option.AssertJs) { if (filename.EndsWith(item)) { fullname = System.IO.Path.Combine(Kooboo.Data.AppSettings.RootPath, "/_admin/kbtest/" + item); } } } if (System.IO.File.Exists(fullname)) { string baserelarive = GetRelative(prepath, command.JsPath); if (command.Command == JsTestCommand.JsCommand.run) { string retryurl = string.Empty; string rawfolder = filename; int lastslash = rawfolder.LastIndexOf("\\"); if (lastslash > -1) { string folder = rawfolder.Substring(0, lastslash); string jsfilename = rawfolder.Substring(lastslash + 1); retryurl = GenerateUrl(option, JsTestCommand.JsCommand.run, folder, jsfilename); } // TODO: Render the k commands. var alltext = IOHelper.ReadAllText(fullname); alltext = RenderServerSide(alltext, root, Context, baserelarive); response.Body = RenderJs(option, alltext, command.Function, retryurl); } else { var alltext = IOHelper.ReadAllText(fullname); alltext = RenderServerSide(alltext, root, Context, baserelarive); response.Body = alltext; } } } }
public static void RenderTestView(RenderContext Context, JsTestOption option, RenderRespnose response, JsTestCommand command) { if (string.IsNullOrEmpty(command.Folder) || command.Folder == "\\" || command.Folder == "/") { // find the start html. var starthtml = JsTestHelper.GetStartHtml(Context, option); string url = GenerateUrl(option, JsTestCommand.JsCommand.run, null, null); var folders = JsTestHelper.ListTestFolders(Context, option); var count = JsTestHelper.CountTest(Context, option, folders.ToList()); string html = "<h3><a href='" + url + "'>run all tests</a> (" + count.ToString() + " tests)</h3>\r\n"; html += "<ul>"; foreach (var item in folders) { var files = JsTestHelper.ListAllTestFiles(Context, option, item); int testcount = JsTestHelper.CountTest(Context, option, item); if (testcount > 0) { html += "<li>"; url = GenerateUrl(option, JsTestCommand.JsCommand.run, item, null); html += "<a href='" + url + "'>run</a> || "; url = GenerateUrl(option, JsTestCommand.JsCommand.view, item, null); html += "<a href='" + url + "'>view</a> || "; html += files.Count.ToString() + " files || "; html += testcount.ToString() + " tests || Folder: "; html += item; html += "</li>"; } } html += "</ul>"; string output = starthtml.Replace(JsTestHelper.PlaceHolder, html); string info = JsTestHelper.GetInfoHtml(Context, option); output = output.Replace("<div id=\"information\"></div>", info); response.Body = output; } else { if (string.IsNullOrEmpty(command.File)) { // view folder... var starthtml = JsTestHelper.GetStartHtml(Context, option); var files = JsTestHelper.ListFolderFiles(Context, option, command.Folder); var count = JsTestHelper.CountTest(Context, option, command.Folder); string url = GenerateUrl(option, JsTestCommand.JsCommand.run, command.Folder, null); string html = "<h3><a href='" + url + "'>run all tests</a> (" + count.ToString() + " tests)</h3>\r\n"; html += "<ul>"; foreach (var item in files) { var functions = JsTestHelper.ListFileFunctions(Context, option, item.Folder, item.file); html += "<li>"; url = GenerateUrl(option, JsTestCommand.JsCommand.run, item.Folder, item.file); html += "<a href='" + url + "'>run</a> || "; url = GenerateUrl(option, JsTestCommand.JsCommand.view, item.Folder, item.file); html += "<a href='" + url + "'>view</a> || "; html += functions.Count.ToString() + " tests || File: " + System.IO.Path.Combine(item.Folder, item.file); html += "</li>"; } html += "</ul>"; response.Body = starthtml.Replace(JsTestHelper.PlaceHolder, html); } else { //view file. var starthtml = JsTestHelper.GetStartHtml(Context, option); string url = GenerateUrl(option, JsTestCommand.JsCommand.run, command.Folder, command.File); var functions = JsTestHelper.ListFileFunctions(Context, option, command.Folder, command.File); string html = "<h3><a href='" + url + "'>run all tests</a> (" + functions.Count.ToString() + " tests)</h3>\r\n"; html += "<ul>"; foreach (var item in functions) { url = GenerateUrl(option, JsTestCommand.JsCommand.run, command.Folder, command.File, item); html += "<li><a href='" + url + "'>run</a> || Test: " + item + "</li>"; } html += "</ul>"; response.Body = starthtml.Replace(JsTestHelper.PlaceHolder, html); } } }
private static void RenderTestRun(RenderContext Context, JsTestOption option, RenderRespnose response, JsTestCommand command) { var starthtml = JsTestHelper.GetStartHtml(Context, option); string html = "<div>"; html += "\r\n<script src='/_admin/kbtest/expect.js'></script>"; html += "\r\n<script src='/_admin/kbtest/mock.js'></script>"; var references = JsTestHelper.GetReferenceJs(Context, option, command.Folder); foreach (var item in references) { html += "\r\n<script src='" + item + "'></script>"; } if (string.IsNullOrEmpty(command.Folder) || command.Folder == "\\" || command.Folder == "/") { // run all. var allfiles = JsTestHelper.ListAllTestFiles(Context, option, null); foreach (var file in allfiles) { string fileurl = GeneratejsFile(option, file, JsTestCommand.JsCommand.run); html += "\r\n<script src='" + fileurl + "'></script>"; } } else if (string.IsNullOrEmpty(command.File)) { // run folder. var allfiles = JsTestHelper.ListAllTestFiles(Context, option, command.Folder); foreach (var file in allfiles) { string fileurl = GeneratejsFile(option, file, JsTestCommand.JsCommand.run); html += "\r\n<script src='" + fileurl + "'></script>"; } } else { if (string.IsNullOrEmpty(command.Function)) { // run file. string filename = Lib.Helper.IOHelper.CombinePath(command.Folder, command.File); string fileurl = GeneratejsFile(option, filename, JsTestCommand.JsCommand.run); html += "\r\n<script src='" + fileurl + "'></script>"; } else { // run function. string filename = Lib.Helper.IOHelper.CombinePath(command.Folder, command.File); html += "<div><h4>You are running one unit test, open console to view any errors</h4></div>"; string fileurl = GeneratejsFile(option, filename, JsTestCommand.JsCommand.run, command.Function); html += "\r\n<script src='" + fileurl + "'></script>"; } } html += "</div>"; response.Body = starthtml.Replace(JsTestHelper.PlaceHolder, html); }