Last active
January 17, 2017 22:32
-
-
Save alexstyl/cda3be7fc91beeeff0e6c9693ac49c99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexstyl.myapplication; | |
import android.content.res.Resources; | |
import android.support.annotation.StringRes; | |
class AndroidStringResources implements StringResources { | |
private final Resources resources; | |
AndroidStringResources(Resources resources) { | |
this.resources = resources; | |
} | |
@Override | |
public String getString(@StringRes int resId) { | |
return resources.getString(resId); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexstyl.myapplication; | |
import java.util.Collections; | |
import java.util.List; | |
public class DisplayName { | |
private final List<String> firstname; | |
private final String lastName; | |
DisplayName(List<String> firstname, String lastName) { | |
this.firstname = firstname; | |
this.lastName = lastName; | |
} | |
public List<String> getFirstNames() { | |
return firstname; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexstyl.myapplication; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
final class DisplayNameFactory { | |
private final StringResources resources; | |
DisplayNameFactory(StringResources resources) { | |
this.resources = resources; | |
} | |
public DisplayName createFromName(String rawName) { | |
if (rawName == null) { | |
String lastName = resources.getString(R.string.anonymous); | |
return new DisplayName(Collections.<String>emptyList(), lastName); | |
} | |
String[] nameParts = rawName.trim().split("\\W"); | |
if (nameParts.length == 1) { | |
return new DisplayName(Collections.<String>emptyList(), nameParts[0]); | |
} | |
ArrayList<String> firstNames = new ArrayList<>(); | |
for (int i = 0; i < nameParts.length - 1; i++) { | |
String namePart = nameParts[i]; | |
firstNames.add(namePart); | |
} | |
return new DisplayName(firstNames, nameParts[nameParts.length - 1]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexstyl.myapplication; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import static org.fest.assertions.api.Assertions.assertThat; | |
@RunWith(MockitoJUnitRunner.class) | |
public class DisplayNameFactoryTest { | |
private DisplayNameFactory displayNameFactory; | |
@Before | |
public void setUp() { | |
StringResources resources = new TestingResources(); | |
displayNameFactory = new DisplayNameFactory(resources); | |
} | |
@Test | |
public void givenNull_thenDisplayNameIsAnonymous() { | |
DisplayName displayName = displayNameFactory.createFromName(null); | |
assertThat(displayName.getFirstNames()).isEmpty(); | |
assertThat(displayName.getLastName()).isEqualTo("Anonymous"); | |
} | |
@Test | |
public void givenLastNameOnly_thenDisplayNameOnlyContainsLastName() { | |
DisplayName displayName = displayNameFactory.createFromName(" Styl"); | |
assertThat(displayName.getLastName()).isEqualTo("Styl"); | |
assertThat(displayName.getFirstNames()).isEmpty(); | |
} | |
@Test | |
public void givenFirstNameAndLastName_thenDisplaynameContainsBothFirstNameAndLastName() { | |
DisplayName displayName = displayNameFactory.createFromName("Alex Styl"); | |
assertThat(displayName.getFirstNames().get(0)).isEqualTo("Alex"); | |
assertThat(displayName.getLastName()).isEqualTo("Styl"); | |
} | |
@Test | |
public void namesSplitByColumn() { | |
DisplayName displayName = displayNameFactory.createFromName("Vaso-Petra Papaxaralampou"); | |
assertThat(displayName.getFirstNames()).contains("Vaso"); | |
assertThat(displayName.getFirstNames()).contains("Petra"); | |
assertThat(displayName.getLastName()).contains("Papaxaralampou"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexstyl.myapplication; | |
import android.support.annotation.StringRes; | |
interface StringResources { | |
String getString(@StringRes int resId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexstyl.myapplication; | |
import android.support.annotation.StringRes; | |
class TestingResources implements StringResources { | |
@Override | |
public String getString(@StringRes int resId) { | |
if (resId == R.string.anonymous) { | |
return "Anonymous"; | |
} | |
throw new IllegalArgumentException("Unsupported string"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment