test/com/google/javascript/rhino/jstype/UnionTypeTest.java
/** Tests the behavior of variants type. */
Tests the behavior of variants type.
Tests the behavior of variants type .
@SuppressWarnings("checked")
  @Test
  public void testUnionType() {
    UnionType nullOrString =
        (UnionType) createUnionType(NULL_TYPE, STRING_OBJECT_TYPE);
    UnionType stringOrNull =
        (UnionType) createUnionType(STRING_OBJECT_TYPE, NULL_TYPE);
    assertType(stringOrNull).isStructurallyEqualTo(nullOrString);
    assertType(nullOrString).isStructurallyEqualTo(stringOrNull);
    assertTypeCanAssignToItself(createUnionType(VOID_TYPE, NUMBER_TYPE));
    assertTypeCanAssignToItself(
        createUnionType(NUMBER_TYPE, STRING_TYPE, OBJECT_TYPE));
    assertTypeCanAssignToItself(createUnionType(NUMBER_TYPE, BOOLEAN_TYPE));
    assertTypeCanAssignToItself(createUnionType(VOID_TYPE));
    UnionType nullOrUnknown =
        (UnionType) createUnionType(NULL_TYPE, unresolvedNamedType);
    assertTrue(nullOrUnknown.isUnknownType());
    assertType(NULL_TYPE.getLeastSupertype(nullOrUnknown)).isStructurallyEqualTo(nullOrUnknown);
    assertType(nullOrUnknown.getLeastSupertype(NULL_TYPE)).isStructurallyEqualTo(nullOrUnknown);
    assertType(NULL_TYPE.getGreatestSubtype(nullOrUnknown)).isStructurallyEqualTo(UNKNOWN_TYPE);
    assertType(nullOrUnknown.getGreatestSubtype(NULL_TYPE)).isStructurallyEqualTo(UNKNOWN_TYPE);
    assertTrue(NULL_TYPE.differsFrom(nullOrUnknown));
    assertTrue(nullOrUnknown.differsFrom(NULL_TYPE));
    assertFalse(nullOrUnknown.differsFrom(unresolvedNamedType));
    assertTrue(NULL_TYPE.isSubtypeOf(nullOrUnknown));
    assertTrue(unresolvedNamedType.isSubtype(nullOrUnknown));
    assertTrue(nullOrUnknown.isSubtype(NULL_TYPE));
    assertType(nullOrUnknown.restrictByNotNullOrUndefined())
        .isStructurallyEqualTo(unresolvedNamedType);
    assertType(nullOrString.findPropertyType("length")).isStructurallyEqualTo(NUMBER_TYPE);
    assertEquals(null, nullOrString.findPropertyType("lengthx"));
    Asserts.assertResolvesToSame(nullOrString);
  }
@ SuppressWarnings ( "checked" ) @ Test public void testUnionType ( ) { UnionType nullOrString = ( UnionType ) createUnionType ( NULL_TYPE , STRING_OBJECT_TYPE ) ; UnionType stringOrNull = ( UnionType ) createUnionType ( STRING_OBJECT_TYPE , NULL_TYPE ) ; assertType ( stringOrNull ) . isStructurallyEqualTo ( nullOrString ) ; assertType ( nullOrString ) . isStructurallyEqualTo ( stringOrNull ) ; assertTypeCanAssignToItself ( createUnionType ( VOID_TYPE , NUMBER_TYPE ) ) ; assertTypeCanAssignToItself ( createUnionType ( NUMBER_TYPE , STRING_TYPE , OBJECT_TYPE ) ) ; assertTypeCanAssignToItself ( createUnionType ( NUMBER_TYPE , BOOLEAN_TYPE ) ) ; assertTypeCanAssignToItself ( createUnionType ( VOID_TYPE ) ) ; UnionType nullOrUnknown = ( UnionType ) createUnionType ( NULL_TYPE , unresolvedNamedType ) ; assertTrue ( nullOrUnknown . isUnknownType ( ) ) ; assertType ( NULL_TYPE . getLeastSupertype ( nullOrUnknown ) ) . isStructurallyEqualTo ( nullOrUnknown ) ; assertType ( nullOrUnknown . getLeastSupertype ( NULL_TYPE ) ) . isStructurallyEqualTo ( nullOrUnknown ) ; assertType ( NULL_TYPE . getGreatestSubtype ( nullOrUnknown ) ) . isStructurallyEqualTo ( UNKNOWN_TYPE ) ; assertType ( nullOrUnknown . getGreatestSubtype ( NULL_TYPE ) ) . isStructurallyEqualTo ( UNKNOWN_TYPE ) ; assertTrue ( NULL_TYPE . differsFrom ( nullOrUnknown ) ) ; assertTrue ( nullOrUnknown . differsFrom ( NULL_TYPE ) ) ; assertFalse ( nullOrUnknown . differsFrom ( unresolvedNamedType ) ) ; assertTrue ( NULL_TYPE . isSubtypeOf ( nullOrUnknown ) ) ; assertTrue ( unresolvedNamedType . isSubtype ( nullOrUnknown ) ) ; assertTrue ( nullOrUnknown . isSubtype ( NULL_TYPE ) ) ; assertType ( nullOrUnknown . restrictByNotNullOrUndefined ( ) ) . isStructurallyEqualTo ( unresolvedNamedType ) ; assertType ( nullOrString . findPropertyType ( "length" ) ) . isStructurallyEqualTo ( NUMBER_TYPE ) ; assertEquals ( null , nullOrString . findPropertyType ( "lengthx" ) ) ; Asserts . assertResolvesToSame ( nullOrString ) ; }
Tests the behavior of variants type.
Tests the behavior of variants type .
// findPropertyType