package com.atlassian.stash.internal.web.soy.functions;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class AbbreviatedCommitMessageFunctionTest {

    private String message;
    private long max;
    private String expected;

    public AbbreviatedCommitMessageFunctionTest(String message, long max, String expected) {
        this.message = message;
        this.max = max;
        this.expected = expected;
    }

    // Keep these test in sync with util/abbreviate-commit-message-test.js
    @Parameterized.Parameters(name = "commit = \"{0}\", max = {1}")
    public static Collection<Object[]> testCases() {
        return Arrays.asList(new Object[][]{
                {"commit message\nwith a new line", 20L, "commit message..."},
                {"commit message\nwith a new line", 17L, "commit message..."},
                {"commit message\nwith a new line", 10L, "commit ..."},
                {"commit message\nwith a new line", 30L, "commit message\nwith a new line"},
                {"commit message without a new line", 29L, "commit message without a n..."},
                {"commit message without a new line", 33L, "commit message without a new line"},
                {"commit message without a new line", 10L, "commit ..."}
        });
    }


    @Test
    public void testValidInput() {
        assertThat(new AbbreviatedCommitMessageFunction().apply(message, max), equalTo(expected));
    }
}
