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// These tests are carried forward from the old go/doc implementation.
6
7package comment
8
9import "testing"
10
11var oldHeadingTests = []struct {
12	line string
13	ok   bool
14}{
15	{"Section", true},
16	{"A typical usage", true},
17	{"ΔΛΞ is Greek", true},
18	{"Foo 42", true},
19	{"", false},
20	{"section", false},
21	{"A typical usage:", false},
22	{"This code:", false},
23	{"δ is Greek", false},
24	{"Foo §", false},
25	{"Fermat's Last Sentence", true},
26	{"Fermat's", true},
27	{"'sX", false},
28	{"Ted 'Too' Bar", false},
29	{"Use n+m", false},
30	{"Scanning:", false},
31	{"N:M", false},
32}
33
34func TestIsOldHeading(t *testing.T) {
35	for _, tt := range oldHeadingTests {
36		if isOldHeading(tt.line, []string{"Text.", "", tt.line, "", "Text."}, 2) != tt.ok {
37			t.Errorf("isOldHeading(%q) = %v, want %v", tt.line, !tt.ok, tt.ok)
38		}
39	}
40}
41
42var autoURLTests = []struct {
43	in, out string
44}{
45	{"", ""},
46	{"http://[::1]:8080/foo.txt", "http://[::1]:8080/foo.txt"},
47	{"https://www.google.com) after", "https://www.google.com"},
48	{"https://www.google.com:30/x/y/z:b::c. After", "https://www.google.com:30/x/y/z:b::c"},
49	{"http://www.google.com/path/:;!-/?query=%34b#093124", "http://www.google.com/path/:;!-/?query=%34b#093124"},
50	{"http://www.google.com/path/:;!-/?query=%34bar#093124", "http://www.google.com/path/:;!-/?query=%34bar#093124"},
51	{"http://www.google.com/index.html! After", "http://www.google.com/index.html"},
52	{"http://www.google.com/", "http://www.google.com/"},
53	{"https://www.google.com/", "https://www.google.com/"},
54	{"http://www.google.com/path.", "http://www.google.com/path"},
55	{"http://en.wikipedia.org/wiki/Camellia_(cipher)", "http://en.wikipedia.org/wiki/Camellia_(cipher)"},
56	{"http://www.google.com/)", "http://www.google.com/"},
57	{"http://gmail.com)", "http://gmail.com"},
58	{"http://gmail.com))", "http://gmail.com"},
59	{"http://gmail.com ((http://gmail.com)) ()", "http://gmail.com"},
60	{"http://example.com/ quux!", "http://example.com/"},
61	{"http://example.com/%2f/ /world.", "http://example.com/%2f/"},
62	{"http: ipsum //host/path", ""},
63	{"javascript://is/not/linked", ""},
64	{"http://foo", "http://foo"},
65	{"https://www.example.com/person/][Person Name]]", "https://www.example.com/person/"},
66	{"http://golang.org/)", "http://golang.org/"},
67	{"http://golang.org/hello())", "http://golang.org/hello()"},
68	{"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"},
69	{"https://foo.bar/bal/x(])", "https://foo.bar/bal/x"}, // inner ] causes (]) to be cut off from URL
70	{"http://bar(])", "http://bar"},                       // same
71}
72
73func TestAutoURL(t *testing.T) {
74	for _, tt := range autoURLTests {
75		url, ok := autoURL(tt.in)
76		if url != tt.out || ok != (tt.out != "") {
77			t.Errorf("autoURL(%q) = %q, %v, want %q, %v", tt.in, url, ok, tt.out, tt.out != "")
78		}
79	}
80}
81