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 function to see how many characters the note has #1173

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,30 @@ private static String truncateString(@NonNull String str, @SuppressWarnings("Sam
}

/**
* Generates an excerpt of a content that does <em>not</em> match the given title
* Generates word counts and an excerpt of a content that does <em>not</em> match the given title
*
* @param content {@link String}
* @param title {@link String} In case the content starts with the title, the excerpt should be generated starting from this point
* @return excerpt String
* @return word counts,excerpt String
*/
@NonNull
public static String generateNoteExcerpt(@NonNull String content, @Nullable String title) {
// CS304 issue link: https://github.com/stefan-niedermann/nextcloud-notes/issues/1087
String wordLen = Integer.toString(content.length());
content = removeMarkdown(replaceCheckboxesWithEmojis(content.trim()));

System.out.println(content);
if (TextUtils.isEmpty(content)) {
return "";
return "Word count: 0";
}
if (!TextUtils.isEmpty(title)) {
final String trimmedTitle = removeMarkdown(replaceCheckboxesWithEmojis(title.trim()));
if (content.startsWith(trimmedTitle)) {
content = content.substring(trimmedTitle.length());
}
}
return truncateString(content.trim(), 200).replace("\n", EXCERPT_LINE_SEPARATOR);
String excerpt = truncateString(content.trim(), 200).replace("\n", EXCERPT_LINE_SEPARATOR);
return "Word count: " + wordLen + " " + excerpt;
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package it.niedermann.owncloud.notes.shared.util;

import android.os.Build;

import junit.framework.TestCase;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

/**
* Tests the NoteUtil
* Created by stefan on 06.10.15.
*/
@RunWith(RobolectricTestRunner.class)
@Config(sdk = {Build.VERSION_CODES.P})
public class NoteUtilTest extends TestCase {

@Test
public void testIsEmptyLine() {
assertTrue(NoteUtil.isEmptyLine(" "));
assertTrue(NoteUtil.isEmptyLine("\n"));
Expand All @@ -27,15 +17,13 @@ public void testIsEmptyLine() {
assertFalse(NoteUtil.isEmptyLine("a \n "));
}

@Test
public void testGetLineWithoutMarkdown() {
assertEquals("Test", NoteUtil.getLineWithoutMarkdown("Test", 0));
assertEquals("Test", NoteUtil.getLineWithoutMarkdown("\nTest", 0));
assertEquals("Foo", NoteUtil.getLineWithoutMarkdown("Foo\nBar", 0));
assertEquals("Bar", NoteUtil.getLineWithoutMarkdown("Foo\nBar", 1));
}

@Test
public void testGenerateNoteTitle() {
assertEquals("Test", NoteUtil.generateNoteTitle("Test"));
assertEquals("Test", NoteUtil.generateNoteTitle("Test\n"));
Expand All @@ -44,39 +32,21 @@ public void testGenerateNoteTitle() {
assertEquals("Test", NoteUtil.generateNoteTitle("\n\nTest"));
}

@Test
public void testGenerateNoteExcerpt() {
// title is different from content → return max. 200 characters starting with the first line which is not empty
assertEquals("Test", NoteUtil.generateNoteExcerpt("Test", "Title"));
assertEquals("Test Foo", NoteUtil.generateNoteExcerpt("Test\nFoo", "Title"));
assertEquals("Test Foo Bar", NoteUtil.generateNoteExcerpt("Test\nFoo\nBar", "Title"));
assertEquals("", NoteUtil.generateNoteExcerpt("", "Title"));

// content actually starts with title → return max. 200 characters starting with the first character after the title
assertEquals("", NoteUtil.generateNoteExcerpt("Title", "Title"));
assertEquals("Foo", NoteUtil.generateNoteExcerpt("Title\nFoo", "Title"));
assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("Title\nTitle\nBar", "Title"));
assertEquals("", NoteUtil.generateNoteExcerpt("", "Title"));

// some empty lines between the actual contents → Should be ignored
assertEquals("", NoteUtil.generateNoteExcerpt("\nTitle", "Title"));
assertEquals("Foo", NoteUtil.generateNoteExcerpt("\n\n\n\nTitle\nFoo", "Title"));
assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("\nTitle\n\n\nTitle\nBar", "\n\nTitle"));
assertEquals("", NoteUtil.generateNoteExcerpt("\n\n\n", "\nTitle"));

// content has markdown while titles markdown is already stripped
assertEquals("", NoteUtil.generateNoteExcerpt("# Title", "Title"));
assertEquals("Foo", NoteUtil.generateNoteExcerpt("Title\n- Foo", "Title"));
assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("# Title\n- Title\n- Bar", "Title"));

// title has markdown while contents markdown is stripped
assertEquals("", NoteUtil.generateNoteExcerpt("Title", "# Title"));
assertEquals("Foo", NoteUtil.generateNoteExcerpt("Title\nFoo", "- Title"));
assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("Title\nTitle\nBar", "- Title"));
// title is different from content → return max. 200 characters starting with the first line which is not empty
// CS304 issue link: https://github.com/stefan-niedermann/nextcloud-notes/issues/1087
public void testGenerateNoteExcerpt1() {
assertEquals("Word count: 4 Test", NoteUtil.generateNoteExcerpt("Test", "Title"));
assertEquals("Word count: 8 Test Foo", NoteUtil.generateNoteExcerpt("Test\nFoo", "Title"));
assertEquals("Word count: 12 Test Foo Bar", NoteUtil.generateNoteExcerpt("Test\nFoo\nBar", "Title"));
assertEquals("Word count: 0", NoteUtil.generateNoteExcerpt("", "Title"));
}

// content and title have markdown
assertEquals("", NoteUtil.generateNoteExcerpt("# Title", "# Title"));
assertEquals("Foo", NoteUtil.generateNoteExcerpt("# Title\n- Foo", "- Title"));
assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("- Title\nTitle\nBar", "- Title"));
// content actually starts with title → return max. 200 characters starting with the first character after the title
// CS304 issue link: https://github.com/stefan-niedermann/nextcloud-notes/issues/1087
public void testGenerateNoteExcerpt2() {
assertEquals("Word count: 5 ", NoteUtil.generateNoteExcerpt("Title", "Title"));
assertEquals("Word count: 9 Foo", NoteUtil.generateNoteExcerpt("Title\nFoo", "Title"));
assertEquals("Word count: 15 Title Bar", NoteUtil.generateNoteExcerpt("Title\nTitle\nBar", "Title"));
assertEquals("Word count: 0", NoteUtil.generateNoteExcerpt("", "Title"));
}
}