package html import ( "html/template" "os" "reflect" "testing" ) func Test_htmlSpecialChars(t *testing.T) { type args struct { text string flags int } tests := []struct { name string args args want string }{ { name: "t1", args: args{text: "Test", flags: EntQuotes}, want: "<a href='test'>Test</a>", }, { name: "t2", args: args{text: "Test", flags: EntCompat}, want: "<a href='test'>Test</a>", }, { name: "t3", args: args{text: "T est", flags: EntCompat | EntSpace}, want: "<a href='test'>T est</a>", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := htmlSpecialChars(tt.args.text, tt.args.flags); got != tt.want { t.Errorf("htmlSpecialChars() = %v, want %v", got, tt.want) } }) } } func Test_htmlSpecialCharsDecode(t *testing.T) { type args struct { text string flags int } tests := []struct { name string args args want string }{ { name: "t1", args: args{ text: "<a href='test'>Test</a>", flags: EntCompat, }, want: "Test", }, { name: "t2", args: args{ text: "<a href='test'>Test</a>", flags: EntQuotes, }, want: "Test", }, { name: "t3", args: args{ text: "
this -> "
\n", flags: EntNoQuotes, }, want: "this -> "
\n", }, { name: "t4", args: args{ text: "this -> "
\n", flags: EntCompat, }, want: "this -> \"
\n", }, { name: "t5", args: args{ text: "this -> "
\n", flags: EntCompat | EntSpace, }, want: "this -> \"
\n", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := htmlSpecialCharsDecode(tt.args.text, tt.args.flags); got != tt.want { t.Errorf("htmlSpecialCharsDecode() = %v, want %v", got, tt.want) } }) } } func TestStripTags(t *testing.T) { type args struct { str string allowable string } tests := []struct { name string args args want string }{ { name: "t1", args: args{ str: "pppppffff
", allowable: "", }, want: "
pppppffff
", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := StripTags(tt.args.str, tt.args.allowable); got != tt.want { t.Errorf("StripTags() = %v, want %v", got, tt.want) } }) } } func TestStripTagsX(t *testing.T) { type args struct { str string allowable string } tests := []struct { name string args args want string }{ { name: "t1", args: args{ str: "pppppffff
", allowable: "", }, want: "
pppppffff
", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := StripTagsX(tt.args.str, tt.args.allowable); got != tt.want { t.Errorf("StripTagsX() = %v, want %v", got, tt.want) } }) } } func BenchmarkStripTags(b *testing.B) { for i := 0; i < b.N; i++ { StripTags(`pppppffff
`, "") } } func BenchmarkStripTagsX(b *testing.B) { for i := 0; i < b.N; i++ { StripTagsX(`
pppppffff
`, "") } } func TestCloseHtmlTag(t *testing.T) { type args struct { str string } tests := []struct { name string args args want string }{ { name: "t1", args: args{str: `
GRANT privileges ON databasename.tablename TO 'username'@'h...`}, want: "", }, { name: "t2", args: args{str: `
", }, { name: "t3", args: args{str: func() string { s, err := os.ReadFile("/home/xing/aaa.html") if err != nil { panic(err) } return string(s) }()}, want: "", }, { name: "t5", args: args{str: ``}, want: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := CloseTag(tt.args.str); !reflect.DeepEqual(got, tt.want) { t.Errorf("CloseTag() = %v, want %v", got, tt.want) } }) } } func Test_clearTag(t *testing.T) { type args struct { s []string } tests := []struct { name string args args want []string }{ { name: "t1", args: args{s: []string{"`}, want: "
", "", "", ""}}, want: []string{"
", ""}, }, { name: "t2", args: args{s: []string{"
", "", "", "", ""}}, want: []string{""}, }, { name: "t3", args: args{s: []string{"", ""}}, want: []string{}, }, { name: "t4", args: args{s: []string{"", ""}}, want: []string{"
", ""}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := UnClosedTag(tt.args.s); !reflect.DeepEqual(got, tt.want) { t.Errorf("UnClosedTag() = %v, want %v", got, tt.want) } }) } } func TestRenderedHtml(t *testing.T) { type args struct { t *template.Template data map[string]any } tests := []struct { name string args args wantR string wantErr bool }{ { name: "t1", args: args{ t: func() *template.Template { tt, err := template.ParseFiles("./a.gohtml") if err != nil { panic(err) } return tt }(), data: map[string]any{ "xx": "oo", }, }, wantR: "oo", wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotR, err := RenderedHtml(tt.args.t, tt.args.data) if (err != nil) != tt.wantErr { t.Errorf("RenderedHtml() error = %v, wantErr %v", err, tt.wantErr) return } if gotR != tt.wantR { t.Errorf("RenderedHtml() gotR = %v, want %v", gotR, tt.wantR) } }) } }