Skip to content

Commit

Permalink
Added unit tests for SlugBuilder.
Browse files Browse the repository at this point in the history
Had to add separator field, so that can keep track of how long the separator is
and fix bug that occurs with long separator Strings.
  • Loading branch information
eddarmitage committed Feb 7, 2016
1 parent 873ce0e commit b0cd47e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/com/eddarmitage/slugger/SlugBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,27 @@ class SlugBuilder {

private final OptionalInt targetLength;
private final boolean enforceHardLimit;
private final CharSequence separator;
private final StringJoiner joiner;

SlugBuilder(CharSequence separator) {
this.targetLength = OptionalInt.empty();
this.enforceHardLimit = false;
this.separator = separator;
this.joiner = new StringJoiner(separator);
}

SlugBuilder(OptionalInt targetLength, boolean enforceHardLimit, CharSequence separator) {
this.targetLength = targetLength;
this.enforceHardLimit = enforceHardLimit;
this.separator = separator;
joiner = new StringJoiner(separator);
}

private SlugBuilder(OptionalInt targetLength, boolean enforceHardLimit, StringJoiner joiner) {
private SlugBuilder(OptionalInt targetLength, boolean enforceHardLimit, CharSequence separator, StringJoiner joiner) {
this.targetLength = targetLength;
this.enforceHardLimit = enforceHardLimit;
this.separator = separator;
this.joiner = joiner;
}

Expand All @@ -56,7 +60,7 @@ public void addWord(String word) {
}

public SlugBuilder merge(SlugBuilder other) {
return new SlugBuilder(targetLength, enforceHardLimit, joiner.merge(other.joiner));
return new SlugBuilder(targetLength, enforceHardLimit, separator, joiner.merge(other.joiner));
}

public String build() {
Expand All @@ -72,7 +76,7 @@ private void addFirstWord(String word) {
}

private boolean wordWillFit(String word) {
return (targetLength.isPresent() && joiner.length() + word.length() <= targetLength.getAsInt()) || !targetLength.isPresent();
return !targetLength.isPresent() || joiner.length() + word.length() + separator.length() <= targetLength.getAsInt();
}

public static Collector<String, SlugBuilder, String> collector(OptionalInt targetLength, boolean enforceHardLimit, CharSequence separator) {
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/com/eddarmitage/slugger/SlugBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.eddarmitage.slugger;

import org.assertj.core.api.SoftAssertions;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.OptionalInt;

import static org.assertj.core.api.Assertions.assertThat;

public class SlugBuilderTest {

@Before
public void setup() {

}

@Test
public void testSlugBuilder_withDefaultConfiguration() {
SlugBuilder slugBuilder = new SlugBuilder("-");
Arrays.asList("one", "two", "three").forEach(slugBuilder::addWord);

assertThat(slugBuilder.build()).isEqualTo("one-two-three");
}

@Test
public void testSlugBuilder_withLengthRestriction() {
SlugBuilder slugBuilder = new SlugBuilder(OptionalInt.of(10), false, "-");
Arrays.asList("one", "two", "three").forEach(slugBuilder::addWord);

assertThat(slugBuilder.build()).isEqualTo("one-two");
}

@Test
public void testSlugBuilder_withLengthRestrictionAndLongSeparator() {
SlugBuilder slugBuilder = new SlugBuilder(OptionalInt.of(10), false, "-----");
Arrays.asList("one", "two", "three").forEach(slugBuilder::addWord);

assertThat(slugBuilder.build()).isEqualTo("one");
}

@Test
public void testSlugBuilder_withLongFirstWord() {
SlugBuilder slugBuilder = new SlugBuilder(OptionalInt.of(10), false, "-");
slugBuilder.addWord("refrigerator");

assertThat(slugBuilder.build()).isEqualTo("refrigerator");
}

@Test
public void testSlugBuilder_withLongFirstWordAndHardLimitsEnforced() {
SlugBuilder slugBuilder = new SlugBuilder(OptionalInt.of(10), true, "-");
slugBuilder.addWord("refrigerator");

assertThat(slugBuilder.build()).isEqualTo("refrigerat");
}

@Test
public void testSlugBuilder_isMergedWithOtherSlugBuilder() {
SlugBuilder firstBuilder = new SlugBuilder("-");
Arrays.asList("one", "two").forEach(firstBuilder::addWord);

SlugBuilder secondBuilder = new SlugBuilder("-");
Arrays.asList("three", "four").forEach(secondBuilder::addWord);

String mergedResult = firstBuilder.merge(secondBuilder).build();

SoftAssertions softly = new SoftAssertions();
softly.assertThat(mergedResult).hasSize(18);
softly.assertThat(mergedResult).isEqualTo("one-two-three-four");
softly.assertAll();
}
}

0 comments on commit b0cd47e

Please sign in to comment.