
package com.facebook.buck.versions;

import static org.junit.Assert.assertThat;

import com.facebook.buck.core.config.FakeBuckConfig;
import com.facebook.buck.core.exceptions.HumanReadableException;
import com.facebook.buck.core.model.BuildTargetFactory;
import com.google.common.collect.ImmutableMap;
import org.hamcrest.Matchers;
import org.hamcrest.junit.ExpectedException;
import org.junit.Rule;
import org.junit.Test;

public class VersionBuckConfigTest {

  @Rule public ExpectedException expectedException = ExpectedException.none();

  @Test
  public void simple() {
    VersionBuckConfig config =
        new VersionBuckConfig(
            FakeBuckConfig.builder()
                .setSections(
                    ImmutableMap.of(
                        "version_universes",
                        ImmutableMap.of(
                            "universe1", "//hello:world=1, //foo:bar=3",
                            "universe2", "//foo:bar=2")))
                .build());
    assertThat(
        config.getVersionUniverses(),
        Matchers.equalTo(
            ImmutableMap.of(
                "universe1",
                VersionUniverse.of(
                    ImmutableMap.of(
                        BuildTargetFactory.newInstance("//hello:world"),
                        Version.of("1"),
                        BuildTargetFactory.newInstance("//foo:bar"),
                        Version.of("3"))),
                "universe2",
                VersionUniverse.of(
                    ImmutableMap.of(
                        BuildTargetFactory.newInstance("//foo:bar"), Version.of("2"))))));
  }

  @Test
  public void invalidTargetVersionPair() {
    VersionBuckConfig config =
        new VersionBuckConfig(
            FakeBuckConfig.builder()
                .setSections(
                    ImmutableMap.of(
                        "version_universes", ImmutableMap.of("invalid", "//hello:world")))
                .build());
    expectedException.expect(HumanReadableException.class);
    expectedException.expectMessage("must specify version selections as a comma-separated");
    config.getVersionUniverses();
  }
}
