package com.atlassian.bitbucket.mesh.git;

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class GitRefPatternTest {

    @Test
    public void testGetPath() {
        assertEquals("refs/heads/", GitRefPattern.HEADS.getPath());
        assertEquals("refs/remotes/", GitRefPattern.REMOTES.getPath());
        assertEquals("refs/tags/", GitRefPattern.TAGS.getPath());
    }

    @Test
    public void testGetSearchOrder() {
        assertOrder(GitRefPattern.HEADS.getSearchOrder("master"), "refs/heads/master");
        assertOrder(GitRefPattern.HEADS.getSearchOrder("heads/master"), "refs/heads/master", "refs/heads/heads/master");
        assertOrder(GitRefPattern.HEADS.getSearchOrder("refs/heads/master"),
                "refs/heads/master", "refs/heads/refs/heads/master");
        assertOrder(GitRefPattern.HEADS.getSearchOrder("something/master"), "refs/heads/something/master");
    }

    @Test
    public void testQualify() {
        assertEquals("refs/heads/master", GitRefPattern.HEADS.qualify("refs/heads/master"));
        assertEquals("refs/heads/master", GitRefPattern.HEADS.qualify("heads/master"));
        assertEquals("refs/heads/master", GitRefPattern.HEADS.qualify("master"));
        //Qualifying one type with a disambiguator from a different type should not be detected
        assertEquals("refs/tags/heads/master", GitRefPattern.TAGS.qualify("heads/master"));
        //Qualifying any random pathed ref should be the same as qualifying an unpathed ref
        assertEquals("refs/heads/something/master", GitRefPattern.HEADS.qualify("something/master"));
    }

    @Test
    public void testUnqualify() {
        assertEquals("master", GitRefPattern.HEADS.unqualify("refs/heads/master"));
        assertEquals("master", GitRefPattern.HEADS.unqualify("heads/master"));
        assertEquals("master", GitRefPattern.HEADS.unqualify("master"));
        //Unqualifying one type with a disambiguator from a different type should not be detected
        assertEquals("refs/heads/master", GitRefPattern.TAGS.unqualify("refs/heads/master"));
        assertEquals("heads/master", GitRefPattern.TAGS.unqualify("heads/master"));
        //Unqualifying any random pathed ref should be the same as unqualifying an unpathed ref
        assertEquals("something/master", GitRefPattern.HEADS.unqualify("something/master"));
    }

    private static void assertOrder(List<String> actual, String... expected) {
        assertNotNull(actual);
        assertEquals(expected.length, actual.size());
        assertArrayEquals(expected, actual.toArray());
    }

}