package main
import (
"strconv"
"testing"
"github.com/Azareal/Gosora/common"
)
// go test -v
// TODO: Write a test for Hello World?
type MEPair struct {
Msg string
Expects string
}
type MEPairList struct {
Items []MEPair
}
func (tlist *MEPairList) Add(msg string, expects string) {
tlist.Items = append(tlist.Items, MEPair{msg, expects})
}
func TestBBCodeRender(t *testing.T) {
//t.Skip()
err := initBbcode(common.Plugins["bbcode"])
if err != nil {
t.Fatal(err)
}
var res string
var msgList = &MEPairList{nil}
msgList.Add("", "")
msgList.Add(" ", " ")
msgList.Add(" ", " ")
msgList.Add(" ", " ")
msgList.Add("[b]", "")
msgList.Add("[b][/b]", "")
msgList.Add("hi", "hi")
msgList.Add("😀", "😀")
msgList.Add("[b]😀[/b]", "😀")
msgList.Add("[b]😀😀😀[/b]", "😀😀😀")
msgList.Add("[b]hi[/b]", "hi")
msgList.Add("[u]hi[/u]", "hi")
msgList.Add("[i]hi[/i]", "hi")
msgList.Add("[s]hi[/s]", "hi")
msgList.Add("[c]hi[/c]", "[c]hi[/c]")
if !testing.Short() {
//msgList.Add("[b]hi[/i]", "[b]hi[/i]")
//msgList.Add("[/b]hi[b]", "[/b]hi[b]")
//msgList.Add("[/b]hi[/b]", "[/b]hi[/b]")
//msgList.Add("[b][b]hi[/b]", "hi")
//msgList.Add("[b][b]hi", "[b][b]hi")
//msgList.Add("[b][b][b]hi", "[b][b][b]hi")
//msgList.Add("[/b]hi", "[/b]hi")
}
msgList.Add("[code]hi[/code]", "hi")
msgList.Add("[code][b]hi[/b][/code]", "[b]hi[/b]")
msgList.Add("[code][b]hi[/code][/b]", "[b]hi[/b]")
msgList.Add("[quote]hi[/quote]", "
hi") msgList.Add("[quote][b]hi[/b][/quote]", "
hi") msgList.Add("[quote][b]h[/b][/quote]", "
h") msgList.Add("[quote][b][/b][/quote]", "
") msgList.Add("[url][/url]", "") msgList.Add("[url]https://github.com/Azareal/Gosora[/url]", "https://github.com/Azareal/Gosora") msgList.Add("[url]http://github.com/Azareal/Gosora[/url]", "http://github.com/Azareal/Gosora") msgList.Add("[url]//github.com/Azareal/Gosora[/url]", "//github.com/Azareal/Gosora") msgList.Add("-ä½ å¥½-", "-ä½ å¥½-") msgList.Add("[i]-ä½ å¥½-[/i]", "-ä½ å¥½-") // TODO: More of these Unicode tests? Emoji, Chinese, etc.? t.Log("Testing bbcodeFullParse") for _, item := range msgList.Items { res = bbcodeFullParse(item.Msg) if res != item.Expects { t.Error("Testing string '" + item.Msg + "'") t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+item.Expects+"'") } } var msg string var expects string msg = "[rand][/rand]" expects = "[Invalid Number][rand][/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) if res != expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+expects+"'") } msg = "[rand]-1[/rand]" expects = "[No Negative Numbers][rand]-1[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) if res != expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+expects+"'") } msg = "[rand]-01[/rand]" expects = "[No Negative Numbers][rand]-01[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) if res != expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+expects+"'") } msg = "[rand]NaN[/rand]" expects = "[Invalid Number][rand]NaN[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) if res != expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+expects+"'") } msg = "[rand]Inf[/rand]" expects = "[Invalid Number][rand]Inf[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) if res != expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+expects+"'") } msg = "[rand]+[/rand]" expects = "[Invalid Number][rand]+[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) if res != expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+expects+"'") } msg = "[rand]1+1[/rand]" expects = "[Invalid Number][rand]1+1[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) if res != expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", "'"+expects+"'") } var conv int msg = "[rand]1[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) conv, err = strconv.Atoi(res) if err != nil || (conv > 1 || conv < 0) { t.Error("Bad output:", "'"+res+"'") t.Error("Expected a number in the range 0-1") } msg = "[rand]0[/rand]" t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) conv, err = strconv.Atoi(res) if err != nil || conv != 0 { t.Error("Bad output:", "'"+res+"'") t.Error("Expected the number 0") } msg = "[rand]2147483647[/rand]" // Signed 32-bit MAX t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) conv, err = strconv.Atoi(res) if err != nil || (conv > 2147483647 || conv < 0) { t.Error("Bad output:", "'"+res+"'") t.Error("Expected a number between 0 and 2147483647") } msg = "[rand]9223372036854775807[/rand]" // Signed 64-bit MAX t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) conv, err = strconv.Atoi(res) if err != nil || (conv > 9223372036854775807 || conv < 0) { t.Error("Bad output:", "'"+res+"'") t.Error("Expected a number between 0 and 9223372036854775807") } // Note: conv is commented out in these two, as these numbers overflow int msg = "[rand]18446744073709551615[/rand]" // Unsigned 64-bit MAX t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) _, err = strconv.Atoi(res) if err != nil && res != "[Invalid Number][rand]18446744073709551615[/rand]" { t.Error("Bad output:", "'"+res+"'") t.Error("Expected a number between 0 and 18446744073709551615") } msg = "[rand]170141183460469231731687303715884105727[/rand]" // Signed 128-bit MAX t.Log("Testing string '" + msg + "'") res = bbcodeFullParse(msg) _, err = strconv.Atoi(res) if err != nil && res != "[Invalid Number][rand]170141183460469231731687303715884105727[/rand]" { t.Error("Bad output:", "'"+res+"'") t.Error("Expected a number between 0 and 170141183460469231731687303715884105727") } /*t.Log("Testing bbcode_regex_parse") for _, item := range msgList { t.Log("Testing string '" + item.Msg + "'") res = bbcodeRegexParse(item.Msg) if res != item.Expects { t.Error("Bad output:", "'"+res+"'") t.Error("Expected:", item.Expects) } }*/ } func TestMarkdownRender(t *testing.T) { //t.Skip() err := initMarkdown(common.Plugins["markdown"]) if err != nil { t.Fatal(err) } var res string var msgList = &MEPairList{nil} var msgList2 = &MEPairList{nil} // TODO: Fix more of these odd cases msgList.Add("", "") msgList.Add(" ", " ") msgList.Add(" ", " ") msgList.Add(" ", " ") msgList.Add("\t", "\t") msgList.Add("\n", "\n") msgList.Add("*", "*") msgList.Add("`", "`") //msgList.Add("**", "") msgList.Add("h", "h") msgList.Add("hi", "hi") msgList.Add("**h**", "h") msgList.Add("**hi**", "hi") msgList.Add("_h_", "h") msgList.Add("_hi_", "hi") msgList.Add("*h*", "h") msgList.Add("*hi*", "hi") msgList.Add("~h~", "
hi") // TODO: Hide the backslash after escaping the item // TODO: Doesn't behave consistently with d in-front of it msgList2.Add("\\`hi`", "\\`hi`") msgList2.Add("#", "#") msgList2.Add("#h", "