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