Nathan Harmata | 6682966 | 2015-03-04 12:05:52 +0000 | [diff] [blame] | 1 | // Copyright 2006-2015 Google Inc. 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 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
Florian Weikert | f31b947 | 2015-08-04 16:36:58 +0000 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 21 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
| 23 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 27 | |
| 28 | import java.util.Arrays; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 29 | import java.util.LinkedHashMap; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
Florian Weikert | f31b947 | 2015-08-04 16:36:58 +0000 | [diff] [blame] | 32 | import java.util.TreeMap; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Test properties of the evaluator's datatypes and utility functions |
| 36 | * without actually creating any parse trees. |
| 37 | */ |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 38 | @RunWith(JUnit4.class) |
| 39 | public class EvalUtilsTest { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 40 | |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 41 | private static List<?> makeList(Object... args) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 42 | return EvalUtils.makeSequence(Arrays.<Object>asList(args), false); |
| 43 | } |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 44 | |
| 45 | private static List<?> makeTuple(Object... args) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 46 | return EvalUtils.makeSequence(Arrays.<Object>asList(args), true); |
| 47 | } |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 48 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 49 | private static Map<Object, Object> makeDict() { |
| 50 | return new LinkedHashMap<>(); |
| 51 | } |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 52 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 53 | private static FilesetEntry makeFilesetEntry() { |
| 54 | try { |
| 55 | return new FilesetEntry(Label.parseAbsolute("//foo:bar"), |
| 56 | Lists.<Label>newArrayList(), Lists.newArrayList("xyz"), "", |
| 57 | FilesetEntry.SymlinkBehavior.COPY, "."); |
| 58 | } catch (Label.SyntaxException e) { |
| 59 | throw new RuntimeException("Bad label: ", e); |
| 60 | } |
| 61 | } |
| 62 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 63 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 64 | public void testDataTypeNames() throws Exception { |
| 65 | assertEquals("string", EvalUtils.getDataTypeName("foo")); |
| 66 | assertEquals("int", EvalUtils.getDataTypeName(3)); |
Francois-Rene Rideau | f249d76 | 2015-03-02 08:14:46 +0000 | [diff] [blame] | 67 | assertEquals("Tuple", EvalUtils.getDataTypeName(makeTuple(1, 2, 3))); |
| 68 | assertEquals("List", EvalUtils.getDataTypeName(makeList(1, 2, 3))); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 69 | assertEquals("dict", EvalUtils.getDataTypeName(makeDict())); |
| 70 | assertEquals("FilesetEntry", EvalUtils.getDataTypeName(makeFilesetEntry())); |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 71 | assertEquals("NoneType", EvalUtils.getDataTypeName(Runtime.NONE)); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 74 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 75 | public void testDatatypeMutability() throws Exception { |
| 76 | assertTrue(EvalUtils.isImmutable("foo")); |
| 77 | assertTrue(EvalUtils.isImmutable(3)); |
| 78 | assertTrue(EvalUtils.isImmutable(makeTuple(1, 2, 3))); |
| 79 | assertFalse(EvalUtils.isImmutable(makeList(1, 2, 3))); |
| 80 | assertFalse(EvalUtils.isImmutable(makeDict())); |
| 81 | assertFalse(EvalUtils.isImmutable(makeFilesetEntry())); |
| 82 | } |
Florian Weikert | f31b947 | 2015-08-04 16:36:58 +0000 | [diff] [blame] | 83 | |
| 84 | @Test |
| 85 | public void testComparatorWithDifferentTypes() throws Exception { |
| 86 | TreeMap<Object, Object> map = new TreeMap<>(EvalUtils.SKYLARK_COMPARATOR); |
| 87 | map.put(2, 3); |
| 88 | map.put("1", 5); |
| 89 | map.put(42, 4); |
| 90 | map.put("test", 7); |
| 91 | map.put(-1, 2); |
| 92 | map.put("4", 6); |
| 93 | map.put(2.0, 1); |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 94 | map.put(Runtime.NONE, 0); |
Florian Weikert | f31b947 | 2015-08-04 16:36:58 +0000 | [diff] [blame] | 95 | |
| 96 | int expected = 0; |
| 97 | // Expected order of keys is NoneType -> Double -> Integers -> Strings |
| 98 | for (Object obj : map.values()) { |
| 99 | assertThat(obj).isEqualTo(expected); |
| 100 | ++expected; |
| 101 | } |
| 102 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 103 | } |