Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for the plugin #15

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions test-docs-generator-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,57 @@
<maven-plugin-api.version>3.9.3</maven-plugin-api.version>
<maven-plugin-annotations.version>3.9.0</maven-plugin-annotations.version>
<maven-project.version>2.2.1</maven-project.version>
<hamcrest.version>2.2</hamcrest.version>
<junit.jupiter.version>5.10.2</junit.jupiter.version>
<junit.platform.version>1.10.2</junit.platform.version>
<maven.surefire.version>3.1.2</maven.surefire.version>

<!-- Allow publishing of this module -->
<maven.deploy.skip>false</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
<version>${hamcrest.version}</version>
</dependency>

<dependency>
Expand All @@ -55,4 +97,14 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<inherited>true</inherited>
<version>${maven.surefire.version}</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class Table {
* @return Markdown table in text format, returned as String
*/
public static String createTable(List<String> headers, List<String> rows) {
if (headers.isEmpty()) {
return "";
}
StringBuilder table = new StringBuilder();

table.append("|");
Expand All @@ -40,12 +43,12 @@ public static String createTable(List<String> headers, List<String> rows) {
public static String createRow(String... content) {
StringBuilder row = new StringBuilder();

row.append("| ");
for (String s : content) {
row.append("| ").append(s);
}

row.append(" |");
row.append(s).append(" | ");
}

return row.toString();
return row.substring(0, row.length() - 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob;

import io.skodjob.annotations.TestDoc;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class DocGeneratorTest {
@Test
void testCreateTableOfSteps() throws IOException {
String expectedFilePath = DocGeneratorTest.class.getClassLoader().getResource("expected-docs.md").getPath();
String generatedFilePath = "target/io/test.md";
DocGenerator.generate(TestClass.class, generatedFilePath);

assertThat(compareFiles(expectedFilePath, generatedFilePath), is(true));
}

public static class TestClass {

@TestDoc(
description = @TestDoc.Desc("Test checking that the application works as expected"),
steps = {
@TestDoc.Step(value = "Create object instance", expected = "Instance of an object is created"),
@TestDoc.Step(value = "Do a magic trick", expected = "Magic trick is done with success"),
@TestDoc.Step(value = "Clean up the test case", expected = "Everything is cleared")
},
usecases = {
@TestDoc.Usecase(id = "core")
}
)
void testMethodOne() {

}

@TestDoc(
description = @TestDoc.Desc("Test checking that the application works as expected. " +
"This is just a little bit longer line, nothing else."),
steps = {
@TestDoc.Step(value = "Create object instance", expected = "Instance of an object is created"),
@TestDoc.Step(value = "Do a magic trick", expected = "Magic trick is done with success"),
@TestDoc.Step(value = "Clean up the test case", expected = "Everything is cleared"),
@TestDoc.Step(value = "Do a magic cleanup check", expected = "Everything magically work")
},
usecases = {
@TestDoc.Usecase(id = "core"),
@TestDoc.Usecase(id = "core+"),
@TestDoc.Usecase(id = "core+++")
}
)
void testMethodTwo() {

}

@TestDoc(
description = @TestDoc.Desc("Test checking that the application works as expected. " +
"This is just a little bit longer line, nothing else."),
steps = {
},
usecases = {
}
)
void testMethodThree() {

}
}

public static boolean compareFiles(String filePath1, String filePath2) throws IOException {
byte[] file1Content = Files.readAllBytes(Paths.get(filePath1));
byte[] file2Content = Files.readAllBytes(Paths.get(filePath2));

return Arrays.equals(file1Content, file2Content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.markdown;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HeaderTest {

@Test
void testFirstLevelHeader() {
String text = "Something";
String expectedHeader = "# Something";

assertThat(expectedHeader, is(Header.firstLevelHeader(text)));
}

@Test
void testSecondLevelHeader() {
String text = "Something";
String expectedHeader = "## Something";

assertThat(expectedHeader, is(Header.secondLevelHeader(text)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.markdown;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class TableTest {

@Test
void testCreateTable() {
// Test data
List<String> headers = Arrays.asList("Name", "Age", "City");
List<String> rows = Arrays.asList("John Doe | 30 | New York", "Jane Smith | 25 | Los Angeles");

// Expected table
String expectedTable = "| Name | Age | City |\n" +
"| - | - | - |\n" +
"John Doe | 30 | New York\n" +
"Jane Smith | 25 | Los Angeles\n";

// Call the method
String actualTable = Table.createTable(headers, rows);

// Assertions
assertThat(actualTable, is(expectedTable));
}

@Test
void testCreateTableWithEmptyHeadersAndRows() {
// Test data
List<String> headers = Arrays.asList();
List<String> rows = Arrays.asList();

// Expected table
String expectedTable = "";

// Call the method
String actualTable = Table.createTable(headers, rows);

// Assertions
assertThat(actualTable, is(expectedTable));
}

@Test
void testCreateTableWithEmptyRows() {
// Test data
List<String> headers = Arrays.asList("Name", "Age");
List<String> rows = Arrays.asList();

// Expected table
String expectedTable = "| Name | Age |\n" +
"| - | - |\n";

// Call the method
String actualTable = Table.createTable(headers, rows);

// Assertions
assertThat(actualTable, is(expectedTable));
}

@Test
void testCreateTableWithSingleHeaderAndRow() {
// Test data
List<String> headers = Arrays.asList("Name");
List<String> rows = Arrays.asList("John Doe");

// Expected table
String expectedTable = "| Name |\n" +
"| - |\n" +
"John Doe\n";

// Call the method
String actualTable = Table.createTable(headers, rows);

// Assertions
assertThat(actualTable, is(expectedTable));
}

@Test
void testCreateRow() {
String item1 = "item1";
String item2 = "item2";
String item3 = "item3";
String expectedRow = String.format("| %s | %s | %s |", item1, item2, item3);

assertThat(Table.createRow(item1, item2, item3), is(expectedRow));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.markdown;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class TextListTest {

@Test
void testCreateTextList() {
// Test data
List<String> objects = Arrays.asList("Apple", "Banana", "Orange");

// Expected list
String expectedList = "* Apple\n" +
"* Banana\n" +
"* Orange\n";

// Call the method
String actualList = TextList.createUnorderedList(objects);

// Assertions
assertThat(actualList, is(expectedList));
}

@Test
void testCreateTextListWithEmptyList() {
// Test data
List<String> objects = List.of();

// Expected list
String expectedList = "";

// Call the method
String actualList = TextList.createUnorderedList(objects);

// Assertions
assertThat(actualList, is(expectedList));
}

@Test
void testCreateTextListWithSingleObject() {
// Test data
List<String> objects = List.of("Apple");

// Expected list
String expectedList = "* Apple\n";

// Call the method
String actualList = TextList.createUnorderedList(objects);

// Assertions
assertThat(actualList, is(expectedList));
}
}
Loading
Loading