github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/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  }{
    48  	{
    49  		in: `Para 1.
    50  Para 1 line 2.
    51  
    52  Para 2.
    53  
    54  Section
    55  
    56  Para 3.
    57  
    58  	pre
    59  	pre1
    60  
    61  Para 4.
    62  	pre
    63  	pre2
    64  `,
    65  		out: []block{
    66  			{opPara, []string{"Para 1.\n", "Para 1 line 2.\n"}},
    67  			{opPara, []string{"Para 2.\n"}},
    68  			{opHead, []string{"Section"}},
    69  			{opPara, []string{"Para 3.\n"}},
    70  			{opPre, []string{"pre\n", "pre1\n"}},
    71  			{opPara, []string{"Para 4.\n"}},
    72  			{opPre, []string{"pre\n", "pre2\n"}},
    73  		},
    74  	},
    75  }
    76  
    77  func TestBlocks(t *testing.T) {
    78  	for i, tt := range blocksTests {
    79  		b := blocks(tt.in)
    80  		if !reflect.DeepEqual(b, tt.out) {
    81  			t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, b, tt.out)
    82  		}
    83  	}
    84  }
    85  
    86  var emphasizeTests = []struct {
    87  	in  string
    88  	out string
    89  }{
    90  	{"http://www.google.com/", `<a href="http://www.google.com/">http://www.google.com/</a>`},
    91  	{"https://www.google.com/", `<a href="https://www.google.com/">https://www.google.com/</a>`},
    92  	{"http://www.google.com/path.", `<a href="http://www.google.com/path">http://www.google.com/path</a>.`},
    93  	{"(http://www.google.com/)", `(<a href="http://www.google.com/">http://www.google.com/</a>)`},
    94  	{"Foo bar http://example.com/ quux!", `Foo bar <a href="http://example.com/">http://example.com/</a> quux!`},
    95  	{"Hello http://example.com/%2f/ /world.", `Hello <a href="http://example.com/%2f/">http://example.com/%2f/</a> /world.`},
    96  	{"Lorem http: ipsum //host/path", "Lorem http: ipsum //host/path"},
    97  	{"javascript://is/not/linked", "javascript://is/not/linked"},
    98  }
    99  
   100  func TestEmphasize(t *testing.T) {
   101  	for i, tt := range emphasizeTests {
   102  		var buf bytes.Buffer
   103  		emphasize(&buf, tt.in, nil, true)
   104  		out := buf.String()
   105  		if out != tt.out {
   106  			t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, out, tt.out)
   107  		}
   108  	}
   109  }