blob: bf9672e573d7f91c318fc966a888cd437f6c28e3 [file] [log] [blame]
Nathan Harmata66829662015-03-04 12:05:52 +00001// Copyright 2006-2015 Google Inc. All Rights Reserved.
Ulf Adams89f012d2015-02-26 13:39:28 +00002//
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
15package com.google.devtools.build.lib.syntax;
16
Florian Weikertf31b9472015-08-04 16:36:58 +000017import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000018import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000021
Ulf Adams89f012d2015-02-26 13:39:28 +000022import com.google.common.collect.Lists;
23
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000024import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
Ulf Adams89f012d2015-02-26 13:39:28 +000027
28import java.util.Arrays;
Ulf Adams89f012d2015-02-26 13:39:28 +000029import java.util.LinkedHashMap;
30import java.util.List;
31import java.util.Map;
Florian Weikertf31b9472015-08-04 16:36:58 +000032import java.util.TreeMap;
Ulf Adams89f012d2015-02-26 13:39:28 +000033
34/**
35 * Test properties of the evaluator's datatypes and utility functions
36 * without actually creating any parse trees.
37 */
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000038@RunWith(JUnit4.class)
39public class EvalUtilsTest {
Ulf Adams89f012d2015-02-26 13:39:28 +000040
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000041 private static List<?> makeList(Object... args) {
Ulf Adams89f012d2015-02-26 13:39:28 +000042 return EvalUtils.makeSequence(Arrays.<Object>asList(args), false);
43 }
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000044
45 private static List<?> makeTuple(Object... args) {
Ulf Adams89f012d2015-02-26 13:39:28 +000046 return EvalUtils.makeSequence(Arrays.<Object>asList(args), true);
47 }
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000048
Ulf Adams89f012d2015-02-26 13:39:28 +000049 private static Map<Object, Object> makeDict() {
50 return new LinkedHashMap<>();
51 }
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000052
Ulf Adams89f012d2015-02-26 13:39:28 +000053 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 Nienhuys6682a312015-02-26 14:51:57 +000063 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000064 public void testDataTypeNames() throws Exception {
65 assertEquals("string", EvalUtils.getDataTypeName("foo"));
66 assertEquals("int", EvalUtils.getDataTypeName(3));
Francois-Rene Rideauf249d762015-03-02 08:14:46 +000067 assertEquals("Tuple", EvalUtils.getDataTypeName(makeTuple(1, 2, 3)));
68 assertEquals("List", EvalUtils.getDataTypeName(makeList(1, 2, 3)));
Ulf Adams89f012d2015-02-26 13:39:28 +000069 assertEquals("dict", EvalUtils.getDataTypeName(makeDict()));
70 assertEquals("FilesetEntry", EvalUtils.getDataTypeName(makeFilesetEntry()));
Francois-Rene Rideau0f7ba342015-08-31 16:16:21 +000071 assertEquals("NoneType", EvalUtils.getDataTypeName(Runtime.NONE));
Ulf Adams89f012d2015-02-26 13:39:28 +000072 }
73
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000074 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000075 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 Weikertf31b9472015-08-04 16:36:58 +000083
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 Rideau0f7ba342015-08-31 16:16:21 +000094 map.put(Runtime.NONE, 0);
Florian Weikertf31b9472015-08-04 16:36:58 +000095
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 Adams89f012d2015-02-26 13:39:28 +0000103}