github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/go/doc/comment_test.go (about)

     1  // Copyright 2011 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 doc
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  var headingTests = []struct {
    15  	line string
    16  	ok   bool
    17  }{
    18  	{"Section", true},
    19  	{"A typical usage", true},
    20  	{"ΔΛΞ is Greek", true},
    21  	{"Foo 42", true},
    22  	{"", false},
    23  	{"section", false},
    24  	{"A typical usage:", false},
    25  	{"This code:", false},
    26  	{"δ is Greek", false},
    27  	{"Foo §", false},
    28  	{"Fermat's Last Sentence", true},
    29  	{"Fermat's", true},
    30  	{"'sX", false},
    31  	{"Ted 'Too' Bar", false},
    32  	{"Use n+m", false},
    33  	{"Scanning:", false},
    34  	{"N:M", false},
    35  }
    36  
    37  func TestIsHeading(t *testing.T) {
    38  	for _, tt := range headingTests {
    39  		if h := heading(tt.line); (len(h) > 0) != tt.ok {
    40  			t.Errorf("isHeading(%q) = %v, want %v", tt.line, h, tt.ok)
    41  		}
    42  	}
    43  }
    44  
    45  var blocksTests = []struct {
    46  	in   string
    47  	out  []block
    48  	text string
    49  }{
    50  	{
    51  		in: `Para 1.
    52  Para 1 line 2.
    53  
    54  Para 2.
    55  
    56  Section
    57  
    58  Para 3.
    59  
    60  	pre
    61  	pre1
    62  
    63  Para 4.
    64  
    65  	pre
    66  	pre1
    67  
    68  	pre2
    69  
    70  Para 5.
    71  
    72  
    73  	pre
    74  
    75  
    76  	pre1
    77  	pre2
    78  
    79  Para 6.
    80  	pre
    81  	pre2
    82  `,
    83  		out: []block{
    84  			{opPara, []string{"Para 1.\n", "Para 1 line 2.\n"}},
    85  			{opPara, []string{"Para 2.\n"}},
    86  			{opHead, []string{"Section"}},
    87  			{opPara, []string{"Para 3.\n"}},
    88  			{opPre, []string{"pre\n", "pre1\n"}},
    89  			{opPara, []string{"Para 4.\n"}},
    90  			{opPre, []string{"pre\n", "pre1\n", "\n", "pre2\n"}},
    91  			{opPara, []string{"Para 5.\n"}},
    92  			{opPre, []string{"pre\n", "\n", "\n", "pre1\n", "pre2\n"}},
    93  			{opPara, []string{"Para 6.\n"}},
    94  			{opPre, []string{"pre\n", "pre2\n"}},
    95  		},
    96  		text: `.   Para 1. Para 1 line 2.
    97  
    98  .   Para 2.
    99  
   100  
   101  .   Section
   102  
   103  .   Para 3.
   104  
   105  $	pre
   106  $	pre1
   107  
   108  .   Para 4.
   109  
   110  $	pre
   111  $	pre1
   112  
   113  $	pre2
   114  
   115  .   Para 5.
   116  
   117  $	pre
   118  
   119  
   120  $	pre1
   121  $	pre2
   122  
   123  .   Para 6.
   124  
   125  $	pre
   126  $	pre2
   127  `,
   128  	},
   129  }
   130  
   131  func TestBlocks(t *testing.T) {
   132  	for i, tt := range blocksTests {
   133  		b := blocks(tt.in)
   134  		if !reflect.DeepEqual(b, tt.out) {
   135  			t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, b, tt.out)
   136  		}
   137  	}
   138  }
   139  
   140  func TestToText(t *testing.T) {
   141  	var buf bytes.Buffer
   142  	for i, tt := range blocksTests {
   143  		ToText(&buf, tt.in, ".   ", "$\t", 40)
   144  		if have := buf.String(); have != tt.text {
   145  			t.Errorf("#%d: mismatch\nhave: %s\nwant: %s\nhave vs want:\n%q\n%q", i, have, tt.text, have, tt.text)
   146  		}
   147  		buf.Reset()
   148  	}
   149  }
   150  
   151  var emphasizeTests = []struct {
   152  	in, out string
   153  }{
   154  	{"", ""},
   155  	{"http://[::1]:8080/foo.txt", `<a href="http://[::1]:8080/foo.txt">http://[::1]:8080/foo.txt</a>`},
   156  	{"before (https://www.google.com) after", `before (<a href="https://www.google.com">https://www.google.com</a>) after`},
   157  	{"before https://www.google.com:30/x/y/z:b::c. After", `before <a href="https://www.google.com:30/x/y/z:b::c">https://www.google.com:30/x/y/z:b::c</a>. After`},
   158  	{"http://www.google.com/path/:;!-/?query=%34b#093124", `<a href="http://www.google.com/path/:;!-/?query=%34b#093124">http://www.google.com/path/:;!-/?query=%34b#093124</a>`},
   159  	{"http://www.google.com/path/:;!-/?query=%34bar#093124", `<a href="http://www.google.com/path/:;!-/?query=%34bar#093124">http://www.google.com/path/:;!-/?query=%34bar#093124</a>`},
   160  	{"http://www.google.com/index.html! After", `<a href="http://www.google.com/index.html">http://www.google.com/index.html</a>! After`},
   161  	{"http://www.google.com/", `<a href="http://www.google.com/">http://www.google.com/</a>`},
   162  	{"https://www.google.com/", `<a href="https://www.google.com/">https://www.google.com/</a>`},
   163  	{"http://www.google.com/path.", `<a href="http://www.google.com/path">http://www.google.com/path</a>.`},
   164  	{"http://en.wikipedia.org/wiki/Camellia_(cipher)", `<a href="http://en.wikipedia.org/wiki/Camellia_(cipher)">http://en.wikipedia.org/wiki/Camellia_(cipher)</a>`},
   165  	{"(http://www.google.com/)", `(<a href="http://www.google.com/">http://www.google.com/</a>)`},
   166  	{"http://gmail.com)", `<a href="http://gmail.com">http://gmail.com</a>)`},
   167  	{"((http://gmail.com))", `((<a href="http://gmail.com">http://gmail.com</a>))`},
   168  	{"http://gmail.com ((http://gmail.com)) ()", `<a href="http://gmail.com">http://gmail.com</a> ((<a href="http://gmail.com">http://gmail.com</a>)) ()`},
   169  	{"Foo bar http://example.com/ quux!", `Foo bar <a href="http://example.com/">http://example.com/</a> quux!`},
   170  	{"Hello http://example.com/%2f/ /world.", `Hello <a href="http://example.com/%2f/">http://example.com/%2f/</a> /world.`},
   171  	{"Lorem http: ipsum //host/path", "Lorem http: ipsum //host/path"},
   172  	{"javascript://is/not/linked", "javascript://is/not/linked"},
   173  	{"http://foo", `<a href="http://foo">http://foo</a>`},
   174  	{"art by [[https://www.example.com/person/][Person Name]]", `art by [[<a href="https://www.example.com/person/">https://www.example.com/person/</a>][Person Name]]`},
   175  	{"please visit (http://golang.org/)", `please visit (<a href="http://golang.org/">http://golang.org/</a>)`},
   176  	{"please visit http://golang.org/hello())", `please visit <a href="http://golang.org/hello()">http://golang.org/hello()</a>)`},
   177  	{"http://git.qemu.org/?p=qemu.git;a=blob;f=qapi-schema.json;hb=HEAD", `<a href="http://git.qemu.org/?p=qemu.git;a=blob;f=qapi-schema.json;hb=HEAD">http://git.qemu.org/?p=qemu.git;a=blob;f=qapi-schema.json;hb=HEAD</a>`},
   178  	{"https://foo.bar/bal/x(])", `<a href="https://foo.bar/bal/x(">https://foo.bar/bal/x(</a>])`}, // inner ] causes (]) to be cut off from URL
   179  	{"foo [ http://bar(])", `foo [ <a href="http://bar(">http://bar(</a>])`},                      // outer [ causes ]) to be cut off from URL
   180  }
   181  
   182  func TestEmphasize(t *testing.T) {
   183  	for i, tt := range emphasizeTests {
   184  		var buf bytes.Buffer
   185  		emphasize(&buf, tt.in, nil, true)
   186  		out := buf.String()
   187  		if out != tt.out {
   188  			t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, out, tt.out)
   189  		}
   190  	}
   191  }
   192  
   193  func TestCommentEscape(t *testing.T) {
   194  	commentTests := []struct {
   195  		in, out string
   196  	}{
   197  		{"typically invoked as ``go tool asm'',", "typically invoked as " + ldquo + "go tool asm" + rdquo + ","},
   198  		{"For more detail, run ``go help test'' and ``go help testflag''", "For more detail, run " + ldquo + "go help test" + rdquo + " and " + ldquo + "go help testflag" + rdquo},
   199  	}
   200  	for i, tt := range commentTests {
   201  		var buf strings.Builder
   202  		commentEscape(&buf, tt.in, true)
   203  		out := buf.String()
   204  		if out != tt.out {
   205  			t.Errorf("#%d: mismatch\nhave: %q\nwant: %q", i, out, tt.out)
   206  		}
   207  	}
   208  }