package com.atlassian.stash.internal.scm.git.version;

import com.atlassian.fugue.Pair;
import com.atlassian.bitbucket.util.Version;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;

public class VersionOutputHandlerTest {

    private static final ImmutableMap<Pair<Version, Version>, Integer> COMPARISONS
            = ImmutableMap.<Pair<Version, Version>, Integer>builder()
            .put(Pair.pair(new Version("1.7.6"), new Version("1.7.6")), 0)
            .put(Pair.pair(new Version("1.7.6"), new Version("1.7.7")), -1)
            .put(Pair.pair(new Version("1.7.8"), new Version("1.7.7")), 1)
            .put(Pair.pair(new Version("1.7.6"), new Version("1.7.7.1")), -1)
            .put(Pair.pair(new Version("1.7.8"), new Version("1.7.7.1")), 1)
            .put(Pair.pair(new Version("1.7.7.1"), new Version("1.7.7.1")), 0)
            .put(Pair.pair(new Version("1.7.7.1"), new Version("1.7.7.2")), -1)
            .put(Pair.pair(new Version("1.7.7.2"), new Version("1.7.7.1")), 1)
            .put(Pair.pair(new Version("1.7.7.2"), new Version("1.7.8.1")), -1)
            .build();

    @Test
    public void testGitVersionOutputHandler() {
        VersionOutputHandler handler = new VersionOutputHandler();

        handler.processLine(1, "git version 1.7.8.3");
        assertEquals(new Version("1.7.8.3"), handler.getOutput());
    }

    @Test
    public void testVersionOutputHandler() {
        VersionOutputHandler handler = new VersionOutputHandler();

        handler.processLine(1, "git version 1.7.6");
        assertEquals(new Version("1.7.6"), handler.getOutput());
    }

    @Test
    public void testCompare() throws Exception {
        for (Map.Entry<Pair<Version, Version>, Integer> entry : COMPARISONS.entrySet()) {
            Version v1 = entry.getKey().left();
            Version v2 = entry.getKey().right();
            assertEquals(v1 + " compared to " + v2,  (int) entry.getValue(), v1.compareTo(v2));
            assertEquals(v2 + " compared to " + v1,  (int) -entry.getValue(), v2.compareTo(v1));
        }
    }
}
