Skip to content

Commit

Permalink
fix: parent topic returns when searching in mind map tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mindolph committed Jun 23, 2023
1 parent f59be39 commit 5729de1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion code/mindmap-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ public T findNext(File baseFolder, T start,
}

boolean findPluginNote = (extrasToFind != null && !extrasToFind.isEmpty())
&& (topicFinders != null && !topicFinders.isEmpty())
&& extrasToFind.contains(Extra.ExtraType.NOTE);
&& (topicFinders != null && !topicFinders.isEmpty())
&& extrasToFind.contains(Extra.ExtraType.NOTE);
boolean findPluginFile = (extrasToFind != null && !extrasToFind.isEmpty())
&& (topicFinders != null && !topicFinders.isEmpty())
&& extrasToFind.contains(Extra.ExtraType.FILE);
&& (topicFinders != null && !topicFinders.isEmpty())
&& extrasToFind.contains(Extra.ExtraType.FILE);

T result = null;

Expand Down Expand Up @@ -527,9 +527,15 @@ public Optional<T> findFirstInTree(T parent, Predicate<T> predicate) {
List<T> children = parent.getChildren();
if (children != null) {
for (T child : children) {
if (findFirstInTree(child, predicate).isPresent()) {
if (predicate.test(child)) {
return Optional.ofNullable(child);
}
else {
Optional<T> inChildren = findFirstInTree(child, predicate);
if (inChildren.isPresent()) {
return inChildren;
}
}
}
}
return Optional.empty();
Expand All @@ -552,9 +558,9 @@ public T findTopicForLink(ExtraTopic link) {
}

/**
* @deprecated
* @param type
* @return
* @deprecated
*/
public List<T> findAllTopicsForExtraType(Extra.ExtraType type) {
List<T> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

import org.junit.Assert;
import org.junit.Test;

public class MindMapTest {
Expand All @@ -44,36 +46,36 @@ public void testMindMapParse_Error_Empty() throws Exception {

@Test(expected = IllegalArgumentException.class)
public void testMindMapParse_Error_JustTextString() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("Who is here"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("Who is here"), TestTopicNode.testTopicCreator);
}

@Test
public void testMindMapParse_Error_OnlyHeader() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("Who is here\n-"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("Who is here\n-"), TestTopicNode.testTopicCreator);
assertNull(map.getRoot());
}

@Test
public void testMindMapParse_Error_HeaderAndOnlyLevelChar() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("Who is here\n-\n#"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("Who is here\n-\n#"), TestTopicNode.testTopicCreator);
assertNull(map.getRoot());
}

@Test
public void testMindMapParse_Error_HeaderAndOnlySpacesAfterLevelChar() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("Who is here\n-\n# "), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("Who is here\n-\n# "), TestTopicNode.testTopicCreator);
assertEquals(" ", map.getRoot().getText());
}

@Test
public void testFindNext_Null() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
assertEquals("Mercury", map.findNext(null, null, Pattern.compile(Pattern.quote("cury")), true, null).getText());
}

@Test
public void testFindNext_NonNull() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
TestTopicNode base = map.findNext(null, null, Pattern.compile(Pattern.quote("cury")), true, null);
assertNull(map.findNext(null, base, Pattern.compile(Pattern.quote("cury")), true, null));
TestTopicNode earth = map.findNext(null, base, Pattern.compile(Pattern.quote("ar")), true, null);
Expand All @@ -85,13 +87,13 @@ public void testFindNext_NonNull() throws Exception {

@Test
public void testFindPrev_Null() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
assertEquals("Solar", map.findPrev(null, null, Pattern.compile(Pattern.quote("lar")), true, null).getText());
}

@Test
public void testFindPrev_NonNull() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator);
TestTopicNode base = map.findNext(null, null, Pattern.compile(Pattern.quote("Deimos")), true, null);
TestTopicNode mars = map.findPrev(null, base, Pattern.compile(Pattern.quote("ar")), true, null);
assertEquals("Mars", mars.getText());
Expand All @@ -104,50 +106,50 @@ public void testFindPrev_NonNull() throws Exception {

@Test
public void testMindMapParse_NoAttributes() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("lkf\n---\n# Hello"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("lkf\n---\n# Hello"), TestTopicNode.testTopicCreator);
assertNull(map.getAttribute("test"));
assertEquals("Hello", map.getRoot().getText());
}

