aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/packages/SkylarkInfoTest.java
blob: da4d7e19d6ad522aaa9941ce592039595a66c365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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.packages;

import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.packages.SkylarkInfo.Layout;
import com.google.devtools.build.lib.syntax.EvalException;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Test class for {@link SkylarkInfo} and its subclasses. */
@RunWith(JUnit4.class)
public class SkylarkInfoTest {

  private static final Layout layoutF1F2 = new Layout(ImmutableList.of("f1", "f2"));
  private static final Layout invertedLayoutF2F1 = new Layout(ImmutableList.of("f2", "f1"));

  @Test
  public void layoutAccessors() {
    Layout layout = new Layout(ImmutableList.of("x", "y", "z"));
    assertThat(layout.size()).isEqualTo(3);
    assertThat(layout.hasField("x")).isTrue();
    assertThat(layout.hasField("q")).isFalse();
    assertThat(layout.getFieldIndex("z")).isEqualTo(2);
    assertThat(layout.getFields()).containsExactly("x", "y", "z").inOrder();
    assertThat(
        layout.entrySet().stream()
            .map(Map.Entry::getKey)
            .collect(ImmutableList.toImmutableList()))
        .containsExactly("x", "y", "z").inOrder();
  }

  @Test
  public void layoutDisallowsDuplicates() {
    assertThrows(
        IllegalArgumentException.class,
        () -> new Layout(ImmutableList.of("x", "y", "x")));
  }

  @Test
  public void layoutEquality() {
    new EqualsTester()
        .addEqualityGroup(
            new Layout(ImmutableList.of("a", "b", "c")),
            new Layout(ImmutableList.of("a", "b", "c")))
        .addEqualityGroup(
            new Layout(ImmutableList.of("x", "y", "z")))
        .addEqualityGroup(
            new Layout(ImmutableList.of("c", "b", "a")))
        .testEquals();
  }

  @Test
  public void nullLocationDefaultsToBuiltin() throws Exception {
    SkylarkInfo info = SkylarkInfo.createSchemaless(makeProvider(), ImmutableMap.of(), null);
    assertThat(info.getCreationLoc()).isEqualTo(Location.BUILTIN);
  }

  @Test
  public void givenLayoutTakesPrecedenceOverProviderLayout() throws Exception {
    SkylarkProvider provider =
        SkylarkProvider.createUnexportedSchemaful(ImmutableList.of("f1", "f2"), Location.BUILTIN);
    SkylarkInfo info =
        SkylarkInfo.createSchemaful(
            provider, invertedLayoutF2F1, new Object[]{5, 4}, Location.BUILTIN);
    assertThat(info.getLayout()).isEqualTo(invertedLayoutF2F1);  // not the one in the provider
  }

  @Test
  public void schemafulValuesMustMatchLayoutArity() throws Exception {
    SkylarkProvider provider = makeProvider();
    IllegalArgumentException expected =
        assertThrows(
            IllegalArgumentException.class,
            () ->
                SkylarkInfo.createSchemaful(
                    provider, layoutF1F2, new Object[] {4}, Location.BUILTIN));
    assertThat(expected).hasMessageThat()
        .contains("Layout has length 2, but number of given values was 1");
  }

  @Test
  public void instancesOfUnexportedProvidersAreMutable() throws Exception {
    SkylarkProvider provider = makeProvider();
    SkylarkInfo mapInfo = makeSchemalessInfoWithF1F2Values(provider, 5, null);
    SkylarkInfo compactInfo = makeSchemafulInfoWithF1F2Values(provider, 5, null);
    assertThat(mapInfo.isImmutable()).isFalse();
    assertThat(compactInfo.isImmutable()).isFalse();
  }

  @Test
  public void instancesOfExportedProvidersMayBeImmutable() throws Exception {
    SkylarkProvider provider = makeExportedProvider();
    SkylarkInfo mapInfo = makeSchemalessInfoWithF1F2Values(provider, 5, null);
    SkylarkInfo compactInfo = makeSchemafulInfoWithF1F2Values(provider, 5, null);
    assertThat(mapInfo.isImmutable()).isTrue();
    assertThat(compactInfo.isImmutable()).isTrue();
  }

