David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.docgen; |
| 15 | |
| 16 | import static org.junit.Assert.assertEquals; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 19 | import java.util.Map; |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.junit.runner.RunWith; |
| 23 | import org.junit.runners.JUnit4; |
| 24 | |
| 25 | /** Unit tests for {@link RuleLinkExpander}. */ |
| 26 | @RunWith(JUnit4.class) |
| 27 | public class RuleLinkExpanderTest { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 28 | private RuleLinkExpander multiPageExpander; |
| 29 | private RuleLinkExpander singlePageExpander; |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 30 | |
| 31 | @Before public void setUp() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 32 | Map<String, String> index = ImmutableMap.<String, String>builder() |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 33 | .put("cc_library", "c-cpp") |
| 34 | .put("cc_binary", "c-cpp") |
| 35 | .put("java_binary", "java") |
David Chen | b0d2046 | 2016-03-01 01:17:26 +0000 | [diff] [blame] | 36 | .put("Fileset", "fileset") |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 37 | .put("proto_library", "protocol-buffer") |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 38 | .build(); |
Ulf Adams | f04cbe3 | 2016-10-28 13:23:41 +0000 | [diff] [blame] | 39 | multiPageExpander = new RuleLinkExpander("product-name", index, false); |
| 40 | singlePageExpander = new RuleLinkExpander("product-name", index, true); |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | private void checkExpandSingle(String docs, String expected) { |
| 44 | assertEquals(expected, singlePageExpander.expand(docs)); |
| 45 | } |
| 46 | |
| 47 | private void checkExpandMulti(String docs, String expected) { |
| 48 | assertEquals(expected, multiPageExpander.expand(docs)); |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test public void testRule() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 52 | checkExpandMulti( |
| 53 | "<a href=\"${link java_binary}\">java_binary rule</a>", |
| 54 | "<a href=\"java.html#java_binary\">java_binary rule</a>"); |
| 55 | checkExpandSingle( |
| 56 | "<a href=\"${link java_binary}\">java_binary rule</a>", |
| 57 | "<a href=\"#java_binary\">java_binary rule</a>"); |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Test public void testRuleAndAttribute() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 61 | checkExpandMulti( |
| 62 | "<a href=\"${link java_binary.runtime_deps}\">runtime_deps attribute</a>", |
| 63 | "<a href=\"java.html#java_binary.runtime_deps\">runtime_deps attribute</a>"); |
| 64 | checkExpandSingle( |
| 65 | "<a href=\"${link java_binary.runtime_deps}\">runtime_deps attribute</a>", |
| 66 | "<a href=\"#java_binary.runtime_deps\">runtime_deps attribute</a>"); |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 67 | } |
| 68 | |
David Chen | b0d2046 | 2016-03-01 01:17:26 +0000 | [diff] [blame] | 69 | @Test public void testUpperCaseRule() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 70 | checkExpandMulti( |
| 71 | "<a href=\"${link Fileset.entries}\">entries</a>", |
| 72 | "<a href=\"fileset.html#Fileset.entries\">entries</a>"); |
| 73 | checkExpandSingle( |
| 74 | "<a href=\"${link Fileset.entries}\">entries</a>", |
| 75 | "<a href=\"#Fileset.entries\">entries</a>"); |
David Chen | b0d2046 | 2016-03-01 01:17:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 78 | @Test public void testRuleExamples() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 79 | checkExpandMulti( |
| 80 | "<a href=\"${link cc_binary_examples}\">examples</a>", |
| 81 | "<a href=\"c-cpp.html#cc_binary_examples\">examples</a>"); |
| 82 | checkExpandSingle( |
| 83 | "<a href=\"${link cc_binary_examples}\">examples</a>", |
| 84 | "<a href=\"#cc_binary_examples\">examples</a>"); |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test public void testRuleArgs() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 88 | checkExpandMulti( |
| 89 | "<a href=\"${link cc_binary_args}\">args</a>", |
| 90 | "<a href=\"c-cpp.html#cc_binary_args\">args</a>"); |
| 91 | checkExpandSingle( |
| 92 | "<a href=\"${link cc_binary_args}\">args</a>", |
| 93 | "<a href=\"#cc_binary_args\">args</a>"); |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 94 | } |
| 95 | |
David Chen | 118ef03 | 2016-05-17 10:36:55 +0000 | [diff] [blame] | 96 | @Test public void testRuleImplicitOutputsj() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 97 | checkExpandMulti( |
| 98 | "<a href=\"${link cc_binary_implicit_outputs}\">args</a>", |
| 99 | "<a href=\"c-cpp.html#cc_binary_implicit_outputs\">args</a>"); |
| 100 | checkExpandSingle( |
| 101 | "<a href=\"${link cc_binary_implicit_outputs}\">args</a>", |
| 102 | "<a href=\"#cc_binary_implicit_outputs\">args</a>"); |
David Chen | 118ef03 | 2016-05-17 10:36:55 +0000 | [diff] [blame] | 103 | } |
| 104 | |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 105 | @Test public void testStaticPageRef() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 106 | checkExpandMulti( |
| 107 | "<a href=\"${link common-definitions}\">Common Definitions</a>", |
| 108 | "<a href=\"common-definitions.html\">Common Definitions</a>"); |
| 109 | checkExpandSingle( |
| 110 | "<a href=\"${link common-definitions}\">Common Definitions</a>", |
| 111 | "<a href=\"#common-definitions\">Common Definitions</a>"); |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Ulf Adams | f04cbe3 | 2016-10-28 13:23:41 +0000 | [diff] [blame] | 114 | @Test public void testUserManualRefIncludesProductName() { |
| 115 | checkExpandMulti( |
| 116 | "<a href=\"${link user-manual#overview}\">Link</a>", |
| 117 | "<a href=\"product-name-user-manual.html#overview\">Link</a>"); |
| 118 | checkExpandSingle( |
| 119 | "<a href=\"${link user-manual#overview}\">Link</a>", |
| 120 | "<a href=\"product-name-user-manual.html#overview\">Link</a>"); |
| 121 | } |
| 122 | |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 123 | @Test(expected = IllegalArgumentException.class) |
| 124 | public void testRefNotFound() { |
| 125 | String docs = "<a href=\"${link foo.bar}\">bar</a>"; |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 126 | multiPageExpander.expand(docs); |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 127 | } |
David Chen | 118ef03 | 2016-05-17 10:36:55 +0000 | [diff] [blame] | 128 | |
David Chen | 60af9db | 2016-08-16 08:43:43 +0000 | [diff] [blame] | 129 | @Test(expected = IllegalArgumentException.class) |
| 130 | public void testIncorrectStaticPageHeadingLink() { |
| 131 | String docs = "<a href=\"${link common-definitions.label-expansion}\">Label Expansion</a>"; |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 132 | multiPageExpander.expand(docs); |
David Chen | 60af9db | 2016-08-16 08:43:43 +0000 | [diff] [blame] | 133 | } |
| 134 | |
David Chen | 118ef03 | 2016-05-17 10:36:55 +0000 | [diff] [blame] | 135 | @Test public void testRuleHeadingLink() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 136 | checkExpandMulti( |
| 137 | "<a href=\"${link cc_library#alwayslink_lib_example}\">examples</a>", |
| 138 | "<a href=\"c-cpp.html#alwayslink_lib_example\">examples</a>"); |
| 139 | checkExpandSingle( |
| 140 | "<a href=\"${link cc_library#alwayslink_lib_example}\">examples</a>", |
| 141 | "<a href=\"#alwayslink_lib_example\">examples</a>"); |
David Chen | 118ef03 | 2016-05-17 10:36:55 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test public void testStaticPageHeadingLink() { |
David Chen | 915dbdf | 2016-08-29 10:25:17 +0000 | [diff] [blame] | 145 | checkExpandMulti( |
| 146 | "<a href=\"${link make-variables#predefined_variables.genrule.cmd}\">genrule cmd</a>", |
| 147 | "<a href=\"make-variables.html#predefined_variables.genrule.cmd\">genrule cmd</a>"); |
| 148 | checkExpandSingle( |
| 149 | "<a href=\"${link make-variables#predefined_variables.genrule.cmd}\">genrule cmd</a>", |
| 150 | "<a href=\"#predefined_variables.genrule.cmd\">genrule cmd</a>"); |
| 151 | } |
| 152 | |
| 153 | @Test public void testExpandRef() { |
| 154 | assertEquals( |
| 155 | "java.html#java_binary.runtime_deps", |
| 156 | multiPageExpander.expandRef("java_binary.runtime_deps")); |
| 157 | assertEquals( |
| 158 | "#java_binary.runtime_deps", |
| 159 | singlePageExpander.expandRef("java_binary.runtime_deps")); |
David Chen | 118ef03 | 2016-05-17 10:36:55 +0000 | [diff] [blame] | 160 | } |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 161 | } |