kendalllane | 6c60a8e | 2019-07-19 08:31:06 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 15 | package com.google.devtools.build.skydoc; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | |
| 19 | import com.google.devtools.build.skydoc.rendering.MarkdownRenderer; |
kendalllane | a3bcea0 | 2019-07-26 08:03:40 -0700 | [diff] [blame^] | 20 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AspectInfo; |
kendalllane | 6c60a8e | 2019-07-19 08:31:06 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeInfo; |
| 22 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType; |
| 23 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.FunctionParamInfo; |
| 24 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderFieldInfo; |
| 25 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo; |
| 26 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo; |
| 27 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.UserDefinedFunctionInfo; |
| 28 | import java.io.IOException; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.junit.runners.JUnit4; |
| 32 | |
| 33 | /** Java tests for MarkdownRenderer */ |
| 34 | @RunWith(JUnit4.class) |
| 35 | public final class MarkdownRendererTest { |
| 36 | |
| 37 | private final String headerTemplatePath = |
| 38 | "com/google/devtools/build/skydoc/test_templates/header.vm"; |
| 39 | private final String ruleTemplatePath = "com/google/devtools/build/skydoc/test_templates/rule.vm"; |
| 40 | private final String providerTemplatePath = |
| 41 | "com/google/devtools/build/skydoc/test_templates/provider.vm"; |
| 42 | private final String funcTemplatePath = "com/google/devtools/build/skydoc/test_templates/func.vm"; |
kendalllane | a3bcea0 | 2019-07-26 08:03:40 -0700 | [diff] [blame^] | 43 | private final String aspectTemplatePath = |
| 44 | "com/google/devtools/build/skydoc/test_templates/aspect.vm"; |
kendalllane | 6c60a8e | 2019-07-19 08:31:06 -0700 | [diff] [blame] | 45 | private final MarkdownRenderer renderer = |
| 46 | new MarkdownRenderer( |
kendalllane | a3bcea0 | 2019-07-26 08:03:40 -0700 | [diff] [blame^] | 47 | headerTemplatePath, |
| 48 | ruleTemplatePath, |
| 49 | providerTemplatePath, |
| 50 | funcTemplatePath, |
| 51 | aspectTemplatePath); |
kendalllane | 6c60a8e | 2019-07-19 08:31:06 -0700 | [diff] [blame] | 52 | |
| 53 | @Test |
| 54 | public void testHeaderStrings() throws IOException { |
| 55 | assertThat(renderer.renderMarkdownHeader()) |
| 56 | .isEqualTo("<!-- Generated with Stardoc: http://skydoc.bazel.build -->\n"); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testRuleStrings() throws IOException { |
| 61 | AttributeInfo attrInfo = |
| 62 | AttributeInfo.newBuilder() |
| 63 | .setName("first") |
| 64 | .setDocString("the first attribute") |
| 65 | .setTypeValue(AttributeType.STRING.getNumber()) |
| 66 | .build(); |
| 67 | RuleInfo ruleInfo = |
| 68 | RuleInfo.newBuilder() |
| 69 | .setRuleName("my_rule") |
| 70 | .setDocString("This rule does things.") |
| 71 | .addAttribute(attrInfo) |
| 72 | .build(); |
| 73 | |
| 74 | assertThat(renderer.render(ruleInfo.getRuleName(), ruleInfo)) |
| 75 | .isEqualTo( |
| 76 | "<a name=\"#my_rule\"></a>\n" |
| 77 | + "\n" |
| 78 | + "## my_rule\n" |
| 79 | + "\n" |
| 80 | + "<pre>\n" |
| 81 | + "my_rule(<a href=\"#my_rule-first\">first</a>)\n" |
| 82 | + "</pre>\n" |
| 83 | + "\n" |
| 84 | + "This rule does things.\n" |
| 85 | + "\n" |
| 86 | + "### Attributes\n" |
| 87 | + "\n" |
| 88 | + " <code>first</code>\n" |
| 89 | + " String; optional\n" |
| 90 | + " <p>\n" |
| 91 | + " the first attribute\n" |
| 92 | + " </p>\n"); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testProviderStrings() throws IOException { |
| 97 | ProviderFieldInfo fieldInfo = |
| 98 | ProviderFieldInfo.newBuilder().setName("one").setDocString("the first field").build(); |
| 99 | ProviderInfo providerInfo = |
| 100 | ProviderInfo.newBuilder() |
| 101 | .setProviderName("my_provider") |
| 102 | .setDocString("This provider does things.") |
| 103 | .addFieldInfo(fieldInfo) |
| 104 | .build(); |
| 105 | |
| 106 | assertThat(renderer.render(providerInfo.getProviderName(), providerInfo)) |
| 107 | .isEqualTo( |
| 108 | "<a name=\"#my_provider\"></a>\n" |
| 109 | + "\n" |
| 110 | + "## my_provider\n" |
| 111 | + "\n" |
| 112 | + "<pre>\n" |
| 113 | + "my_provider(<a href=\"#my_provider-one\">one</a>)\n" |
| 114 | + "</pre>\n" |
| 115 | + "\n" |
| 116 | + "This provider does things.\n" |
| 117 | + "\n" |
| 118 | + "### Fields\n" |
| 119 | + "\n" |
| 120 | + "<code>one</code><\n" |
| 121 | + "<p>the first field</p>\n" |
| 122 | + "\n"); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testFunctionStrings() throws IOException { |
| 127 | FunctionParamInfo paramInfo = |
| 128 | FunctionParamInfo.newBuilder() |
| 129 | .setName("param1") |
| 130 | .setDocString("the first parameter") |
| 131 | .setDefaultValue("32") |
| 132 | .build(); |
| 133 | UserDefinedFunctionInfo funcInfo = |
| 134 | UserDefinedFunctionInfo.newBuilder() |
| 135 | .setFunctionName("my_function") |
| 136 | .setDocString("This function does something.") |
| 137 | .addParameter(paramInfo) |
| 138 | .build(); |
| 139 | |
| 140 | assertThat(renderer.render(funcInfo)) |
| 141 | .isEqualTo( |
| 142 | "<a name=\"#my_function\"></a>\n" |
| 143 | + "\n" |
| 144 | + "## my_function\n" |
| 145 | + "\n" |
| 146 | + "<pre>\n" |
| 147 | + "my_function(<a href=\"#my_function-param1\">param1</a>)\n" |
| 148 | + "</pre>\n" |
| 149 | + "\n" |
| 150 | + "This function does something.\n" |
| 151 | + "\n" |
| 152 | + "### Parameters\n" |
| 153 | + "\n" |
| 154 | + " <code>param1</code>\n" |
| 155 | + " optional. default is <code>32</code>\n" |
| 156 | + " <p>\n" |
| 157 | + " the first parameter\n" |
| 158 | + " </p>\n"); |
| 159 | } |
kendalllane | a3bcea0 | 2019-07-26 08:03:40 -0700 | [diff] [blame^] | 160 | |
| 161 | @Test |
| 162 | public void testAspectStrings() throws IOException { |
| 163 | AttributeInfo attrInfo = |
| 164 | AttributeInfo.newBuilder() |
| 165 | .setName("first") |
| 166 | .setDocString("the first attribute") |
| 167 | .setTypeValue(AttributeType.STRING.getNumber()) |
| 168 | .build(); |
| 169 | AspectInfo aspectInfo = |
| 170 | AspectInfo.newBuilder() |
| 171 | .setAspectName("my_aspect") |
| 172 | .setDocString("This aspect does things.") |
| 173 | .addAttribute(attrInfo) |
| 174 | .addAspectAttribute("deps") |
| 175 | .build(); |
| 176 | |
| 177 | assertThat(renderer.render(aspectInfo.getAspectName(), aspectInfo)) |
| 178 | .isEqualTo( |
| 179 | "<a name=\"#my_aspect\"></a>\n" |
| 180 | + "\n" |
| 181 | + "## my_aspect\n" |
| 182 | + "\n" |
| 183 | + "<pre>\n" |
| 184 | + "null(<a href=\"#null-first\">first</a>)\n" |
| 185 | + "</pre>\n" |
| 186 | + "\n" |
| 187 | + "This aspect does things.\n" |
| 188 | + "\n" |
| 189 | + "### Aspect Attributes\n" |
| 190 | + "\n" |
| 191 | + " <code>deps</code><\n" |
| 192 | + " String; required.\n" |
| 193 | + "\n" |
| 194 | + "### Attributes\n" |
| 195 | + "\n" |
| 196 | + " <code>first</code>\n" |
| 197 | + " String; optional\n" |
| 198 | + " <p>\n" |
| 199 | + " the first attribute\n" |
| 200 | + " </p>\n"); |
| 201 | } |
kendalllane | 6c60a8e | 2019-07-19 08:31:06 -0700 | [diff] [blame] | 202 | } |