  @Test
  public void mutableIfContentsAreMutable() throws Exception {
    SkylarkProvider provider = makeExportedProvider();
    SkylarkInfo mapInfo = makeSchemalessInfoWithF1F2Values(provider, 5, new Object());
    SkylarkInfo compactInfo = makeSchemafulInfoWithF1F2Values(provider, 5, new Object());
    assertThat(mapInfo.isImmutable()).isFalse();
    assertThat(compactInfo.isImmutable()).isFalse();
  }

  @Test
  public void equality_DifferentProviders() throws Exception {
    SkylarkProvider provider1 = makeProvider();
    SkylarkProvider provider2 = makeProvider();
    new EqualsTester()
        .addEqualityGroup(
            makeSchemalessInfoWithF1F2Values(provider1, 4, 5),
            makeSchemafulInfoWithF1F2Values(provider1, 4, 5),
            makeInvertedSchemafulInfoWithF1F2Values(provider1, 4, 5))
        .addEqualityGroup(
            makeSchemalessInfoWithF1F2Values(provider2, 4, 5),
            makeSchemafulInfoWithF1F2Values(provider2, 4, 5),
            makeInvertedSchemafulInfoWithF1F2Values(provider2, 4, 5))
        .testEquals();
  }

  @Test
  public void equality_DifferentValues() throws Exception {
    SkylarkProvider provider = makeProvider();
    // These comparisons include the case where the physical array is {4, 5} on both instances but
    // they compare different due to different layouts.
    new EqualsTester()
        .addEqualityGroup(
            makeSchemalessInfoWithF1F2Values(provider, 4, 5),
            makeSchemafulInfoWithF1F2Values(provider, 4, 5),
            makeInvertedSchemafulInfoWithF1F2Values(provider, 4, 5))
        .addEqualityGroup(
            makeSchemalessInfoWithF1F2Values(provider, 5, 4),
            makeSchemafulInfoWithF1F2Values(provider, 5, 4),
            makeInvertedSchemafulInfoWithF1F2Values(provider, 5, 4))
        .addEqualityGroup(
            makeSchemalessInfoWithF1F2Values(provider, 4, null),
            makeSchemafulInfoWithF1F2Values(provider, 4, null),
            makeInvertedSchemafulInfoWithF1F2Values(provider, 4, null))
        .testEquals();
  }

  @Test
  public void concatWithDifferentProvidersFails() throws Exception {
    SkylarkProvider provider1 = makeProvider();
    SkylarkProvider provider2 = makeProvider();
    SkylarkInfo info1 = makeSchemalessInfoWithF1F2Values(provider1, 4, 5);
    SkylarkInfo info2 = makeSchemalessInfoWithF1F2Values(provider2, 4, 5);
    EvalException expected =
        assertThrows(
            EvalException.class, () -> info1.getConcatter().concat(info1, info2, Location.BUILTIN));
    assertThat(expected).hasMessageThat()
        .contains("Cannot use '+' operator on instances of different providers");
  }

  @Test
  public void concatWithOverlappingFieldsFails() throws Exception {
    SkylarkProvider provider1 = makeProvider();
    SkylarkInfo info1 = makeSchemalessInfoWithF1F2Values(provider1, 4, 5);
    SkylarkInfo info2 = makeSchemalessInfoWithF1F2Values(provider1, 4, null);
    EvalException expected =
        assertThrows(
            EvalException.class, () -> info1.getConcatter().concat(info1, info2, Location.BUILTIN));
    assertThat(expected).hasMessageThat()
        .contains("Cannot use '+' operator on provider instances with overlapping field(s): f1");
  }

