Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertThrows; |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 18 | |
cparsons | a7c0afb | 2019-08-30 08:14:51 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 22 | import java.util.Arrays; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | import org.junit.runners.JUnit4; |
Florian Weikert | b914f3c | 2015-07-29 13:35:19 +0000 | [diff] [blame] | 29 | |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 30 | /** Tests for Depset. */ |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 31 | @RunWith(JUnit4.class) |
adonovan | 6ea8e25 | 2020-02-03 14:33:14 -0800 | [diff] [blame] | 32 | public final class DepsetTest extends EvaluationTestCase { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 33 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 34 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 35 | public void testConstructor() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 36 | exec("s = depset(order='default')"); |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 37 | assertThat(lookup("s")).isInstanceOf(Depset.class); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 40 | @Test |
Googler | bbde229 | 2019-11-21 12:51:51 -0800 | [diff] [blame] | 41 | public void testTuples() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 42 | exec( |
cparsons | a5b764f | 2018-12-06 15:07:58 -0800 | [diff] [blame] | 43 | "s_one = depset([('1', '2'), ('3', '4')])", |
| 44 | "s_two = depset(direct = [('1', '2'), ('3', '4'), ('5', '6')])", |
| 45 | "s_three = depset(transitive = [s_one, s_two])", |
| 46 | "s_four = depset(direct = [('1', '3')], transitive = [s_one, s_two])", |
cparsons | e210bec | 2019-01-03 14:33:38 -0800 | [diff] [blame] | 47 | "s_five = depset(direct = [('1', '3', '5')], transitive = [s_one, s_two])", |
cparsons | a5b764f | 2018-12-06 15:07:58 -0800 | [diff] [blame] | 48 | "s_six = depset(transitive = [s_one, s_five])", |
| 49 | "s_seven = depset(direct = [('1', '3')], transitive = [s_one, s_five])", |
Googler | bbde229 | 2019-11-21 12:51:51 -0800 | [diff] [blame] | 50 | "s_eight = depset(direct = [(1, 3)], transitive = [s_one, s_two])"); // note, tuple of int |
| 51 | assertThat(get("s_one").getContentType()).isEqualTo(SkylarkType.TUPLE); |
| 52 | assertThat(get("s_two").getContentType()).isEqualTo(SkylarkType.TUPLE); |
| 53 | assertThat(get("s_three").getContentType()).isEqualTo(SkylarkType.TUPLE); |
cparsons | a5b764f | 2018-12-06 15:07:58 -0800 | [diff] [blame] | 54 | assertThat(get("s_eight").getContentType()).isEqualTo(SkylarkType.TUPLE); |
cparsons | e210bec | 2019-01-03 14:33:38 -0800 | [diff] [blame] | 55 | |
ulfjack | 512244b | 2020-01-14 05:15:08 -0800 | [diff] [blame] | 56 | assertThat(get("s_four").getSet(Tuple.class).toList()) |
cparsons | e210bec | 2019-01-03 14:33:38 -0800 | [diff] [blame] | 57 | .containsExactly( |
| 58 | Tuple.of("1", "3"), Tuple.of("1", "2"), Tuple.of("3", "4"), Tuple.of("5", "6")); |
ulfjack | 512244b | 2020-01-14 05:15:08 -0800 | [diff] [blame] | 59 | assertThat(get("s_five").getSet(Tuple.class).toList()) |
cparsons | e210bec | 2019-01-03 14:33:38 -0800 | [diff] [blame] | 60 | .containsExactly( |
| 61 | Tuple.of("1", "3", "5"), Tuple.of("1", "2"), Tuple.of("3", "4"), Tuple.of("5", "6")); |
ulfjack | 512244b | 2020-01-14 05:15:08 -0800 | [diff] [blame] | 62 | assertThat(get("s_eight").getSet(Tuple.class).toList()) |
cparsons | e210bec | 2019-01-03 14:33:38 -0800 | [diff] [blame] | 63 | .containsExactly( |
| 64 | Tuple.of(1, 3), Tuple.of("1", "2"), Tuple.of("3", "4"), Tuple.of("5", "6")); |
cparsons | a5b764f | 2018-12-06 15:07:58 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
Jon Brandvein | b3d0bdd | 2017-01-13 17:46:29 +0000 | [diff] [blame] | 68 | public void testGetSet() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 69 | exec("s = depset(['a', 'b'])"); |
ulfjack | 512244b | 2020-01-14 05:15:08 -0800 | [diff] [blame] | 70 | assertThat(get("s").getSet(String.class).toList()).containsExactly("a", "b").inOrder(); |
| 71 | assertThat(get("s").getSet(Object.class).toList()).containsExactly("a", "b").inOrder(); |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 72 | assertThrows(Depset.TypeException.class, () -> get("s").getSet(Integer.class)); |
Jon Brandvein | b3d0bdd | 2017-01-13 17:46:29 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 76 | public void testGetSetDirect() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 77 | exec("s = depset(direct = ['a', 'b'])"); |
ulfjack | 512244b | 2020-01-14 05:15:08 -0800 | [diff] [blame] | 78 | assertThat(get("s").getSet(String.class).toList()).containsExactly("a", "b").inOrder(); |
| 79 | assertThat(get("s").getSet(Object.class).toList()).containsExactly("a", "b").inOrder(); |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 80 | assertThrows(Depset.TypeException.class, () -> get("s").getSet(Integer.class)); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testGetSetItems() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 85 | exec("s = depset(items = ['a', 'b'])"); |
ulfjack | 512244b | 2020-01-14 05:15:08 -0800 | [diff] [blame] | 86 | assertThat(get("s").getSet(String.class).toList()).containsExactly("a", "b").inOrder(); |
| 87 | assertThat(get("s").getSet(Object.class).toList()).containsExactly("a", "b").inOrder(); |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 88 | assertThrows(Depset.TypeException.class, () -> get("s").getSet(Integer.class)); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | @Test |
Jon Brandvein | 3cfeeec | 2017-01-20 04:23:37 +0000 | [diff] [blame] | 93 | public void testToCollection() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 94 | exec("s = depset(['a', 'b'])"); |
Jon Brandvein | 3cfeeec | 2017-01-20 04:23:37 +0000 | [diff] [blame] | 95 | assertThat(get("s").toCollection(String.class)).containsExactly("a", "b").inOrder(); |
| 96 | assertThat(get("s").toCollection(Object.class)).containsExactly("a", "b").inOrder(); |
| 97 | assertThat(get("s").toCollection()).containsExactly("a", "b").inOrder(); |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 98 | assertThrows(Depset.TypeException.class, () -> get("s").toCollection(Integer.class)); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testToCollectionDirect() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 103 | exec("s = depset(direct = ['a', 'b'])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 104 | assertThat(get("s").toCollection(String.class)).containsExactly("a", "b").inOrder(); |
| 105 | assertThat(get("s").toCollection(Object.class)).containsExactly("a", "b").inOrder(); |
| 106 | assertThat(get("s").toCollection()).containsExactly("a", "b").inOrder(); |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 107 | assertThrows(Depset.TypeException.class, () -> get("s").toCollection(Integer.class)); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testToCollectionItems() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 112 | exec("s = depset(items = ['a', 'b'])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 113 | assertThat(get("s").toCollection(String.class)).containsExactly("a", "b").inOrder(); |
| 114 | assertThat(get("s").toCollection(Object.class)).containsExactly("a", "b").inOrder(); |
| 115 | assertThat(get("s").toCollection()).containsExactly("a", "b").inOrder(); |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 116 | assertThrows(Depset.TypeException.class, () -> get("s").toCollection(Integer.class)); |
Jon Brandvein | 3cfeeec | 2017-01-20 04:23:37 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 120 | public void testOrder() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 121 | exec("s = depset(['a', 'b'], order='postorder')"); |
Jon Brandvein | 052f9ce | 2017-01-19 21:50:34 +0000 | [diff] [blame] | 122 | assertThat(get("s").getSet(String.class).getOrder()).isEqualTo(Order.COMPILE_ORDER); |
| 123 | } |
| 124 | |
| 125 | @Test |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 126 | public void testOrderDirect() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 127 | exec("s = depset(direct = ['a', 'b'], order='postorder')"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 128 | assertThat(get("s").getSet(String.class).getOrder()).isEqualTo(Order.COMPILE_ORDER); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testOrderItems() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 133 | exec("s = depset(items = ['a', 'b'], order='postorder')"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 134 | assertThat(get("s").getSet(String.class).getOrder()).isEqualTo(Order.COMPILE_ORDER); |
| 135 | } |
| 136 | |
| 137 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 138 | public void testBadOrder() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 139 | new Scenario() |
| 140 | .testIfExactError("Invalid order: non_existing", "depset(['a'], order='non_existing')"); |
Jon Brandvein | 5b792dc | 2017-01-12 20:22:07 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 144 | public void testBadOrderDirect() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 145 | new Scenario() |
| 146 | .testIfExactError( |
| 147 | "Invalid order: non_existing", "depset(direct = ['a'], order='non_existing')"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testBadOrderItems() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 152 | new Scenario() |
| 153 | .testIfExactError( |
| 154 | "Invalid order: non_existing", "depset(items = ['a'], order='non_existing')"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 158 | public void testEmptyGenericType() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 159 | exec("s = depset()"); |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 160 | assertThat(get("s").getContentType()).isEqualTo(SkylarkType.TOP); |
Jon Brandvein | b3d0bdd | 2017-01-13 17:46:29 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 164 | public void testHomogeneousGenericType() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 165 | exec("s = depset(['a', 'b', 'c'])"); |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 166 | assertThat(get("s").getContentType()).isEqualTo(SkylarkType.of(String.class)); |
Jon Brandvein | 5b792dc | 2017-01-12 20:22:07 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 170 | public void testHomogeneousGenericTypeDirect() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 171 | exec("s = depset(['a', 'b', 'c'], transitive = [])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 172 | assertThat(get("s").getContentType()).isEqualTo(SkylarkType.of(String.class)); |
| 173 | } |
| 174 | |
| 175 | @Test |
| 176 | public void testHomogeneousGenericTypeItems() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 177 | exec("s = depset(items = ['a', 'b', 'c'], transitive = [])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 178 | assertThat(get("s").getContentType()).isEqualTo(SkylarkType.of(String.class)); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testHomogeneousGenericTypeTransitive() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 183 | exec("s = depset(['a', 'b', 'c'], transitive = [depset(['x'])])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 184 | assertThat(get("s").getContentType()).isEqualTo(SkylarkType.of(String.class)); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void testTransitiveIncompatibleOrder() throws Exception { |
| 189 | checkEvalError( |
| 190 | "Order 'postorder' is incompatible with order 'topological'", |
| 191 | "depset(['a', 'b'], order='postorder',", |
| 192 | " transitive = [depset(['c', 'd'], order='topological')])"); |
| 193 | } |
| 194 | |
| 195 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 196 | public void testBadGenericType() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 197 | new Scenario() |
| 198 | .testIfExactError( |
| 199 | "cannot add an item of type 'int' to a depset of 'string'", "depset(['a', 5])"); |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Test |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 203 | public void testBadGenericTypeDirect() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 204 | new Scenario() |
| 205 | .testIfExactError( |
| 206 | "cannot add an item of type 'int' to a depset of 'string'", |
| 207 | "depset(direct = ['a', 5])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void testBadGenericTypeItems() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 212 | new Scenario() |
| 213 | .testIfExactError( |
| 214 | "cannot add an item of type 'int' to a depset of 'string'", "depset(items = ['a', 5])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void testBadGenericTypeTransitive() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 219 | new Scenario() |
| 220 | .testIfExactError( |
| 221 | "cannot add an item of type 'int' to a depset of 'string'", |
| 222 | "depset(['a', 'b'], transitive=[depset([1])])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Test |
| 226 | public void testLegacyAndNewApi() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 227 | new Scenario() |
| 228 | .testIfExactError( |
| 229 | "Do not pass both 'direct' and 'items' argument to depset constructor.", |
| 230 | "depset(['a', 'b'], direct = ['c', 'd'])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Test |
| 234 | public void testItemsAndTransitive() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 235 | new Scenario() |
| 236 | .testIfExactError( |
adonovan | 8580390 | 2020-04-16 14:46:57 -0700 | [diff] [blame^] | 237 | "for items, got depset, want sequence", |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 238 | "depset(items = depset(), transitive = [depset()])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Test |
| 242 | public void testTooManyPositionals() throws Exception { |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 243 | new Scenario() |
cparsons | 7ac265d | 2019-04-16 15:31:17 -0700 | [diff] [blame] | 244 | .testIfErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 245 | "depset() accepts no more than 2 positional arguments but got 3", |
| 246 | "depset([], 'default', [])"); |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | @Test |
| 251 | public void testTransitiveOrder() throws Exception { |
| 252 | assertContainsInOrder("depset([], transitive=[depset(['a', 'b', 'c'])])", "a", "b", "c"); |
| 253 | assertContainsInOrder("depset(['a'], transitive = [depset(['b', 'c'])])", "b", "c", "a"); |
| 254 | assertContainsInOrder("depset(['a', 'b'], transitive = [depset(['c'])])", "c", "a", "b"); |
| 255 | assertContainsInOrder("depset(['a', 'b', 'c'], transitive = [depset([])])", "a", "b", "c"); |
| 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void testTransitiveOrderItems() throws Exception { |
| 260 | assertContainsInOrder("depset(items=[], transitive=[depset(['a', 'b', 'c'])])", "a", "b", "c"); |
| 261 | assertContainsInOrder("depset(items=['a'], transitive = [depset(['b', 'c'])])", "b", "c", "a"); |
| 262 | assertContainsInOrder("depset(items=['a', 'b'], transitive = [depset(['c'])])", "c", "a", "b"); |
| 263 | assertContainsInOrder("depset(items=['a', 'b', 'c'], transitive = [depset([])])", |
| 264 | "a", "b", "c"); |
| 265 | } |
| 266 | |
| 267 | @Test |
| 268 | public void testTransitiveOrderDirect() throws Exception { |
| 269 | assertContainsInOrder("depset(direct=[], transitive=[depset(['a', 'b', 'c'])])", "a", "b", "c"); |
| 270 | assertContainsInOrder("depset(direct=['a'], transitive = [depset(['b', 'c'])])", "b", "c", "a"); |
| 271 | assertContainsInOrder("depset(direct=['a', 'b'], transitive = [depset(['c'])])", "c", "a", "b"); |
| 272 | assertContainsInOrder("depset(direct=['a', 'b', 'c'], transitive = [depset([])])", |
| 273 | "a", "b", "c"); |
| 274 | } |
| 275 | |
| 276 | @Test |
laurentlb | 2bbda4a | 2017-12-07 10:38:46 -0800 | [diff] [blame] | 277 | public void testIncompatibleUnion() throws Exception { |
laurentlb | 8073508 | 2020-03-27 07:35:09 -0700 | [diff] [blame] | 278 | new Scenario() |
| 279 | .testIfErrorContains("unsupported binary operation: depset + list", "depset([]) + ['a']"); |
laurentlb | 2bbda4a | 2017-12-07 10:38:46 -0800 | [diff] [blame] | 280 | |
laurentlb | 8073508 | 2020-03-27 07:35:09 -0700 | [diff] [blame] | 281 | new Scenario() |
| 282 | .testIfErrorContains("unsupported binary operation: depset | list", "depset([]) | ['a']"); |
laurentlb | 2bbda4a | 2017-12-07 10:38:46 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Jon Brandvein | 5b792dc | 2017-01-12 20:22:07 +0000 | [diff] [blame] | 285 | private void assertContainsInOrder(String statement, Object... expectedElements) |
| 286 | throws Exception { |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 287 | assertThat(((Depset) eval(statement)).toCollection()) |
dslomov | 2317ef8 | 2017-09-28 11:19:54 -0400 | [diff] [blame] | 288 | .containsExactly(expectedElements) |
| 289 | .inOrder(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 292 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 293 | public void testToString() throws Exception { |
laurentlb | 8073508 | 2020-03-27 07:35:09 -0700 | [diff] [blame] | 294 | exec("s = depset([3, 4, 5], transitive = [depset([2, 4, 6])])", "x = str(s)"); |
Vladimir Moskva | ba4f0bb | 2017-01-30 15:45:49 +0000 | [diff] [blame] | 295 | assertThat(lookup("x")).isEqualTo("depset([2, 4, 6, 3, 5])"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Laurent Le Brun | c7a6e36 | 2015-03-09 20:55:40 +0000 | [diff] [blame] | 298 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 299 | public void testToStringWithOrder() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 300 | exec( |
laurentlb | 8073508 | 2020-03-27 07:35:09 -0700 | [diff] [blame] | 301 | "s = depset([3, 4, 5], transitive = [depset([2, 4, 6])], ", |
| 302 | " order = 'topological')", |
Jon Brandvein | 5b792dc | 2017-01-12 20:22:07 +0000 | [diff] [blame] | 303 | "x = str(s)"); |
laurentlb | 8073508 | 2020-03-27 07:35:09 -0700 | [diff] [blame] | 304 | assertThat(lookup("x")).isEqualTo("depset([3, 5, 6, 4, 2], order = \"topological\")"); |
Laurent Le Brun | c7a6e36 | 2015-03-09 20:55:40 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 307 | private Depset get(String varname) throws Exception { |
| 308 | return (Depset) lookup(varname); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 309 | } |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 310 | |
Florian Weikert | b914f3c | 2015-07-29 13:35:19 +0000 | [diff] [blame] | 311 | @Test |
Jon Brandvein | b3d0bdd | 2017-01-13 17:46:29 +0000 | [diff] [blame] | 312 | public void testToList() throws Exception { |
laurentlb | 8073508 | 2020-03-27 07:35:09 -0700 | [diff] [blame] | 313 | setSemantics(); |
| 314 | exec("s = depset([3, 4, 5], transitive = [depset([2, 4, 6])])", "x = s.to_list()"); |
Jon Brandvein | b3d0bdd | 2017-01-13 17:46:29 +0000 | [diff] [blame] | 315 | Object value = lookup("x"); |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 316 | assertThat(value).isInstanceOf(StarlarkList.class); |
Jon Brandvein | b3d0bdd | 2017-01-13 17:46:29 +0000 | [diff] [blame] | 317 | assertThat((Iterable<?>) value).containsExactly(2, 4, 6, 3, 5).inOrder(); |
| 318 | } |
| 319 | |
| 320 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 321 | public void testOrderCompatibility() throws Exception { |
Florian Weikert | b914f3c | 2015-07-29 13:35:19 +0000 | [diff] [blame] | 322 | // Two sets are compatible if |
| 323 | // (a) both have the same order or |
Jon Brandvein | 052f9ce | 2017-01-19 21:50:34 +0000 | [diff] [blame] | 324 | // (b) at least one order is "default" |
Florian Weikert | b914f3c | 2015-07-29 13:35:19 +0000 | [diff] [blame] | 325 | |
| 326 | for (Order first : Order.values()) { |
Googler | ae586a0 | 2019-11-22 11:10:37 -0800 | [diff] [blame] | 327 | Depset s1 = Depset.legacyOf(first, Tuple.of("1", "11")); |
Florian Weikert | b914f3c | 2015-07-29 13:35:19 +0000 | [diff] [blame] | 328 | |
| 329 | for (Order second : Order.values()) { |
Googler | ae586a0 | 2019-11-22 11:10:37 -0800 | [diff] [blame] | 330 | Depset s2 = Depset.legacyOf(second, Tuple.of("2", "22")); |
Florian Weikert | b914f3c | 2015-07-29 13:35:19 +0000 | [diff] [blame] | 331 | |
| 332 | boolean compatible = true; |
| 333 | |
| 334 | try { |
Googler | ae586a0 | 2019-11-22 11:10:37 -0800 | [diff] [blame] | 335 | Depset.unionOf(s1, s2); |
Florian Weikert | b914f3c | 2015-07-29 13:35:19 +0000 | [diff] [blame] | 336 | } catch (Exception ex) { |
| 337 | compatible = false; |
| 338 | } |
| 339 | |
| 340 | assertThat(compatible).isEqualTo(areOrdersCompatible(first, second)); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private boolean areOrdersCompatible(Order first, Order second) { |
| 346 | return first == Order.STABLE_ORDER || second == Order.STABLE_ORDER || first == second; |
| 347 | } |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 348 | |
| 349 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 350 | public void testOrderComplexUnion() throws Exception { |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 351 | // {1, 11, {2, 22}, {3, 33}, {4, 44}} |
| 352 | List<String> preOrder = Arrays.asList("1", "11", "2", "22", "3", "33", "4", "44"); |
| 353 | List<String> postOrder = Arrays.asList("2", "22", "3", "33", "4", "44", "1", "11"); |
| 354 | |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 355 | MergeStrategy strategy = |
| 356 | new MergeStrategy() { |
| 357 | @Override |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 358 | public Depset merge(Depset[] sets) throws Exception { |
Googler | ae586a0 | 2019-11-22 11:10:37 -0800 | [diff] [blame] | 359 | Depset union = Depset.unionOf(sets[0], sets[1]); |
| 360 | union = Depset.unionOf(union, sets[2]); |
| 361 | union = Depset.unionOf(union, sets[3]); |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 362 | |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 363 | return union; |
| 364 | } |
| 365 | }; |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 366 | |
| 367 | runComplexOrderTest(strategy, preOrder, postOrder); |
| 368 | } |
| 369 | |
| 370 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 371 | public void testOrderBalancedTree() throws Exception { |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 372 | // {{1, 11, {2, 22}}, {3, 33, {4, 44}}} |
| 373 | List<String> preOrder = Arrays.asList("1", "11", "2", "22", "3", "33", "4", "44"); |
| 374 | List<String> postOrder = Arrays.asList("2", "22", "4", "44", "3", "33", "1", "11"); |
| 375 | |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 376 | MergeStrategy strategy = |
| 377 | new MergeStrategy() { |
| 378 | @Override |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 379 | public Depset merge(Depset[] sets) throws Exception { |
Googler | ae586a0 | 2019-11-22 11:10:37 -0800 | [diff] [blame] | 380 | Depset leftUnion = Depset.unionOf(sets[0], sets[1]); |
| 381 | Depset rightUnion = Depset.unionOf(sets[2], sets[3]); |
| 382 | Depset union = Depset.unionOf(leftUnion, rightUnion); |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 383 | |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 384 | return union; |
| 385 | } |
| 386 | }; |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 387 | |
| 388 | runComplexOrderTest(strategy, preOrder, postOrder); |
| 389 | } |
| 390 | |
| 391 | @Test |
Jon Brandvein | c8e1cfb | 2017-01-19 18:43:37 +0000 | [diff] [blame] | 392 | public void testOrderManyLevelsOfNesting() throws Exception { |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 393 | // {1, 11, {2, 22, {3, 33, {4, 44}}}} |
| 394 | List<String> preOrder = Arrays.asList("1", "11", "2", "22", "3", "33", "4", "44"); |
| 395 | List<String> postOrder = Arrays.asList("4", "44", "3", "33", "2", "22", "1", "11"); |
| 396 | |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 397 | MergeStrategy strategy = |
| 398 | new MergeStrategy() { |
| 399 | @Override |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 400 | public Depset merge(Depset[] sets) throws Exception { |
Googler | ae586a0 | 2019-11-22 11:10:37 -0800 | [diff] [blame] | 401 | Depset union = Depset.unionOf(sets[2], sets[3]); |
| 402 | union = Depset.unionOf(sets[1], union); |
| 403 | union = Depset.unionOf(sets[0], union); |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 404 | |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 405 | return union; |
| 406 | } |
| 407 | }; |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 408 | |
| 409 | runComplexOrderTest(strategy, preOrder, postOrder); |
| 410 | } |
| 411 | |
cparsons | a7c0afb | 2019-08-30 08:14:51 -0700 | [diff] [blame] | 412 | @Test |
Googler | 7450e10 | 2019-11-26 13:52:51 -0800 | [diff] [blame] | 413 | public void testMutableDepsetElementsLegacyBehavior() throws Exception { |
| 414 | // See b/144992997 and github.com/bazelbuild/bazel/issues/10313. |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 415 | setSemantics("--incompatible_always_check_depset_elements=false"); |
Googler | bf1eaed | 2019-11-25 09:52:57 -0800 | [diff] [blame] | 416 | |
Googler | 7450e10 | 2019-11-26 13:52:51 -0800 | [diff] [blame] | 417 | // Test legacy depset(...) and new depset(direct=...) constructors. |
| 418 | |
| 419 | // mutable list should be an error |
| 420 | checkEvalError("depset elements must not be mutable values", "depset([[1,2,3]])"); |
| 421 | checkEvalError("depsets cannot contain items of type 'list'", "depset(direct=[[1,2,3]])"); |
| 422 | |
| 423 | // struct containing mutable list should be an error |
| 424 | checkEvalError("depset elements must not be mutable values", "depset([struct(a=[])])"); |
| 425 | eval("depset(direct=[struct(a=[])])"); // no error (!) |
| 426 | |
| 427 | // tuple of frozen list currently gives no error (this may change) |
| 428 | update("x", StarlarkList.empty()); |
| 429 | eval("depset([(x,)])"); |
| 430 | eval("depset(direct=[(x,)])"); |
| 431 | |
| 432 | // any list (even frozen) is an error, even with legacy constructor |
| 433 | checkEvalError("depsets cannot contain items of type 'list'", "depset([x])"); |
| 434 | checkEvalError("depsets cannot contain items of type 'list'", "depset(direct=[x])"); |
| 435 | |
| 436 | // toplevel dict is an error, even with legacy constructor |
| 437 | checkEvalError("depset elements must not be mutable values", "depset([{}])"); |
| 438 | checkEvalError("depsets cannot contain items of type 'dict'", "depset(direct=[{}])"); |
| 439 | |
| 440 | // struct containing dict should be an error |
| 441 | checkEvalError("depset elements must not be mutable values", "depset([struct(a={})])"); |
| 442 | eval("depset(direct=[struct(a={})])"); // no error (!) |
| 443 | } |
| 444 | |
| 445 | @Test |
| 446 | public void testMutableDepsetElementsDesiredBehavior() throws Exception { |
| 447 | // See b/144992997 and github.com/bazelbuild/bazel/issues/10313. |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 448 | setSemantics("--incompatible_always_check_depset_elements=true"); |
Googler | 7450e10 | 2019-11-26 13:52:51 -0800 | [diff] [blame] | 449 | |
| 450 | // Test legacy depset(...) and new depset(direct=...) constructors. |
| 451 | |
| 452 | // mutable list should be an error |
Googler | bf1eaed | 2019-11-25 09:52:57 -0800 | [diff] [blame] | 453 | checkEvalError("depset elements must not be mutable values", "depset([[1,2,3]])"); |
| 454 | checkEvalError("depset elements must not be mutable values", "depset(direct=[[1,2,3]])"); |
| 455 | |
Googler | 7450e10 | 2019-11-26 13:52:51 -0800 | [diff] [blame] | 456 | // struct containing mutable list should be an error |
Googler | bf1eaed | 2019-11-25 09:52:57 -0800 | [diff] [blame] | 457 | checkEvalError("depset elements must not be mutable values", "depset([struct(a=[])])"); |
| 458 | checkEvalError("depset elements must not be mutable values", "depset(direct=[struct(a=[])])"); |
| 459 | |
Googler | 7450e10 | 2019-11-26 13:52:51 -0800 | [diff] [blame] | 460 | // tuple of frozen list currently gives no error (this may change) |
Googler | bf1eaed | 2019-11-25 09:52:57 -0800 | [diff] [blame] | 461 | update("x", StarlarkList.empty()); |
Googler | 7450e10 | 2019-11-26 13:52:51 -0800 | [diff] [blame] | 462 | eval("depset([(x,)])"); |
| 463 | eval("depset(direct=[(x,)])"); |
| 464 | |
| 465 | // any list (even frozen) is an error, even with legacy constructor |
| 466 | checkEvalError("depsets cannot contain items of type 'list'", "depset([x,])"); |
| 467 | checkEvalError("depsets cannot contain items of type 'list'", "depset(direct=[x,])"); |
| 468 | |
| 469 | // toplevel dict is an error, even with legacy constructor |
| 470 | checkEvalError("depset elements must not be mutable values", "depset([{}])"); |
| 471 | checkEvalError("depset elements must not be mutable values", "depset(direct=[{}])"); |
| 472 | |
| 473 | // struct containing dict should be an error |
| 474 | checkEvalError("depset elements must not be mutable values", "depset([struct(a={})])"); |
| 475 | checkEvalError("depset elements must not be mutable values", "depset(direct=[struct(a={})])"); |
Googler | bf1eaed | 2019-11-25 09:52:57 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | @Test |
cparsons | a7c0afb | 2019-08-30 08:14:51 -0700 | [diff] [blame] | 479 | public void testDepthExceedsLimitDuringIteration() throws Exception { |
| 480 | NestedSet.setApplicationDepthLimit(2000); |
adonovan | 469d855 | 2020-02-11 09:07:25 -0800 | [diff] [blame] | 481 | new Scenario() |
cparsons | a7c0afb | 2019-08-30 08:14:51 -0700 | [diff] [blame] | 482 | .setUp( |
| 483 | "def create_depset(depth):", |
| 484 | " x = depset([0])", |
| 485 | " for i in range(1, depth):", |
| 486 | " x = depset([i], transitive = [x])", |
laurentlb | 9f9f5ec | 2019-11-15 10:37:49 -0800 | [diff] [blame] | 487 | " for element in x.to_list():", |
cparsons | a7c0afb | 2019-08-30 08:14:51 -0700 | [diff] [blame] | 488 | " str(x)", |
| 489 | " return None") |
| 490 | .testEval("create_depset(1000)", "None") |
| 491 | .testIfErrorContains("depset exceeded maximum depth 2000", "create_depset(3000)"); |
| 492 | } |
| 493 | |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 494 | private interface MergeStrategy { |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 495 | Depset merge(Depset[] sets) throws Exception; |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | private void runComplexOrderTest( |
| 499 | MergeStrategy strategy, List<String> preOrder, List<String> postOrder) throws Exception { |
| 500 | Map<Order, List<String>> expected = createExpectedMap(preOrder, postOrder); |
| 501 | for (Order order : Order.values()) { |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 502 | Depset union = strategy.merge(makeFourSets(order)); |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 503 | assertThat(union.toCollection()).containsExactlyElementsIn(expected.get(order)).inOrder(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | private Map<Order, List<String>> createExpectedMap( |
| 508 | List<String> preOrder, List<String> postOrder) { |
| 509 | Map<Order, List<String>> expected = new HashMap<>(); |
| 510 | |
| 511 | for (Order order : Order.values()) { |
| 512 | expected.put(order, isPostOrder(order) ? postOrder : preOrder); |
| 513 | } |
| 514 | |
| 515 | return expected; |
| 516 | } |
| 517 | |
| 518 | private boolean isPostOrder(Order order) { |
| 519 | return order == Order.STABLE_ORDER || order == Order.COMPILE_ORDER; |
| 520 | } |
| 521 | |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 522 | private Depset[] makeFourSets(Order order) throws Exception { |
| 523 | return new Depset[] { |
Googler | ae586a0 | 2019-11-22 11:10:37 -0800 | [diff] [blame] | 524 | Depset.legacyOf(order, Tuple.of("1", "11")), |
| 525 | Depset.legacyOf(order, Tuple.of("2", "22")), |
| 526 | Depset.legacyOf(order, Tuple.of("3", "33")), |
| 527 | Depset.legacyOf(order, Tuple.of("4", "44")) |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 528 | }; |
Pedro Liberal Fernandez | 45bddab | 2017-01-11 13:31:14 +0000 | [diff] [blame] | 529 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 530 | } |