/* * Copyright 2014-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package com.facebook.buck.model; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.util.Optional; import org.junit.Test; public class FlavorDomainTest { @Test public void getFlavor() { Flavor flavor = InternalFlavor.of("hello"); FlavorDomain<String> domain = new FlavorDomain<>("test", ImmutableMap.of(flavor, "something")); BuildTarget target = BuildTargetFactory.newInstance("//:test#hello"); assertEquals(Optional.of(flavor), domain.getFlavor(target)); target = BuildTargetFactory.newInstance("//:test#invalid"); assertEquals(Optional.empty(), domain.getFlavor(target)); } @Test public void multipleFlavorsForSameDomainShouldThrow() { Flavor hello = InternalFlavor.of("hello"); Flavor goodbye = InternalFlavor.of("goodbye"); FlavorDomain<String> domain = new FlavorDomain<>( "test", ImmutableMap.of( hello, "something", goodbye, "something")); BuildTarget target = BuildTarget.builder(BuildTargetFactory.newInstance("//:test")) .addAllFlavors(ImmutableSet.of(hello, goodbye)) .build(); try { domain.getFlavor(target); fail("should have thrown"); } catch (FlavorDomainException e) { assertTrue(e.getMessage().contains("multiple \"test\" flavors")); } } @Test public void getValue() throws FlavorDomainException { Flavor flavor = InternalFlavor.of("hello"); FlavorDomain<String> domain = new FlavorDomain<>("test", ImmutableMap.of(flavor, "something")); String val = domain.getValue(flavor); assertEquals("something", val); try { domain.getValue(InternalFlavor.of("invalid")); fail("should have thrown"); } catch (FlavorDomainException e) { assertTrue(e.getMessage().contains("has no flavor")); } } }