golang.org/x/tools@v0.21.0/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  		{"Markup—_especially_italic_text_—can easily be overused.",
    76  			`Markup—<i>especially italic text</i>—can easily be overused.`},
    77  		{"`go`get`'s codebase", // ascii U+0027 ' before s
    78  			`<code>go get</code>'s codebase`},
    79  		{"`go`get`’s codebase", // unicode right single quote U+2019 ’ before s
    80  			`<code>go get</code>’s codebase`},
    81  		{"a_variable_name",
    82  			`a_variable_name`},
    83  	}
    84  	for _, test := range tests {
    85  		out := font(test.in)
    86  		if out != test.out {
    87  			t.Errorf("font(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
    88  		}
    89  	}
    90  }
    91  
    92  func TestStyle(t *testing.T) {
    93  	var tests = []struct {
    94  		in  string
    95  		out string
    96  	}{
    97  		{"", ""},
    98  		{" ", " "},
    99  		{"\tx", "\tx"},
   100  		{"_a_", "<i>a</i>"},
   101  		{"*a*", "<b>a</b>"},
   102  		{"`a`", "<code>a</code>"},
   103  		{"_a_b_", "<i>a b</i>"},
   104  		{"_a__b_", "<i>a_b</i>"},
   105  		{"_a___b_", "<i>a_ b</i>"},
   106  		{"*a**b*?", "<b>a*b</b>?"},
   107  		{"_a_<>_b_.", "<i>a &lt;&gt; b</i>."},
   108  		{"(_a_<>_b_)", "(<i>a &lt;&gt; b</i>)"},
   109  		{"((_a_), _b_, _c_).", "((<i>a</i>), <i>b</i>, <i>c</i>)."},
   110  		{"(_a)", "(_a)"},
   111  	}
   112  	for _, test := range tests {
   113  		out := string(Style(test.in))
   114  		if out != test.out {
   115  			t.Errorf("style(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
   116  		}
   117  	}
   118  }
   119  
   120  func ExampleStyle() {
   121  	const s = "*Gophers* are _clearly_ > *cats*!"
   122  	fmt.Println(Style(s))
   123  	// Output: <b>Gophers</b> are <i>clearly</i> &gt; <b>cats</b>!
   124  }