aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/docgen/RuleLinkExpanderTest.java
blob: fbacd0c080a9a6f5681bcdfd6220b7b91cbde4b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.docgen;

import static org.junit.Assert.assertEquals;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link RuleLinkExpander}. */
@RunWith(JUnit4.class)
public class RuleLinkExpanderTest {
  private RuleLinkExpander multiPageExpander;
  private RuleLinkExpander singlePageExpander;

  @Before public void setUp() {
    Map<String, String> index = ImmutableMap.<String, String>builder()
        .put("cc_library", "c-cpp")
        .put("cc_binary", "c-cpp")
        .put("java_binary", "java")
        .put("Fileset", "fileset")
        .put("proto_library", "protocol-buffer")
        .build();
    multiPageExpander = new RuleLinkExpander(index, false);
    singlePageExpander = new RuleLinkExpander(index, true);
  }

  private void checkExpandSingle(String docs, String expected) {
    assertEquals(expected, singlePageExpander.expand(docs));
  }

  private void checkExpandMulti(String docs, String expected) {
    assertEquals(expected, multiPageExpander.expand(docs));
  }

  @Test public void testRule() {
    checkExpandMulti(
        "<a href=\"${link java_binary}\">java_binary rule</a>",
        "<a href=\"java.html#java_binary\">java_binary rule</a>");
    checkExpandSingle(
        "<a href=\"${link java_binary}\">java_binary rule</a>",
        "<a href=\"#java_binary\">java_binary rule</a>");
  }

  @Test public void testRuleAndAttribute() {
    checkExpandMulti(
        "<a href=\"${link java_binary.runtime_deps}\">runtime_deps attribute</a>",
        "<a href=\"java.html#java_binary.runtime_deps\">runtime_deps attribute</a>");
    checkExpandSingle(
        "<a href=\"${link java_binary.runtime_deps}\">runtime_deps attribute</a>",
        "<a href=\"#java_binary.runtime_deps\">runtime_deps attribute</a>");
  }

  @Test public void testUpperCaseRule() {
    checkExpandMulti(
        "<a href=\"${link Fileset.entries}\">entries</a>",
        "<a href=\"fileset.html#Fileset.entries\">entries</a>");
    checkExpandSingle(
        "<a href=\"${link Fileset.entries}\">entries</a>",
        "<a href=\"#Fileset.entries\">entries</a>");
  }

  @Test public void testRuleExamples() {
    checkExpandMulti(
        "<a href=\"${link cc_binary_examples}\">examples</a>",
        "<a href=\"c-cpp.html#cc_binary_examples\">examples</a>");
    checkExpandSingle(
        "<a href=\"${link cc_binary_examples}\">examples</a>",
        "<a href=\"#cc_binary_examples\">examples</a>");
  }

  @Test public void testRuleArgs() {
    checkExpandMulti(
        "<a href=\"${link cc_binary_args}\">args</a>",
        "<a href=\"c-cpp.html#cc_binary_args\">args</a>");
    checkExpandSingle(
        "<a href=\"${link cc_binary_args}\">args</a>",
        "<a href=\"#cc_binary_args\">args</a>");
  }

  @Test public void testRuleImplicitOutputsj() {
    checkExpandMulti(
        "<a href=\"${link cc_binary_implicit_outputs}\">args</a>",
        "<a href=\"c-cpp.html#cc_binary_implicit_outputs\">args</a>");
    checkExpandSingle(
        "<a href=\"${link cc_binary_implicit_outputs}\">args</a>",
        "<a href=\"#cc_binary_implicit_outputs\">args</a>");
  }

  @Test public void testStaticPageRef() {
    checkExpandMulti(
        "<a href=\"${link common-definitions}\">Common Definitions</a>",
        "<a href=\"common-definitions.html\">Common Definitions</a>");
    checkExpandSingle(
        "<a href=\"${link common-definitions}\">Common Definitions</a>",
        "<a href=\"#common-definitions\">Common Definitions</a>");
  }

  @Test(expected = IllegalArgumentException.class)
  public void testRefNotFound() {
    String docs = "<a href=\"${link foo.bar}\">bar</a>";
    multiPageExpander.expand(docs);
  }

  @Test(expected = IllegalArgumentException.class)
  public void testIncorrectStaticPageHeadingLink() {
    String docs = "<a href=\"${link common-definitions.label-expansion}\">Label Expansion</a>";
    multiPageExpander.expand(docs);
  }

  @Test public void testRuleHeadingLink() {
    checkExpandMulti(
        "<a href=\"${link cc_library#alwayslink_lib_example}\">examples</a>",
        "<a href=\"c-cpp.html#alwayslink_lib_example\">examples</a>");
    checkExpandSingle(
        "<a href=\"${link cc_library#alwayslink_lib_example}\">examples</a>",
        "<a href=\"#alwayslink_lib_example\">examples</a>");
  }

  @Test public void testStaticPageHeadingLink() {
    checkExpandMulti(
        "<a href=\"${link make-variables#predefined_variables.genrule.cmd}\">genrule cmd</a>",
        "<a href=\"make-variables.html#predefined_variables.genrule.cmd\">genrule cmd</a>");
    checkExpandSingle(
        "<a href=\"${link make-variables#predefined_variables.genrule.cmd}\">genrule cmd</a>",
        "<a href=\"#predefined_variables.genrule.cmd\">genrule cmd</a>");
  }

  @Test public void testExpandRef() {
    assertEquals(
        "java.html#java_binary.runtime_deps",
        multiPageExpander.expandRef("java_binary.runtime_deps"));
    assertEquals(
        "#java_binary.runtime_deps",
        singlePageExpander.expandRef("java_binary.runtime_deps"));
  }
}