krz/devianter
A DeviantArt guest API library for Go.
clone: git clone https://gitbay.org/krz/devianter.git
v0.3.2: comments_test.go · raw
1package devianter
2
3import "testing"
4
5// Regression: flattenComment's shape check used to read m[0] and m[len(m)-1]
6// without a length check, so a comment with an empty markup body panicked with
7// index out of range and killed the caller's process.
8func TestFlattenCommentEmptyMarkup(t *testing.T) {
9 if got := flattenComment(""); got != "" {
10 t.Errorf("want an empty comment for empty markup, got %q", got)
11 }
12}
13
14func TestFlattenComment(t *testing.T) {
15 // A newer, Draft.js-encoded body is flattened to its text.
16 draft := `{"blocks":[{"text":"hello there"}]}`
17 if got := flattenComment(draft); got != "hello there" {
18 t.Errorf("want the Draft.js block text, got %q", got)
19 }
20
21 // An older, plain-HTML body passes through untouched.
22 html := "<b>hello</b> there"
23 if got := flattenComment(html); got != html {
24 t.Errorf("want plain HTML passed through, got %q", got)
25 }
26
27 // Regression: the block loop used to assign rather than accumulate, so every
28 // block but the last was silently dropped and a multi-paragraph comment came
29 // back as its closing line only.
30 multi := `{"blocks":[{"text":"first"},{"text":"second"},{"text":"third"}]}`
31 if got, want := flattenComment(multi), "first\nsecond\nthird"; got != want {
32 t.Errorf("want every block, one per line:\n got %q\nwant %q", got, want)
33 }
34
35 // An empty block is a blank line in the comment, not something to skip.
36 blank := `{"blocks":[{"text":"first"},{"text":""},{"text":"third"}]}`
37 if got, want := flattenComment(blank), "first\n\nthird"; got != want {
38 t.Errorf("want an empty block preserved as a blank line:\n got %q\nwant %q", got, want)
39 }
40
41 // Brace-shaped markup that isn't a Draft.js document falls back to itself
42 // rather than to an empty string.
43 if got := flattenComment("{}"); got != "{}" {
44 t.Errorf("want the original markup when there are no blocks, got %q", got)
45 }
46
47 // A single brace satisfies neither end of the shape check.
48 if got := flattenComment("{"); got != "{" {
49 t.Errorf("want a lone brace passed through, got %q", got)
50 }
51}