package io.github.plastix.forage.util;

import android.content.res.Resources;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.github.plastix.forage.ForageRoboelectricUnitTestRunner;
import io.github.plastix.forage.R;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class StringUtilsTest {

    private static Resources resources;

    /**
     * Runs once when the unit test class loads. Set up some objects to be reused by tests.
     */
    @BeforeClass
    public static void onlyOnce() {
        resources = mock(Resources.class);
        when(resources.getString(R.string.unit_feet)).thenReturn("ft");
        when(resources.getString(R.string.unit_miles)).thenReturn("mi");
    }


    //
    // Unit tests for join()
    //

    @Test
    public void join_shouldJoinStrings() {
        String[] input = {"a", "b", "c"};
        String delimiter = ",";
        String result = StringUtils.join(delimiter, input);

        assertThat(result).isEqualTo("a,b,c");
    }

    @Test
    public void join_joinsTwoLongList() {
        String[] input = {"a", "b",};
        String delimiter = ",";
        String result = StringUtils.join(delimiter, input);

        assertThat(result).isEqualTo("a,b");
    }

    @Test
    public void join_joinsOneLongList() {
        String[] input = {"a"};
        String delimiter = ",";
        String result = StringUtils.join(delimiter, input);

        assertThat(result).isEqualTo("a");
    }

    @Test
    public void join_joinsWithEmptyDelimiter() {
        String[] input = {"a", "b", "c"};
        String delimiter = "";
        String result = StringUtils.join(delimiter, input);

        assertThat(result).isEqualTo("abc");
    }

    @Test
    public void join_emptyListReturnsEmptyString() {
        String[] input = {};
        String delimiter = ",";
        String result = StringUtils.join(delimiter, input);

        assertThat(result).isEqualTo("");
    }

    //
    // Unit tests for stripHtml()
    //
    // TODO Needs to be refactored since it depends on Android resources.

    //
    // Unit tests for capitalize()
    //

    @Test
    public void capitalize_regularStringCapitalized() {
        String input = "hello world!";
        String expected = "Hello world!";

        assertThat(StringUtils.capitalize(input)).isEqualTo(expected);
    }

    @Test
    public void capitalize_alreadyCapitalString() {
        String input = "Hello world!";
        String expected = "Hello world!";

        assertThat(StringUtils.capitalize(input)).isEqualTo(expected);
    }

    @Test
    public void capitalize_singleCharString() {
        String input = "a";
        String expected = "A";

        assertThat(StringUtils.capitalize(input)).isEqualTo(expected);

    }

    @Test
    public void capitalize_emptyString() {
        String input = "";

        assertThat(StringUtils.capitalize(input)).isEqualTo(input);
    }

    //
    // Unit tests for humanReadableImperialDistance()
    //


    @Test
    public void humanReadableImperialDistance_showMiles() {
        double dist = 1.5;
        String expected = "1.50 mi";

        assertThat(StringUtils.humanReadableImperialDistance(resources, dist)).isEqualTo(expected);
    }

    @Test
    public void humanReadableImperialDistance_showFeetWhenLessThanOneMile() {
        double dist = 0.5;
        String expected = "2640.00 ft";

        assertThat(StringUtils.humanReadableImperialDistance(resources, dist)).isEqualTo(expected);
    }

    @Test
    public void humanReadableImperialDistance_zeroMiles() {
        double dist = 0;
        String expected = "0.00 ft";

        assertThat(StringUtils.humanReadableImperialDistance(resources, dist)).isEqualTo(expected);
    }

    @Test
    public void humanReadableImperialDistance_oneMileDisplaysInFeet() {
        double dist = 1;
        String expected = "5280.00 ft";

        assertThat(StringUtils.humanReadableImperialDistance(resources, dist)).isEqualTo(expected);
    }
}

@@ -70,16 +70,73 @@
 sources;
+%0Aimport android.text.Html;%0Aimport android.text.Spannable;
 %0A%0Aimport
@@ -219,75 +219,174 @@
 th;%0A
-%0A
 import 
-io.github.plastix.forage.ForageRoboelectricUnitTest
+org.powermock.api.mockito.PowerMockito;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMock
 Runner;%0A
 impo
@@ -381,16 +381,17 @@
 Runner;%0A
+%0A
 import i
@@ -552,14 +552,116 @@
 ito.
-when;%0A
+verify;%0Aimport static org.mockito.Mockito.when;%0A%0A@RunWith(PowerMockRunner.class)%0A@PrepareForTest(Html.class)
 %0Apub
@@ -2366,81 +2366,530 @@
  //%0A
-    // TODO Needs to be refactored since it depends on Android resources.
+%0A    @Test%0A    public void stripHtml_delegatesToAndroidClass() %7B%0A        PowerMockito.mockStatic(Html.class);%0A        String input = %22input string%22;%0A        String out = %22output string%22;%0A        Spannable spannable = mock(Spannable.class);%0A        when(spannable.toString()).thenReturn(out);%0A        when(Html.fromHtml(input)).thenReturn(spannable);%0A%0A        String output = StringUtils.stripHtml(input);%0A%0A        PowerMockito.verifyStatic();%0A        Html.fromHtml(input);%0A%0A        assertThat(out).isEqualTo(output);%0A    %7D
 %0A%0A  

Add StringUtils.stripHtml unit test using Powermockl