// Copyright 2015 Frustra. All rights reserved. // Use of this source code is governed by the MIT // license that can be found in the LICENSE file. package bbcode import ( "strings" "testing" ) var fullTestInput = `the quick brown [b]fox[/b]: [url=http://example][img]http://example.png[/img][/url]` var fullTestOutput = `the quick brown fox:
` func TestFullBasic(t *testing.T) { c := NewCompiler(false, false) input := fullTestInput output := fullTestOutput for in, out := range basicTests { input += in output += out } result := c.Compile(input) if result != output { t.Errorf("Failed to compile %s.\nExpected: %s, got: %s\n", input, output, result) } } func BenchmarkFullBasic(b *testing.B) { c := NewCompiler(false, false) input := fullTestInput for in := range basicTests { input += in } b.SetBytes(int64(len(input))) b.ReportAllocs() for i := 0; i < b.N; i++ { c.Compile(input) } } var basicTests = map[string]string{ ``: ``, `[url]http://example.com[/url]`: `http://example.com`, `[img]http://example.com[/img]`: ``, `[img][/img]`: ``, `[url=http://example.com]example[/url]`: `example`, `[img=http://example.com][/img]`: ``, `[B]bold[/b]`: `bold`, `[i]italic[/i]`: `italic`, `[u]underline[/U]`: `underline`, `[s]strikethrough[/s]`: `strikethrough`, `[u][b]something[/b] then [b]something else[/b][/u]`: `something then something else`, `blank[b][/b]`: `blank`, "test\nnewline\nnewline": `test
newline
newline`, "test\n\nnewline": `test

newline`, "[b]test[/b]\n\nnewline": `test

newline`, "[b]test\nnewline[/b]": `test
newline
`, "[code][b]some[/b]\n[i]stuff[/i]\n[/quote][/code][b]more[/b]": "
[b]some[/b]\n[i]stuff[/i]\n[/quote]
more", "[quote name=Someguy]hello[/quote]": `
Someguy said:hello
`, "[center]hello[/center]": `
hello
`, "[size=6]hello[/size]": `hello`, "[center][b][color=#00BFFF][size=6]hello[/size][/color][/b][/center]": `
hello
`, `[not a tag][/not ]`: `[not a tag][/not ]`, `[not a tag]`: `[not a tag]`, } var basicMultiArgTests = map[string][]string{ `[img=http://example.com]alt text[/img]`: []string{``}, `[img = foo]bar[/img]`: []string{``}, } func TestCompile(t *testing.T) { c := NewCompiler(false, false) for in, out := range basicTests { result := c.Compile(in) if result != out { t.Errorf("Failed to compile %s.\nExpected: %s, got: %s\n", in, out, result) } } for in, out := range basicMultiArgTests { result := c.Compile(in) if !strings.HasPrefix(result, out[0]) || !strings.HasSuffix(result, out[len(out)-1]) { t.Errorf("Failed to compile %s.\nExpected: %s, got: %s\n", in, out, result) } for i := 1; i < len(out)-1; i++ { if !strings.Contains(result, out[i]) { t.Errorf("Failed to compile %s.\nExpected: %s, got: %s\n", in, out, result) } } } } var sanitizationTests = map[string]string{ `