Skip to content

Commit

Permalink
Remove unit-test that RULES.md is up-to-date with Notice classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdferris-v2 committed Jun 27, 2023
1 parent d9c1ecb commit c0f8d9d
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 83 deletions.
12 changes: 0 additions & 12 deletions main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,4 @@ dependencies {
testImplementation 'com.google.truth:truth:1.0.1'
testImplementation 'com.google.truth.extensions:truth-java8-extension:1.0.1'
testImplementation 'org.mockito:mockito-core:4.5.1'
}

// A custom task to copy RULES.md into the test resource directory so that we can reference it in
// unit tests. See NoticeDocumentationTest for more details.
tasks.register('copyRulesMarkdown', Copy) {
from "$rootDir/RULES.md"
into "$projectDir/build/resources/test"
}

test {
// Make sure `copyRulesMarkdown` runs before we run any tests.
dependsOn 'copyRulesMarkdown'
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package org.mobilitydata.gtfsvalidator.validator;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import com.google.common.collect.ImmutableList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
Expand All @@ -26,37 +17,6 @@

@RunWith(JUnit4.class)
public class NoticeDocumentationTest {
private static Pattern MARKDOWN_NOTICE_SIMPLE_CLASS_NAME_ANCHOR_PATTERN =
Pattern.compile("^<a name=\"(\\w+(Error|Notice))\"/>");

private static Pattern MARKDOWN_NOTICE_CODE_HEADER_PATTERN =
Pattern.compile("^### (([a-z0-9]+_)*[a-z0-9]+)");

/**
* If this test is failing, it likely means you need to update RULES.md in the project root
* directory to include an entry for a new notice.
*/
@Test
public void testThatRulesMarkdownContainsAnchorsForAllValidationNotices() throws IOException {
Set<String> fromMarkdown = readNoticeSimpleClassNamesFromRulesMarkdown();
Set<String> fromSource =
discoverValidationNoticeClasses().map(Class::getSimpleName).collect(Collectors.toSet());

assertThat(fromMarkdown).isEqualTo(fromSource);
}

/**
* If this test is failing, it likely means you need to update RULES.md in the project root
* directory to include an entry for a new notice.
*/
@Test
public void testThatRulesMarkdownContainsHeadersForAllValidationNotices() throws IOException {
Set<String> fromMarkdown = readNoticeCodesFromRulesMarkdown();
Set<String> fromSource =
discoverValidationNoticeClasses().map(Notice::getCode).collect(Collectors.toSet());

assertThat(fromMarkdown).isEqualTo(fromSource);
}

@Test
public void testThatAllValidationNoticesAreDocumented() {
Expand Down Expand Up @@ -164,35 +124,4 @@ private static Stream<Class<Notice>> discoverValidationNoticeClasses() {
return ClassGraphDiscovery.discoverNoticeSubclasses(ClassGraphDiscovery.DEFAULT_NOTICE_PACKAGES)
.stream();
}

private static Set<String> readNoticeSimpleClassNamesFromRulesMarkdown() throws IOException {
return readValuesFromRulesMarkdown(MARKDOWN_NOTICE_SIMPLE_CLASS_NAME_ANCHOR_PATTERN);
}

private static Set<String> readNoticeCodesFromRulesMarkdown() throws IOException {
return readValuesFromRulesMarkdown(MARKDOWN_NOTICE_CODE_HEADER_PATTERN);
}

private static Set<String> readValuesFromRulesMarkdown(Pattern pattern) throws IOException {
// RULES.md is copied into the main/build/resources/test resource directory by a custom copy
// rule in the main/build.gradle file.
try (InputStream in = NoticeDocumentationTest.class.getResourceAsStream("/RULES.md")) {
// Scan lines from the markdown file, find those that match our regex pattern, and pull out
// the matching group.
return new BufferedReader(new InputStreamReader(in))
.lines()
.map(line -> maybeMatchAndExtract(pattern, line))
.flatMap(Optional::stream)
.collect(Collectors.toSet());
}
}

private static Optional<String> maybeMatchAndExtract(Pattern p, String line) {
Matcher m = p.matcher(line);
if (m.matches()) {
return Optional.of(m.group(1));
} else {
return Optional.empty();
}
}
}

0 comments on commit c0f8d9d

Please sign in to comment.