@Test
public void testMindMapParse_oneAttributeDoubleNewLine() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n\n---\n# Hello"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n\n---\n# Hello"), TestTopicNode.testTopicCreator);
assertEquals("Hi", map.getAttribute("test"));
assertEquals("Hello", map.getRoot().getText());
}

@Test
public void testMindMapParse_oneAttributeSingleNewLine() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n---\n# Hello"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n---\n# Hello"), TestTopicNode.testTopicCreator);
assertEquals("Hi", map.getAttribute("test"));
assertEquals("Hello", map.getRoot().getText());
}

@Test
public void testMindMapParse_overridenAttributes_SingleNewLine() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n# Hello"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n# Hello"), TestTopicNode.testTopicCreator);
assertEquals("Lo", map.getAttribute("test"));
assertEquals("Hello", map.getRoot().getText());
}

@Test
public void testMindMapParse_overridenAttributes_SeveralNewLine() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n\n\n\n# Hello"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n\n\n\n# Hello"), TestTopicNode.testTopicCreator);
assertEquals("Lo", map.getAttribute("test"));
assertEquals("Hello", map.getRoot().getText());
}

@Test
public void testMindMapWrite_WithoutAttributes() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>();
MindMap<TestTopicNode> map = new MindMap<>();
StringWriter writer = new StringWriter();
map.write(writer);
assertEquals("Mind Map generated by Mindolph \n> __version__=`1.1`\n---\n", writer.toString());
}

@Test
public void testMindMapWrite_WithAttribute() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>();
MindMap<TestTopicNode> map = new MindMap<>();
map.setAttribute("hello", "World");
StringWriter writer = new StringWriter();
map.write(writer);
Expand All @@ -166,7 +168,7 @@ public void testIteration_TwoLevel() throws Exception {

@Test
public void testIteration_Empty() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("---\n"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("---\n"), TestTopicNode.testTopicCreator);
assertFalse(map.iterator().hasNext());
}

Expand All @@ -182,7 +184,7 @@ public void testIteration_OnlyRoot() throws Exception {

@Test
public void testIteration_OnlyRoot_WithCodeSnippetsJavaAndShell() throws Exception {
MindMap<TestTopicNode>map = new MindMap<>(new StringReader("---\n# root\n```Java\nSystem.exit(0);\n```\n```Shell\nexit\n```"), TestTopicNode.testTopicCreator);
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("---\n# root\n```Java\nSystem.exit(0);\n```\n```Shell\nexit\n```"), TestTopicNode.testTopicCreator);
assertEquals(2, map.getRoot().getCodeSnippets().size());
assertEquals("System.exit(0);\n", map.getRoot().getCodeSnippet("Java"));
assertEquals("exit\n", map.getRoot().getCodeSnippet("Shell"));
Expand All @@ -199,4 +201,28 @@ public void testSerializableDeserializable_NoErrors() throws Exception {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
assertTrue(in.readObject() instanceof MindMap);
}

@Test
public void testFindFirstInTree() throws Exception {
MindMap<TestTopicNode> map = new MindMap<>(new StringReader("""
---
# root
## child1
### child1.1
### child1.2
## child2
### child2.1
### child2.2
"""), TestTopicNode.testTopicCreator);
map.traverseTopicTree(t -> {
System.out.println(t.getText());
});
map.anyMatchInTree(t -> "child2.2".equals(t.getText()));

Optional<TestTopicNode> firstInTree = map.findFirstInTree(t -> "child2.2".equals(t.getText()));
Assert.assertNotNull(firstInTree);
Assert.assertTrue(firstInTree.isPresent());
Assert.assertEquals("child2.2", firstInTree.get().getText());
System.out.println(firstInTree.get().getText());
}
}

0 comments on commit 5729de1

Please sign in to comment.