Debian Codegraph @

present/style_test.go

raw symbols SCIP
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package present
6
7 import (
8 "fmt"
9 "reflect"
10 "testing"
11 )
12
13 func TestSplit(t *testing.T) {
14 var tests = []struct {
15 in string
16 out []string
17 }{
18 {"", []string{}},
19 {" ", []string{" "}},
20 {"abc", []string{"abc"}},
21 {"abc def", []string{"abc", " ", "def"}},
22 {"abc def ", []string{"abc", " ", "def", " "}},
23 {"hey [[http://golang.org][Gophers]] around",
24 []string{"hey", " ", "[[http://golang.org][Gophers]]", " ", "around"}},
25 {"A [[http://golang.org/doc][two words]] link",
26 []string{"A", " ", "[[http://golang.org/doc][two words]]", " ", "link"}},
27 {"Visit [[http://golang.org/doc]] now",
28 []string{"Visit", " ", "[[http://golang.org/doc]]", " ", "now"}},
29 {"not [[http://golang.org/doc][a [[link]] ]] around",
30 []string{"not", " ", "[[http://golang.org/doc][a [[link]]", " ", "]]", " ", "around"}},
31 {"[[http://golang.org][foo bar]]",
32 []string{"[[http://golang.org][foo bar]]"}},
33 {"ends with [[http://golang.org][link]]",
34 []string{"ends", " ", "with", " ", "[[http://golang.org][link]]"}},
35 {"my talk ([[http://talks.golang.org/][slides here]])",
36 []string{"my", " ", "talk", " ", "(", "[[http://talks.golang.org/][slides here]]", ")"}},
37 }
38 for _, test := range tests {
39 out := split(test.in)
40 if !reflect.DeepEqual(out, test.out) {
41 t.Errorf("split(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
42 }
43 }
44 }
45
46 func TestFont(t *testing.T) {
47 var tests = []struct {
48 in string
49 out string
50 }{
51 {"", ""},
52 {" ", " "},
53 {"\tx", "\tx"},
54 {"_a_", "<i>a</i>"},
55 {"*a*", "<b>a</b>"},
56 {"`a`", "<code>a</code>"},
57 {"_a_b_", "<i>a b</i>"},
58 {"_a__b_", "<i>a_b</i>"},
59 {"_a___b_", "<i>a_ b</i>"},
60 {"*a**b*?", "<b>a*b</b>?"},
61 {"_a_<>_b_.", "<i>a <> b</i>."},
62 {"(_a_)", "(<i>a</i>)"},
63 {"((_a_), _b_, _c_).", "((<i>a</i>), <i>b</i>, <i>c</i>)."},
64 {"(_a)", "(_a)"},
65 {"_Why_use_scoped__ptr_? Use plain ***ptr* instead.", "<i>Why use scoped_ptr</i>? Use plain <b>*ptr</b> instead."},
66 {"_hey_ [[http://golang.org][*Gophers*]] *around*",
67 `<i>hey</i> <a href="http://golang.org" target="_blank"><b>Gophers</b></a> <b>around</b>`},
68 {"_hey_ [[http://golang.org][so _many_ *Gophers*]] *around*",
69 `<i>hey</i> <a href="http://golang.org" target="_blank">so <i>many</i> <b>Gophers</b></a> <b>around</b>`},
70 {"Visit [[http://golang.org]] now",
71 `Visit <a href="http://golang.org" target="_blank">golang.org</a> now`},
72 {"my talk ([[http://talks.golang.org/][slides here]])",
73 `my talk (<a href="http://talks.golang.org/" target="_blank">slides here</a>)`},
74 {"Markup—_especially_italic_text_—can easily be overused.",
75 `Markup—<i>especially italic text</i>—can easily be overused.`},
76 {"`go`get`'s codebase", // ascii U+0027 ' before s
77 `<code>go get</code>'s codebase`},
78 {"`go`get`’s codebase", // unicode right single quote U+2019 ’ before s
79 `<code>go get</code>’s codebase`},
80 {"a_variable_name",
81 `a_variable_name`},
82 }
83 for _, test := range tests {
84 out := font(test.in)
85 if out != test.out {
86 t.Errorf("font(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
87 }
88 }
89 }
90
91 func TestStyle(t *testing.T) {
92 var tests = []struct {
93 in string
94 out string
95 }{
96 {"", ""},
97 {" ", " "},
98 {"\tx", "\tx"},
99 {"_a_", "<i>a</i>"},
100 {"*a*", "<b>a</b>"},
101 {"`a`", "<code>a</code>"},
102 {"_a_b_", "<i>a b</i>"},
103 {"_a__b_", "<i>a_b</i>"},
104 {"_a___b_", "<i>a_ b</i>"},
105 {"*a**b*?", "<b>a*b</b>?"},
106 {"_a_<>_b_.", "<i>a &lt;&gt; b</i>."},
107 {"(_a_<>_b_)", "(<i>a &lt;&gt; b</i>)"},
108 {"((_a_), _b_, _c_).", "((<i>a</i>), <i>b</i>, <i>c</i>)."},
109 {"(_a)", "(_a)"},
110 }
111 for _, test := range tests {
112 out := string(Style(test.in))
113 if out != test.out {
114 t.Errorf("style(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
115 }
116 }
117 }
118
119 func ExampleStyle() {
120 const s = "*Gophers* are _clearly_ > *cats*!"
121 fmt.Println(Style(s))
122 // Output: <b>Gophers</b> are <i>clearly</i> &gt; <b>cats</b>!
123 }