From aa4f40ba742f187e5bd73d502513986ef8d05f10 Mon Sep 17 00:00:00 2001 From: aisensiy Date: Fri, 25 Aug 2017 11:19:26 +0800 Subject: [PATCH] slug update --- .../java/io/spring/core/article/Article.java | 2 +- .../io/spring/core/article/ArticleTest.java | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/spring/core/article/ArticleTest.java diff --git a/src/main/java/io/spring/core/article/Article.java b/src/main/java/io/spring/core/article/Article.java index 3762908..65c021a 100644 --- a/src/main/java/io/spring/core/article/Article.java +++ b/src/main/java/io/spring/core/article/Article.java @@ -57,6 +57,6 @@ public class Article { } private String toSlug(String title) { - return title.toLowerCase().replace(' ', '-'); + return title.toLowerCase().replaceAll("[\\&|[\\uFE30-\\uFFA0]|\\’|\\”|\\s\\?\\,\\.]+", "-"); } } diff --git a/src/test/java/io/spring/core/article/ArticleTest.java b/src/test/java/io/spring/core/article/ArticleTest.java new file mode 100644 index 0000000..ffff26e --- /dev/null +++ b/src/test/java/io/spring/core/article/ArticleTest.java @@ -0,0 +1,39 @@ +package io.spring.core.article; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class ArticleTest { + + @Test + public void should_get_right_slug() throws Exception { + Article article = new Article("a new title", "desc", "body", new String[]{"java"}, "123"); + assertThat(article.getSlug(), is("a-new-title")); + } + + @Test + public void should_get_right_slug_with_number_in_title() throws Exception { + Article article = new Article("a new title 2", "desc", "body", new String[]{"java"}, "123"); + assertThat(article.getSlug(), is("a-new-title-2")); + } + + @Test + public void should_get_lower_case_slug() throws Exception { + Article article = new Article("A NEW TITLE", "desc", "body", new String[]{"java"}, "123"); + assertThat(article.getSlug(), is("a-new-title")); + } + + @Test + public void should_handle_other_language() throws Exception { + Article article = new Article("中文:标题", "desc", "body", new String[]{"java"}, "123"); + assertThat(article.getSlug(), is("中文-标题")); + } + + @Test + public void should_handle_commas() throws Exception { + Article article = new Article("what?the.hell,w", "desc", "body", new String[]{"java"}, "123"); + assertThat(article.getSlug(), is("what-the-hell-w")); + } +} \ No newline at end of file