diff options
author | 2017-08-09 21:17:40 +0200 | |
---|---|---|
committer | 2017-08-10 13:48:00 +0200 | |
commit | 7740f68c7ed2b925628f2a60e774b5c04ca5bbed (patch) | |
tree | 2d32813cc00e0c4cb988bef32fd939e2d6a9da71 /src | |
parent | 3a0ce7780a5707450d7fb013c006e90bf6861e27 (diff) |
Open source apple-package java tests
RELNOTES: None.
PiperOrigin-RevId: 164755768
Diffstat (limited to 'src')
5 files changed, 361 insertions, 1 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index 1f364f2a13..8e55f839ff 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -37,6 +37,7 @@ filegroup( "//src/test/java/com/google/devtools/build/lib/skylark:srcs", "//src/test/java/com/google/devtools/build/lib/skyframe:srcs", "//src/test/java/com/google/devtools/build/lib/rules/android:srcs", + "//src/test/java/com/google/devtools/build/lib/rules/apple:srcs", "//src/test/java/com/google/devtools/build/lib/rules/config:srcs", "//src/test/java/com/google/devtools/build/lib/analysis/featurecontrol:srcs", "//src/test/java/com/google/devtools/build/lib/analysis/platform:srcs", @@ -589,7 +590,10 @@ java_library( srcs = glob([ "packages/util/*.java", ]), - resources = ["packages/util/MOCK_ANDROID_CROSSTOOL"], + resources = [ + "packages/util/MOCK_ANDROID_CROSSTOOL", + "packages/util/MOCK_OSX_CROSSTOOL", + ], deps = [ ":foundations_testutil", ":testutil", diff --git a/src/test/java/com/google/devtools/build/lib/rules/apple/BUILD b/src/test/java/com/google/devtools/build/lib/rules/apple/BUILD new file mode 100644 index 0000000000..2b85ae5bff --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/apple/BUILD @@ -0,0 +1,30 @@ +filegroup( + name = "srcs", + srcs = glob(["**"]), + visibility = ["//src/test/java/com/google/devtools/build/lib:__pkg__"], +) + +java_library( + name = "AppleRulesTests_lib", + srcs = glob(["*.java"]), + deps = [ + "//src/main/java/com/google/devtools/build/lib:build-base", + "//src/main/java/com/google/devtools/build/lib/rules/apple", + "//src/test/java/com/google/devtools/build/lib:analysis_testutil", + "//third_party:guava", + "//third_party:guava-testlib", + "//third_party:jsr305", + "//third_party:junit4", + "//third_party:truth", + ], +) + +java_test( + name = "AppleRulesTests", + test_class = "com.google.devtools.build.lib.AllTests", + runtime_deps = [ + ":AppleRulesTests_lib", + "//src/main/java/com/google/devtools/build/lib:util", + "//src/test/java/com/google/devtools/build/lib:testutil", + ], +) diff --git a/src/test/java/com/google/devtools/build/lib/rules/apple/ComparatorTester.java b/src/test/java/com/google/devtools/build/lib/rules/apple/ComparatorTester.java new file mode 100644 index 0000000000..f90a969873 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/apple/ComparatorTester.java @@ -0,0 +1,129 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// 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.google.devtools.build.lib.rules.apple; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import javax.annotation.Nullable; + +/** + * Tests that a given comparator (or the implementation of {@link Comparable}) is correct. To use, + * repeatedly call {@link #addEqualityGroup(Object...)} with sets of objects that should be equal. + * The calls to {@link #addEqualityGroup(Object...)} must be made in sorted order. Then call {@link + * #testCompare()} to test the comparison. For example: + * + * <pre>{@code + * new ComparatorTester() + * .addEqualityGroup(1) + * .addEqualityGroup(2) + * .addEqualityGroup(3) + * .testCompare(); + * }</pre> + */ +public class ComparatorTester { + @SuppressWarnings({"unchecked", "rawtypes"}) + private final @Nullable Comparator comparator; + + /** The items that we are checking, stored as a sorted set of equivalence classes. */ + private final List<List<Object>> equalityGroups; + + /** + * Creates a new instance that tests the order of objects using the natural order (as defined by + * {@link Comparable}). + */ + public ComparatorTester() { + this(null); + } + + /** + * Creates a new instance that tests the order of objects using the given comparator. Or, if the + * comparator is {@code null}, the natural ordering (as defined by {@link Comparable}) + */ + public ComparatorTester(@Nullable Comparator<?> comparator) { + this.equalityGroups = new ArrayList<>(); + this.comparator = comparator; + } + + /** + * Adds a set of objects to the test which should all compare as equal. All of the elements in + * {@code objects} must be greater than any element of {@code objects} in a previous call to + * {@link #addEqualityGroup(Object...)}. + * + * @return {@code this} (to allow chaining of calls) + */ + public ComparatorTester addEqualityGroup(Object... objects) { + Preconditions.checkNotNull(objects); + Preconditions.checkArgument(objects.length > 0, "Array must not be empty"); + equalityGroups.add(ImmutableList.copyOf(objects)); + return this; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private int compare(Object a, Object b) { + int compareValue; + if (comparator == null) { + compareValue = ((Comparable) a).compareTo(b); + } else { + compareValue = comparator.compare(a, b); + } + return compareValue; + } + + public final void testCompare() { + for (int referenceIndex = 0; referenceIndex < equalityGroups.size(); referenceIndex++) { + for (Object reference : equalityGroups.get(referenceIndex)) { + testNullCompare(reference); + testClassCast(reference); + for (int otherIndex = 0; otherIndex < equalityGroups.size(); otherIndex++) { + for (Object other : equalityGroups.get(otherIndex)) { + assertThat(compare(reference, other)) + .isEqualTo(Integer.compare(referenceIndex, otherIndex)); + } + } + } + } + } + + private void testNullCompare(Object obj) { + // Comparator does not require any specific behavior for null. + if (comparator == null) { + try { + compare(obj, null); + fail("Expected NullPointerException in " + obj + ".compare(null)"); + } catch (NullPointerException expected) { + } + } + } + + @SuppressWarnings("unchecked") + private void testClassCast(Object obj) { + if (comparator == null) { + try { + compare(obj, ICanNotBeCompared.INSTANCE); + fail("Expected ClassCastException in " + obj + ".compareTo(otherObject)"); + } catch (ClassCastException expected) { + } + } + } + + private static final class ICanNotBeCompared { + static final ComparatorTester.ICanNotBeCompared INSTANCE = new ICanNotBeCompared(); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/apple/DottedVersionTest.java b/src/test/java/com/google/devtools/build/lib/rules/apple/DottedVersionTest.java new file mode 100644 index 0000000000..dbcf87af2a --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/apple/DottedVersionTest.java @@ -0,0 +1,133 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// 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.google.devtools.build.lib.rules.apple; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +import com.google.common.testing.EqualsTester; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for {@link DottedVersion}. + */ +@RunWith(JUnit4.class) +public class DottedVersionTest { + + @Test + public void testCompareTo() throws Exception { + new ComparatorTester() + .addEqualityGroup(DottedVersion.fromString("0"), DottedVersion.fromString("0.0.0")) + .addEqualityGroup(DottedVersion.fromString("0.1"), DottedVersion.fromString("0.01")) + .addEqualityGroup(DottedVersion.fromString("0.2"), DottedVersion.fromString("0.2.0")) + .addEqualityGroup(DottedVersion.fromString("0.2.1")) + .addEqualityGroup(DottedVersion.fromString("1.2alpha")) + .addEqualityGroup(DottedVersion.fromString("1.2alpha1")) + .addEqualityGroup(DottedVersion.fromString("1.2alpha2")) + .addEqualityGroup(DottedVersion.fromString("1.2beta1")) + .addEqualityGroup(DottedVersion.fromString("1.2beta12")) + .addEqualityGroup(DottedVersion.fromString("1.2beta12.1")) + .addEqualityGroup(DottedVersion.fromString("1.2.0"), DottedVersion.fromString("1.2")) + .addEqualityGroup(DottedVersion.fromString("1.20")) + .testCompare(); + } + + @Test + public void testEquals() throws Exception { + new EqualsTester() + .addEqualityGroup(DottedVersion.fromString("0"), DottedVersion.fromString("0.0.0")) + .addEqualityGroup(DottedVersion.fromString("0.1"), DottedVersion.fromString("0.01")) + .addEqualityGroup(DottedVersion.fromString("0.2"), DottedVersion.fromString("0.2.0")) + .addEqualityGroup(DottedVersion.fromString("1.2xy2"), DottedVersion.fromString("1.2xy2")) + .addEqualityGroup( + DottedVersion.fromString("1.2x"), + DottedVersion.fromString("1.2x0"), + DottedVersion.fromString("1.2x0.0")) + .testEquals(); + } + + @Test + public void testToStringWithMinimumComponent() throws Exception { + DottedVersion dottedVersion = DottedVersion.fromString("42.8"); + assertThat(dottedVersion.toStringWithMinimumComponents(0)).isEqualTo("42.8"); + assertThat(dottedVersion.toStringWithMinimumComponents(1)).isEqualTo("42.8"); + assertThat(dottedVersion.toStringWithMinimumComponents(2)).isEqualTo("42.8"); + assertThat(dottedVersion.toStringWithMinimumComponents(3)).isEqualTo("42.8.0"); + assertThat(dottedVersion.toStringWithMinimumComponents(4)).isEqualTo("42.8.0.0"); + } + + @Test + public void testToStringWithMinimumComponent_trailingZero() throws Exception { + DottedVersion dottedVersion = DottedVersion.fromString("4.3alpha3.0"); + assertThat(dottedVersion.toStringWithMinimumComponents(0)).isEqualTo("4.3alpha3.0"); + assertThat(dottedVersion.toStringWithMinimumComponents(1)).isEqualTo("4.3alpha3.0"); + assertThat(dottedVersion.toStringWithMinimumComponents(2)).isEqualTo("4.3alpha3.0"); + assertThat(dottedVersion.toStringWithMinimumComponents(3)).isEqualTo("4.3alpha3.0"); + assertThat(dottedVersion.toStringWithMinimumComponents(4)).isEqualTo("4.3alpha3.0.0"); + assertThat(dottedVersion.toStringWithMinimumComponents(5)).isEqualTo("4.3alpha3.0.0.0"); + } + + @Test + public void testToStringWithMinimumComponents_zeroComponent() throws Exception { + DottedVersion zeroComponent = DottedVersion.fromString("0"); + assertThat(zeroComponent.toStringWithMinimumComponents(0)).isEqualTo("0"); + assertThat(zeroComponent.toStringWithMinimumComponents(1)).isEqualTo("0"); + assertThat(zeroComponent.toStringWithMinimumComponents(2)).isEqualTo("0.0"); + assertThat(zeroComponent.toStringWithMinimumComponents(3)).isEqualTo("0.0.0"); + } + + @Test + public void testIllegalVersion_noLeadingInteger() throws Exception { + try { + DottedVersion.fromString("a"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessageThat().contains("a"); + } + } + + @Test + public void testIllegalVersion_empty() throws Exception { + try { + DottedVersion.fromString(""); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException expected) {} + } + + @Test + public void testIllegalVersion_punctuation() throws Exception { + try { + DottedVersion.fromString("2:3"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException expected) {} + } + + @Test + public void testIllegalVersion_emptyComponent() throws Exception { + try { + DottedVersion.fromString("1..3"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException expected) {} + } + + @Test + public void testIllegalVersion_negativeComponent() throws Exception { + try { + DottedVersion.fromString("1.-1"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException expected) {} + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/apple/SwiftConfigurationTest.java b/src/test/java/com/google/devtools/build/lib/rules/apple/SwiftConfigurationTest.java new file mode 100644 index 0000000000..96b352e4e3 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/apple/SwiftConfigurationTest.java @@ -0,0 +1,64 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// 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.google.devtools.build.lib.rules.apple; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Skylark interface to SwiftConfiguration. */ +@RunWith(JUnit4.class) +public class SwiftConfigurationTest extends BuildViewTestCase { + @Test + public void testSkylarkApi() throws Exception { + scratch.file("examples/rule/BUILD"); + scratch.file( + "examples/rule/apple_rules.bzl", + "def swift_binary_impl(ctx):", + " copts = ctx.fragments.swift.copts()", + " wmo = ctx.fragments.swift.enable_whole_module_optimization()", + " return struct(", + " copts=copts,", + " wmo=wmo,", + " )", + "swift_binary = rule(", + " implementation = swift_binary_impl,", + " fragments = ['swift']", + ")"); + + scratch.file("examples/swift_skylark/a.m"); + scratch.file( + "examples/swift_skylark/BUILD", + "package(default_visibility = ['//visibility:public'])", + "load('/examples/rule/apple_rules', 'swift_binary')", + "swift_binary(", + " name='my_target',", + ")"); + + useConfiguration("--swiftcopt=foo", "--swiftcopt=bar", "--swift_whole_module_optimization"); + ConfiguredTarget skylarkTarget = getConfiguredTarget("//examples/swift_skylark:my_target"); + + @SuppressWarnings("unchecked") + List<String> copts = (List<String>) skylarkTarget.get("copts"); + + assertThat(copts).containsAllOf("foo", "bar"); + assertThat((Boolean) skylarkTarget.get("wmo")).isTrue(); + } +} |