  @Test
  public void compactConcatReturnsCompact() throws Exception {
    SkylarkProvider provider = makeProvider();
    SkylarkInfo info1 = makeSchemafulInfoWithF1F2Values(provider, 4, null);
    SkylarkInfo info2 = makeSchemafulInfoWithF1F2Values(provider, null, 5);
    SkylarkInfo result = (SkylarkInfo) info1.getConcatter().concat(info1, info2, Location.BUILTIN);
    assertThat(result.isCompact()).isTrue();
    assertThat((result).getFieldNames()).containsExactly("f1", "f2");
    assertThat((result).getValue("f1")).isEqualTo(4);
    assertThat((result).getValue("f2")).isEqualTo(5);
  }

  @Test
  public void compactConcatWithDifferentLayoutsReturnsMap() throws Exception {
    SkylarkProvider provider = makeProvider();
    SkylarkInfo info1 = makeSchemafulInfoWithF1F2Values(provider, 4, null);
    SkylarkInfo info2 = makeInvertedSchemafulInfoWithF1F2Values(provider, null, 5);
    SkylarkInfo result = (SkylarkInfo) info1.getConcatter().concat(info1, info2, Location.BUILTIN);
    assertThat(result.isCompact()).isFalse();
    assertThat((result).getFieldNames()).containsExactly("f1", "f2");
    assertThat(result.getValue("f1")).isEqualTo(4);
    assertThat(result.getValue("f2")).isEqualTo(5);
  }

  @Test
  public void allOtherConcatReturnsMap() throws Exception {
    SkylarkProvider provider = makeProvider();
    SkylarkInfo info1 = makeSchemalessInfoWithF1F2Values(provider, 4, null);
    SkylarkInfo info2 = makeSchemafulInfoWithF1F2Values(provider, null, 5);
    SkylarkInfo result = (SkylarkInfo) info1.getConcatter().concat(info1, info2, Location.BUILTIN);
    assertThat(result.isCompact()).isFalse();
    assertThat((result).getFieldNames()).containsExactly("f1", "f2");
    assertThat((result).getValue("f1")).isEqualTo(4);
    assertThat((result).getValue("f2")).isEqualTo(5);
  }

  /** Creates an unexported schemaless provider type with builtin location. */
  private static SkylarkProvider makeProvider() {
    return SkylarkProvider.createUnexportedSchemaless(Location.BUILTIN);
  }

  /** Creates an exported schemaless provider type with builtin location. */
  private static SkylarkProvider makeExportedProvider() {
    SkylarkProvider.SkylarkKey key = new SkylarkProvider.SkylarkKey(
        Label.parseAbsoluteUnchecked("//package:target"), "provider");
    return SkylarkProvider.createExportedSchemaless(key, Location.BUILTIN);
  }

  /**
   * Creates a schemaless instance of a provider with the given values for fields f1 and f2. Either
   * field value may be null, in which case it is omitted.
   */
  private static SkylarkInfo makeSchemalessInfoWithF1F2Values(
      SkylarkProvider provider, @Nullable Object v1, @Nullable Object v2) {
    ImmutableMap.Builder<String, Object> values = ImmutableMap.builder();
    if (v1 != null) {
      values.put("f1", v1);
    }
    if (v2 != null) {
      values.put("f2", v2);
    }
    return SkylarkInfo.createSchemaless(provider, values.build(), Location.BUILTIN);
  }

  /**
   * Creates a schemaful instance of a provider with the given values for fields f1 and f2. Either
   * field value may be null, in which case it is omitted.
   */
  private static SkylarkInfo makeSchemafulInfoWithF1F2Values(
      SkylarkProvider provider, @Nullable Object v1, @Nullable Object v2) {
    return SkylarkInfo.createSchemaful(
        provider, layoutF1F2, new Object[]{v1, v2}, Location.BUILTIN);
  }

  /**
   * Same as {@link #makeSchemafulInfoWithF1F2Values}, except the layout in the resulting
   * CompactSkylarkInfo is reversed.
   */
  private static SkylarkInfo makeInvertedSchemafulInfoWithF1F2Values(
      SkylarkProvider provider, @Nullable Object v1, @Nullable Object v2) {
    return SkylarkInfo.createSchemaful(
        provider, invertedLayoutF2F1, new Object[]{v2, v1}, Location.BUILTIN);
  }
}