Skip to content

Commit be94031

Browse files
lauraharkercopybara-github
authored andcommitted
Add a simple ObjectColor
PiperOrigin-RevId: 322172708
1 parent bc9138d commit be94031

File tree

8 files changed

+148
-0
lines changed

8 files changed

+148
-0
lines changed

src/com/google/javascript/jscomp/colors/Color.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ public interface Color {
2525

2626
boolean isUnion();
2727

28+
boolean isObject();
29+
2830
ImmutableCollection<Color> getAlternates();
2931
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2020 The Closure Compiler Authors.
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.javascript.jscomp.colors;
18+
19+
import com.google.auto.value.AutoValue;
20+
import com.google.common.collect.ImmutableCollection;
21+
22+
/**
23+
* A user-defined object type. For now each type is defined by a unique class name and file source.
24+
*/
25+
@AutoValue
26+
public abstract class ObjectColor implements Color {
27+
28+
public static ObjectColor create(String className, String fileName) {
29+
return new AutoValue_ObjectColor(className, fileName);
30+
}
31+
32+
@Override
33+
public boolean isPrimitive() {
34+
return false;
35+
}
36+
37+
@Override
38+
public boolean isUnion() {
39+
return false;
40+
}
41+
42+
@Override
43+
public boolean isObject() {
44+
return true;
45+
}
46+
47+
@Override
48+
public ImmutableCollection<Color> getAlternates() {
49+
throw new UnsupportedOperationException();
50+
}
51+
52+
public abstract String getClassName();
53+
54+
public abstract String getFilename();
55+
}

src/com/google/javascript/jscomp/colors/PrimitiveColor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public boolean isUnion() {
4040
return false;
4141
}
4242

43+
@Override
44+
public boolean isObject() {
45+
return false;
46+
}
47+
4348
@Override
4449
public ImmutableCollection<Color> getAlternates() {
4550
// In theory we could consider a primitive a union of a single value. It's not clear whether

src/com/google/javascript/jscomp/colors/UnionColor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public boolean isUnion() {
5353
return true;
5454
}
5555

56+
@Override
57+
public boolean isObject() {
58+
return false;
59+
}
60+
5661
@Override
5762
public abstract ImmutableCollection<Color> getAlternates();
5863
}

src/com/google/javascript/jscomp/testing/ColorSubject.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public void isUnion() {
5656
check("isUnion").that(actualNonNull().isUnion()).isTrue();
5757
}
5858

59+
public void isObject() {
60+
check("isObject").that(actualNonNull().isObject()).isTrue();
61+
}
62+
5963
public void hasAlternates(Color... alternates) {
6064
isUnion();
6165
check("getAlternates().containsExactly()")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2020 The Closure Compiler Authors.
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.javascript.jscomp.colors;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.javascript.jscomp.testing.ColorSubject.assertThat;
21+
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
25+
26+
@RunWith(JUnit4.class)
27+
public class ObjectColorTest {
28+
@Test
29+
public void isPrimitiveReturnsFalse() {
30+
ObjectColor foo = ObjectColor.create("Foo", "test.js");
31+
32+
assertThat(foo.isPrimitive()).isFalse();
33+
}
34+
35+
@Test
36+
public void isObjectReturnsTrue() {
37+
ObjectColor foo = ObjectColor.create("Foo", "test.js");
38+
39+
assertThat(foo).isObject();
40+
}
41+
42+
@Test
43+
public void isUnionHandlesReturnsFalse() {
44+
ObjectColor foo = ObjectColor.create("Foo", "test.js");
45+
46+
assertThat(foo.isUnion()).isFalse();
47+
}
48+
49+
@Test
50+
public void objectEqualityBasedOnClassAndFileName() {
51+
assertThat(ObjectColor.create("Foo", "test.js"))
52+
.isEqualTo(ObjectColor.create("Foo", "test.js"));
53+
}
54+
}

test/com/google/javascript/jscomp/colors/PrimitiveColorTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public void isPrimitiveReturnsTrue() {
3232
assertThat((Color) PrimitiveColor.UNKNOWN).isPrimitive();
3333
}
3434

35+
@Test
36+
public void isObjectReturnsFalse() {
37+
assertThat(PrimitiveColor.UNKNOWN.isObject()).isFalse();
38+
}
39+
3540
@Test
3641
public void isUnionReturnsFalse() {
3742
assertThat(PrimitiveColor.NUMBER.isUnion()).isFalse();

test/com/google/javascript/jscomp/colors/UnionColorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public void isPrimitiveReturnsFalse() {
3535
assertThat(numberOrString.isPrimitive()).isFalse();
3636
}
3737

38+
@Test
39+
public void isObjectReturnsFalse() {
40+
UnionColor numberOrString =
41+
UnionColor.create(ImmutableSet.of(PrimitiveColor.STRING, PrimitiveColor.NUMBER));
42+
43+
assertThat(numberOrString.isObject()).isFalse();
44+
}
45+
3846
@Test
3947
public void isUnionHandlesReturnsTrue() {
4048
UnionColor union =
@@ -91,4 +99,14 @@ public void unknownTypeIsNotSpecialCased() {
9199
assertThat(union).isUnion();
92100
assertThat(union.getAlternates()).hasSize(2);
93101
}
102+
103+
@Test
104+
public void unionOfEquivalentObjectsNotAllowed() {
105+
assertThrows(
106+
IllegalArgumentException.class,
107+
() ->
108+
UnionColor.create(
109+
ImmutableSet.of(
110+
ObjectColor.create("Foo", "test.js"), ObjectColor.create("Foo", "test.js"))));
111+
}
94112
}

0 commit comments

Comments
 (0)