1 /* 2 * Copyright 2018 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.turbine.model; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.ImmutableMap; 23 import com.google.common.testing.EqualsTester; 24 import com.google.turbine.binder.bound.EnumConstantValue; 25 import com.google.turbine.binder.bound.TurbineAnnotationValue; 26 import com.google.turbine.binder.bound.TurbineClassValue; 27 import com.google.turbine.binder.sym.ClassSymbol; 28 import com.google.turbine.binder.sym.FieldSymbol; 29 import com.google.turbine.model.Const.ArrayInitValue; 30 import com.google.turbine.model.Const.IntValue; 31 import com.google.turbine.type.AnnoInfo; 32 import com.google.turbine.type.Type.ClassTy; 33 import com.google.turbine.type.Type.PrimTy; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.junit.runners.JUnit4; 37 38 @RunWith(JUnit4.class) 39 public class ConstTest { 40 @Test equalsTest()41 public void equalsTest() { 42 new EqualsTester() 43 .addEqualityGroup(new Const.BooleanValue(true), new Const.BooleanValue(true)) 44 .addEqualityGroup(new Const.BooleanValue(false), new Const.BooleanValue(false)) 45 .addEqualityGroup(new Const.IntValue(1), new Const.IntValue(1)) 46 .addEqualityGroup(new Const.IntValue(2), new Const.IntValue(2)) 47 .addEqualityGroup(new Const.LongValue(1), new Const.LongValue(1)) 48 .addEqualityGroup(new Const.LongValue(2), new Const.LongValue(2)) 49 .addEqualityGroup(new Const.CharValue('x'), new Const.CharValue('x')) 50 .addEqualityGroup(new Const.CharValue('y'), new Const.CharValue('y')) 51 .addEqualityGroup(new Const.FloatValue(1), new Const.FloatValue(1)) 52 .addEqualityGroup(new Const.FloatValue(2), new Const.FloatValue(2)) 53 .addEqualityGroup(new Const.DoubleValue(1), new Const.DoubleValue(1)) 54 .addEqualityGroup(new Const.DoubleValue(2), new Const.DoubleValue(2)) 55 .addEqualityGroup(new Const.StringValue("a"), new Const.StringValue("a")) 56 .addEqualityGroup(new Const.StringValue("b"), new Const.StringValue("b")) 57 .addEqualityGroup(new Const.ShortValue((short) 1), new Const.ShortValue((short) 1)) 58 .addEqualityGroup(new Const.ShortValue((short) 2), new Const.ShortValue((short) 2)) 59 .addEqualityGroup(new Const.ByteValue((byte) 1), new Const.ByteValue((byte) 1)) 60 .addEqualityGroup(new Const.ByteValue((byte) 2), new Const.ByteValue((byte) 2)) 61 .addEqualityGroup( 62 new Const.ArrayInitValue( 63 ImmutableList.of(new Const.IntValue(1), new Const.IntValue(2))), 64 new Const.ArrayInitValue( 65 ImmutableList.of(new Const.IntValue(1), new Const.IntValue(2)))) 66 .addEqualityGroup( 67 new Const.ArrayInitValue( 68 ImmutableList.of(new Const.IntValue(3), new Const.IntValue(4))), 69 new Const.ArrayInitValue( 70 ImmutableList.of(new Const.IntValue(3), new Const.IntValue(4)))) 71 .addEqualityGroup( 72 new TurbineAnnotationValue( 73 new AnnoInfo( 74 null, 75 new ClassSymbol("test/Anno"), 76 null, 77 ImmutableMap.of("value", new Const.IntValue(3)))), 78 new TurbineAnnotationValue( 79 new AnnoInfo( 80 null, 81 new ClassSymbol("test/Anno"), 82 null, 83 ImmutableMap.of("value", new Const.IntValue(3))))) 84 .addEqualityGroup( 85 new TurbineAnnotationValue( 86 new AnnoInfo( 87 null, 88 new ClassSymbol("test/Anno"), 89 null, 90 ImmutableMap.of("value", new Const.IntValue(4)))), 91 new TurbineAnnotationValue( 92 new AnnoInfo( 93 null, 94 new ClassSymbol("test/Anno"), 95 null, 96 ImmutableMap.of("value", new Const.IntValue(4))))) 97 .addEqualityGroup( 98 new TurbineClassValue(ClassTy.asNonParametricClassTy(new ClassSymbol("test/Clazz"))), 99 new TurbineClassValue(ClassTy.asNonParametricClassTy(new ClassSymbol("test/Clazz")))) 100 .addEqualityGroup( 101 new TurbineClassValue(ClassTy.asNonParametricClassTy(new ClassSymbol("test/Other"))), 102 new TurbineClassValue(ClassTy.asNonParametricClassTy(new ClassSymbol("test/Other")))) 103 .addEqualityGroup( 104 new TurbineClassValue(PrimTy.create(TurbineConstantTypeKind.INT, ImmutableList.of())), 105 new TurbineClassValue(PrimTy.create(TurbineConstantTypeKind.INT, ImmutableList.of()))) 106 .testEquals(); 107 } 108 109 @Test toStringTest()110 public void toStringTest() { 111 assertThat(new Const.CharValue('\t').toString()).isEqualTo("\'\\t\'"); 112 assertThat(new EnumConstantValue(new FieldSymbol(new ClassSymbol("Foo"), "CONST")).toString()) 113 .isEqualTo("CONST"); 114 assertThat(makeAnno(ImmutableMap.of())).isEqualTo("@p.Anno"); 115 assertThat(makeAnno(ImmutableMap.of("value", new IntValue(1)))).isEqualTo("@p.Anno(1)"); 116 assertThat(makeAnno(ImmutableMap.of("x", new IntValue(1)))).isEqualTo("@p.Anno(x=1)"); 117 assertThat( 118 makeAnno( 119 ImmutableMap.of("value", new ArrayInitValue(ImmutableList.of(new IntValue(1)))))) 120 .isEqualTo("@p.Anno({1})"); 121 assertThat( 122 makeAnno( 123 ImmutableMap.of( 124 "value", 125 new ArrayInitValue(ImmutableList.of(new IntValue(1), new IntValue(2)))))) 126 .isEqualTo("@p.Anno({1, 2})"); 127 assertThat( 128 makeAnno(ImmutableMap.of("xs", new ArrayInitValue(ImmutableList.of(new IntValue(1)))))) 129 .isEqualTo("@p.Anno(xs={1})"); 130 assertThat(makeAnno(ImmutableMap.of("x", new IntValue(1), "y", new IntValue(2)))) 131 .isEqualTo("@p.Anno(x=1, y=2)"); 132 assertThat(new Const.StringValue("\"").toString()).isEqualTo("\"\\\"\""); 133 assertThat(new Const.ByteValue((byte) 42).toString()).isEqualTo("(byte)0x2a"); 134 assertThat(new Const.ShortValue((short) 42).toString()).isEqualTo("42"); 135 } 136 makeAnno(ImmutableMap<String, Const> value)137 private static String makeAnno(ImmutableMap<String, Const> value) { 138 return new TurbineAnnotationValue(new AnnoInfo(null, new ClassSymbol("p/Anno"), null, value)) 139 .toString(); 140 } 141 } 142