|
| 1 | +/* |
| 2 | + * Copyright 2014-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.restdocs.asciidoctor; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | + |
| 21 | +import org.asciidoctor.Asciidoctor; |
| 22 | +import org.asciidoctor.Attributes; |
| 23 | +import org.asciidoctor.Options; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for {@link DefaultAttributesPreprocessor}. |
| 30 | + * |
| 31 | + * @author Andy Wilkinson |
| 32 | + */ |
| 33 | +public class DefaultAttributesPreprocessorTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void snippetsAttributeIsSet() { |
| 37 | + String converted = createAsciidoctor().convert("{snippets}", createOptions("projectdir=../../..")); |
| 38 | + assertThat(converted).contains("build" + File.separatorChar + "generated-snippets"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void snippetsAttributeFromConvertArgumentIsNotOverridden() { |
| 43 | + String converted = createAsciidoctor().convert("{snippets}", |
| 44 | + createOptions("snippets=custom projectdir=../../..")); |
| 45 | + assertThat(converted).contains("custom"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void snippetsAttributeFromDocumentPreambleIsNotOverridden() { |
| 50 | + String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}", |
| 51 | + createOptions("projectdir=../../..")); |
| 52 | + assertThat(converted).contains("custom"); |
| 53 | + } |
| 54 | + |
| 55 | + private Options createOptions(String attributes) { |
| 56 | + Options options = Options.builder().build(); |
| 57 | + options.setAttributes(Attributes.builder().arguments(attributes).build()); |
| 58 | + return options; |
| 59 | + } |
| 60 | + |
| 61 | + private Asciidoctor createAsciidoctor() { |
| 62 | + Asciidoctor asciidoctor = Asciidoctor.Factory.create(); |
| 63 | + asciidoctor.javaExtensionRegistry().preprocessor(new DefaultAttributesPreprocessor()); |
| 64 | + return asciidoctor; |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments