Skip to content

Commit

Permalink
Add test cases for BlurbMarkdownParser
Browse files Browse the repository at this point in the history
  • Loading branch information
georgetayqy committed May 12, 2024
1 parent 3e6ec16 commit c5cbd37
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/main/java/reposense/model/BlurbMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public BlurbMap() {
this.urlBlurbMap = new HashMap<>();
}

public Map<String, String> getAllMappings() {
return new HashMap<>(this.urlBlurbMap);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/reposense/parser/BlurbMarkdownParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public BlurbMap parse() throws IOException, InvalidMarkdownException {
counter = blurbRecord.getNextPosition();

// add the recorded entry into the BlurbMap
builder = builder.withRecord(url, blurb.toString());
// strip the trailing /n
builder = builder.withRecord(url, blurb.toString().stripTrailing());
blurb.setLength(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
},
"blurbs": {
"urlBlurbMap": {
"https://www.github.com/user/repo/branch": "first blurb\n",
"https://www.github.com/user/repo/branch2": "second blurb\n",
"https://www.github.com/user/repo/branch3": "third blurb\n"
"https://www.github.com/user/repo/branch": "first blurb",
"https://www.github.com/user/repo/branch2": "second blurb",
"https://www.github.com/user/repo/branch3": "third blurb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
},
"blurbs": {
"urlBlurbMap": {
"https://www.github.com/user/repo/branch": "first blurb\n",
"https://www.github.com/user/repo/branch2": "second blurb\n",
"https://www.github.com/user/repo/branch3": "third blurb\n"
"https://www.github.com/user/repo/branch": "first blurb",
"https://www.github.com/user/repo/branch2": "second blurb",
"https://www.github.com/user/repo/branch3": "third blurb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
},
"blurbs": {
"urlBlurbMap": {
"https://www.github.com/user/repo/branch": "first blurb\n",
"https://www.github.com/user/repo/branch2": "second blurb\n",
"https://www.github.com/user/repo/branch3": "third blurb\n"
"https://www.github.com/user/repo/branch": "first blurb",
"https://www.github.com/user/repo/branch2": "second blurb",
"https://www.github.com/user/repo/branch3": "third blurb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
},
"blurbs": {
"urlBlurbMap": {
"https://www.github.com/user/repo/branch": "first blurb\n",
"https://www.github.com/user/repo/branch2": "second blurb\n",
"https://www.github.com/user/repo/branch3": "third blurb\n"
"https://www.github.com/user/repo/branch": "first blurb",
"https://www.github.com/user/repo/branch2": "second blurb",
"https://www.github.com/user/repo/branch3": "third blurb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
},
"blurbs": {
"urlBlurbMap": {
"https://www.github.com/user/repo/branch": "first blurb\n",
"https://www.github.com/user/repo/branch2": "second blurb\n",
"https://www.github.com/user/repo/branch3": "third blurb\n"
"https://www.github.com/user/repo/branch": "first blurb",
"https://www.github.com/user/repo/branch2": "second blurb",
"https://www.github.com/user/repo/branch3": "third blurb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
},
"blurbs": {
"urlBlurbMap": {
"https://www.github.com/user/repo/branch": "first blurb\n",
"https://www.github.com/user/repo/branch2": "second blurb\n",
"https://www.github.com/user/repo/branch3": "third blurb\n"
"https://www.github.com/user/repo/branch": "first blurb",
"https://www.github.com/user/repo/branch2": "second blurb",
"https://www.github.com/user/repo/branch3": "third blurb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"blurbs": {
"urlBlurbMap": {
"https://github.com/reposense/testrepo-Alpha/tree/master": "Master branch of testrepo-Alpha\n"
"https://github.com/reposense/testrepo-Alpha/tree/master": "Master branch of testrepo-Alpha"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"blurbs": {
"urlBlurbMap": {
"https://github.com/reposense/testrepo-Alpha/tree/master": "Master branch of testrepo-Alpha\n"
"https://github.com/reposense/testrepo-Alpha/tree/master": "Master branch of testrepo-Alpha"
}
}
}
101 changes: 101 additions & 0 deletions src/test/java/reposense/parser/BlurbMarkdownParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package reposense.parser;

import static reposense.util.TestUtil.loadResource;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import reposense.parser.exceptions.InvalidMarkdownException;

public class BlurbMarkdownParserTest {
private static final Path EMPTY_BLURB_TESTER = loadResource(BlurbMarkdownParserTest.class,
"BlurbMarkdownParserTest/empty_blurbs.md");
private static final Path MALFORMED_URL_TESTER = loadResource(BlurbMarkdownParserTest.class,
"BlurbMarkdownParserTest/malformed_url_blurb.md");
private static final Path MALFORMED_DELIMITER_TESTER = loadResource(BlurbMarkdownParserTest.class,
"BlurbMarkdownParserTest/malformed_delimiter_blurb.md");
private static final Path MULTILINE_BLURB_TESTER = loadResource(BlurbMarkdownParserTest.class,
"BlurbMarkdownParserTest/multiline_blurb.md");
private static final Path MULTIPLE_BLURB_TESTER = loadResource(BlurbMarkdownParser.class,
"BlurbMarkdownParserTest/multiple_blurbs.md");

@Test
public void parse_emptyBlurbTest_throwsException() {
Assertions.assertThrows(
InvalidMarkdownException.class, () -> new BlurbMarkdownParser(EMPTY_BLURB_TESTER).parse()
);
}

@Test
public void parse_malformedUrlBlurbTest_throwsException() {
Assertions.assertThrows(
InvalidMarkdownException.class, () -> new BlurbMarkdownParser(MALFORMED_URL_TESTER).parse()
);
}

@Test
public void parse_malformedDelimiterBlurbTest_success() throws Exception {
BlurbMarkdownParser bmp = new BlurbMarkdownParser(MALFORMED_DELIMITER_TESTER);
Map<String, String> bm = bmp.parse().getAllMappings();
Assertions.assertTrue(bm.containsKey("https://github.com/reposense/testrepo-Alpha/tree/master"));
Assertions.assertEquals(
bm.get("https://github.com/reposense/testrepo-Alpha/tree/master"),
"Master branch of testrepo-Alpha\n"
+ "<!--repo--bunchofcharacters that don't count>\n"
+ "https://github.com/reposense/testrepo-Alpha/tree/master\n"
+ "Master branch of testrepo-Alpha\n"
+ "<!--repo - this is not legal and will be captured as part of the text -->"
);
}

@Test
public void parse_multilineBlurbTest_success() throws Exception {
BlurbMarkdownParser bmp = new BlurbMarkdownParser(MULTILINE_BLURB_TESTER);
Map<String, String> bm = bmp.parse().getAllMappings();
Assertions.assertTrue(bm.containsKey("https://github.com/reposense/testrepo-Alpha/tree/master"));
Assertions.assertEquals(
bm.get("https://github.com/reposense/testrepo-Alpha/tree/master"),
"Master branch of testrepo-Alpha\n"
+ "A\n"
+ "long\n"
+ "line\n"
+ "of\n"
+ "description\n"
+ "of\n"
+ "testrepo\n"
+ "Alpha"
);
}

@Test
public void parse_multipleBlurbTest_success() throws Exception {
BlurbMarkdownParser bmp = new BlurbMarkdownParser(MULTIPLE_BLURB_TESTER);
Map<String, String> bm = bmp.parse().getAllMappings();
Assertions.assertTrue(bm.keySet().containsAll(
List.of("https://github.com/reposense/testrepo-Alpha/tree/master",
"https://github.com/reposense/testrepo-Beta/tree/master",
"https://github.com/reposense/testrepo-Gamma/tree/master",
"https://github.com/reposense/testrepo-Sigma/tree/master")
));
Assertions.assertEquals(
bm.get("https://github.com/reposense/testrepo-Alpha/tree/master"),
"Master branch of testrepo-Alpha"
);
Assertions.assertEquals(
bm.get("https://github.com/reposense/testrepo-Beta/tree/master"),
"Master branch of testrepo-Beta"
);
Assertions.assertEquals(
bm.get("https://github.com/reposense/testrepo-Gamma/tree/master"),
"Master branch of testrepo-Gamma"
);
Assertions.assertEquals(
bm.get("https://github.com/reposense/testrepo-Sigma/tree/master"),
"Master branch of testrepo-Sigma"
);
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
https://github.com/reposense/testrepo-Alpha/tree/master
Master branch of testrepo-Alpha
<!--repo--bunchofcharacters that don't count>
https://github.com/reposense/testrepo-Alpha/tree/master
Master branch of testrepo-Alpha
<!--repo - this is not legal and will be captured as part of the text -->
<!--repo-->but this is legal and will be ignored
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
://github/reposense/testrepo-Alp
Malformed URL Test
10 changes: 10 additions & 0 deletions src/test/resources/BlurbMarkdownParserTest/multiline_blurb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
https://github.com/reposense/testrepo-Alpha/tree/master
Master branch of testrepo-Alpha
A
long
line
of
description
of
testrepo
Alpha
12 changes: 12 additions & 0 deletions src/test/resources/BlurbMarkdownParserTest/multiple_blurbs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
https://github.com/reposense/testrepo-Alpha/tree/master
Master branch of testrepo-Alpha
<!--repo-->
https://github.com/reposense/testrepo-Beta/tree/master
Master branch of testrepo-Beta
<!--repo-->
https://github.com/reposense/testrepo-Gamma/tree/master
Master branch of testrepo-Gamma
<!--repo-->
https://github.com/reposense/testrepo-Sigma/tree/master
Master branch of testrepo-Sigma
<!--repo-->

0 comments on commit c5cbd37

Please sign in to comment.