github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/present/style_test.go (about) 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 {"(_a)", "(_a)"}, 66 {"_Why_use_scoped__ptr_? Use plain ***ptr* instead.", "<i>Why use scoped_ptr</i>? Use plain <b>*ptr</b> instead."}, 67 {"_hey_ [[http://golang.org][*Gophers*]] *around*", 68 `<i>hey</i> <a href="http://golang.org" target="_blank"><b>Gophers</b></a> <b>around</b>`}, 69 {"_hey_ [[http://golang.org][so _many_ *Gophers*]] *around*", 70 `<i>hey</i> <a href="http://golang.org" target="_blank">so <i>many</i> <b>Gophers</b></a> <b>around</b>`}, 71 {"Visit [[http://golang.org]] now", 72 `Visit <a href="http://golang.org" target="_blank">golang.org</a> now`}, 73 {"my talk ([[http://talks.golang.org/][slides here]])", 74 `my talk (<a href="http://talks.golang.org/" target="_blank">slides here</a>)`}, 75 } 76 for _, test := range tests { 77 out := font(test.in) 78 if out != test.out { 79 t.Errorf("font(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out) 80 } 81 } 82 } 83 84 func TestStyle(t *testing.T) { 85 var tests = []struct { 86 in string 87 out string 88 }{ 89 {"", ""}, 90 {" ", " "}, 91 {"\tx", "\tx"}, 92 {"_a_", "<i>a</i>"}, 93 {"*a*", "<b>a</b>"}, 94 {"`a`", "<code>a</code>"}, 95 {"_a_b_", "<i>a b</i>"}, 96 {"_a__b_", "<i>a_b</i>"}, 97 {"_a___b_", "<i>a_ b</i>"}, 98 {"*a**b*?", "<b>a*b</b>?"}, 99 {"_a_<>_b_.", "<i>a <> b</i>."}, 100 {"(_a_<>_b_)", "(<i>a <> b</i>)"}, 101 {"((_a_), _b_, _c_).", "((<i>a</i>), <i>b</i>, <i>c</i>)."}, 102 {"(_a)", "(_a)"}, 103 } 104 for _, test := range tests { 105 out := string(Style(test.in)) 106 if out != test.out { 107 t.Errorf("style(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out) 108 } 109 } 110 } 111 112 func ExampleStyle() { 113 const s = "*Gophers* are _clearly_ > *cats*!" 114 fmt.Println(Style(s)) 115 // Output: <b>Gophers</b> are <i>clearly</i> > <b>cats</b>! 116 }