package com.atlassian.bitbucket.util;

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/** Sanity test for coverage. */
public class TextUtilsTest {

    private static String LONG_PATH = "this/is/a/long/path/with/lots/of/segments/AbstractProxyBeanSingletonFactory.java";
    private static String LONG_PATH_NO_NAME = "this/is/a/long/path/with/lots/of/segments/";
    private static String SHORT_PATH_NO_SEGS = "path";

    @Test
    public void testPluralise() throws Exception {
        assertEquals("word", TextUtils.pluralise("word", 1));
        assertEquals("words", TextUtils.pluralise("word", 42));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testPluraliseNegative() throws Exception {
        TextUtils.pluralise("word", -42);
    }

    @Test
    public void testPluraliseSingularOrPlural() throws Exception {
        assertEquals("tooth", TextUtils.pluralise("tooth", "teeth", 1));
        assertEquals("teeth", TextUtils.pluralise("tooth", "teeth", 42));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testPluraliseSingularOrPluralWithNegative() throws Exception {
        TextUtils.pluralise("tooth", "teeth", -42);
    }

    @Test
    public void testTruncatePath() throws Exception {
        assertEquals("this/.../segments/AbstractProxyBeanSingletonFactory.java", TextUtils.truncatePath(LONG_PATH, 1, 2, 40));
        assertEquals("one/.../four", TextUtils.truncatePath("one/three/two/four", 1, 1, 12));
        assertEquals("one/.../four/five", TextUtils.truncatePath("one/three/two/four/five", 1, 1, 17));
        assertEquals("one/.../four/five/", TextUtils.truncatePath("one/three/two/four/five/", 1, 1, 18));
    }

    @Test
    public void testTruncatePathWithTrailingSlash() throws Exception {
        assertEquals("this/.../segments/", TextUtils.truncatePath(LONG_PATH_NO_NAME, 1, 1, 5));
    }

    @Test
    public void testTruncatePathShortEnough() throws Exception {
        assertEquals(LONG_PATH, TextUtils.truncatePath(LONG_PATH, 1, 2, 100));
    }

    @Test
    public void testUntruncatablePath() throws Exception {
        assertEquals(SHORT_PATH_NO_SEGS, TextUtils.truncatePath(SHORT_PATH_NO_SEGS, 1, 2, 1));
    }

    @Test
    public void testTruncatePathSmallTruncation() throws Exception {
        assertEquals("this/.../with/lots/of/segments/AbstractProxyBeanSingletonFactory.java", TextUtils.truncatePath(LONG_PATH, 1, 2, 71));
    }

    @Test
    public void testSameLength() throws Exception {
        assertEquals(LONG_PATH, TextUtils.truncatePath(LONG_PATH, 1, 1, LONG_PATH.length()));
    }

    @Test
    public void testTruncateWithLongerResult() throws Exception {
        String path = "this/is/short";
        assertEquals("this/is/short", TextUtils.truncatePath(path, 1, 1, path.length() - 1));
    }

    @Test
    public void testInvalidArguments() throws Exception {
        assertInvalidArguments(1, 1, 0);
        assertInvalidArguments(-1, -5, 0);
    }

    private void assertInvalidArguments(int startSegment, int endSegment, int len) {
        try {
            TextUtils.truncatePath(LONG_PATH, startSegment, endSegment, len);
            Assert.fail(String.format("Start %d and end %d should throw an exception", startSegment, endSegment));
        } catch (IllegalArgumentException ex) {
            // if we get here it's good!
        }
    }
}
