Skip to content

Commit

Permalink
Add a unit test that validates that similiar RecordTypes collapse in …
Browse files Browse the repository at this point in the history
…the UnionTypeBuilder

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=140668393
  • Loading branch information
concavelenz authored and blickly committed Dec 1, 2016
1 parent 1aa8739 commit 5abfb93
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ void setSynthesized(boolean synthesized) {
* @param propertyNode the node that holds this property definition
* @return The builder itself for chaining purposes.
*/
public RecordTypeBuilder addProperty(String name, JSType type, Node
propertyNode) {
public RecordTypeBuilder addProperty(String name, JSType type, Node propertyNode) {
isEmpty = false;
properties.put(name, new RecordProperty(type, propertyNode));
return this;
Expand Down
7 changes: 7 additions & 0 deletions src/com/google/javascript/rhino/jstype/UnionTypeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
import static com.google.javascript.rhino.jstype.JSTypeNative.UNKNOWN_TYPE;
import static com.google.javascript.rhino.jstype.JSTypeNative.VOID_TYPE;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.javascript.rhino.jstype.JSType.SubtypingMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -121,6 +123,11 @@ ImmutableList<JSType> getAlternates() {
return ImmutableList.copyOf(alternates);
}

@VisibleForTesting
int getAlternatesCount() {
return alternates.size();
}

private boolean isSubtype(
JSType rightType, JSType leftType, boolean isStructural) {
// if thisType or thatType is an unresolved templatized type,
Expand Down
25 changes: 25 additions & 0 deletions test/com/google/javascript/rhino/jstype/UnionTypeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ public void testRemovalOfDupes3() {
assertEquals("(Error|function (): Error)", union.toString());
}

public void testRemovalOfDuplicateRecordTypes1() {
UnionTypeBuilder builder = new UnionTypeBuilder(registry);

addRecordType(builder, false);
addRecordType(builder, false);

assertEquals(1, builder.getAlternatesCount());
}

public void testRemovalOfDuplicateRecordTypes2() {
UnionTypeBuilder builder = new UnionTypeBuilder(registry);

addRecordType(builder, true);
addRecordType(builder, true);

assertEquals(1, builder.getAlternatesCount());
}

private void addRecordType(UnionTypeBuilder builder, boolean inferred) {
RecordTypeBuilder recBuilder = new RecordTypeBuilder(registry);
recBuilder.setSynthesized(inferred);
recBuilder.addProperty("prop", NUMBER_TYPE, null);
builder.addAlternate(recBuilder.build());
}

public void assertUnion(String expected, JSType ... types) {
UnionTypeBuilder builder = new UnionTypeBuilder(registry);
for (JSType type : types) {
Expand Down

0 comments on commit 5abfb93

Please sign in to comment.