// 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 com.google.common.truth.Truth.assertThat; 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 index = ImmutableMap.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("product-name", index, false); singlePageExpander = new RuleLinkExpander("product-name", index, true); } private void checkExpandSingle(String docs, String expected) { assertThat(singlePageExpander.expand(docs)).isEqualTo(expected); } private void checkExpandMulti(String docs, String expected) { assertThat(multiPageExpander.expand(docs)).isEqualTo(expected); } @Test public void testRule() { checkExpandMulti( "java_binary rule", "java_binary rule"); checkExpandSingle( "java_binary rule", "java_binary rule"); } @Test public void testRuleAndAttribute() { checkExpandMulti( "runtime_deps attribute", "runtime_deps attribute"); checkExpandSingle( "runtime_deps attribute", "runtime_deps attribute"); } @Test public void testUpperCaseRule() { checkExpandMulti( "entries", "entries"); checkExpandSingle( "entries", "entries"); } @Test public void testRuleExamples() { checkExpandMulti( "examples", "examples"); checkExpandSingle( "examples", "examples"); } @Test public void testRuleArgs() { checkExpandMulti( "args", "args"); checkExpandSingle( "args", "args"); } @Test public void testRuleImplicitOutputsj() { checkExpandMulti( "args", "args"); checkExpandSingle( "args", "args"); } @Test public void testStaticPageRef() { checkExpandMulti( "Common Definitions", "Common Definitions"); checkExpandSingle( "Common Definitions", "Common Definitions"); } @Test public void testUserManualRefIncludesProductName() { checkExpandMulti( "Link", "Link"); checkExpandSingle( "Link", "Link"); } @Test(expected = IllegalArgumentException.class) public void testRefNotFound() { String docs = "bar"; multiPageExpander.expand(docs); } @Test(expected = IllegalArgumentException.class) public void testIncorrectStaticPageHeadingLink() { String docs = "Label Expansion"; multiPageExpander.expand(docs); } @Test public void testRuleHeadingLink() { checkExpandMulti( "examples", "examples"); checkExpandSingle( "examples", "examples"); } @Test public void testStaticPageHeadingLink() { checkExpandMulti( "genrule cmd", "genrule cmd"); checkExpandSingle( "genrule cmd", "genrule cmd"); } @Test public void testExpandRef() { assertThat(multiPageExpander.expandRef("java_binary.runtime_deps")) .isEqualTo("java.html#java_binary.runtime_deps"); assertThat(singlePageExpander.expandRef("java_binary.runtime_deps")) .isEqualTo("#java_binary.runtime_deps"); } }