From c2aa3bc48d44b511ad7e832889fc0ff3d1c02156 Mon Sep 17 00:00:00 2001 From: shuangshuang <40511415+shuang6@users.noreply.github.com> Date: Thu, 15 Apr 2021 09:31:51 +0800 Subject: [PATCH] Add project unit test cases (#133) Add project unit test cases (#133) --- .../test/SourceFile/test_case/pdfMarkups.lsp | 162 +++++++++++++ .../test_case/project_no_lisp_file.prj | 16 ++ .../test_case/project_test_file.prj | 16 ++ .../test/SourceFile/test_case/test_remove.lsp | 3 + .../src/test/suite/DocumentContainer.test.ts | 5 +- .../src/test/suite/LispContainer.test.ts | 4 +- .../src/test/suite/ReadonlyDocument.test.ts | 3 +- extension/src/test/suite/project.test.ts | 218 ++++++++++++++++-- 8 files changed, 405 insertions(+), 22 deletions(-) create mode 100644 extension/src/test/SourceFile/test_case/pdfMarkups.lsp create mode 100644 extension/src/test/SourceFile/test_case/project_no_lisp_file.prj create mode 100644 extension/src/test/SourceFile/test_case/project_test_file.prj create mode 100644 extension/src/test/SourceFile/test_case/test_remove.lsp diff --git a/extension/src/test/SourceFile/test_case/pdfMarkups.lsp b/extension/src/test/SourceFile/test_case/pdfMarkups.lsp new file mode 100644 index 00000000..60e4ebb8 --- /dev/null +++ b/extension/src/test/SourceFile/test_case/pdfMarkups.lsp @@ -0,0 +1,162 @@ +; random sample file + +(defun sampleFunc (x y / a b c d) + (setq a (list 1 2 3 4) ; does this screw it all up? + d (setq b 0) + b (mapcar '+ a)) + (foreach x a + (setq d (1+ d)) + ) + + (defun SymPtrOnly () + (setq gv 32) + ) + + (defun c (a b / q) + (defun q (r j / z) + (setq z (* r j)) + ) + + (q a b) + ) +) + + +(setq some "random" ; does this trip? + global (list "variables" "to" "test") + with (vl-sort '(lambda(a b) (setq c (< a b))) global) +) + +(foreach rando global + (setq some (strcat some " " rando)) +) + + + +(defun DumpPidMarkups (/ path pdfList pdfMarkups lineList compList equpList chckList textList resp contractDrawings downloadPdfs downloadPath badFiles noMarkups) + + (defun collectMarkups ( file / pchckList pcompList pequpList plineList ptextList markups fixAnno ret ) + (setq ret nil) + (if (not (vl-catch-all-error-p (setq markups (vl-catch-all-apply 'NS:XfiniumPDF:GetAnnotations (list file 1))))) ; Hard code page 1 + (if (> (length markups) 0) + (progn + (setq markups (vl-remove-if '(lambda (a) (or (/= (type a) 'LIST) (/= (strcase (nth 3 a)) "FREETEXT") (null (nth 5 a)) (= (nth 5 a) ""))) markups)) + (setq markups (mapcar '(lambda (a) (list (vl-filename-base file) (nth 4 a) (nth 5 a) (nth 6 a))) markups)) + (if (> (length markups) 0) + (progn + (foreach anno markups + (setq fixAnno (mapcar '(lambda (l) (if (= (type l) 'STR) (acet-str-replace "\r" "-" (vl-string-trim " " (vl-string-trim (chr 9) l))) l)) anno)) + (cond + ((vl-string-search "LINENUMBER" (strcase (nth 1 fixAnno))) + (setq plineList (cons fixAnno plineList))) + ((vl-string-search "COMPONENT" (strcase (nth 1 fixAnno))) + (setq pcompList (cons fixAnno pcompList))) + ((vl-string-search "EQUIPMENT" (strcase (nth 1 fixAnno))) + (setq pequpList (cons fixAnno pequpList))) + ((and (null (vl-string-search "NOT IN SCOPE" (nth 2 fixAnno)))(>= (- (strlen (nth 2 fixAnno)) (strlen (acet-str-replace "-" "" (nth 2 fixAnno)))) 3)) + (setq pchckList (cons fixAnno pchckList))) + (t + (setq ptextList (cons fixAnno ptextList))) + ) + ) + (setq ret (list plineList pcompList pequpList pchckList ptextList)) + ) + ) + ) + ) + (setq ret (vl-filename-base file)) + ) + ret + ) + + (defun getPdfList ( pth / retList ) + (setq retList (cadr (NS:ACAD:FilePicker "Select P&IDs to export markups" "Select files" GV:ProjPath "*.pdf"))) + (cond + ((null retList) (exit)) + ((and (vl-consp retList) (= (length retList) 1) (= (car retList) "")) + (setq retList getPdfList)) + ((and (vl-consp retList) (> (length retList) 1) (vl-every 'findfile retList)) + (terpri) + (prompt (strcat (itoa (length retList)) " PDFs selected for processing"))) + ) + retList + ) + + + (if (null GV:ProjIni) (progn (NS:ACAD:MessageBox *GV:ProjIni* "Project.ini error" 0 16)(exit))) + + (setq resp (car (NS:ACAD:MessageBox "Do you want to download drawings?" "P&ID Download and Export" 3 32))) + (cond + ((= resp 2) ; Cancel + (terpri) + (prompt "P&ID Markup Export Terminated") + (exit) + ) + ((= resp 6) ; Yes + (setq contractDrawings (vl-catch-all-apply 'NS:Sharepoint:Read (list t GV:ExePath GV:ProjUrl "Contract Drawings" "ID" "Name"))) + (if (vl-catch-all-error-p contractDrawings) (progn (alert "Error reading from Sharepoint") (exit))) + (setq contractDrawings (vl-remove-if '(lambda (d) (null (vl-string-search ".pdf" (car d)))) (mapcar 'reverse contractDrawings)) + downloadPdfs (cadr (NS:ACAD:ListBox "Select PDFs to Download" "Download Drawings" (acad_strlsort (mapcar 'car contractDrawings)) t))) + (if (null downloadPdfs) (exit)) + (setq downloadPath (caadr (NS:ACAD:DirPicker "Select Download Path" "Download files" GV:ProjPath))) + (if (null downloadPath) (exit)) + (foreach pdf downloadPdfs + (setq downloadIds (cons (cadr (assoc pdf contractDrawings)) downloadIds)) + ) + (NS:SharePoint:Download GV:ExePath (cadr(assoc "SITE" GV:ProjIni)) "Contract Drawings" "ID" (mapcar 'itoa downloadIds) downloadPath) + (setq defaultPath downloadPath) + ) + ((= resp 7) ; No + (setq defaultPath GV:ProjPath)) + ) + (setq pdfList (getPdfList defaultPath)) + (if (and pdfList (> (length pdfList) 0)) + (foreach pdf pdfList + (setq pdfMarkups (collectMarkups pdf)) + (cond + ((null pdfMarkups) + (setq noMarkups (cons (vl-filename-base pdf) noMarkups))) + ((= (type pdfMarkups) 'STR) + (setq badFiles (cons pdfMarkups badFiles))) + ((listp pdfMarkups) + (if (nth 0 pdfMarkups) + (setq lineList (append (nth 0 pdfMarkups) lineList))) + (if (nth 1 pdfMarkups) + (setq compList (append (nth 1 pdfMarkups) compList))) + (if (nth 2 pdfMarkups) + (setq equpList (append (nth 2 pdfMarkups) equpList))) + (if (nth 3 pdfMarkups) + (setq chckList (append (nth 3 pdfMarkups) chckList))) + (if (nth 4 pdfMarkups) + (setq textList (append (nth 4 pdfMarkups) textList)))) + ) + ) + ) + (if lineList (IO:WriteLines (cons (list "Page" "Subject" "LineTag" "Author") lineList) (strcat (vl-filename-directory (car pdfList)) "\\Linenumber List.csv"))) + (if compList (IO:WriteLines (cons (list "Page" "Subject" "ComponentTag" "Author") compList) (strcat (vl-filename-directory (car pdfList)) "\\Components List.csv"))) + (if equpList (IO:WriteLines (cons (list "Page" "Subject" "EquipmentTag" "Author") equpList) (strcat (vl-filename-directory (car pdfList)) "\\Equipment List.csv"))) + (if chckList (IO:WriteLines (cons (list "Page" "Subject" "Contents" "Author") chckList) (strcat (vl-filename-directory (car pdfList)) "\\Review Required List.csv"))) + (if textList (IO:WriteLines (cons (list "Page" "Subject" "Contents" "Author") textList) (strcat (vl-filename-directory (car pdfList)) "\\FreeText List.csv"))) + (if badFiles (NS:ACAD:ListBox "Error reading the following files" "File Errors" (acad_strlsort badFiles) t)) + (if noMarkups (NS:ACAD:ListBox "There were no markups on the following files" "No markups" (acad_strlsort noMarkups) t)) + (terpri) + (prompt "P&ID Markup Export Complete") + (princ) + ) + + +(defun doStuff (pth / retList) + (setq retList (cadr (NS:ACAD:FilePicker "Select P&IDs to export markups" "Select files" GV:ProjPath "*.pdf" ))) + (cond + ((null retList) (exit)) + ((and (vl-consp retList) (= (length retList) 1) (= (car retList) "")) + (setq retList getPdfList) + ) + ((and (vl-consp retList) (> (length retList) 1) (vl-every 'findfile retList)) + (terpri) + (prompt (strcat (itoa (length retList)) " PDFs selected for processing")) + ) + ) + retList +) + diff --git a/extension/src/test/SourceFile/test_case/project_no_lisp_file.prj b/extension/src/test/SourceFile/test_case/project_no_lisp_file.prj new file mode 100644 index 00000000..3c12ce74 --- /dev/null +++ b/extension/src/test/SourceFile/test_case/project_no_lisp_file.prj @@ -0,0 +1,16 @@ +;;; VLisp project file [V2.0] project_no_lisp_file saved to:[d:\2\AutoLispExt\test_case] at:[4/13/2021] +(VLISP-PROJECT-LIST + :NAME + project_no_lisp_file + :OWN-LIST + nil + :FAS-DIRECTORY + nil + :TMP-DIRECTORY + nil + :PROJECT-KEYS + (:BUILD (:standard)) + :CONTEXT-ID + :AUTOLISP +) +;;; EOF \ No newline at end of file diff --git a/extension/src/test/SourceFile/test_case/project_test_file.prj b/extension/src/test/SourceFile/test_case/project_test_file.prj new file mode 100644 index 00000000..d16a1ffb --- /dev/null +++ b/extension/src/test/SourceFile/test_case/project_test_file.prj @@ -0,0 +1,16 @@ +;;; VLisp project file [V2.0] project_test_file saved to:[d:\github\AutoLispExt\test_case] at:[6/16/2020] +(VLISP-PROJECT-LIST + :NAME + project_test_file + :OWN-LIST + ("test_remove") + :FAS-DIRECTORY + nil + :TMP-DIRECTORY + nil + :PROJECT-KEYS + (:BUILD (:standard)) + :CONTEXT-ID + :AUTOLISP +) +;;; EOF \ No newline at end of file diff --git a/extension/src/test/SourceFile/test_case/test_remove.lsp b/extension/src/test/SourceFile/test_case/test_remove.lsp new file mode 100644 index 00000000..b01cc999 --- /dev/null +++ b/extension/src/test/SourceFile/test_case/test_remove.lsp @@ -0,0 +1,3 @@ + +(print "this 55 is a test22") +(command "-layer") diff --git a/extension/src/test/suite/DocumentContainer.test.ts b/extension/src/test/suite/DocumentContainer.test.ts index 2ac8afc3..b86808d6 100644 --- a/extension/src/test/suite/DocumentContainer.test.ts +++ b/extension/src/test/suite/DocumentContainer.test.ts @@ -6,8 +6,9 @@ import { LispParser } from '../../format/parser'; import { Sexpression, LispContainer } from '../../format/sexpression'; import { ReadonlyDocument } from '../../project/readOnlyDocument'; -let assert = chai.assert; -let lispFileTest = path.join(__dirname + "/../../../test_case/pdfMarkups.lsp"); +var assert = require('chai').assert; +let prefixpath = __filename + "/../../../../extension/src/test/SourceFile/test_case/"; +let lispFileTest = path.join(prefixpath + "pdfMarkups.lsp"); suite("LispParser.DocumentContainer Tests", function () { test("Original atomsForest vs DocumentContainer", function () { diff --git a/extension/src/test/suite/LispContainer.test.ts b/extension/src/test/suite/LispContainer.test.ts index ade85027..b959c5b9 100644 --- a/extension/src/test/suite/LispContainer.test.ts +++ b/extension/src/test/suite/LispContainer.test.ts @@ -5,7 +5,9 @@ import { ILispFragment, LispContainer } from '../../format/sexpression'; import { ReadonlyDocument } from '../../project/readOnlyDocument'; import { LispParser } from '../../format/parser'; -let lispFileTest = path.join(__dirname + "/../../../test_case/pdfMarkups.lsp"); +let prefixpath = __filename + "/../../../../extension/src/test/SourceFile/test_case/"; +let lispFileTest = path.join(prefixpath + "pdfMarkups.lsp"); +// let project_path = path.join(__dirname + "\\..\\..\\..\\test_case\\pdfMarkups.lsp"); let pos1: Position = new Position(98, 100); // based on line: " downloadPdfs (cadr (NS:ACAD:ListBox "Select PDFs to Download" "Download Drawings" (acad_strlsort (mapcar 'car contractDrawings)) t)))" let pos2: Position = new Position(100, 100); // based on line: " (setq downloadPath (caadr (NS:ACAD:DirPicker "Select Download Path" "Download files" GV:ProjPath)))" diff --git a/extension/src/test/suite/ReadonlyDocument.test.ts b/extension/src/test/suite/ReadonlyDocument.test.ts index 2392cc58..aebbac9d 100644 --- a/extension/src/test/suite/ReadonlyDocument.test.ts +++ b/extension/src/test/suite/ReadonlyDocument.test.ts @@ -3,7 +3,8 @@ import { TextDocument } from 'vscode'; import { ReadonlyDocument } from '../../project/readOnlyDocument'; var assert = require('chai').assert; -let lispFileTest = path.join(__dirname + "/../../../test_case/pdfMarkups.lsp"); +let prefixpath = __filename + "/../../../../extension/src/test/SourceFile/test_case/"; +let lispFileTest = path.join(prefixpath + "pdfMarkups.lsp"); suite("ReadonlyDocument Tests", function () { diff --git a/extension/src/test/suite/project.test.ts b/extension/src/test/suite/project.test.ts index f51a95ee..30546cbe 100644 --- a/extension/src/test/suite/project.test.ts +++ b/extension/src/test/suite/project.test.ts @@ -1,46 +1,51 @@ import * as path from 'path'; import { OpenProjectFile } from '../../project/openProject'; import { createProject } from '../../project/createProject'; -import { Uri } from 'vscode'; +import * as vscode from 'vscode'; +import { ProjectTreeProvider, addLispFileNode2ProjectTree } from '../../project/projectTree'; +import { AddFile2Project } from '../../project/addFile2Project'; +import { SaveProject } from '../../project/saveProject'; +import { excludeFromProject } from '../../project/excludefile'; +import * as fs from 'fs-extra'; +import { RefreshProject } from '../../project/refreshProject'; +import { getTreeItemTitle } from '../../project/projectutil'; +import { openLspFile } from '../../project/openLspFile'; +import { hasFileWithSameName } from '../../project/projectTree'; var expect = require('chai').expect; suite("Project related Tests", function () { - + let prefixpath = __filename + "/../../../../extension/src/test/SourceFile/test_case/"; + let projpath = path.join(prefixpath + "project_test_file.prj"); + let ret = vscode.Uri.file(projpath); + let rootNode= OpenProjectFile(ret); suite("Project open Tests", function () { - // Defines a Mocha unit test, test case: open nonexisting project file test("open nonexisting project file", function () { try { - OpenProjectFile(Uri.file("empty.prj")); + OpenProjectFile(vscode.Uri.file("empty.prj")); } catch (err) { expect(err.message.startsWith("Can't read project file:")).to.be.true; } }); - // Defines a Mocha unit test, test case: open an existing project file + test("open an existing project file", function () { - let project_path = path.join(__dirname + "/../../../test_case/project_test_file.prj"); - let ret = Uri.file(project_path); try { - //OpenProjectFile() is different with OpenProject() - expect(OpenProjectFile(ret).projectName).to.be.equals("project_test_file"); + expect(rootNode.projectName).to.be.equals("project_test_file"); } catch (err) { console.log(err); expect(err).to.be.equals(""); - } - + } }); }); suite("Project create Tests", function () { - // Defines a Mocha unit test, test case: Successed to create a new project test("create a new project", async function () { - let project_path = path.join(__dirname + "/../../../test_case/createPrj.prj"); + let projpath = path.join(prefixpath + "createPrj.prj"); try { - let returnValue = await createProject(project_path); - //after createproject(), need to click "create" button. So under the "__dirname" folder, which don't contains createPrj.prj + let returnValue = await createProject(projpath); expect(returnValue.projectName).to.be.equals("createPrj"); } catch (err) { @@ -49,15 +54,192 @@ suite("Project related Tests", function () { } }); - // Defines a Mocha unit test, test case: Failed to create a new project, only PRJ files are allowed. test("failed to create a new project", function () { - let project_path_err = path.join(__dirname + "/../../../test_case/createPr.pr"); + let projpath_err = path.join(prefixpath + "createPr.pr"); try { - createProject(project_path_err); + createProject(projpath_err); } catch (err) { expect(err.message.startsWith("Only PRJ files are allowed.")).to.be.true; } }); }); + + suite("Project add '.lsp' file Tests",function(){ + let destprojpath = path.join(prefixpath + "project_test_file_add.prj"); + fs.copyFileSync(projpath, destprojpath); + let ret = vscode.Uri.file(destprojpath); + let rootNode= OpenProjectFile(ret); + + test("add '.lsp' file to project", async function(){ + let fileName= path.join(prefixpath + "test_case/pdfMarkups.lsp"); + try{ + addLispFileNode2ProjectTree(rootNode, fileName, null); + expect(rootNode.sourceFiles.length).to.be.equals(2); + } + catch (err) { + expect(err).to.be.equals(""); + } + fs.removeSync(destprojpath); + }); + + test("failed to add wrong extension file to project",async function(){ + if (ProjectTreeProvider.hasProjectOpened() == true) { + let projpath = path.join(prefixpath + "add_fail_case.txt"); + let projpath1 = path.join(prefixpath + "add_fail_case.lsp.lsp"); + let projpath2 = path.join(prefixpath + "test_remove.lsp"); + let array = [projpath, projpath1, projpath2]; + for (let i=0;i<3;i++) { + let ret = vscode.Uri.file(array[i]); + let arr = new Array(); + arr.push(ret); + let prom = AddFile2Project(arr); + try{ + prom + .then(response => { + return; + }) + .catch(error => { + expect(error).to.be.equals("Only LSP files are allowed."); + }) + } + catch(err){ + expect(err).to.be.equals(""); + } + } + } + }); + }); + + suite("Project remove '.lsp' file Tests", function(){ + test("Successed to remove '.lsp' file from project", async function(){ + let destprojpath = path.join(prefixpath + "project_test_file_remove.prj"); + fs.copyFileSync(projpath, destprojpath); + let ret = vscode.Uri.file(destprojpath); + let rootNode= OpenProjectFile(ret); + ProjectTreeProvider.instance().updateData(rootNode); + try { + await excludeFromProject(rootNode.sourceFiles[0]); + expect(rootNode.sourceFiles.length).to.be.equals(0); + } + catch (err) { + console.log(err); + expect(err).to.be.equals(""); + } + fs.removeSync(destprojpath); + }); + + test("Fail to remove '.lsp' file from unopened project", async function(){ + ProjectTreeProvider.instance().updateData(null); + try{ + await excludeFromProject(rootNode.sourceFiles[0]); + } + catch (err) { + expect(err.startsWith("A project must be open")).to.be.true; + } + }); + }); + + suite("Project save project Test", function(){ + test("Successed to save project", async function(){ + let destprojpath = path.join(prefixpath + "project_test_file_add.prj"); + fs.copyFileSync(projpath, destprojpath); + let ret = vscode.Uri.file(destprojpath); + let rootNode= OpenProjectFile(ret); + ProjectTreeProvider.instance().updateData(rootNode); + let fileName= path.join(prefixpath + "pdfMarkups.lsp"); + try{ + addLispFileNode2ProjectTree(rootNode, fileName, null); + await SaveProject(true); + OpenProjectFile(ret); + expect(rootNode.sourceFiles.length).to.be.equals(2); + } + catch (err) { + expect(err).to.be.equals(""); + } + fs.removeSync(destprojpath); + }); + + test("No project to save", async function(){ + ProjectTreeProvider.instance().updateData(null); + try{ + let msg = SaveProject(true); + msg + .then(response => { + return; + }) + .catch(error => { + expect(error).to.be.equals("No project to save."); + }) + } + catch (err) { + console.log(err); + expect(err).to.be.equals(""); + } + }); + }); + + suite("Project refresh Test", function(){ + test("Successed to refresh project", function(){ + try { + RefreshProject(); + } + catch (err) { + console.log(err); + expect(err).to.be.equals(""); + } + }); + + test("file with same name", function(){ + let rootNode= OpenProjectFile(ret); + let fileName = "test_remove.lsp"; + try { + hasFileWithSameName(fileName, rootNode); + } + catch (err) { + console.log(err); + expect(err).to.be.equals(""); + } + }); + }); + + suite("Project getTreeItemTitle Test", function(){ + test("Successed to getTreeItemTitle ptoject", function(){ + let treeNode = new vscode.TreeItem(rootNode.getDisplayText()); + try { + getTreeItemTitle(treeNode); + } + catch (err) { + console.log(err); + expect(err).to.be.equals(""); + } + + }); + }); + + suite("Project open lisp file Test", function(){ + test("Successed to open lisp file", async function(){ + let clickedTreeItem = rootNode.sourceFiles[0]; + try { + await openLspFile(clickedTreeItem); + } + catch (err) { + console.log(err); + expect(err).to.be.equals(""); + } + }); + + test("Failed to open lisp file", async function(){ + let projpath = path.join(prefixpath + "project_no_lisp_file.prj"); + let ret = vscode.Uri.file(projpath); + let rootNode= OpenProjectFile(ret); + let clickedTreeItem = rootNode.sourceFiles[0]; + try{ + await openLspFile(clickedTreeItem); + } + catch (err) { + expect(err.startsWith("File doesn't exist: ")).to.be.true; + } + }); + }); });