Add a unit test for the AsciidoctorFormatter Change-Id: Iceff1d61512b9489e3a0a2bee9e3c5b208635853
diff --git a/src/test/java/com/googlesource/gerrit/plugins/x-docs/AsciidoctorFormatterTest.java b/src/test/java/com/googlesource/gerrit/plugins/x-docs/AsciidoctorFormatterTest.java new file mode 100644 index 0000000..903c9e1 --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/x-docs/AsciidoctorFormatterTest.java
@@ -0,0 +1,78 @@ +// Copyright (C) 2016 The Android Open Source Project +// +// 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.googlesource.gerrit.plugins.xdocs.formatter; + +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertEquals; + +import com.googlesource.gerrit.plugins.xdocs.ConfigSection; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.lang.StringUtils; +import org.easymock.IAnswer; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class AsciidoctorFormatterTest { + + private ConfigSection cfg; + private AsciidoctorFormatter formatter; + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + @Before + public void setUp() throws IOException { + FormatterUtil util = createNiceMock(FormatterUtil.class); + + // To simplify things, make applyCss() a no-op and return the HTML code as-is. + expect(util.applyCss(anyString(), anyString(), anyString())).andAnswer( + new IAnswer<String>() { + @Override + public String answer() throws Throwable { + // The first argument is the HTML code. + return (String) getCurrentArguments()[0]; + } + } + ); + + replay(util); + + cfg = createNiceMock(ConfigSection.class); + + // Do not expect any behavior from the ConfigSection itself. + replay(cfg); + + Formatters formatters = createNiceMock(Formatters.class); + + // Avoid a NPE by just returning the ConfigSection mock object. + expect(formatters.getFormatterConfig(anyString(), anyString())).andReturn(cfg); + + replay(formatters); + + formatter = new AsciidoctorFormatter(tmp.newFolder(), util, formatters); + } + + @Test + public void basicTextFormattingWorks() throws IOException { + String raw = "_italic_ *bold* `monospace`"; + String formatted = "<em>italic</em> <strong>bold</strong> <code>monospace</code>"; + assertEquals(StringUtils.countMatches(formatter.format(null, null, null, null, cfg, raw), formatted), 1); + } +}