github.com/vlifesystems/rulehunter@v0.0.0-20180501090014-673078aa4a83/html/html_test.go (about) 1 package html 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "testing" 8 9 "github.com/vlifesystems/rulehunter/report" 10 ) 11 12 func TestGenReportURLDir(t *testing.T) { 13 cases := []struct { 14 mode report.ModeKind 15 category string 16 title string 17 want string 18 }{ 19 {mode: report.Train, 20 category: "", 21 title: "This could be very interesting", 22 want: "reports/nocategory/this-could-be-very-interesting/train/", 23 }, 24 {mode: report.Train, 25 category: "acme or emca", 26 title: "This could be very interesting", 27 want: "reports/category/acme-or-emca/this-could-be-very-interesting/train/", 28 }, 29 {mode: report.Test, 30 category: "", 31 title: "This could be very interesting", 32 want: "reports/nocategory/this-could-be-very-interesting/test/", 33 }, 34 {mode: report.Test, 35 category: "acme or emca", 36 title: "This could be very interesting", 37 want: "reports/category/acme-or-emca/this-could-be-very-interesting/test/", 38 }, 39 } 40 for _, c := range cases { 41 got := genReportURLDir(c.mode, c.category, c.title) 42 if got != c.want { 43 t.Errorf("genReportFilename(%s, %s) got: %s, want: %s", 44 c.category, c.title, got, c.want) 45 } 46 } 47 } 48 49 func TestEscapeString(t *testing.T) { 50 cases := []struct { 51 in string 52 want string 53 }{ 54 {"This is a TITLE", 55 "this-is-a-title"}, 56 {" hello how are % you423 33 today __ --", 57 "hello-how-are-you423-33-today"}, 58 {"-- hello how are %^& you423 33 today __ --", 59 "hello-how-are-you423-33-today"}, 60 {"hello((_ how are % you423 33 today", 61 "hello-how-are-you423-33-today"}, 62 {"This is it's TITLE", 63 "this-is-its-title"}, 64 {"", ""}, 65 } 66 for _, c := range cases { 67 got := escapeString(c.in) 68 if got != c.want { 69 t.Errorf("escapeString(%s) got: %s, want: %s", c.in, got, c.want) 70 } 71 } 72 } 73 74 func TestCreatePageErrorError(t *testing.T) { 75 err := CreatePageError{ 76 Filename: "/tmp/somefilename.html", 77 Op: "execute", 78 Err: errors.New("can't write to file"), 79 } 80 want := "can't create html page for filename: /tmp/somefilename.html, can't write to file (execute)" 81 got := err.Error() 82 if got != want { 83 t.Errorf("Error - got: %s, want: %s", got, want) 84 } 85 } 86 87 /*********************************************** 88 * Helper Functions 89 ***********************************************/ 90 91 func checkFilesExist(files []string) error { 92 for _, f := range files { 93 if _, err := os.Stat(f); os.IsNotExist(err) { 94 return fmt.Errorf("file doesn't exist: %s", f) 95 } 96 } 97 return nil 98 } 99 100 func removeFiles(files []string) error { 101 for _, f := range files { 102 if err := os.Remove(f); err != nil { 103 return err 104 } 105 } 106 return nil 107 }