blob: 34b9797ff283e832583aba29da385843eae82442 [file] [log] [blame]
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library analyzer.test.generated.all_the_rest_test;
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/src/dart/ast/utilities.dart' hide ConstantEvaluator;
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/error.dart';
import 'package:analyzer/src/generated/java_core.dart';
import 'package:analyzer/src/generated/java_engine_io.dart';
import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/resolver.dart';
import 'package:analyzer/src/generated/scanner.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/sdk_io.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:analyzer/src/generated/testing/ast_factory.dart';
import 'package:analyzer/src/generated/testing/element_factory.dart';
import 'package:analyzer/src/generated/testing/test_type_provider.dart';
import 'package:analyzer/src/generated/testing/token_factory.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
import 'package:path/path.dart';
import 'package:source_span/source_span.dart';
import 'package:unittest/unittest.dart';
import '../reflective_tests.dart';
import '../utils.dart';
import 'parser_test.dart';
import 'resolver_test.dart';
import 'test_support.dart';
main() {
initializeTestEnvironment();
runReflectiveTests(ContentCacheTest);
runReflectiveTests(CustomUriResolverTest);
runReflectiveTests(DartObjectImplTest);
runReflectiveTests(DartUriResolverTest);
runReflectiveTests(DeclaredVariablesTest);
runReflectiveTests(DirectoryBasedDartSdkTest);
runReflectiveTests(DirectoryBasedSourceContainerTest);
runReflectiveTests(ElementBuilderTest);
runReflectiveTests(ElementLocatorTest);
runReflectiveTests(EnumMemberBuilderTest);
runReflectiveTests(ErrorReporterTest);
runReflectiveTests(ErrorSeverityTest);
runReflectiveTests(ExitDetectorTest);
runReflectiveTests(ExitDetectorTest2);
runReflectiveTests(FileBasedSourceTest);
runReflectiveTests(FileUriResolverTest);
runReflectiveTests(SDKLibrariesReaderTest);
runReflectiveTests(UriKindTest);
}
@reflectiveTest
class ContentCacheTest {
void test_setContents() {
Source source = new TestSource();
ContentCache cache = new ContentCache();
expect(cache.getContents(source), isNull);
expect(cache.getModificationStamp(source), isNull);
String contents = "library lib;";
expect(cache.setContents(source, contents), isNull);
expect(cache.getContents(source), contents);
expect(cache.getModificationStamp(source), isNotNull);
expect(cache.setContents(source, contents), contents);
expect(cache.setContents(source, null), contents);
expect(cache.getContents(source), isNull);
expect(cache.getModificationStamp(source), isNull);
expect(cache.setContents(source, null), isNull);
}
}
@reflectiveTest
class CustomUriResolverTest {
void test_creation() {
expect(new CustomUriResolver({}), isNotNull);
}
void test_resolve_unknown_uri() {
UriResolver resolver =
new CustomUriResolver({'custom:library': '/path/to/library.dart',});
Source result =
resolver.resolveAbsolute(parseUriWithException("custom:non_library"));
expect(result, isNull);
}
void test_resolve_uri() {
String path =
FileUtilities2.createFile("/path/to/library.dart").getAbsolutePath();
UriResolver resolver = new CustomUriResolver({'custom:library': path,});
Source result =
resolver.resolveAbsolute(parseUriWithException("custom:library"));
expect(result, isNotNull);
expect(result.fullName, path);
}
}
@reflectiveTest
class DartObjectImplTest extends EngineTestCase {
TypeProvider _typeProvider = new TestTypeProvider();
void test_add_knownDouble_knownDouble() {
_assertAdd(_doubleValue(3.0), _doubleValue(1.0), _doubleValue(2.0));
}
void test_add_knownDouble_knownInt() {
_assertAdd(_doubleValue(3.0), _doubleValue(1.0), _intValue(2));
}
void test_add_knownDouble_unknownDouble() {
_assertAdd(_doubleValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_add_knownDouble_unknownInt() {
_assertAdd(_doubleValue(null), _doubleValue(1.0), _intValue(null));
}
void test_add_knownInt_knownInt() {
_assertAdd(_intValue(3), _intValue(1), _intValue(2));
}
void test_add_knownInt_knownString() {
_assertAdd(null, _intValue(1), _stringValue("2"));
}
void test_add_knownInt_unknownDouble() {
_assertAdd(_doubleValue(null), _intValue(1), _doubleValue(null));
}
void test_add_knownInt_unknownInt() {
_assertAdd(_intValue(null), _intValue(1), _intValue(null));
}
void test_add_knownString_knownInt() {
_assertAdd(null, _stringValue("1"), _intValue(2));
}
void test_add_knownString_knownString() {
_assertAdd(_stringValue("ab"), _stringValue("a"), _stringValue("b"));
}
void test_add_knownString_unknownString() {
_assertAdd(_stringValue(null), _stringValue("a"), _stringValue(null));
}
void test_add_unknownDouble_knownDouble() {
_assertAdd(_doubleValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_add_unknownDouble_knownInt() {
_assertAdd(_doubleValue(null), _doubleValue(null), _intValue(2));
}
void test_add_unknownInt_knownDouble() {
_assertAdd(_doubleValue(null), _intValue(null), _doubleValue(2.0));
}
void test_add_unknownInt_knownInt() {
_assertAdd(_intValue(null), _intValue(null), _intValue(2));
}
void test_add_unknownString_knownString() {
_assertAdd(_stringValue(null), _stringValue(null), _stringValue("b"));
}
void test_add_unknownString_unknownString() {
_assertAdd(_stringValue(null), _stringValue(null), _stringValue(null));
}
void test_bitAnd_knownInt_knownInt() {
_assertBitAnd(_intValue(2), _intValue(6), _intValue(3));
}
void test_bitAnd_knownInt_knownString() {
_assertBitAnd(null, _intValue(6), _stringValue("3"));
}
void test_bitAnd_knownInt_unknownInt() {
_assertBitAnd(_intValue(null), _intValue(6), _intValue(null));
}
void test_bitAnd_knownString_knownInt() {
_assertBitAnd(null, _stringValue("6"), _intValue(3));
}
void test_bitAnd_unknownInt_knownInt() {
_assertBitAnd(_intValue(null), _intValue(null), _intValue(3));
}
void test_bitAnd_unknownInt_unknownInt() {
_assertBitAnd(_intValue(null), _intValue(null), _intValue(null));
}
void test_bitNot_knownInt() {
_assertBitNot(_intValue(-4), _intValue(3));
}
void test_bitNot_knownString() {
_assertBitNot(null, _stringValue("6"));
}
void test_bitNot_unknownInt() {
_assertBitNot(_intValue(null), _intValue(null));
}
void test_bitOr_knownInt_knownInt() {
_assertBitOr(_intValue(7), _intValue(6), _intValue(3));
}
void test_bitOr_knownInt_knownString() {
_assertBitOr(null, _intValue(6), _stringValue("3"));
}
void test_bitOr_knownInt_unknownInt() {
_assertBitOr(_intValue(null), _intValue(6), _intValue(null));
}
void test_bitOr_knownString_knownInt() {
_assertBitOr(null, _stringValue("6"), _intValue(3));
}
void test_bitOr_unknownInt_knownInt() {
_assertBitOr(_intValue(null), _intValue(null), _intValue(3));
}
void test_bitOr_unknownInt_unknownInt() {
_assertBitOr(_intValue(null), _intValue(null), _intValue(null));
}
void test_bitXor_knownInt_knownInt() {
_assertBitXor(_intValue(5), _intValue(6), _intValue(3));
}
void test_bitXor_knownInt_knownString() {
_assertBitXor(null, _intValue(6), _stringValue("3"));
}
void test_bitXor_knownInt_unknownInt() {
_assertBitXor(_intValue(null), _intValue(6), _intValue(null));
}
void test_bitXor_knownString_knownInt() {
_assertBitXor(null, _stringValue("6"), _intValue(3));
}
void test_bitXor_unknownInt_knownInt() {
_assertBitXor(_intValue(null), _intValue(null), _intValue(3));
}
void test_bitXor_unknownInt_unknownInt() {
_assertBitXor(_intValue(null), _intValue(null), _intValue(null));
}
void test_concatenate_knownInt_knownString() {
_assertConcatenate(null, _intValue(2), _stringValue("def"));
}
void test_concatenate_knownString_knownInt() {
_assertConcatenate(null, _stringValue("abc"), _intValue(3));
}
void test_concatenate_knownString_knownString() {
_assertConcatenate(
_stringValue("abcdef"), _stringValue("abc"), _stringValue("def"));
}
void test_concatenate_knownString_unknownString() {
_assertConcatenate(
_stringValue(null), _stringValue("abc"), _stringValue(null));
}
void test_concatenate_unknownString_knownString() {
_assertConcatenate(
_stringValue(null), _stringValue(null), _stringValue("def"));
}
void test_divide_knownDouble_knownDouble() {
_assertDivide(_doubleValue(3.0), _doubleValue(6.0), _doubleValue(2.0));
}
void test_divide_knownDouble_knownInt() {
_assertDivide(_doubleValue(3.0), _doubleValue(6.0), _intValue(2));
}
void test_divide_knownDouble_unknownDouble() {
_assertDivide(_doubleValue(null), _doubleValue(6.0), _doubleValue(null));
}
void test_divide_knownDouble_unknownInt() {
_assertDivide(_doubleValue(null), _doubleValue(6.0), _intValue(null));
}
void test_divide_knownInt_knownInt() {
_assertDivide(_doubleValue(3.0), _intValue(6), _intValue(2));
}
void test_divide_knownInt_knownString() {
_assertDivide(null, _intValue(6), _stringValue("2"));
}
void test_divide_knownInt_unknownDouble() {
_assertDivide(_doubleValue(null), _intValue(6), _doubleValue(null));
}
void test_divide_knownInt_unknownInt() {
_assertDivide(_doubleValue(null), _intValue(6), _intValue(null));
}
void test_divide_knownString_knownInt() {
_assertDivide(null, _stringValue("6"), _intValue(2));
}
void test_divide_unknownDouble_knownDouble() {
_assertDivide(_doubleValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_divide_unknownDouble_knownInt() {
_assertDivide(_doubleValue(null), _doubleValue(null), _intValue(2));
}
void test_divide_unknownInt_knownDouble() {
_assertDivide(_doubleValue(null), _intValue(null), _doubleValue(2.0));
}
void test_divide_unknownInt_knownInt() {
_assertDivide(_doubleValue(null), _intValue(null), _intValue(2));
}
void test_equalEqual_bool_false() {
_assertEqualEqual(_boolValue(false), _boolValue(false), _boolValue(true));
}
void test_equalEqual_bool_true() {
_assertEqualEqual(_boolValue(true), _boolValue(true), _boolValue(true));
}
void test_equalEqual_bool_unknown() {
_assertEqualEqual(_boolValue(null), _boolValue(null), _boolValue(false));
}
void test_equalEqual_double_false() {
_assertEqualEqual(_boolValue(false), _doubleValue(2.0), _doubleValue(4.0));
}
void test_equalEqual_double_true() {
_assertEqualEqual(_boolValue(true), _doubleValue(2.0), _doubleValue(2.0));
}
void test_equalEqual_double_unknown() {
_assertEqualEqual(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_equalEqual_int_false() {
_assertEqualEqual(_boolValue(false), _intValue(-5), _intValue(5));
}
void test_equalEqual_int_true() {
_assertEqualEqual(_boolValue(true), _intValue(5), _intValue(5));
}
void test_equalEqual_int_unknown() {
_assertEqualEqual(_boolValue(null), _intValue(null), _intValue(3));
}
void test_equalEqual_list_empty() {
_assertEqualEqual(null, _listValue(), _listValue());
}
void test_equalEqual_list_false() {
_assertEqualEqual(null, _listValue(), _listValue());
}
void test_equalEqual_map_empty() {
_assertEqualEqual(null, _mapValue(), _mapValue());
}
void test_equalEqual_map_false() {
_assertEqualEqual(null, _mapValue(), _mapValue());
}
void test_equalEqual_null() {
_assertEqualEqual(_boolValue(true), _nullValue(), _nullValue());
}
void test_equalEqual_string_false() {
_assertEqualEqual(
_boolValue(false), _stringValue("abc"), _stringValue("def"));
}
void test_equalEqual_string_true() {
_assertEqualEqual(
_boolValue(true), _stringValue("abc"), _stringValue("abc"));
}
void test_equalEqual_string_unknown() {
_assertEqualEqual(
_boolValue(null), _stringValue(null), _stringValue("def"));
}
void test_equals_list_false_differentSizes() {
expect(
_listValue([_boolValue(true)]) ==
_listValue([_boolValue(true), _boolValue(false)]),
isFalse);
}
void test_equals_list_false_sameSize() {
expect(_listValue([_boolValue(true)]) == _listValue([_boolValue(false)]),
isFalse);
}
void test_equals_list_true_empty() {
expect(_listValue(), _listValue());
}
void test_equals_list_true_nonEmpty() {
expect(_listValue([_boolValue(true)]), _listValue([_boolValue(true)]));
}
void test_equals_map_true_empty() {
expect(_mapValue(), _mapValue());
}
void test_equals_symbol_false() {
expect(_symbolValue("a") == _symbolValue("b"), isFalse);
}
void test_equals_symbol_true() {
expect(_symbolValue("a"), _symbolValue("a"));
}
void test_getValue_bool_false() {
expect(_boolValue(false).toBoolValue(), false);
}
void test_getValue_bool_true() {
expect(_boolValue(true).toBoolValue(), true);
}
void test_getValue_bool_unknown() {
expect(_boolValue(null).toBoolValue(), isNull);
}
void test_getValue_double_known() {
double value = 2.3;
expect(_doubleValue(value).toDoubleValue(), value);
}
void test_getValue_double_unknown() {
expect(_doubleValue(null).toDoubleValue(), isNull);
}
void test_getValue_int_known() {
int value = 23;
expect(_intValue(value).toIntValue(), value);
}
void test_getValue_int_unknown() {
expect(_intValue(null).toIntValue(), isNull);
}
void test_getValue_list_empty() {
Object result = _listValue().toListValue();
_assertInstanceOfObjectArray(result);
List<Object> array = result as List<Object>;
expect(array, hasLength(0));
}
void test_getValue_list_valid() {
Object result = _listValue([_intValue(23)]).toListValue();
_assertInstanceOfObjectArray(result);
List<Object> array = result as List<Object>;
expect(array, hasLength(1));
}
void test_getValue_map_empty() {
Object result = _mapValue().toMapValue();
EngineTestCase.assertInstanceOf((obj) => obj is Map, Map, result);
Map map = result as Map;
expect(map, hasLength(0));
}
void test_getValue_map_valid() {
Object result =
_mapValue([_stringValue("key"), _stringValue("value")]).toMapValue();
EngineTestCase.assertInstanceOf((obj) => obj is Map, Map, result);
Map map = result as Map;
expect(map, hasLength(1));
}
void test_getValue_null() {
expect(_nullValue().isNull, isTrue);
}
void test_getValue_string_known() {
String value = "twenty-three";
expect(_stringValue(value).toStringValue(), value);
}
void test_getValue_string_unknown() {
expect(_stringValue(null).toStringValue(), isNull);
}
void test_greaterThan_knownDouble_knownDouble_false() {
_assertGreaterThan(_boolValue(false), _doubleValue(1.0), _doubleValue(2.0));
}
void test_greaterThan_knownDouble_knownDouble_true() {
_assertGreaterThan(_boolValue(true), _doubleValue(2.0), _doubleValue(1.0));
}
void test_greaterThan_knownDouble_knownInt_false() {
_assertGreaterThan(_boolValue(false), _doubleValue(1.0), _intValue(2));
}
void test_greaterThan_knownDouble_knownInt_true() {
_assertGreaterThan(_boolValue(true), _doubleValue(2.0), _intValue(1));
}
void test_greaterThan_knownDouble_unknownDouble() {
_assertGreaterThan(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_greaterThan_knownDouble_unknownInt() {
_assertGreaterThan(_boolValue(null), _doubleValue(1.0), _intValue(null));
}
void test_greaterThan_knownInt_knownInt_false() {
_assertGreaterThan(_boolValue(false), _intValue(1), _intValue(2));
}
void test_greaterThan_knownInt_knownInt_true() {
_assertGreaterThan(_boolValue(true), _intValue(2), _intValue(1));
}
void test_greaterThan_knownInt_knownString() {
_assertGreaterThan(null, _intValue(1), _stringValue("2"));
}
void test_greaterThan_knownInt_unknownDouble() {
_assertGreaterThan(_boolValue(null), _intValue(1), _doubleValue(null));
}
void test_greaterThan_knownInt_unknownInt() {
_assertGreaterThan(_boolValue(null), _intValue(1), _intValue(null));
}
void test_greaterThan_knownString_knownInt() {
_assertGreaterThan(null, _stringValue("1"), _intValue(2));
}
void test_greaterThan_unknownDouble_knownDouble() {
_assertGreaterThan(_boolValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_greaterThan_unknownDouble_knownInt() {
_assertGreaterThan(_boolValue(null), _doubleValue(null), _intValue(2));
}
void test_greaterThan_unknownInt_knownDouble() {
_assertGreaterThan(_boolValue(null), _intValue(null), _doubleValue(2.0));
}
void test_greaterThan_unknownInt_knownInt() {
_assertGreaterThan(_boolValue(null), _intValue(null), _intValue(2));
}
void test_greaterThanOrEqual_knownDouble_knownDouble_false() {
_assertGreaterThanOrEqual(
_boolValue(false), _doubleValue(1.0), _doubleValue(2.0));
}
void test_greaterThanOrEqual_knownDouble_knownDouble_true() {
_assertGreaterThanOrEqual(
_boolValue(true), _doubleValue(2.0), _doubleValue(1.0));
}
void test_greaterThanOrEqual_knownDouble_knownInt_false() {
_assertGreaterThanOrEqual(
_boolValue(false), _doubleValue(1.0), _intValue(2));
}
void test_greaterThanOrEqual_knownDouble_knownInt_true() {
_assertGreaterThanOrEqual(
_boolValue(true), _doubleValue(2.0), _intValue(1));
}
void test_greaterThanOrEqual_knownDouble_unknownDouble() {
_assertGreaterThanOrEqual(
_boolValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_greaterThanOrEqual_knownDouble_unknownInt() {
_assertGreaterThanOrEqual(
_boolValue(null), _doubleValue(1.0), _intValue(null));
}
void test_greaterThanOrEqual_knownInt_knownInt_false() {
_assertGreaterThanOrEqual(_boolValue(false), _intValue(1), _intValue(2));
}
void test_greaterThanOrEqual_knownInt_knownInt_true() {
_assertGreaterThanOrEqual(_boolValue(true), _intValue(2), _intValue(2));
}
void test_greaterThanOrEqual_knownInt_knownString() {
_assertGreaterThanOrEqual(null, _intValue(1), _stringValue("2"));
}
void test_greaterThanOrEqual_knownInt_unknownDouble() {
_assertGreaterThanOrEqual(
_boolValue(null), _intValue(1), _doubleValue(null));
}
void test_greaterThanOrEqual_knownInt_unknownInt() {
_assertGreaterThanOrEqual(_boolValue(null), _intValue(1), _intValue(null));
}
void test_greaterThanOrEqual_knownString_knownInt() {
_assertGreaterThanOrEqual(null, _stringValue("1"), _intValue(2));
}
void test_greaterThanOrEqual_unknownDouble_knownDouble() {
_assertGreaterThanOrEqual(
_boolValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_greaterThanOrEqual_unknownDouble_knownInt() {
_assertGreaterThanOrEqual(
_boolValue(null), _doubleValue(null), _intValue(2));
}
void test_greaterThanOrEqual_unknownInt_knownDouble() {
_assertGreaterThanOrEqual(
_boolValue(null), _intValue(null), _doubleValue(2.0));
}
void test_greaterThanOrEqual_unknownInt_knownInt() {
_assertGreaterThanOrEqual(_boolValue(null), _intValue(null), _intValue(2));
}
void test_hasKnownValue_bool_false() {
expect(_boolValue(false).hasKnownValue, isTrue);
}
void test_hasKnownValue_bool_true() {
expect(_boolValue(true).hasKnownValue, isTrue);
}
void test_hasKnownValue_bool_unknown() {
expect(_boolValue(null).hasKnownValue, isFalse);
}
void test_hasKnownValue_double_known() {
expect(_doubleValue(2.3).hasKnownValue, isTrue);
}
void test_hasKnownValue_double_unknown() {
expect(_doubleValue(null).hasKnownValue, isFalse);
}
void test_hasKnownValue_dynamic() {
expect(_dynamicValue().hasKnownValue, isTrue);
}
void test_hasKnownValue_int_known() {
expect(_intValue(23).hasKnownValue, isTrue);
}
void test_hasKnownValue_int_unknown() {
expect(_intValue(null).hasKnownValue, isFalse);
}
void test_hasKnownValue_list_empty() {
expect(_listValue().hasKnownValue, isTrue);
}
void test_hasKnownValue_list_invalidElement() {
expect(_listValue([_dynamicValue]).hasKnownValue, isTrue);
}
void test_hasKnownValue_list_valid() {
expect(_listValue([_intValue(23)]).hasKnownValue, isTrue);
}
void test_hasKnownValue_map_empty() {
expect(_mapValue().hasKnownValue, isTrue);
}
void test_hasKnownValue_map_invalidKey() {
expect(_mapValue([_dynamicValue(), _stringValue("value")]).hasKnownValue,
isTrue);
}
void test_hasKnownValue_map_invalidValue() {
expect(_mapValue([_stringValue("key"), _dynamicValue()]).hasKnownValue,
isTrue);
}
void test_hasKnownValue_map_valid() {
expect(
_mapValue([_stringValue("key"), _stringValue("value")]).hasKnownValue,
isTrue);
}
void test_hasKnownValue_null() {
expect(_nullValue().hasKnownValue, isTrue);
}
void test_hasKnownValue_num() {
expect(_numValue().hasKnownValue, isFalse);
}
void test_hasKnownValue_string_known() {
expect(_stringValue("twenty-three").hasKnownValue, isTrue);
}
void test_hasKnownValue_string_unknown() {
expect(_stringValue(null).hasKnownValue, isFalse);
}
void test_identical_bool_false() {
_assertIdentical(_boolValue(false), _boolValue(false), _boolValue(true));
}
void test_identical_bool_true() {
_assertIdentical(_boolValue(true), _boolValue(true), _boolValue(true));
}
void test_identical_bool_unknown() {
_assertIdentical(_boolValue(null), _boolValue(null), _boolValue(false));
}
void test_identical_double_false() {
_assertIdentical(_boolValue(false), _doubleValue(2.0), _doubleValue(4.0));
}
void test_identical_double_true() {
_assertIdentical(_boolValue(true), _doubleValue(2.0), _doubleValue(2.0));
}
void test_identical_double_unknown() {
_assertIdentical(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_identical_int_false() {
_assertIdentical(_boolValue(false), _intValue(-5), _intValue(5));
}
void test_identical_int_true() {
_assertIdentical(_boolValue(true), _intValue(5), _intValue(5));
}
void test_identical_int_unknown() {
_assertIdentical(_boolValue(null), _intValue(null), _intValue(3));
}
void test_identical_list_empty() {
_assertIdentical(_boolValue(true), _listValue(), _listValue());
}
void test_identical_list_false() {
_assertIdentical(
_boolValue(false), _listValue(), _listValue([_intValue(3)]));
}
void test_identical_map_empty() {
_assertIdentical(_boolValue(true), _mapValue(), _mapValue());
}
void test_identical_map_false() {
_assertIdentical(_boolValue(false), _mapValue(),
_mapValue([_intValue(1), _intValue(2)]));
}
void test_identical_null() {
_assertIdentical(_boolValue(true), _nullValue(), _nullValue());
}
void test_identical_string_false() {
_assertIdentical(
_boolValue(false), _stringValue("abc"), _stringValue("def"));
}
void test_identical_string_true() {
_assertIdentical(
_boolValue(true), _stringValue("abc"), _stringValue("abc"));
}
void test_identical_string_unknown() {
_assertIdentical(_boolValue(null), _stringValue(null), _stringValue("def"));
}
void test_integerDivide_knownDouble_knownDouble() {
_assertIntegerDivide(_intValue(3), _doubleValue(6.0), _doubleValue(2.0));
}
void test_integerDivide_knownDouble_knownInt() {
_assertIntegerDivide(_intValue(3), _doubleValue(6.0), _intValue(2));
}
void test_integerDivide_knownDouble_unknownDouble() {
_assertIntegerDivide(
_intValue(null), _doubleValue(6.0), _doubleValue(null));
}
void test_integerDivide_knownDouble_unknownInt() {
_assertIntegerDivide(_intValue(null), _doubleValue(6.0), _intValue(null));
}
void test_integerDivide_knownInt_knownInt() {
_assertIntegerDivide(_intValue(3), _intValue(6), _intValue(2));
}
void test_integerDivide_knownInt_knownString() {
_assertIntegerDivide(null, _intValue(6), _stringValue("2"));
}
void test_integerDivide_knownInt_unknownDouble() {
_assertIntegerDivide(_intValue(null), _intValue(6), _doubleValue(null));
}
void test_integerDivide_knownInt_unknownInt() {
_assertIntegerDivide(_intValue(null), _intValue(6), _intValue(null));
}
void test_integerDivide_knownString_knownInt() {
_assertIntegerDivide(null, _stringValue("6"), _intValue(2));
}
void test_integerDivide_unknownDouble_knownDouble() {
_assertIntegerDivide(
_intValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_integerDivide_unknownDouble_knownInt() {
_assertIntegerDivide(_intValue(null), _doubleValue(null), _intValue(2));
}
void test_integerDivide_unknownInt_knownDouble() {
_assertIntegerDivide(_intValue(null), _intValue(null), _doubleValue(2.0));
}
void test_integerDivide_unknownInt_knownInt() {
_assertIntegerDivide(_intValue(null), _intValue(null), _intValue(2));
}
void test_isBoolNumStringOrNull_bool_false() {
expect(_boolValue(false).isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_bool_true() {
expect(_boolValue(true).isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_bool_unknown() {
expect(_boolValue(null).isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_double_known() {
expect(_doubleValue(2.3).isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_double_unknown() {
expect(_doubleValue(null).isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_dynamic() {
expect(_dynamicValue().isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_int_known() {
expect(_intValue(23).isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_int_unknown() {
expect(_intValue(null).isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_list() {
expect(_listValue().isBoolNumStringOrNull, isFalse);
}
void test_isBoolNumStringOrNull_null() {
expect(_nullValue().isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_num() {
expect(_numValue().isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_string_known() {
expect(_stringValue("twenty-three").isBoolNumStringOrNull, isTrue);
}
void test_isBoolNumStringOrNull_string_unknown() {
expect(_stringValue(null).isBoolNumStringOrNull, isTrue);
}
void test_lessThan_knownDouble_knownDouble_false() {
_assertLessThan(_boolValue(false), _doubleValue(2.0), _doubleValue(1.0));
}
void test_lessThan_knownDouble_knownDouble_true() {
_assertLessThan(_boolValue(true), _doubleValue(1.0), _doubleValue(2.0));
}
void test_lessThan_knownDouble_knownInt_false() {
_assertLessThan(_boolValue(false), _doubleValue(2.0), _intValue(1));
}
void test_lessThan_knownDouble_knownInt_true() {
_assertLessThan(_boolValue(true), _doubleValue(1.0), _intValue(2));
}
void test_lessThan_knownDouble_unknownDouble() {
_assertLessThan(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_lessThan_knownDouble_unknownInt() {
_assertLessThan(_boolValue(null), _doubleValue(1.0), _intValue(null));
}
void test_lessThan_knownInt_knownInt_false() {
_assertLessThan(_boolValue(false), _intValue(2), _intValue(1));
}
void test_lessThan_knownInt_knownInt_true() {
_assertLessThan(_boolValue(true), _intValue(1), _intValue(2));
}
void test_lessThan_knownInt_knownString() {
_assertLessThan(null, _intValue(1), _stringValue("2"));
}
void test_lessThan_knownInt_unknownDouble() {
_assertLessThan(_boolValue(null), _intValue(1), _doubleValue(null));
}
void test_lessThan_knownInt_unknownInt() {
_assertLessThan(_boolValue(null), _intValue(1), _intValue(null));
}
void test_lessThan_knownString_knownInt() {
_assertLessThan(null, _stringValue("1"), _intValue(2));
}
void test_lessThan_unknownDouble_knownDouble() {
_assertLessThan(_boolValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_lessThan_unknownDouble_knownInt() {
_assertLessThan(_boolValue(null), _doubleValue(null), _intValue(2));
}
void test_lessThan_unknownInt_knownDouble() {
_assertLessThan(_boolValue(null), _intValue(null), _doubleValue(2.0));
}
void test_lessThan_unknownInt_knownInt() {
_assertLessThan(_boolValue(null), _intValue(null), _intValue(2));
}
void test_lessThanOrEqual_knownDouble_knownDouble_false() {
_assertLessThanOrEqual(
_boolValue(false), _doubleValue(2.0), _doubleValue(1.0));
}
void test_lessThanOrEqual_knownDouble_knownDouble_true() {
_assertLessThanOrEqual(
_boolValue(true), _doubleValue(1.0), _doubleValue(2.0));
}
void test_lessThanOrEqual_knownDouble_knownInt_false() {
_assertLessThanOrEqual(_boolValue(false), _doubleValue(2.0), _intValue(1));
}
void test_lessThanOrEqual_knownDouble_knownInt_true() {
_assertLessThanOrEqual(_boolValue(true), _doubleValue(1.0), _intValue(2));
}
void test_lessThanOrEqual_knownDouble_unknownDouble() {
_assertLessThanOrEqual(
_boolValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_lessThanOrEqual_knownDouble_unknownInt() {
_assertLessThanOrEqual(
_boolValue(null), _doubleValue(1.0), _intValue(null));
}
void test_lessThanOrEqual_knownInt_knownInt_false() {
_assertLessThanOrEqual(_boolValue(false), _intValue(2), _intValue(1));
}
void test_lessThanOrEqual_knownInt_knownInt_true() {
_assertLessThanOrEqual(_boolValue(true), _intValue(1), _intValue(2));
}
void test_lessThanOrEqual_knownInt_knownString() {
_assertLessThanOrEqual(null, _intValue(1), _stringValue("2"));
}
void test_lessThanOrEqual_knownInt_unknownDouble() {
_assertLessThanOrEqual(_boolValue(null), _intValue(1), _doubleValue(null));
}
void test_lessThanOrEqual_knownInt_unknownInt() {
_assertLessThanOrEqual(_boolValue(null), _intValue(1), _intValue(null));
}
void test_lessThanOrEqual_knownString_knownInt() {
_assertLessThanOrEqual(null, _stringValue("1"), _intValue(2));
}
void test_lessThanOrEqual_unknownDouble_knownDouble() {
_assertLessThanOrEqual(
_boolValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_lessThanOrEqual_unknownDouble_knownInt() {
_assertLessThanOrEqual(_boolValue(null), _doubleValue(null), _intValue(2));
}
void test_lessThanOrEqual_unknownInt_knownDouble() {
_assertLessThanOrEqual(
_boolValue(null), _intValue(null), _doubleValue(2.0));
}
void test_lessThanOrEqual_unknownInt_knownInt() {
_assertLessThanOrEqual(_boolValue(null), _intValue(null), _intValue(2));
}
void test_logicalAnd_false_false() {
_assertLogicalAnd(_boolValue(false), _boolValue(false), _boolValue(false));
}
void test_logicalAnd_false_null() {
try {
_assertLogicalAnd(_boolValue(false), _boolValue(false), _nullValue());
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalAnd_false_string() {
try {
_assertLogicalAnd(
_boolValue(false), _boolValue(false), _stringValue("false"));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalAnd_false_true() {
_assertLogicalAnd(_boolValue(false), _boolValue(false), _boolValue(true));
}
void test_logicalAnd_null_false() {
try {
_assertLogicalAnd(_boolValue(false), _nullValue(), _boolValue(false));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalAnd_null_true() {
try {
_assertLogicalAnd(_boolValue(false), _nullValue(), _boolValue(true));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalAnd_string_false() {
try {
_assertLogicalAnd(
_boolValue(false), _stringValue("true"), _boolValue(false));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalAnd_string_true() {
try {
_assertLogicalAnd(
_boolValue(false), _stringValue("false"), _boolValue(true));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalAnd_true_false() {
_assertLogicalAnd(_boolValue(false), _boolValue(true), _boolValue(false));
}
void test_logicalAnd_true_null() {
_assertLogicalAnd(null, _boolValue(true), _nullValue());
}
void test_logicalAnd_true_string() {
try {
_assertLogicalAnd(
_boolValue(false), _boolValue(true), _stringValue("true"));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalAnd_true_true() {
_assertLogicalAnd(_boolValue(true), _boolValue(true), _boolValue(true));
}
void test_logicalNot_false() {
_assertLogicalNot(_boolValue(true), _boolValue(false));
}
void test_logicalNot_null() {
_assertLogicalNot(null, _nullValue());
}
void test_logicalNot_string() {
try {
_assertLogicalNot(_boolValue(true), _stringValue(null));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalNot_true() {
_assertLogicalNot(_boolValue(false), _boolValue(true));
}
void test_logicalNot_unknown() {
_assertLogicalNot(_boolValue(null), _boolValue(null));
}
void test_logicalOr_false_false() {
_assertLogicalOr(_boolValue(false), _boolValue(false), _boolValue(false));
}
void test_logicalOr_false_null() {
_assertLogicalOr(null, _boolValue(false), _nullValue());
}
void test_logicalOr_false_string() {
try {
_assertLogicalOr(
_boolValue(false), _boolValue(false), _stringValue("false"));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalOr_false_true() {
_assertLogicalOr(_boolValue(true), _boolValue(false), _boolValue(true));
}
void test_logicalOr_null_false() {
try {
_assertLogicalOr(_boolValue(false), _nullValue(), _boolValue(false));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalOr_null_true() {
try {
_assertLogicalOr(_boolValue(true), _nullValue(), _boolValue(true));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalOr_string_false() {
try {
_assertLogicalOr(
_boolValue(false), _stringValue("true"), _boolValue(false));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalOr_string_true() {
try {
_assertLogicalOr(
_boolValue(true), _stringValue("false"), _boolValue(true));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalOr_true_false() {
_assertLogicalOr(_boolValue(true), _boolValue(true), _boolValue(false));
}
void test_logicalOr_true_null() {
try {
_assertLogicalOr(_boolValue(true), _boolValue(true), _nullValue());
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalOr_true_string() {
try {
_assertLogicalOr(
_boolValue(true), _boolValue(true), _stringValue("true"));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_logicalOr_true_true() {
_assertLogicalOr(_boolValue(true), _boolValue(true), _boolValue(true));
}
void test_minus_knownDouble_knownDouble() {
_assertMinus(_doubleValue(1.0), _doubleValue(4.0), _doubleValue(3.0));
}
void test_minus_knownDouble_knownInt() {
_assertMinus(_doubleValue(1.0), _doubleValue(4.0), _intValue(3));
}
void test_minus_knownDouble_unknownDouble() {
_assertMinus(_doubleValue(null), _doubleValue(4.0), _doubleValue(null));
}
void test_minus_knownDouble_unknownInt() {
_assertMinus(_doubleValue(null), _doubleValue(4.0), _intValue(null));
}
void test_minus_knownInt_knownInt() {
_assertMinus(_intValue(1), _intValue(4), _intValue(3));
}
void test_minus_knownInt_knownString() {
_assertMinus(null, _intValue(4), _stringValue("3"));
}
void test_minus_knownInt_unknownDouble() {
_assertMinus(_doubleValue(null), _intValue(4), _doubleValue(null));
}
void test_minus_knownInt_unknownInt() {
_assertMinus(_intValue(null), _intValue(4), _intValue(null));
}
void test_minus_knownString_knownInt() {
_assertMinus(null, _stringValue("4"), _intValue(3));
}
void test_minus_unknownDouble_knownDouble() {
_assertMinus(_doubleValue(null), _doubleValue(null), _doubleValue(3.0));
}
void test_minus_unknownDouble_knownInt() {
_assertMinus(_doubleValue(null), _doubleValue(null), _intValue(3));
}
void test_minus_unknownInt_knownDouble() {
_assertMinus(_doubleValue(null), _intValue(null), _doubleValue(3.0));
}
void test_minus_unknownInt_knownInt() {
_assertMinus(_intValue(null), _intValue(null), _intValue(3));
}
void test_negated_double_known() {
_assertNegated(_doubleValue(2.0), _doubleValue(-2.0));
}
void test_negated_double_unknown() {
_assertNegated(_doubleValue(null), _doubleValue(null));
}
void test_negated_int_known() {
_assertNegated(_intValue(-3), _intValue(3));
}
void test_negated_int_unknown() {
_assertNegated(_intValue(null), _intValue(null));
}
void test_negated_string() {
_assertNegated(null, _stringValue(null));
}
void test_notEqual_bool_false() {
_assertNotEqual(_boolValue(false), _boolValue(true), _boolValue(true));
}
void test_notEqual_bool_true() {
_assertNotEqual(_boolValue(true), _boolValue(false), _boolValue(true));
}
void test_notEqual_bool_unknown() {
_assertNotEqual(_boolValue(null), _boolValue(null), _boolValue(false));
}
void test_notEqual_double_false() {
_assertNotEqual(_boolValue(false), _doubleValue(2.0), _doubleValue(2.0));
}
void test_notEqual_double_true() {
_assertNotEqual(_boolValue(true), _doubleValue(2.0), _doubleValue(4.0));
}
void test_notEqual_double_unknown() {
_assertNotEqual(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
}
void test_notEqual_int_false() {
_assertNotEqual(_boolValue(false), _intValue(5), _intValue(5));
}
void test_notEqual_int_true() {
_assertNotEqual(_boolValue(true), _intValue(-5), _intValue(5));
}
void test_notEqual_int_unknown() {
_assertNotEqual(_boolValue(null), _intValue(null), _intValue(3));
}
void test_notEqual_null() {
_assertNotEqual(_boolValue(false), _nullValue(), _nullValue());
}
void test_notEqual_string_false() {
_assertNotEqual(
_boolValue(false), _stringValue("abc"), _stringValue("abc"));
}
void test_notEqual_string_true() {
_assertNotEqual(_boolValue(true), _stringValue("abc"), _stringValue("def"));
}
void test_notEqual_string_unknown() {
_assertNotEqual(_boolValue(null), _stringValue(null), _stringValue("def"));
}
void test_performToString_bool_false() {
_assertPerformToString(_stringValue("false"), _boolValue(false));
}
void test_performToString_bool_true() {
_assertPerformToString(_stringValue("true"), _boolValue(true));
}
void test_performToString_bool_unknown() {
_assertPerformToString(_stringValue(null), _boolValue(null));
}
void test_performToString_double_known() {
_assertPerformToString(_stringValue("2.0"), _doubleValue(2.0));
}
void test_performToString_double_unknown() {
_assertPerformToString(_stringValue(null), _doubleValue(null));
}
void test_performToString_int_known() {
_assertPerformToString(_stringValue("5"), _intValue(5));
}
void test_performToString_int_unknown() {
_assertPerformToString(_stringValue(null), _intValue(null));
}
void test_performToString_null() {
_assertPerformToString(_stringValue("null"), _nullValue());
}
void test_performToString_string_known() {
_assertPerformToString(_stringValue("abc"), _stringValue("abc"));
}
void test_performToString_string_unknown() {
_assertPerformToString(_stringValue(null), _stringValue(null));
}
void test_remainder_knownDouble_knownDouble() {
_assertRemainder(_doubleValue(1.0), _doubleValue(7.0), _doubleValue(2.0));
}
void test_remainder_knownDouble_knownInt() {
_assertRemainder(_doubleValue(1.0), _doubleValue(7.0), _intValue(2));
}
void test_remainder_knownDouble_unknownDouble() {
_assertRemainder(_doubleValue(null), _doubleValue(7.0), _doubleValue(null));
}
void test_remainder_knownDouble_unknownInt() {
_assertRemainder(_doubleValue(null), _doubleValue(6.0), _intValue(null));
}
void test_remainder_knownInt_knownInt() {
_assertRemainder(_intValue(1), _intValue(7), _intValue(2));
}
void test_remainder_knownInt_knownString() {
_assertRemainder(null, _intValue(7), _stringValue("2"));
}
void test_remainder_knownInt_unknownDouble() {
_assertRemainder(_doubleValue(null), _intValue(7), _doubleValue(null));
}
void test_remainder_knownInt_unknownInt() {
_assertRemainder(_intValue(null), _intValue(7), _intValue(null));
}
void test_remainder_knownString_knownInt() {
_assertRemainder(null, _stringValue("7"), _intValue(2));
}
void test_remainder_unknownDouble_knownDouble() {
_assertRemainder(_doubleValue(null), _doubleValue(null), _doubleValue(2.0));
}
void test_remainder_unknownDouble_knownInt() {
_assertRemainder(_doubleValue(null), _doubleValue(null), _intValue(2));
}
void test_remainder_unknownInt_knownDouble() {
_assertRemainder(_doubleValue(null), _intValue(null), _doubleValue(2.0));
}
void test_remainder_unknownInt_knownInt() {
_assertRemainder(_intValue(null), _intValue(null), _intValue(2));
}
void test_shiftLeft_knownInt_knownInt() {
_assertShiftLeft(_intValue(48), _intValue(6), _intValue(3));
}
void test_shiftLeft_knownInt_knownString() {
_assertShiftLeft(null, _intValue(6), _stringValue(null));
}
void test_shiftLeft_knownInt_tooLarge() {
_assertShiftLeft(
_intValue(null),
_intValue(6),
new DartObjectImpl(
_typeProvider.intType, new IntState(LONG_MAX_VALUE)));
}
void test_shiftLeft_knownInt_unknownInt() {
_assertShiftLeft(_intValue(null), _intValue(6), _intValue(null));
}
void test_shiftLeft_knownString_knownInt() {
_assertShiftLeft(null, _stringValue(null), _intValue(3));
}
void test_shiftLeft_unknownInt_knownInt() {
_assertShiftLeft(_intValue(null), _intValue(null), _intValue(3));
}
void test_shiftLeft_unknownInt_unknownInt() {
_assertShiftLeft(_intValue(null), _intValue(null), _intValue(null));
}
void test_shiftRight_knownInt_knownInt() {
_assertShiftRight(_intValue(6), _intValue(48), _intValue(3));
}
void test_shiftRight_knownInt_knownString() {
_assertShiftRight(null, _intValue(48), _stringValue(null));
}
void test_shiftRight_knownInt_tooLarge() {
_assertShiftRight(
_intValue(null),
_intValue(48),
new DartObjectImpl(
_typeProvider.intType, new IntState(LONG_MAX_VALUE)));
}
void test_shiftRight_knownInt_unknownInt() {
_assertShiftRight(_intValue(null), _intValue(48), _intValue(null));
}
void test_shiftRight_knownString_knownInt() {
_assertShiftRight(null, _stringValue(null), _intValue(3));
}
void test_shiftRight_unknownInt_knownInt() {
_assertShiftRight(_intValue(null), _intValue(null), _intValue(3));
}
void test_shiftRight_unknownInt_unknownInt() {
_assertShiftRight(_intValue(null), _intValue(null), _intValue(null));
}
void test_stringLength_int() {
try {
_assertStringLength(_intValue(null), _intValue(0));
fail("Expected EvaluationException");
} on EvaluationException {}
}
void test_stringLength_knownString() {
_assertStringLength(_intValue(3), _stringValue("abc"));
}
void test_stringLength_unknownString() {
_assertStringLength(_intValue(null), _stringValue(null));
}
void test_times_knownDouble_knownDouble() {
_assertTimes(_doubleValue(6.0), _doubleValue(2.0), _doubleValue(3.0));
}
void test_times_knownDouble_knownInt() {
_assertTimes(_doubleValue(6.0), _doubleValue(2.0), _intValue(3));
}
void test_times_knownDouble_unknownDouble() {
_assertTimes(_doubleValue(null), _doubleValue(2.0), _doubleValue(null));
}
void test_times_knownDouble_unknownInt() {
_assertTimes(_doubleValue(null), _doubleValue(2.0), _intValue(null));
}
void test_times_knownInt_knownInt() {
_assertTimes(_intValue(6), _intValue(2), _intValue(3));
}
void test_times_knownInt_knownString() {
_assertTimes(null, _intValue(2), _stringValue("3"));
}
void test_times_knownInt_unknownDouble() {
_assertTimes(_doubleValue(null), _intValue(2), _doubleValue(null));
}
void test_times_knownInt_unknownInt() {
_assertTimes(_intValue(null), _intValue(2), _intValue(null));
}
void test_times_knownString_knownInt() {
_assertTimes(null, _stringValue("2"), _intValue(3));
}
void test_times_unknownDouble_knownDouble() {
_assertTimes(_doubleValue(null), _doubleValue(null), _doubleValue(3.0));
}
void test_times_unknownDouble_knownInt() {
_assertTimes(_doubleValue(null), _doubleValue(null), _intValue(3));
}
void test_times_unknownInt_knownDouble() {
_assertTimes(_doubleValue(null), _intValue(null), _doubleValue(3.0));
}
void test_times_unknownInt_knownInt() {
_assertTimes(_intValue(null), _intValue(null), _intValue(3));
}
/**
* Assert that the result of adding the left and right operands is the expected value, or that the
* operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertAdd(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.add(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.add(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of bit-anding the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertBitAnd(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.bitAnd(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.bitAnd(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the bit-not of the operand is the expected value, or that the operation throws an
* exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param operand the operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertBitNot(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
try {
operand.bitNot(_typeProvider);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = operand.bitNot(_typeProvider);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of bit-oring the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertBitOr(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.bitOr(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.bitOr(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of bit-xoring the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertBitXor(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.bitXor(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.bitXor(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of concatenating the left and right operands is the expected value, or
* that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertConcatenate(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.concatenate(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.concatenate(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of dividing the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertDivide(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.divide(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.divide(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of comparing the left and right operands for equality is the expected
* value, or that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertEqualEqual(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.equalEqual(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.equalEqual(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of comparing the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertGreaterThan(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.greaterThan(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.greaterThan(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of comparing the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertGreaterThanOrEqual(DartObjectImpl expected,
DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.greaterThanOrEqual(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.greaterThanOrEqual(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of comparing the left and right operands using
* identical() is the expected value.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
*/
void _assertIdentical(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
DartObjectImpl result =
leftOperand.isIdentical(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
void _assertInstanceOfObjectArray(Object result) {
// TODO(scheglov) implement
}
/**
* Assert that the result of dividing the left and right operands as integers is the expected
* value, or that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertIntegerDivide(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.integerDivide(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.integerDivide(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of comparing the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertLessThan(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.lessThan(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.lessThan(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of comparing the left and right operands is the expected value, or that
* the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertLessThanOrEqual(DartObjectImpl expected,
DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.lessThanOrEqual(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.lessThanOrEqual(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of logical-anding the left and right operands is the expected value, or
* that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertLogicalAnd(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.logicalAnd(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.logicalAnd(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the logical-not of the operand is the expected value, or that the operation throws
* an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param operand the operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertLogicalNot(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
try {
operand.logicalNot(_typeProvider);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = operand.logicalNot(_typeProvider);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of logical-oring the left and right operands is the expected value, or
* that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertLogicalOr(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.logicalOr(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.logicalOr(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of subtracting the left and right operands is the expected value, or
* that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertMinus(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.minus(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.minus(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the negation of the operand is the expected value, or that the operation throws an
* exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param operand the operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertNegated(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
try {
operand.negated(_typeProvider);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = operand.negated(_typeProvider);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of comparing the left and right operands for inequality is the expected
* value, or that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertNotEqual(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.notEqual(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.notEqual(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that converting the operand to a string is the expected value, or that the operation
* throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param operand the operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertPerformToString(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
try {
operand.performToString(_typeProvider);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = operand.performToString(_typeProvider);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of taking the remainder of the left and right operands is the expected
* value, or that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertRemainder(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.remainder(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.remainder(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of multiplying the left and right operands is the expected value, or
* that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertShiftLeft(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.shiftLeft(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.shiftLeft(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of multiplying the left and right operands is the expected value, or
* that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertShiftRight(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.shiftRight(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result =
leftOperand.shiftRight(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the length of the operand is the expected value, or that the operation throws an
* exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param operand the operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertStringLength(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
try {
operand.stringLength(_typeProvider);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = operand.stringLength(_typeProvider);
expect(result, isNotNull);
expect(result, expected);
}
}
/**
* Assert that the result of multiplying the left and right operands is the expected value, or
* that the operation throws an exception if the expected value is `null`.
*
* @param expected the expected result of the operation
* @param leftOperand the left operand to the operation
* @param rightOperand the left operand to the operation
* @throws EvaluationException if the result is an exception when it should not be
*/
void _assertTimes(DartObjectImpl expected, DartObjectImpl leftOperand,
DartObjectImpl rightOperand) {
if (expected == null) {
try {
leftOperand.times(_typeProvider, rightOperand);
fail("Expected an EvaluationException");
} on EvaluationException {}
} else {
DartObjectImpl result = leftOperand.times(_typeProvider, rightOperand);
expect(result, isNotNull);
expect(result, expected);
}
}
DartObjectImpl _boolValue(bool value) {
if (value == null) {
return new DartObjectImpl(
_typeProvider.boolType, BoolState.UNKNOWN_VALUE);
} else if (identical(value, false)) {
return new DartObjectImpl(_typeProvider.boolType, BoolState.FALSE_STATE);
} else if (identical(value, true)) {
return new DartObjectImpl(_typeProvider.boolType, BoolState.TRUE_STATE);
}
fail("Invalid boolean value used in test");
return null;
}
DartObjectImpl _doubleValue(double value) {
if (value == null) {
return new DartObjectImpl(
_typeProvider.doubleType, DoubleState.UNKNOWN_VALUE);
} else {
return new DartObjectImpl(
_typeProvider.doubleType, new DoubleState(value));
}
}
DartObjectImpl _dynamicValue() {
return new DartObjectImpl(
_typeProvider.nullType, DynamicState.DYNAMIC_STATE);
}
DartObjectImpl _intValue(int value) {
if (value == null) {
return new DartObjectImpl(_typeProvider.intType, IntState.UNKNOWN_VALUE);
} else {
return new DartObjectImpl(_typeProvider.intType, new IntState(value));
}
}
DartObjectImpl _listValue(
[List<DartObjectImpl> elements = DartObjectImpl.EMPTY_LIST]) {
return new DartObjectImpl(_typeProvider.listType, new ListState(elements));
}
DartObjectImpl _mapValue(
[List<DartObjectImpl> keyElementPairs = DartObjectImpl.EMPTY_LIST]) {
Map<DartObjectImpl, DartObjectImpl> map =
new Map<DartObjectImpl, DartObjectImpl>();
int count = keyElementPairs.length;
for (int i = 0; i < count;) {
map[keyElementPairs[i++]] = keyElementPairs[i++];
}
return new DartObjectImpl(_typeProvider.mapType, new MapState(map));
}
DartObjectImpl _nullValue() {
return new DartObjectImpl(_typeProvider.nullType, NullState.NULL_STATE);
}
DartObjectImpl _numValue() {
return new DartObjectImpl(_typeProvider.nullType, NumState.UNKNOWN_VALUE);
}
DartObjectImpl _stringValue(String value) {
if (value == null) {
return new DartObjectImpl(
_typeProvider.stringType, StringState.UNKNOWN_VALUE);
} else {
return new DartObjectImpl(
_typeProvider.stringType, new StringState(value));
}
}
DartObjectImpl _symbolValue(String value) {
return new DartObjectImpl(_typeProvider.symbolType, new SymbolState(value));
}
}
@reflectiveTest
class DartUriResolverTest {
void test_creation() {
JavaFile sdkDirectory = DirectoryBasedDartSdk.defaultSdkDirectory;
expect(sdkDirectory, isNotNull);
DartSdk sdk = new DirectoryBasedDartSdk(sdkDirectory);
expect(new DartUriResolver(sdk), isNotNull);
}
void test_isDartUri_null_scheme() {
Uri uri = parseUriWithException("foo.dart");
expect('', uri.scheme);
expect(DartUriResolver.isDartUri(uri), isFalse);
}
void test_resolve_dart() {
JavaFile sdkDirectory = DirectoryBasedDartSdk.defaultSdkDirectory;
expect(sdkDirectory, isNotNull);
DartSdk sdk = new DirectoryBasedDartSdk(sdkDirectory);
UriResolver resolver = new DartUriResolver(sdk);
Source result =
resolver.resolveAbsolute(parseUriWithException("dart:core"));
expect(result, isNotNull);
}
void test_resolve_dart_nonExistingLibrary() {
JavaFile sdkDirectory = DirectoryBasedDartSdk.defaultSdkDirectory;
expect(sdkDirectory, isNotNull);
DartSdk sdk = new DirectoryBasedDartSdk(sdkDirectory);
UriResolver resolver = new DartUriResolver(sdk);
Source result = resolver.resolveAbsolute(parseUriWithException("dart:cor"));
expect(result, isNull);
}
void test_resolve_nonDart() {
JavaFile sdkDirectory = DirectoryBasedDartSdk.defaultSdkDirectory;
expect(sdkDirectory, isNotNull);
DartSdk sdk = new DirectoryBasedDartSdk(sdkDirectory);
UriResolver resolver = new DartUriResolver(sdk);
Source result = resolver
.resolveAbsolute(parseUriWithException("package:some/file.dart"));
expect(result, isNull);
}
}
@reflectiveTest
class DeclaredVariablesTest extends EngineTestCase {
void test_getBool_false() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
variables.define(variableName, "false");
DartObject object = variables.getBool(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toBoolValue(), false);
}
void test_getBool_invalid() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
variables.define(variableName, "not true");
_assertNullDartObject(
typeProvider, variables.getBool(typeProvider, variableName));
}
void test_getBool_true() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
variables.define(variableName, "true");
DartObject object = variables.getBool(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toBoolValue(), true);
}
void test_getBool_undefined() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
_assertUnknownDartObject(
typeProvider.boolType, variables.getBool(typeProvider, variableName));
}
void test_getInt_invalid() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
variables.define(variableName, "four score and seven years");
_assertNullDartObject(
typeProvider, variables.getInt(typeProvider, variableName));
}
void test_getInt_undefined() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
_assertUnknownDartObject(
typeProvider.intType, variables.getInt(typeProvider, variableName));
}
void test_getInt_valid() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
variables.define(variableName, "23");
DartObject object = variables.getInt(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toIntValue(), 23);
}
void test_getString_defined() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
String value = "value";
DeclaredVariables variables = new DeclaredVariables();
variables.define(variableName, value);
DartObject object = variables.getString(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toStringValue(), value);
}
void test_getString_undefined() {
TestTypeProvider typeProvider = new TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = new DeclaredVariables();
_assertUnknownDartObject(typeProvider.stringType,
variables.getString(typeProvider, variableName));
}
void _assertNullDartObject(TestTypeProvider typeProvider, DartObject result) {
expect(result.type, typeProvider.nullType);
}
void _assertUnknownDartObject(
ParameterizedType expectedType, DartObject result) {
expect((result as DartObjectImpl).isUnknown, isTrue);
expect(result.type, expectedType);
}
}
@reflectiveTest
class DirectoryBasedDartSdkTest {
void fail_getDocFileFor() {
DirectoryBasedDartSdk sdk = _createDartSdk();
JavaFile docFile = sdk.getDocFileFor("html");
expect(docFile, isNotNull);
}
void test_creation() {
DirectoryBasedDartSdk sdk = _createDartSdk();
expect(sdk, isNotNull);
}
void test_fromFile_invalid() {
DirectoryBasedDartSdk sdk = _createDartSdk();
expect(
sdk.fromFileUri(new JavaFile("/not/in/the/sdk.dart").toURI()), isNull);
}
void test_fromFile_library() {
DirectoryBasedDartSdk sdk = _createDartSdk();
Source source = sdk.fromFileUri(new JavaFile.relative(
new JavaFile.relative(sdk.libraryDirectory, "core"), "core.dart")
.toURI());
expect(source, isNotNull);
expect(source.isInSystemLibrary, isTrue);
expect(source.uri.toString(), "dart:core");
}
void test_fromFile_part() {
DirectoryBasedDartSdk sdk = _createDartSdk();
Source source = sdk.fromFileUri(new JavaFile.relative(
new JavaFile.relative(sdk.libraryDirectory, "core"), "num.dart")
.toURI());
expect(source, isNotNull);
expect(source.isInSystemLibrary, isTrue);
expect(source.uri.toString(), "dart:core/num.dart");
}
void test_getDart2JsExecutable() {
DirectoryBasedDartSdk sdk = _createDartSdk();
JavaFile executable = sdk.dart2JsExecutable;
expect(executable, isNotNull);
expect(executable.exists(), isTrue);
expect(executable.isExecutable(), isTrue);
}
void test_getDirectory() {
DirectoryBasedDartSdk sdk = _createDartSdk();
JavaFile directory = sdk.directory;
expect(directory, isNotNull);
expect(directory.exists(), isTrue);
}
void test_getDocDirectory() {
DirectoryBasedDartSdk sdk = _createDartSdk();
JavaFile directory = sdk.docDirectory;
expect(directory, isNotNull);
}
void test_getLibraryDirectory() {
DirectoryBasedDartSdk sdk = _createDartSdk();
JavaFile directory = sdk.libraryDirectory;
expect(directory, isNotNull);
expect(directory.exists(), isTrue);
}
void test_getPubExecutable() {
DirectoryBasedDartSdk sdk = _createDartSdk();
JavaFile executable = sdk.pubExecutable;
expect(executable, isNotNull);
expect(executable.exists(), isTrue);
expect(executable.isExecutable(), isTrue);
}
void test_getSdkVersion() {
DirectoryBasedDartSdk sdk = _createDartSdk();
String version = sdk.sdkVersion;
expect(version, isNotNull);
expect(version.length > 0, isTrue);
}
void test_getVmExecutable() {
DirectoryBasedDartSdk sdk = _createDartSdk();
JavaFile executable = sdk.vmExecutable;
expect(executable, isNotNull);
expect(executable.exists(), isTrue);
expect(executable.isExecutable(), isTrue);
}
DirectoryBasedDartSdk _createDartSdk() {
JavaFile sdkDirectory = DirectoryBasedDartSdk.defaultSdkDirectory;
expect(sdkDirectory, isNotNull,
reason:
"No SDK configured; set the property 'com.google.dart.sdk' on the command line");
return new DirectoryBasedDartSdk(sdkDirectory);
}
}
@reflectiveTest
class DirectoryBasedSourceContainerTest {
void test_contains() {
JavaFile dir = FileUtilities2.createFile("/does/not/exist");
JavaFile file1 = FileUtilities2.createFile("/does/not/exist/some.dart");
JavaFile file2 =
FileUtilities2.createFile("/does/not/exist/folder/some2.dart");
JavaFile file3 = FileUtilities2.createFile("/does/not/exist3/some3.dart");
FileBasedSource source1 = new FileBasedSource(file1);
FileBasedSource source2 = new FileBasedSource(file2);
FileBasedSource source3 = new FileBasedSource(file3);
DirectoryBasedSourceContainer container =
new DirectoryBasedSourceContainer.con1(dir);
expect(container.contains(source1), isTrue);
expect(container.contains(source2), isTrue);
expect(container.contains(source3), isFalse);
}
}
@reflectiveTest
class ElementBuilderTest extends ParserTestCase {
void fail_visitMethodDeclaration_setter_duplicate() {
// https://github.com/dart-lang/sdk/issues/25601
String code = r'''
class C {
set zzz(x) {}
set zzz(y) {}
}
''';
CompilationUnit unit = ParserTestCase.parseCompilationUnit(code);
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
unit.accept(builder);
ClassElement classElement = holder.types[0];
for (PropertyAccessorElement accessor in classElement.accessors) {
expect(accessor.variable.setter, same(accessor));
}
}
void test_visitCatchClause() {
// } catch (e, s) {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String exceptionParameterName = "e";
String stackParameterName = "s";
CatchClause clause =
AstFactory.catchClause2(exceptionParameterName, stackParameterName);
clause.accept(builder);
List<LocalVariableElement> variables = holder.localVariables;
expect(variables, hasLength(2));
VariableElement exceptionVariable = variables[0];
expect(exceptionVariable, isNotNull);
expect(exceptionVariable.name, exceptionParameterName);
expect(exceptionVariable.hasImplicitType, isTrue);
expect(exceptionVariable.isSynthetic, isFalse);
expect(exceptionVariable.isConst, isFalse);
expect(exceptionVariable.isFinal, isFalse);
expect(exceptionVariable.initializer, isNull);
VariableElement stackVariable = variables[1];
expect(stackVariable, isNotNull);
expect(stackVariable.name, stackParameterName);
expect(stackVariable.isSynthetic, isFalse);
expect(stackVariable.isConst, isFalse);
expect(stackVariable.isFinal, isFalse);
expect(stackVariable.initializer, isNull);
}
void test_visitCatchClause_withType() {
// } on E catch (e) {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String exceptionParameterName = "e";
CatchClause clause = AstFactory.catchClause4(
AstFactory.typeName4('E'), exceptionParameterName);
clause.accept(builder);
List<LocalVariableElement> variables = holder.localVariables;
expect(variables, hasLength(1));
VariableElement exceptionVariable = variables[0];
expect(exceptionVariable, isNotNull);
expect(exceptionVariable.name, exceptionParameterName);
expect(exceptionVariable.hasImplicitType, isFalse);
}
void test_visitClassDeclaration_abstract() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "C";
ClassDeclaration classDeclaration = AstFactory.classDeclaration(
Keyword.ABSTRACT, className, null, null, null, null);
classDeclaration.accept(builder);
List<ClassElement> types = holder.types;
expect(types, hasLength(1));
ClassElement type = types[0];
expect(type, isNotNull);
expect(type.name, className);
List<TypeParameterElement> typeParameters = type.typeParameters;
expect(typeParameters, hasLength(0));
expect(type.isAbstract, isTrue);
expect(type.isMixinApplication, isFalse);
expect(type.isSynthetic, isFalse);
}
void test_visitClassDeclaration_minimal() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "C";
ClassDeclaration classDeclaration =
AstFactory.classDeclaration(null, className, null, null, null, null);
classDeclaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
classDeclaration.accept(builder);
List<ClassElement> types = holder.types;
expect(types, hasLength(1));
ClassElement type = types[0];
expect(type, isNotNull);
expect(type.name, className);
List<TypeParameterElement> typeParameters = type.typeParameters;
expect(typeParameters, hasLength(0));
expect(type.isAbstract, isFalse);
expect(type.isMixinApplication, isFalse);
expect(type.isSynthetic, isFalse);
_assertHasDocRange(type, 50, 7);
}
void test_visitClassDeclaration_parameterized() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "C";
String firstVariableName = "E";
String secondVariableName = "F";
ClassDeclaration classDeclaration = AstFactory.classDeclaration(
null,
className,
AstFactory.typeParameterList([firstVariableName, secondVariableName]),
null,
null,
null);
classDeclaration.accept(builder);
List<ClassElement> types = holder.types;
expect(types, hasLength(1));
ClassElement type = types[0];
expect(type, isNotNull);
expect(type.name, className);
List<TypeParameterElement> typeParameters = type.typeParameters;
expect(typeParameters, hasLength(2));
expect(typeParameters[0].name, firstVariableName);
expect(typeParameters[1].name, secondVariableName);
expect(type.isAbstract, isFalse);
expect(type.isMixinApplication, isFalse);
expect(type.isSynthetic, isFalse);
}
void test_visitClassDeclaration_withMembers() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "C";
String typeParameterName = "E";
String fieldName = "f";
String methodName = "m";
ClassDeclaration classDeclaration = AstFactory.classDeclaration(
null,
className,
AstFactory.typeParameterList([typeParameterName]),
null,
null,
null, [
AstFactory.fieldDeclaration2(
false, null, [AstFactory.variableDeclaration(fieldName)]),
AstFactory.methodDeclaration2(
null,
null,
null,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2())
]);
classDeclaration.accept(builder);
List<ClassElement> types = holder.types;
expect(types, hasLength(1));
ClassElement type = types[0];
expect(type, isNotNull);
expect(type.name, className);
expect(type.isAbstract, isFalse);
expect(type.isMixinApplication, isFalse);
expect(type.isSynthetic, isFalse);
List<TypeParameterElement> typeParameters = type.typeParameters;
expect(typeParameters, hasLength(1));
TypeParameterElement typeParameter = typeParameters[0];
expect(typeParameter, isNotNull);
expect(typeParameter.name, typeParameterName);
List<FieldElement> fields = type.fields;
expect(fields, hasLength(1));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, fieldName);
List<MethodElement> methods = type.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
expect(method.name, methodName);
}
void test_visitClassTypeAlias() {
// class B {}
// class M {}
// class C = B with M
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
ClassElementImpl classB = ElementFactory.classElement2('B', []);
ConstructorElementImpl constructorB =
ElementFactory.constructorElement2(classB, '', []);
constructorB.setModifier(Modifier.SYNTHETIC, true);
classB.constructors = [constructorB];
ClassElement classM = ElementFactory.classElement2('M', []);
WithClause withClause =
AstFactory.withClause([AstFactory.typeName(classM, [])]);
ClassTypeAlias alias = AstFactory.classTypeAlias(
'C', null, null, AstFactory.typeName(classB, []), withClause, null);
alias.accept(builder);
List<ClassElement> types = holder.types;
expect(types, hasLength(1));
ClassElement type = types[0];
expect(alias.element, same(type));
expect(type.name, equals('C'));
expect(type.isAbstract, isFalse);
expect(type.isMixinApplication, isTrue);
expect(type.isSynthetic, isFalse);
expect(type.typeParameters, isEmpty);
expect(type.fields, isEmpty);
expect(type.methods, isEmpty);
}
void test_visitClassTypeAlias_abstract() {
// class B {}
// class M {}
// abstract class C = B with M
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
ClassElementImpl classB = ElementFactory.classElement2('B', []);
ConstructorElementImpl constructorB =
ElementFactory.constructorElement2(classB, '', []);
constructorB.setModifier(Modifier.SYNTHETIC, true);
classB.constructors = [constructorB];
ClassElement classM = ElementFactory.classElement2('M', []);
WithClause withClause =
AstFactory.withClause([AstFactory.typeName(classM, [])]);
ClassTypeAlias classCAst = AstFactory.classTypeAlias('C', null,
Keyword.ABSTRACT, AstFactory.typeName(classB, []), withClause, null);
classCAst.accept(builder);
List<ClassElement> types = holder.types;
expect(types, hasLength(1));
ClassElement type = types[0];
expect(type.isAbstract, isTrue);
expect(type.isMixinApplication, isTrue);
}
void test_visitClassTypeAlias_typeParams() {
// class B {}
// class M {}
// class C<T> = B with M
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
ClassElementImpl classB = ElementFactory.classElement2('B', []);
ConstructorElementImpl constructorB =
ElementFactory.constructorElement2(classB, '', []);
constructorB.setModifier(Modifier.SYNTHETIC, true);
classB.constructors = [constructorB];
ClassElementImpl classM = ElementFactory.classElement2('M', []);
WithClause withClause =
AstFactory.withClause([AstFactory.typeName(classM, [])]);
ClassTypeAlias classCAst = AstFactory.classTypeAlias(
'C',
AstFactory.typeParameterList(['T']),
null,
AstFactory.typeName(classB, []),
withClause,
null);
classCAst.accept(builder);
List<ClassElement> types = holder.types;
expect(types, hasLength(1));
ClassElement type = types[0];
expect(type.typeParameters, hasLength(1));
expect(type.typeParameters[0].name, equals('T'));
}
void test_visitConstructorDeclaration_external() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "A";
ConstructorDeclaration constructorDeclaration =
AstFactory.constructorDeclaration2(
null,
null,
AstFactory.identifier3(className),
null,
AstFactory.formalParameterList(),
null,
AstFactory.blockFunctionBody2());
constructorDeclaration.externalKeyword =
TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
constructorDeclaration.accept(builder);
List<ConstructorElement> constructors = holder.constructors;
expect(constructors, hasLength(1));
ConstructorElement constructor = constructors[0];
expect(constructor, isNotNull);
expect(constructor.isExternal, isTrue);
expect(constructor.isFactory, isFalse);
expect(constructor.name, "");
expect(constructor.functions, hasLength(0));
expect(constructor.labels, hasLength(0));
expect(constructor.localVariables, hasLength(0));
expect(constructor.parameters, hasLength(0));
}
void test_visitConstructorDeclaration_factory() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "A";
ConstructorDeclaration constructorDeclaration =
AstFactory.constructorDeclaration2(
null,
Keyword.FACTORY,
AstFactory.identifier3(className),
null,
AstFactory.formalParameterList(),
null,
AstFactory.blockFunctionBody2());
constructorDeclaration.accept(builder);
List<ConstructorElement> constructors = holder.constructors;
expect(constructors, hasLength(1));
ConstructorElement constructor = constructors[0];
expect(constructor, isNotNull);
expect(constructor.isExternal, isFalse);
expect(constructor.isFactory, isTrue);
expect(constructor.name, "");
expect(constructor.functions, hasLength(0));
expect(constructor.labels, hasLength(0));
expect(constructor.localVariables, hasLength(0));
expect(constructor.parameters, hasLength(0));
}
void test_visitConstructorDeclaration_minimal() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "A";
ConstructorDeclaration constructorDeclaration =
AstFactory.constructorDeclaration2(
null,
null,
AstFactory.identifier3(className),
null,
AstFactory.formalParameterList(),
null,
AstFactory.blockFunctionBody2());
constructorDeclaration.documentationComment = AstFactory
.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
constructorDeclaration.accept(builder);
List<ConstructorElement> constructors = holder.constructors;
expect(constructors, hasLength(1));
ConstructorElement constructor = constructors[0];
expect(constructor, isNotNull);
_assertHasDocRange(constructor, 50, 7);
expect(constructor.isExternal, isFalse);
expect(constructor.isFactory, isFalse);
expect(constructor.name, "");
expect(constructor.functions, hasLength(0));
expect(constructor.labels, hasLength(0));
expect(constructor.localVariables, hasLength(0));
expect(constructor.parameters, hasLength(0));
}
void test_visitConstructorDeclaration_named() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "A";
String constructorName = "c";
ConstructorDeclaration constructorDeclaration =
AstFactory.constructorDeclaration2(
null,
null,
AstFactory.identifier3(className),
constructorName,
AstFactory.formalParameterList(),
null,
AstFactory.blockFunctionBody2());
constructorDeclaration.accept(builder);
List<ConstructorElement> constructors = holder.constructors;
expect(constructors, hasLength(1));
ConstructorElement constructor = constructors[0];
expect(constructor, isNotNull);
expect(constructor.isExternal, isFalse);
expect(constructor.isFactory, isFalse);
expect(constructor.name, constructorName);
expect(constructor.functions, hasLength(0));
expect(constructor.labels, hasLength(0));
expect(constructor.localVariables, hasLength(0));
expect(constructor.parameters, hasLength(0));
expect(constructorDeclaration.name.staticElement, same(constructor));
expect(constructorDeclaration.element, same(constructor));
}
void test_visitConstructorDeclaration_unnamed() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String className = "A";
ConstructorDeclaration constructorDeclaration =
AstFactory.constructorDeclaration2(
null,
null,
AstFactory.identifier3(className),
null,
AstFactory.formalParameterList(),
null,
AstFactory.blockFunctionBody2());
constructorDeclaration.accept(builder);
List<ConstructorElement> constructors = holder.constructors;
expect(constructors, hasLength(1));
ConstructorElement constructor = constructors[0];
expect(constructor, isNotNull);
expect(constructor.isExternal, isFalse);
expect(constructor.isFactory, isFalse);
expect(constructor.name, "");
expect(constructor.functions, hasLength(0));
expect(constructor.labels, hasLength(0));
expect(constructor.localVariables, hasLength(0));
expect(constructor.parameters, hasLength(0));
expect(constructorDeclaration.element, same(constructor));
}
void test_visitDeclaredIdentifier_noType() {
// var i
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
var variableName = 'i';
DeclaredIdentifier identifier =
AstFactory.declaredIdentifier3(variableName);
AstFactory.forEachStatement(
identifier, AstFactory.nullLiteral(), AstFactory.emptyStatement());
identifier.accept(builder);
List<LocalVariableElement> variables = holder.localVariables;
expect(variables, hasLength(1));
LocalVariableElement variable = variables[0];
expect(variable, isNotNull);
expect(variable.hasImplicitType, isTrue);
expect(variable.isConst, isFalse);
expect(variable.isDeprecated, isFalse);
expect(variable.isFinal, isFalse);
expect(variable.isOverride, isFalse);
expect(variable.isPrivate, isFalse);
expect(variable.isPublic, isTrue);
expect(variable.isSynthetic, isFalse);
expect(variable.name, variableName);
}
void test_visitDeclaredIdentifier_type() {
// E i
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
var variableName = 'i';
DeclaredIdentifier identifier =
AstFactory.declaredIdentifier4(AstFactory.typeName4('E'), variableName);
AstFactory.forEachStatement(
identifier, AstFactory.nullLiteral(), AstFactory.emptyStatement());
identifier.accept(builder);
List<LocalVariableElement> variables = holder.localVariables;
expect(variables, hasLength(1));
LocalVariableElement variable = variables[0];
expect(variable, isNotNull);
expect(variable.hasImplicitType, isFalse);
expect(variable.isConst, isFalse);
expect(variable.isDeprecated, isFalse);
expect(variable.isFinal, isFalse);
expect(variable.isOverride, isFalse);
expect(variable.isPrivate, isFalse);
expect(variable.isPublic, isTrue);
expect(variable.isSynthetic, isFalse);
expect(variable.name, variableName);
}
void test_visitDefaultFormalParameter_noType() {
// p = 0
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = 'p';
DefaultFormalParameter formalParameter =
AstFactory.positionalFormalParameter(
AstFactory.simpleFormalParameter3(parameterName),
AstFactory.integer(0));
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter.hasImplicitType, isTrue);
expect(parameter.isConst, isFalse);
expect(parameter.isDeprecated, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isInitializingFormal, isFalse);
expect(parameter.isOverride, isFalse);
expect(parameter.isPrivate, isFalse);
expect(parameter.isPublic, isTrue);
expect(parameter.isSynthetic, isFalse);
expect(parameter.name, parameterName);
}
void test_visitDefaultFormalParameter_type() {
// E p = 0
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = 'p';
DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter(
AstFactory.simpleFormalParameter4(
AstFactory.typeName4('E'), parameterName),
AstFactory.integer(0));
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter.hasImplicitType, isFalse);
expect(parameter.isConst, isFalse);
expect(parameter.isDeprecated, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isInitializingFormal, isFalse);
expect(parameter.isOverride, isFalse);
expect(parameter.isPrivate, isFalse);
expect(parameter.isPublic, isTrue);
expect(parameter.isSynthetic, isFalse);
expect(parameter.name, parameterName);
}
void test_visitEnumDeclaration() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String enumName = "E";
EnumDeclaration enumDeclaration =
AstFactory.enumDeclaration2(enumName, ["ONE"]);
enumDeclaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
enumDeclaration.accept(builder);
List<ClassElement> enums = holder.enums;
expect(enums, hasLength(1));
ClassElement enumElement = enums[0];
expect(enumElement, isNotNull);
_assertHasDocRange(enumElement, 50, 7);
expect(enumElement.name, enumName);
}
void test_visitFieldDeclaration() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String firstFieldName = "x";
String secondFieldName = "y";
FieldDeclaration fieldDeclaration =
AstFactory.fieldDeclaration2(false, null, [
AstFactory.variableDeclaration(firstFieldName),
AstFactory.variableDeclaration(secondFieldName)
]);
fieldDeclaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
fieldDeclaration.accept(builder);
List<FieldElement> fields = holder.fields;
expect(fields, hasLength(2));
FieldElement firstField = fields[0];
expect(firstField, isNotNull);
_assertHasDocRange(firstField, 50, 7);
expect(firstField.name, firstFieldName);
expect(firstField.initializer, isNull);
expect(firstField.isConst, isFalse);
expect(firstField.isFinal, isFalse);
expect(firstField.isSynthetic, isFalse);
FieldElement secondField = fields[1];
expect(secondField, isNotNull);
_assertHasDocRange(secondField, 50, 7);
expect(secondField.name, secondFieldName);
expect(secondField.initializer, isNull);
expect(secondField.isConst, isFalse);
expect(secondField.isFinal, isFalse);
expect(secondField.isSynthetic, isFalse);
}
void test_visitFieldFormalParameter() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "p";
FieldFormalParameter formalParameter =
AstFactory.fieldFormalParameter(null, null, parameterName);
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.name, parameterName);
expect(parameter.initializer, isNull);
expect(parameter.isConst, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isSynthetic, isFalse);
expect(parameter.parameterKind, ParameterKind.REQUIRED);
expect(parameter.parameters, hasLength(0));
}
void test_visitFieldFormalParameter_funtionTyped() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "p";
FieldFormalParameter formalParameter = AstFactory.fieldFormalParameter(
null,
null,
parameterName,
AstFactory
.formalParameterList([AstFactory.simpleFormalParameter3("a")]));
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.name, parameterName);
expect(parameter.initializer, isNull);
expect(parameter.isConst, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isSynthetic, isFalse);
expect(parameter.parameterKind, ParameterKind.REQUIRED);
expect(parameter.parameters, hasLength(1));
}
void test_visitFormalParameterList() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String firstParameterName = "a";
String secondParameterName = "b";
FormalParameterList parameterList = AstFactory.formalParameterList([
AstFactory.simpleFormalParameter3(firstParameterName),
AstFactory.simpleFormalParameter3(secondParameterName)
]);
parameterList.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(2));
expect(parameters[0].name, firstParameterName);
expect(parameters[1].name, secondParameterName);
}
void test_visitFunctionDeclaration_external() {
// external f();
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String functionName = "f";
FunctionDeclaration declaration = AstFactory.functionDeclaration(
null,
null,
functionName,
AstFactory.functionExpression2(
AstFactory.formalParameterList(), AstFactory.emptyFunctionBody()));
declaration.externalKeyword =
TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
declaration.accept(builder);
List<FunctionElement> functions = holder.functions;
expect(functions, hasLength(1));
FunctionElement function = functions[0];
expect(function, isNotNull);
expect(function.name, functionName);
expect(declaration.element, same(function));
expect(declaration.functionExpression.element, same(function));
expect(function.hasImplicitReturnType, isTrue);
expect(function.isExternal, isTrue);
expect(function.isSynthetic, isFalse);
expect(function.typeParameters, hasLength(0));
}
void test_visitFunctionDeclaration_getter() {
// get f() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String functionName = "f";
FunctionDeclaration declaration = AstFactory.functionDeclaration(
null,
Keyword.GET,
functionName,
AstFactory.functionExpression2(
AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()));
declaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
declaration.accept(builder);
List<PropertyAccessorElement> accessors = holder.accessors;
expect(accessors, hasLength(1));
PropertyAccessorElement accessor = accessors[0];
expect(accessor, isNotNull);
_assertHasDocRange(accessor, 50, 7);
expect(accessor.name, functionName);
expect(declaration.element, same(accessor));
expect(declaration.functionExpression.element, same(accessor));
expect(accessor.hasImplicitReturnType, isTrue);
expect(accessor.isGetter, isTrue);
expect(accessor.isExternal, isFalse);
expect(accessor.isSetter, isFalse);
expect(accessor.isSynthetic, isFalse);
expect(accessor.typeParameters, hasLength(0));
PropertyInducingElement variable = accessor.variable;
EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement,
TopLevelVariableElement, variable);
expect(variable.isSynthetic, isTrue);
}
void test_visitFunctionDeclaration_plain() {
// T f() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String functionName = "f";
FunctionDeclaration declaration = AstFactory.functionDeclaration(
AstFactory.typeName4('T'),
null,
functionName,
AstFactory.functionExpression2(
AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()));
declaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
declaration.accept(builder);
List<FunctionElement> functions = holder.functions;
expect(functions, hasLength(1));
FunctionElement function = functions[0];
expect(function, isNotNull);
_assertHasDocRange(function, 50, 7);
expect(function.hasImplicitReturnType, isFalse);
expect(function.name, functionName);
expect(declaration.element, same(function));
expect(declaration.functionExpression.element, same(function));
expect(function.isExternal, isFalse);
expect(function.isSynthetic, isFalse);
expect(function.typeParameters, hasLength(0));
}
void test_visitFunctionDeclaration_setter() {
// set f() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String functionName = "f";
FunctionDeclaration declaration = AstFactory.functionDeclaration(
null,
Keyword.SET,
functionName,
AstFactory.functionExpression2(
AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()));
declaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
declaration.accept(builder);
List<PropertyAccessorElement> accessors = holder.accessors;
expect(accessors, hasLength(1));
PropertyAccessorElement accessor = accessors[0];
expect(accessor, isNotNull);
_assertHasDocRange(accessor, 50, 7);
expect(accessor.hasImplicitReturnType, isTrue);
expect(accessor.name, "$functionName=");
expect(declaration.element, same(accessor));
expect(declaration.functionExpression.element, same(accessor));
expect(accessor.isGetter, isFalse);
expect(accessor.isExternal, isFalse);
expect(accessor.isSetter, isTrue);
expect(accessor.isSynthetic, isFalse);
expect(accessor.typeParameters, hasLength(0));
PropertyInducingElement variable = accessor.variable;
EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement,
TopLevelVariableElement, variable);
expect(variable.isSynthetic, isTrue);
}
void test_visitFunctionDeclaration_typeParameters() {
// f<E>() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String functionName = 'f';
String typeParameterName = 'E';
FunctionExpression expression = AstFactory.functionExpression3(
AstFactory.typeParameterList([typeParameterName]),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2());
FunctionDeclaration declaration =
AstFactory.functionDeclaration(null, null, functionName, expression);
declaration.accept(builder);
List<FunctionElement> functions = holder.functions;
expect(functions, hasLength(1));
FunctionElement function = functions[0];
expect(function, isNotNull);
expect(function.hasImplicitReturnType, isTrue);
expect(function.name, functionName);
expect(function.isExternal, isFalse);
expect(function.isSynthetic, isFalse);
expect(declaration.element, same(function));
expect(expression.element, same(function));
List<TypeParameterElement> typeParameters = function.typeParameters;
expect(typeParameters, hasLength(1));
TypeParameterElement typeParameter = typeParameters[0];
expect(typeParameter, isNotNull);
expect(typeParameter.name, typeParameterName);
}
void test_visitFunctionExpression() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
FunctionExpression expression = AstFactory.functionExpression2(
AstFactory.formalParameterList(), AstFactory.blockFunctionBody2());
expression.accept(builder);
List<FunctionElement> functions = holder.functions;
expect(functions, hasLength(1));
FunctionElement function = functions[0];
expect(function, isNotNull);
expect(expression.element, same(function));
expect(function.hasImplicitReturnType, isTrue);
expect(function.isSynthetic, isFalse);
expect(function.typeParameters, hasLength(0));
}
void test_visitFunctionTypeAlias() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String aliasName = "F";
String parameterName = "E";
FunctionTypeAlias aliasNode = AstFactory.typeAlias(
null, aliasName, AstFactory.typeParameterList([parameterName]), null);
aliasNode.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
aliasNode.accept(builder);
List<FunctionTypeAliasElement> aliases = holder.typeAliases;
expect(aliases, hasLength(1));
FunctionTypeAliasElement alias = aliases[0];
expect(alias, isNotNull);
_assertHasDocRange(alias, 50, 7);
expect(alias.name, aliasName);
expect(alias.parameters, hasLength(0));
List<TypeParameterElement> typeParameters = alias.typeParameters;
expect(typeParameters, hasLength(1));
TypeParameterElement typeParameter = typeParameters[0];
expect(typeParameter, isNotNull);
expect(typeParameter.name, parameterName);
}
void test_visitFunctionTypedFormalParameter() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "p";
FunctionTypedFormalParameter formalParameter =
AstFactory.functionTypedFormalParameter(null, parameterName);
_useParameterInMethod(formalParameter, 100, 110);
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.name, parameterName);
expect(parameter.initializer, isNull);
expect(parameter.isConst, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isSynthetic, isFalse);
expect(parameter.parameterKind, ParameterKind.REQUIRED);
SourceRange visibleRange = parameter.visibleRange;
expect(100, visibleRange.offset);
expect(110, visibleRange.end);
}
void test_visitFunctionTypedFormalParameter_withTypeParameters() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "p";
FunctionTypedFormalParameter formalParameter =
AstFactory.functionTypedFormalParameter(null, parameterName);
formalParameter.typeParameters = AstFactory.typeParameterList(['F']);
_useParameterInMethod(formalParameter, 100, 110);
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.name, parameterName);
expect(parameter.initializer, isNull);
expect(parameter.isConst, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isSynthetic, isFalse);
expect(parameter.parameterKind, ParameterKind.REQUIRED);
expect(parameter.typeParameters, hasLength(1));
SourceRange visibleRange = parameter.visibleRange;
expect(100, visibleRange.offset);
expect(110, visibleRange.end);
}
void test_visitLabeledStatement() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String labelName = "l";
LabeledStatement statement = AstFactory.labeledStatement(
[AstFactory.label2(labelName)], AstFactory.breakStatement());
statement.accept(builder);
List<LabelElement> labels = holder.labels;
expect(labels, hasLength(1));
LabelElement label = labels[0];
expect(label, isNotNull);
expect(label.name, labelName);
expect(label.isSynthetic, isFalse);
}
void test_visitMethodDeclaration_abstract() {
// m();
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
null,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.emptyFunctionBody());
methodDeclaration.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
expect(method.hasImplicitReturnType, isTrue);
expect(method.name, methodName);
expect(method.functions, hasLength(0));
expect(method.labels, hasLength(0));
expect(method.localVariables, hasLength(0));
expect(method.parameters, hasLength(0));
expect(method.typeParameters, hasLength(0));
expect(method.isAbstract, isTrue);
expect(method.isExternal, isFalse);
expect(method.isStatic, isFalse);
expect(method.isSynthetic, isFalse);
}
void test_visitMethodDeclaration_external() {
// external m();
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
null,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.emptyFunctionBody());
methodDeclaration.externalKeyword =
TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
methodDeclaration.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
expect(method.hasImplicitReturnType, isTrue);
expect(method.name, methodName);
expect(method.functions, hasLength(0));
expect(method.labels, hasLength(0));
expect(method.localVariables, hasLength(0));
expect(method.parameters, hasLength(0));
expect(method.typeParameters, hasLength(0));
expect(method.isAbstract, isFalse);
expect(method.isExternal, isTrue);
expect(method.isStatic, isFalse);
expect(method.isSynthetic, isFalse);
}
void test_visitMethodDeclaration_getter() {
// get m() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
Keyword.GET,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2());
methodDeclaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
methodDeclaration.accept(builder);
List<FieldElement> fields = holder.fields;
expect(fields, hasLength(1));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, methodName);
expect(field.isSynthetic, isTrue);
expect(field.setter, isNull);
PropertyAccessorElement getter = field.getter;
expect(getter, isNotNull);
_assertHasDocRange(getter, 50, 7);
expect(getter.hasImplicitReturnType, isTrue);
expect(getter.isAbstract, isFalse);
expect(getter.isExternal, isFalse);
expect(getter.isGetter, isTrue);
expect(getter.isSynthetic, isFalse);
expect(getter.name, methodName);
expect(getter.variable, field);
expect(getter.functions, hasLength(0));
expect(getter.labels, hasLength(0));
expect(getter.localVariables, hasLength(0));
expect(getter.parameters, hasLength(0));
}
void test_visitMethodDeclaration_getter_abstract() {
// get m();
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
Keyword.GET,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.emptyFunctionBody());
methodDeclaration.accept(builder);
List<FieldElement> fields = holder.fields;
expect(fields, hasLength(1));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, methodName);
expect(field.isSynthetic, isTrue);
expect(field.setter, isNull);
PropertyAccessorElement getter = field.getter;
expect(getter, isNotNull);
expect(getter.hasImplicitReturnType, isTrue);
expect(getter.isAbstract, isTrue);
expect(getter.isExternal, isFalse);
expect(getter.isGetter, isTrue);
expect(getter.isSynthetic, isFalse);
expect(getter.name, methodName);
expect(getter.variable, field);
expect(getter.functions, hasLength(0));
expect(getter.labels, hasLength(0));
expect(getter.localVariables, hasLength(0));
expect(getter.parameters, hasLength(0));
}
void test_visitMethodDeclaration_getter_external() {
// external get m();
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration(
null,
null,
Keyword.GET,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList());
methodDeclaration.externalKeyword =
TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
methodDeclaration.accept(builder);
List<FieldElement> fields = holder.fields;
expect(fields, hasLength(1));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, methodName);
expect(field.isSynthetic, isTrue);
expect(field.setter, isNull);
PropertyAccessorElement getter = field.getter;
expect(getter, isNotNull);
expect(getter.hasImplicitReturnType, isTrue);
expect(getter.isAbstract, isFalse);
expect(getter.isExternal, isTrue);
expect(getter.isGetter, isTrue);
expect(getter.isSynthetic, isFalse);
expect(getter.name, methodName);
expect(getter.variable, field);
expect(getter.functions, hasLength(0));
expect(getter.labels, hasLength(0));
expect(getter.localVariables, hasLength(0));
expect(getter.parameters, hasLength(0));
}
void test_visitMethodDeclaration_minimal() {
// T m() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
AstFactory.typeName4('T'),
null,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2());
methodDeclaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
methodDeclaration.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
_assertHasDocRange(method, 50, 7);
expect(method.hasImplicitReturnType, isFalse);
expect(method.name, methodName);
expect(method.functions, hasLength(0));
expect(method.labels, hasLength(0));
expect(method.localVariables, hasLength(0));
expect(method.parameters, hasLength(0));
expect(method.typeParameters, hasLength(0));
expect(method.isAbstract, isFalse);
expect(method.isExternal, isFalse);
expect(method.isStatic, isFalse);
expect(method.isSynthetic, isFalse);
}
void test_visitMethodDeclaration_operator() {
// operator +(addend) {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "+";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
null,
Keyword.OPERATOR,
AstFactory.identifier3(methodName),
AstFactory
.formalParameterList([AstFactory.simpleFormalParameter3("addend")]),
AstFactory.blockFunctionBody2());
methodDeclaration.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
expect(method.hasImplicitReturnType, isTrue);
expect(method.name, methodName);
expect(method.functions, hasLength(0));
expect(method.labels, hasLength(0));
expect(method.localVariables, hasLength(0));
expect(method.parameters, hasLength(1));
expect(method.typeParameters, hasLength(0));
expect(method.isAbstract, isFalse);
expect(method.isExternal, isFalse);
expect(method.isStatic, isFalse);
expect(method.isSynthetic, isFalse);
}
void test_visitMethodDeclaration_setter() {
// set m() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
Keyword.SET,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2());
methodDeclaration.documentationComment = AstFactory.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
methodDeclaration.accept(builder);
List<FieldElement> fields = holder.fields;
expect(fields, hasLength(1));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, methodName);
expect(field.isSynthetic, isTrue);
expect(field.getter, isNull);
PropertyAccessorElement setter = field.setter;
expect(setter, isNotNull);
_assertHasDocRange(setter, 50, 7);
expect(setter.hasImplicitReturnType, isTrue);
expect(setter.isAbstract, isFalse);
expect(setter.isExternal, isFalse);
expect(setter.isSetter, isTrue);
expect(setter.isSynthetic, isFalse);
expect(setter.name, "$methodName=");
expect(setter.displayName, methodName);
expect(setter.variable, field);
expect(setter.functions, hasLength(0));
expect(setter.labels, hasLength(0));
expect(setter.localVariables, hasLength(0));
expect(setter.parameters, hasLength(0));
}
void test_visitMethodDeclaration_setter_abstract() {
// set m();
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
Keyword.SET,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.emptyFunctionBody());
methodDeclaration.accept(builder);
List<FieldElement> fields = holder.fields;
expect(fields, hasLength(1));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, methodName);
expect(field.isSynthetic, isTrue);
expect(field.getter, isNull);
PropertyAccessorElement setter = field.setter;
expect(setter, isNotNull);
expect(setter.hasImplicitReturnType, isTrue);
expect(setter.isAbstract, isTrue);
expect(setter.isExternal, isFalse);
expect(setter.isSetter, isTrue);
expect(setter.isSynthetic, isFalse);
expect(setter.name, "$methodName=");
expect(setter.displayName, methodName);
expect(setter.variable, field);
expect(setter.functions, hasLength(0));
expect(setter.labels, hasLength(0));
expect(setter.localVariables, hasLength(0));
expect(setter.parameters, hasLength(0));
}
void test_visitMethodDeclaration_setter_external() {
// external m();
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration(
null,
null,
Keyword.SET,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList());
methodDeclaration.externalKeyword =
TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
methodDeclaration.accept(builder);
List<FieldElement> fields = holder.fields;
expect(fields, hasLength(1));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, methodName);
expect(field.isSynthetic, isTrue);
expect(field.getter, isNull);
PropertyAccessorElement setter = field.setter;
expect(setter, isNotNull);
expect(setter.hasImplicitReturnType, isTrue);
expect(setter.isAbstract, isFalse);
expect(setter.isExternal, isTrue);
expect(setter.isSetter, isTrue);
expect(setter.isSynthetic, isFalse);
expect(setter.name, "$methodName=");
expect(setter.displayName, methodName);
expect(setter.variable, field);
expect(setter.functions, hasLength(0));
expect(setter.labels, hasLength(0));
expect(setter.localVariables, hasLength(0));
expect(setter.parameters, hasLength(0));
}
void test_visitMethodDeclaration_static() {
// static m() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
Keyword.STATIC,
null,
null,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2());
methodDeclaration.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
expect(method.hasImplicitReturnType, isTrue);
expect(method.name, methodName);
expect(method.functions, hasLength(0));
expect(method.labels, hasLength(0));
expect(method.localVariables, hasLength(0));
expect(method.parameters, hasLength(0));
expect(method.typeParameters, hasLength(0));
expect(method.isAbstract, isFalse);
expect(method.isExternal, isFalse);
expect(method.isStatic, isTrue);
expect(method.isSynthetic, isFalse);
}
void test_visitMethodDeclaration_typeParameters() {
// m<E>() {}
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
null,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2());
methodDeclaration.typeParameters = AstFactory.typeParameterList(['E']);
methodDeclaration.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
expect(method.hasImplicitReturnType, isTrue);
expect(method.name, methodName);
expect(method.functions, hasLength(0));
expect(method.labels, hasLength(0));
expect(method.localVariables, hasLength(0));
expect(method.parameters, hasLength(0));
expect(method.typeParameters, hasLength(1));
expect(method.isAbstract, isFalse);
expect(method.isExternal, isFalse);
expect(method.isStatic, isFalse);
expect(method.isSynthetic, isFalse);
}
void test_visitMethodDeclaration_withMembers() {
// m(p) { var v; try { l: return; } catch (e) {} }
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String methodName = "m";
String parameterName = "p";
String localVariableName = "v";
String labelName = "l";
String exceptionParameterName = "e";
MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
null,
null,
null,
null,
AstFactory.identifier3(methodName),
AstFactory.formalParameterList(
[AstFactory.simpleFormalParameter3(parameterName)]),
AstFactory.blockFunctionBody2([
AstFactory.variableDeclarationStatement2(
Keyword.VAR, [AstFactory.variableDeclaration(localVariableName)]),
AstFactory.tryStatement2(
AstFactory.block([
AstFactory.labeledStatement([AstFactory.label2(labelName)],
AstFactory.returnStatement())
]),
[AstFactory.catchClause(exceptionParameterName)])
]));
methodDeclaration.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
MethodElement method = methods[0];
expect(method, isNotNull);
expect(method.hasImplicitReturnType, isTrue);
expect(method.name, methodName);
expect(method.typeParameters, hasLength(0));
expect(method.isAbstract, isFalse);
expect(method.isExternal, isFalse);
expect(method.isStatic, isFalse);
expect(method.isSynthetic, isFalse);
List<VariableElement> parameters = method.parameters;
expect(parameters, hasLength(1));
VariableElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.name, parameterName);
List<VariableElement> localVariables = method.localVariables;
expect(localVariables, hasLength(2));
VariableElement firstVariable = localVariables[0];
VariableElement secondVariable = localVariables[1];
expect(firstVariable, isNotNull);
expect(secondVariable, isNotNull);
expect(
(firstVariable.name == localVariableName &&
secondVariable.name == exceptionParameterName) ||
(firstVariable.name == exceptionParameterName &&
secondVariable.name == localVariableName),
isTrue);
List<LabelElement> labels = method.labels;
expect(labels, hasLength(1));
LabelElement label = labels[0];
expect(label, isNotNull);
expect(label.name, labelName);
}
void test_visitNamedFormalParameter() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "p";
DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter(
AstFactory.simpleFormalParameter3(parameterName),
AstFactory.identifier3("42"));
_useParameterInMethod(formalParameter, 100, 110);
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.name, parameterName);
expect(parameter.isConst, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isSynthetic, isFalse);
expect(parameter.parameterKind, ParameterKind.NAMED);
{
SourceRange visibleRange = parameter.visibleRange;
expect(100, visibleRange.offset);
expect(110, visibleRange.end);
}
expect(parameter.defaultValueCode, "42");
FunctionElement initializer = parameter.initializer;
expect(initializer, isNotNull);
expect(initializer.isSynthetic, isTrue);
}
void test_visitSimpleFormalParameter_noType() {
// p
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "p";
SimpleFormalParameter formalParameter =
AstFactory.simpleFormalParameter3(parameterName);
_useParameterInMethod(formalParameter, 100, 110);
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.hasImplicitType, isTrue);
expect(parameter.initializer, isNull);
expect(parameter.isConst, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isSynthetic, isFalse);
expect(parameter.name, parameterName);
expect(parameter.parameterKind, ParameterKind.REQUIRED);
{
SourceRange visibleRange = parameter.visibleRange;
expect(100, visibleRange.offset);
expect(110, visibleRange.end);
}
}
void test_visitSimpleFormalParameter_type() {
// T p
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "p";
SimpleFormalParameter formalParameter = AstFactory.simpleFormalParameter4(
AstFactory.typeName4('T'), parameterName);
_useParameterInMethod(formalParameter, 100, 110);
formalParameter.accept(builder);
List<ParameterElement> parameters = holder.parameters;
expect(parameters, hasLength(1));
ParameterElement parameter = parameters[0];
expect(parameter, isNotNull);
expect(parameter.hasImplicitType, isFalse);
expect(parameter.initializer, isNull);
expect(parameter.isConst, isFalse);
expect(parameter.isFinal, isFalse);
expect(parameter.isSynthetic, isFalse);
expect(parameter.name, parameterName);
expect(parameter.parameterKind, ParameterKind.REQUIRED);
{
SourceRange visibleRange = parameter.visibleRange;
expect(100, visibleRange.offset);
expect(110, visibleRange.end);
}
}
void test_visitTypeAlias_minimal() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String aliasName = "F";
TypeAlias typeAlias = AstFactory.typeAlias(null, aliasName, null, null);
typeAlias.accept(builder);
List<FunctionTypeAliasElement> aliases = holder.typeAliases;
expect(aliases, hasLength(1));
FunctionTypeAliasElement alias = aliases[0];
expect(alias, isNotNull);
expect(alias.name, aliasName);
expect(alias.type, isNotNull);
expect(alias.isSynthetic, isFalse);
}
void test_visitTypeAlias_withFormalParameters() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String aliasName = "F";
String firstParameterName = "x";
String secondParameterName = "y";
TypeAlias typeAlias = AstFactory.typeAlias(
null,
aliasName,
AstFactory.typeParameterList(),
AstFactory.formalParameterList([
AstFactory.simpleFormalParameter3(firstParameterName),
AstFactory.simpleFormalParameter3(secondParameterName)
]));
typeAlias.accept(builder);
List<FunctionTypeAliasElement> aliases = holder.typeAliases;
expect(aliases, hasLength(1));
FunctionTypeAliasElement alias = aliases[0];
expect(alias, isNotNull);
expect(alias.name, aliasName);
expect(alias.type, isNotNull);
expect(alias.isSynthetic, isFalse);
List<VariableElement> parameters = alias.parameters;
expect(parameters, hasLength(2));
expect(parameters[0].name, firstParameterName);
expect(parameters[1].name, secondParameterName);
List<TypeParameterElement> typeParameters = alias.typeParameters;
expect(typeParameters, isNotNull);
expect(typeParameters, hasLength(0));
}
void test_visitTypeAlias_withTypeParameters() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String aliasName = "F";
String firstTypeParameterName = "A";
String secondTypeParameterName = "B";
TypeAlias typeAlias = AstFactory.typeAlias(
null,
aliasName,
AstFactory.typeParameterList(
[firstTypeParameterName, secondTypeParameterName]),
AstFactory.formalParameterList());
typeAlias.accept(builder);
List<FunctionTypeAliasElement> aliases = holder.typeAliases;
expect(aliases, hasLength(1));
FunctionTypeAliasElement alias = aliases[0];
expect(alias, isNotNull);
expect(alias.name, aliasName);
expect(alias.type, isNotNull);
expect(alias.isSynthetic, isFalse);
List<VariableElement> parameters = alias.parameters;
expect(parameters, isNotNull);
expect(parameters, hasLength(0));
List<TypeParameterElement> typeParameters = alias.typeParameters;
expect(typeParameters, hasLength(2));
expect(typeParameters[0].name, firstTypeParameterName);
expect(typeParameters[1].name, secondTypeParameterName);
}
void test_visitTypeParameter() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String parameterName = "E";
TypeParameter typeParameter = AstFactory.typeParameter(parameterName);
typeParameter.accept(builder);
List<TypeParameterElement> typeParameters = holder.typeParameters;
expect(typeParameters, hasLength(1));
TypeParameterElement typeParameterElement = typeParameters[0];
expect(typeParameterElement, isNotNull);
expect(typeParameterElement.name, parameterName);
expect(typeParameterElement.bound, isNull);
expect(typeParameterElement.isSynthetic, isFalse);
}
void test_visitVariableDeclaration_inConstructor() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
//
// C() {var v;}
//
String variableName = "v";
VariableDeclaration variable =
AstFactory.variableDeclaration2(variableName, null);
Statement statement =
AstFactory.variableDeclarationStatement2(null, [variable]);
ConstructorDeclaration constructor = AstFactory.constructorDeclaration2(
null,
null,
AstFactory.identifier3("C"),
"C",
AstFactory.formalParameterList(),
null,
AstFactory.blockFunctionBody2([statement]));
constructor.accept(builder);
List<ConstructorElement> constructors = holder.constructors;
expect(constructors, hasLength(1));
List<LocalVariableElement> variableElements =
constructors[0].localVariables;
expect(variableElements, hasLength(1));
LocalVariableElement variableElement = variableElements[0];
expect(variableElement.hasImplicitType, isTrue);
expect(variableElement.name, variableName);
}
void test_visitVariableDeclaration_inMethod() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
//
// m() {T v;}
//
String variableName = "v";
VariableDeclaration variable =
AstFactory.variableDeclaration2(variableName, null);
Statement statement = AstFactory.variableDeclarationStatement(
null, AstFactory.typeName4('T'), [variable]);
MethodDeclaration method = AstFactory.methodDeclaration2(
null,
null,
null,
null,
AstFactory.identifier3("m"),
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2([statement]));
method.accept(builder);
List<MethodElement> methods = holder.methods;
expect(methods, hasLength(1));
List<LocalVariableElement> variableElements = methods[0].localVariables;
expect(variableElements, hasLength(1));
LocalVariableElement variableElement = variableElements[0];
expect(variableElement.hasImplicitType, isFalse);
expect(variableElement.name, variableName);
}
void test_visitVariableDeclaration_localNestedInFunction() {
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
//
// var f = () {var v;};
//
String variableName = "v";
VariableDeclaration variable =
AstFactory.variableDeclaration2(variableName, null);
Statement statement =
AstFactory.variableDeclarationStatement2(null, [variable]);
Expression initializer = AstFactory.functionExpression2(
AstFactory.formalParameterList(),
AstFactory.blockFunctionBody2([statement]));
String fieldName = "f";
VariableDeclaration field =
AstFactory.variableDeclaration2(fieldName, initializer);
FieldDeclaration fieldDeclaration =
AstFactory.fieldDeclaration2(false, null, [field]);
fieldDeclaration.accept(builder);
List<FieldElement> variables = holder.fields;
expect(variables, hasLength(1));
FieldElement fieldElement = variables[0];
expect(fieldElement, isNotNull);
FunctionElement initializerElement = fieldElement.initializer;
expect(initializerElement, isNotNull);
List<FunctionElement> functionElements = initializerElement.functions;
expect(functionElements, hasLength(1));
List<LocalVariableElement> variableElements =
functionElements[0].localVariables;
expect(variableElements, hasLength(1));
LocalVariableElement variableElement = variableElements[0];
expect(variableElement.hasImplicitType, isTrue);
expect(variableElement.isConst, isFalse);
expect(variableElement.isFinal, isFalse);
expect(variableElement.isSynthetic, isFalse);
expect(variableElement.name, variableName);
}
void test_visitVariableDeclaration_noInitializer() {
// var v;
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String variableName = "v";
VariableDeclaration variableDeclaration =
AstFactory.variableDeclaration2(variableName, null);
AstFactory.variableDeclarationList2(null, [variableDeclaration]);
variableDeclaration.accept(builder);
List<TopLevelVariableElement> variables = holder.topLevelVariables;
expect(variables, hasLength(1));
TopLevelVariableElement variable = variables[0];
expect(variable, isNotNull);
expect(variable.hasImplicitType, isTrue);
expect(variable.initializer, isNull);
expect(variable.name, variableName);
expect(variable.isConst, isFalse);
expect(variable.isFinal, isFalse);
expect(variable.isSynthetic, isFalse);
expect(variable.getter, isNotNull);
expect(variable.setter, isNotNull);
}
void test_visitVariableDeclaration_top_const_hasInitializer() {
// const v = 42;
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String variableName = "v";
VariableDeclaration variableDeclaration =
AstFactory.variableDeclaration2(variableName, AstFactory.integer(42));
AstFactory.variableDeclarationList2(Keyword.CONST, [variableDeclaration]);
variableDeclaration.accept(builder);
List<TopLevelVariableElement> variables = holder.topLevelVariables;
expect(variables, hasLength(1));
TopLevelVariableElement variable = variables[0];
expect(variable, new isInstanceOf<ConstTopLevelVariableElementImpl>());
expect(variable.initializer, isNotNull);
expect(variable.name, variableName);
expect(variable.hasImplicitType, isTrue);
expect(variable.isConst, isTrue);
expect(variable.isFinal, isFalse);
expect(variable.isSynthetic, isFalse);
expect(variable.getter, isNotNull);
expect(variable.setter, isNull);
}
void test_visitVariableDeclaration_top_docRange() {
// final a, b;
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
VariableDeclaration variableDeclaration1 =
AstFactory.variableDeclaration('a');
VariableDeclaration variableDeclaration2 =
AstFactory.variableDeclaration('b');
TopLevelVariableDeclaration topLevelVariableDeclaration = AstFactory
.topLevelVariableDeclaration(
Keyword.FINAL, null, [variableDeclaration1, variableDeclaration2]);
topLevelVariableDeclaration.documentationComment = AstFactory
.documentationComment(
[TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
topLevelVariableDeclaration.accept(builder);
List<TopLevelVariableElement> variables = holder.topLevelVariables;
expect(variables, hasLength(2));
TopLevelVariableElement variable1 = variables[0];
expect(variable1, isNotNull);
_assertHasDocRange(variable1, 50, 7);
TopLevelVariableElement variable2 = variables[1];
expect(variable2, isNotNull);
_assertHasDocRange(variable2, 50, 7);
}
void test_visitVariableDeclaration_top_final() {
// final v;
ElementHolder holder = new ElementHolder();
ElementBuilder builder = new ElementBuilder(holder);
String variableName = "v";
VariableDeclaration variableDeclaration =
AstFactory.variableDeclaration2(variableName, null);
AstFactory.variableDeclarationList2(Keyword.FINAL, [variableDeclaration]);
variableDeclaration.accept(builder);
List<TopLevelVariableElement> variables = holder.topLevelVariables;
expect(variables, hasLength(1));
TopLevelVariableElement variable = variables[0];
expect(variable, isNotNull);
expect(variable.hasImplicitType, isTrue);
expect(variable.initializer, isNull);
expect(variable.name, variableName);
expect(variable.isConst, isFalse);
expect(variable.isFinal, isTrue);
expect(variable.isSynthetic, isFalse);
expect(variable.getter, isNotNull);
expect(variable.setter, isNull);
}
void _assertHasDocRange(
Element element, int expectedOffset, int expectedLength) {
// Cast to dynamic here to avoid a hint about @deprecated docRange.
SourceRange docRange = (element as dynamic).docRange;
expect(docRange, isNotNull);
expect(docRange.offset, expectedOffset);
expect(docRange.length, expectedLength);
}
void _useParameterInMethod(
FormalParameter formalParameter, int blockOffset, int blockEnd) {
Block block = AstFactory.block();
block.leftBracket.offset = blockOffset;
block.rightBracket.offset = blockEnd - 1;
BlockFunctionBody body = AstFactory.blockFunctionBody(block);
AstFactory.methodDeclaration2(
null,
null,
null,
null,
AstFactory.identifier3("main"),
AstFactory.formalParameterList([formalParameter]),
body);
}
}
@reflectiveTest
class ElementLocatorTest extends ResolverTestCase {
void fail_locate_ExportDirective() {
AstNode id = _findNodeIn("export", "export 'dart:core';");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ImportElement, ImportElement, element);
}
void fail_locate_Identifier_libraryDirective() {
AstNode id = _findNodeIn("foo", "library foo.bar;");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is LibraryElement, LibraryElement, element);
}
void fail_locate_Identifier_partOfDirective() {
// Can't resolve the library element without the library declaration.
// AstNode id = findNodeIn("foo", "part of foo.bar;");
// Element element = ElementLocator.locate(id);
// assertInstanceOf(LibraryElement.class, element);
fail("Test this case");
}
@override
void reset() {
AnalysisOptionsImpl analysisOptions = new AnalysisOptionsImpl();
analysisOptions.hint = false;
resetWithOptions(analysisOptions);
}
void test_locate_AssignmentExpression() {
AstNode id = _findNodeIn(
"+=",
r'''
int x = 0;
void main() {
x += 1;
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is MethodElement, MethodElement, element);
}
void test_locate_BinaryExpression() {
AstNode id = _findNodeIn("+", "var x = 3 + 4;");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is MethodElement, MethodElement, element);
}
void test_locate_ClassDeclaration() {
AstNode id = _findNodeIn("class", "class A { }");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ClassElement, ClassElement, element);
}
void test_locate_CompilationUnit() {
CompilationUnit cu = _resolveContents("// only comment");
expect(cu.element, isNotNull);
Element element = ElementLocator.locate(cu);
expect(element, same(cu.element));
}
void test_locate_ConstructorDeclaration() {
AstNode id = _findNodeIndexedIn(
"bar",
0,
r'''
class A {
A.bar() {}
}''');
ConstructorDeclaration declaration =
id.getAncestor((node) => node is ConstructorDeclaration);
Element element = ElementLocator.locate(declaration);
EngineTestCase.assertInstanceOf(
(obj) => obj is ConstructorElement, ConstructorElement, element);
}
void test_locate_FunctionDeclaration() {
AstNode id = _findNodeIn("f", "int f() => 3;");
FunctionDeclaration declaration =
id.getAncestor((node) => node is FunctionDeclaration);
Element element = ElementLocator.locate(declaration);
EngineTestCase.assertInstanceOf(
(obj) => obj is FunctionElement, FunctionElement, element);
}
void
test_locate_Identifier_annotationClass_namedConstructor_forSimpleFormalParameter() {
AstNode id = _findNodeIndexedIn(
"Class",
2,
r'''
class Class {
const Class.name();
}
void main(@Class.name() parameter) {
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ClassElement, ClassElement, element);
}
void
test_locate_Identifier_annotationClass_unnamedConstructor_forSimpleFormalParameter() {
AstNode id = _findNodeIndexedIn(
"Class",
2,
r'''
class Class {
const Class();
}
void main(@Class() parameter) {
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ConstructorElement, ConstructorElement, element);
}
void test_locate_Identifier_className() {
AstNode id = _findNodeIn("A", "class A { }");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ClassElement, ClassElement, element);
}
void test_locate_Identifier_constructor_named() {
AstNode id = _findNodeIndexedIn(
"bar",
0,
r'''
class A {
A.bar() {}
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ConstructorElement, ConstructorElement, element);
}
void test_locate_Identifier_constructor_unnamed() {
AstNode id = _findNodeIndexedIn(
"A",
1,
r'''
class A {
A() {}
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ConstructorElement, ConstructorElement, element);
}
void test_locate_Identifier_fieldName() {
AstNode id = _findNodeIn("x", "class A { var x; }");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is FieldElement, FieldElement, element);
}
void test_locate_Identifier_propertAccess() {
AstNode id = _findNodeIn(
"length",
r'''
void main() {
int x = 'foo'.length;
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf((obj) => obj is PropertyAccessorElement,
PropertyAccessorElement, element);
}
void test_locate_ImportDirective() {
AstNode id = _findNodeIn("import", "import 'dart:core';");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is ImportElement, ImportElement, element);
}
void test_locate_IndexExpression() {
AstNode id = _findNodeIndexedIn(
"\\[",
1,
r'''
void main() {
List x = [1, 2];
var y = x[0];
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is MethodElement, MethodElement, element);
}
void test_locate_InstanceCreationExpression() {
AstNode node = _findNodeIndexedIn(
"A(",
0,
r'''
class A {}
void main() {
new A();
}''');
Element element = ElementLocator.locate(node);
EngineTestCase.assertInstanceOf(
(obj) => obj is ConstructorElement, ConstructorElement, element);
}
void test_locate_InstanceCreationExpression_type_prefixedIdentifier() {
// prepare: new pref.A()
SimpleIdentifier identifier = AstFactory.identifier3("A");
PrefixedIdentifier prefixedIdentifier =
AstFactory.identifier4("pref", identifier);
InstanceCreationExpression creation =
AstFactory.instanceCreationExpression2(
Keyword.NEW, AstFactory.typeName3(prefixedIdentifier));
// set ClassElement
ClassElement classElement = ElementFactory.classElement2("A");
identifier.staticElement = classElement;
// set ConstructorElement
ConstructorElement constructorElement =
ElementFactory.constructorElement2(classElement, null);
creation.constructorName.staticElement = constructorElement;
// verify that "A" is resolved to ConstructorElement
Element element = ElementLocator.locate(identifier);
expect(element, same(classElement));
}
void test_locate_InstanceCreationExpression_type_simpleIdentifier() {
// prepare: new A()
SimpleIdentifier identifier = AstFactory.identifier3("A");
InstanceCreationExpression creation =
AstFactory.instanceCreationExpression2(
Keyword.NEW, AstFactory.typeName3(identifier));
// set ClassElement
ClassElement classElement = ElementFactory.classElement2("A");
identifier.staticElement = classElement;
// set ConstructorElement
ConstructorElement constructorElement =
ElementFactory.constructorElement2(classElement, null);
creation.constructorName.staticElement = constructorElement;
// verify that "A" is resolved to ConstructorElement
Element element = ElementLocator.locate(identifier);
expect(element, same(classElement));
}
void test_locate_LibraryDirective() {
AstNode id = _findNodeIn("library", "library foo;");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is LibraryElement, LibraryElement, element);
}
void test_locate_MethodDeclaration() {
AstNode id = _findNodeIn(
"m",
r'''
class A {
void m() {}
}''');
MethodDeclaration declaration =
id.getAncestor((node) => node is MethodDeclaration);
Element element = ElementLocator.locate(declaration);
EngineTestCase.assertInstanceOf(
(obj) => obj is MethodElement, MethodElement, element);
}
void test_locate_MethodInvocation_method() {
AstNode id = _findNodeIndexedIn(
"bar",
1,
r'''
class A {
int bar() => 42;
}
void main() {
var f = new A().bar();
}''');
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is MethodElement, MethodElement, element);
}
void test_locate_MethodInvocation_topLevel() {
String code = r'''
foo(x) {}
void main() {
foo(0);
}''';
CompilationUnit cu = _resolveContents(code);
int offset = code.indexOf('foo(0)');
AstNode node = new NodeLocator(offset).searchWithin(cu);
MethodInvocation invocation =
node.getAncestor((n) => n is MethodInvocation);
Element element = ElementLocator.locate(invocation);
EngineTestCase.assertInstanceOf(
(obj) => obj is FunctionElement, FunctionElement, element);
}
void test_locate_PartOfDirective() {
Source librarySource = addNamedSource(
'/lib.dart',
'''
library my.lib;
part 'part.dart';
''');
Source unitSource = addNamedSource(
'/part.dart',
'''
part of my.lib;
''');
CompilationUnit unit =
analysisContext.resolveCompilationUnit2(unitSource, librarySource);
PartOfDirective partOf = unit.directives.first;
Element element = ElementLocator.locate(partOf);
EngineTestCase.assertInstanceOf(
(obj) => obj is LibraryElement, LibraryElement, element);
}
void test_locate_PostfixExpression() {
AstNode id = _findNodeIn("++", "int addOne(int x) => x++;");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is MethodElement, MethodElement, element);
}
void test_locate_PrefixedIdentifier() {
AstNode id = _findNodeIn(
"int",
r'''
import 'dart:core' as core;
core.int value;''');
PrefixedIdentifier identifier =
id.getAncestor((node) => node is PrefixedIdentifier);
Element element = ElementLocator.locate(identifier);
EngineTestCase.assertInstanceOf(
(obj) => obj is ClassElement, ClassElement, element);
}
void test_locate_PrefixExpression() {
AstNode id = _findNodeIn("++", "int addOne(int x) => ++x;");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is MethodElement, MethodElement, element);
}
void test_locate_StringLiteral_exportUri() {
addNamedSource("/foo.dart", "library foo;");
AstNode id = _findNodeIn("'foo.dart'", "export 'foo.dart';");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is LibraryElement, LibraryElement, element);
}
void test_locate_StringLiteral_expression() {
AstNode id = _findNodeIn("abc", "var x = 'abc';");
Element element = ElementLocator.locate(id);
expect(element, isNull);
}
void test_locate_StringLiteral_importUri() {
addNamedSource("/foo.dart", "library foo; class A {}");
AstNode id =
_findNodeIn("'foo.dart'", "import 'foo.dart'; class B extends A {}");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf(
(obj) => obj is LibraryElement, LibraryElement, element);
}
void test_locate_StringLiteral_partUri() {
addNamedSource("/foo.dart", "part of app;");
AstNode id = _findNodeIn("'foo.dart'", "library app; part 'foo.dart';");
Element element = ElementLocator.locate(id);
EngineTestCase.assertInstanceOf((obj) => obj is CompilationUnitElement,
CompilationUnitElement, element);
}
void test_locate_VariableDeclaration() {
AstNode id = _findNodeIn("x", "var x = 'abc';");
VariableDeclaration declaration =
id.getAncestor((node) => node is VariableDeclaration);
Element element = ElementLocator.locate(declaration);
EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement,
TopLevelVariableElement, element);
}
/**
* Find the first AST node matching a pattern in the resolved AST for the given source.
*
* [nodePattern] the (unique) pattern used to identify the node of interest.
* [code] the code to resolve.
* Returns the matched node in the resolved AST for the given source lines.
*/
AstNode _findNodeIn(String nodePattern, String code) {
return _findNodeIndexedIn(nodePattern, 0, code);
}
/**
* Find the AST node matching the given indexed occurrence of a pattern in the resolved AST for
* the given source.
*
* [nodePattern] the pattern used to identify the node of interest.
* [index] the index of the pattern match of interest.
* [code] the code to resolve.
* Returns the matched node in the resolved AST for the given source lines
*/
AstNode _findNodeIndexedIn(String nodePattern, int index, String code) {
CompilationUnit cu = _resolveContents(code);
int start = _getOffsetOfMatch(code, nodePattern, index);
int end = start + nodePattern.length;
return new NodeLocator(start, end).searchWithin(cu);
}
int _getOffsetOfMatch(String contents, String pattern, int matchIndex) {
if (matchIndex == 0) {
return contents.indexOf(pattern);
}
JavaPatternMatcher matcher =
new JavaPatternMatcher(new RegExp(pattern), contents);
int count = 0;
while (matcher.find()) {
if (count == matchIndex) {
return matcher.start();
}
++count;
}
return -1;
}
/**
* Parse, resolve and verify the given source lines to produce a fully
* resolved AST.
*
* [code] the code to resolve.
*
* Returns the result of resolving the AST structure representing the content
* of the source.
*
* Throws if source cannot be verified.
*/
CompilationUnit _resolveContents(String code) {
Source source = addSource(code);
LibraryElement library = resolve2(source);
assertNoErrors(source);
verify([source]);
return analysisContext.resolveCompilationUnit(source, library);
}
}
@reflectiveTest
class EnumMemberBuilderTest extends EngineTestCase {
void test_visitEnumDeclaration_multiple() {
String firstName = "ONE";
String secondName = "TWO";
String thirdName = "THREE";
EnumDeclaration enumDeclaration =
AstFactory.enumDeclaration2("E", [firstName, secondName, thirdName]);
ClassElement enumElement = _buildElement(enumDeclaration);
List<FieldElement> fields = enumElement.fields;
expect(fields, hasLength(5));
FieldElement constant = fields[2];
expect(constant, isNotNull);
expect(constant.name, firstName);
expect(constant.isStatic, isTrue);
expect((constant as FieldElementImpl).evaluationResult, isNotNull);
_assertGetter(constant);
constant = fields[3];
expect(constant, isNotNull);
expect(constant.name, secondName);
expect(constant.isStatic, isTrue);
expect((constant as FieldElementImpl).evaluationResult, isNotNull);
_assertGetter(constant);
constant = fields[4];
expect(constant, isNotNull);
expect(constant.name, thirdName);
expect(constant.isStatic, isTrue);
expect((constant as FieldElementImpl).evaluationResult, isNotNull);
_assertGetter(constant);
}
void test_visitEnumDeclaration_single() {
String firstName = "ONE";
EnumDeclaration enumDeclaration =
AstFactory.enumDeclaration2("E", [firstName]);
ClassElement enumElement = _buildElement(enumDeclaration);
List<FieldElement> fields = enumElement.fields;
expect(fields, hasLength(3));
FieldElement field = fields[0];
expect(field, isNotNull);
expect(field.name, "index");
expect(field.isStatic, isFalse);
expect(field.isSynthetic, isTrue);
_assertGetter(field);
field = fields[1];
expect(field, isNotNull);
expect(field.name, "values");
expect(field.isStatic, isTrue);
expect(field.isSynthetic, isTrue);
expect((field as FieldElementImpl).evaluationResult, isNotNull);
_assertGetter(field);
FieldElement constant = fields[2];
expect(constant, isNotNull);
expect(constant.name, firstName);
expect(constant.isStatic, isTrue);
expect((constant as FieldElementImpl).evaluationResult, isNotNull);
_assertGetter(constant);
}
void _assertGetter(FieldElement field) {
PropertyAccessorElement getter = field.getter;
expect(getter, isNotNull);
expect(getter.variable, same(field));
expect(getter.type, isNotNull);
}
ClassElement _buildElement(EnumDeclaration enumDeclaration) {
ElementHolder holder = new ElementHolder();
ElementBuilder elementBuilder = new ElementBuilder(holder);
enumDeclaration.accept(elementBuilder);
EnumMemberBuilder memberBuilder =
new EnumMemberBuilder(new TestTypeProvider());
enumDeclaration.accept(memberBuilder);
List<ClassElement> enums = holder.enums;
expect(enums, hasLength(1));
return enums[0];
}
}
@reflectiveTest
class ErrorReporterTest extends EngineTestCase {
/**
* Create a type with the given name in a compilation unit with the given name.
*
* @param fileName the name of the compilation unit containing the class
* @param typeName the name of the type to be created
* @return the type that was created
*/
InterfaceType createType(String fileName, String typeName) {
CompilationUnitElementImpl unit = ElementFactory.compilationUnit(fileName);
ClassElementImpl element = ElementFactory.classElement2(typeName);
unit.types = <ClassElement>[element];
return element.type;
}
void test_creation() {
GatheringErrorListener listener = new GatheringErrorListener();
TestSource source = new TestSource();
expect(new ErrorReporter(listener, source), isNotNull);
}
void test_reportErrorForElement_named() {
DartType type = createType("/test1.dart", "A");
ClassElement element = type.element;
GatheringErrorListener listener = new GatheringErrorListener();
ErrorReporter reporter = new ErrorReporter(listener, element.source);
reporter.reportErrorForElement(
StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER,
element,
['A']);
AnalysisError error = listener.errors[0];
expect(error.offset, element.nameOffset);
}
void test_reportErrorForElement_unnamed() {
ImportElementImpl element =
ElementFactory.importFor(ElementFactory.library(null, ''), null);
GatheringErrorListener listener = new GatheringErrorListener();
ErrorReporter reporter = new ErrorReporter(
listener,
new NonExistingSource(
'/test.dart', toUri('/test.dart'), UriKind.FILE_URI));
reporter.reportErrorForElement(
StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER,
element,
['A']);
AnalysisError error = listener.errors[0];
expect(error.offset, element.nameOffset);
}
void test_reportErrorForSpan() {
GatheringErrorListener listener = new GatheringErrorListener();
ErrorReporter reporter = new ErrorReporter(listener, new TestSource());
var src = '''
foo: bar
zap: baz
''';
int offset = src.indexOf('baz');
int length = 'baz'.length;
SourceSpan span = new SourceFile(src).span(offset, offset + length);
reporter.reportErrorForSpan(
AnalysisOptionsWarningCode.UNSUPPORTED_OPTION_WITH_LEGAL_VALUE,
span,
['test', 'zip', 'zap']);
expect(listener.errors, hasLength(1));
expect(listener.errors.first.offset, offset);
expect(listener.errors.first.length, length);
}
void test_reportTypeErrorForNode_differentNames() {
DartType firstType = createType("/test1.dart", "A");
DartType secondType = createType("/test2.dart", "B");
GatheringErrorListener listener = new GatheringErrorListener();
ErrorReporter reporter =
new ErrorReporter(listener, firstType.element.source);
reporter.reportTypeErrorForNode(
StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE,
AstFactory.identifier3("x"),
[firstType, secondType]);
AnalysisError error = listener.errors[0];
expect(error.message.indexOf("(") < 0, isTrue);
}
void test_reportTypeErrorForNode_sameName() {
String typeName = "A";
DartType firstType = createType("/test1.dart", typeName);
DartType secondType = createType("/test2.dart", typeName);
GatheringErrorListener listener = new GatheringErrorListener();
ErrorReporter reporter =
new ErrorReporter(listener, firstType.element.source);
reporter.reportTypeErrorForNode(
StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE,
AstFactory.identifier3("x"),
[firstType, secondType]);
AnalysisError error = listener.errors[0];
expect(error.message.indexOf("(") >= 0, isTrue);
}
}
@reflectiveTest
class ErrorSeverityTest extends EngineTestCase {
void test_max_error_error() {
expect(ErrorSeverity.ERROR.max(ErrorSeverity.ERROR),
same(ErrorSeverity.ERROR));
}
void test_max_error_none() {
expect(
ErrorSeverity.ERROR.max(ErrorSeverity.NONE), same(ErrorSeverity.ERROR));
}
void test_max_error_warning() {
expect(ErrorSeverity.ERROR.max(ErrorSeverity.WARNING),
same(ErrorSeverity.ERROR));
}
void test_max_none_error() {
expect(
ErrorSeverity.NONE.max(ErrorSeverity.ERROR), same(ErrorSeverity.ERROR));
}
void test_max_none_none() {
expect(
ErrorSeverity.NONE.max(ErrorSeverity.NONE), same(ErrorSeverity.NONE));
}
void test_max_none_warning() {
expect(ErrorSeverity.NONE.max(ErrorSeverity.WARNING),
same(ErrorSeverity.WARNING));
}
void test_max_warning_error() {
expect(ErrorSeverity.WARNING.max(ErrorSeverity.ERROR),
same(ErrorSeverity.ERROR));
}
void test_max_warning_none() {
expect(ErrorSeverity.WARNING.max(ErrorSeverity.NONE),
same(ErrorSeverity.WARNING));
}
void test_max_warning_warning() {
expect(ErrorSeverity.WARNING.max(ErrorSeverity.WARNING),
same(ErrorSeverity.WARNING));
}
}
/**
* Tests for the [ExitDetector] that do not require that the AST be resolved.
*
* See [ExitDetectorTest2] for tests that require the AST to be resolved.
*/
@reflectiveTest
class ExitDetectorTest extends ParserTestCase {
void fail_doStatement_continue_with_label() {
_assertFalse("{ x: do { continue x; } while(true); }");
}
void fail_whileStatement_continue_with_label() {
_assertFalse("{ x: while (true) { continue x; } }");
}
void fail_whileStatement_doStatement_scopeRequired() {
_assertTrue("{ while (true) { x: do { continue x; } while(true); }");
}
void test_asExpression() {
_assertFalse("a as Object;");
}
void test_asExpression_throw() {
_assertTrue("throw '' as Object;");
}
void test_assertStatement() {
_assertFalse("assert(a);");
}
void test_assertStatement_throw() {
_assertFalse("assert((throw 0));");
}
void test_assignmentExpression() {
_assertFalse("v = 1;");
}
void test_assignmentExpression_lhs_throw() {
_assertTrue("a[throw ''] = 0;");
}
void test_assignmentExpression_rhs_throw() {
_assertTrue("v = throw '';");
}
void test_await_false() {
_assertFalse("await x;");
}
void test_await_throw_true() {
_assertTrue("bool b = await (throw '' || true);");
}
void test_binaryExpression_and() {
_assertFalse("a && b;");
}
void test_binaryExpression_and_lhs() {
_assertTrue("throw '' && b;");
}
void test_binaryExpression_and_rhs() {
_assertFalse("a && (throw '');");
}
void test_binaryExpression_and_rhs2() {
_assertFalse("false && (throw '');");
}
void test_binaryExpression_and_rhs3() {
_assertTrue("true && (throw '');");
}
void test_binaryExpression_ifNull() {
_assertFalse("a ?? b;");
}
void test_binaryExpression_ifNull_lhs() {
_assertTrue("throw '' ?? b;");
}
void test_binaryExpression_ifNull_rhs() {
_assertFalse("a ?? (throw '');");
}
void test_binaryExpression_ifNull_rhs2() {
_assertFalse("null ?? (throw '');");
}
void test_binaryExpression_or() {
_assertFalse("a || b;");
}
void test_binaryExpression_or_lhs() {
_assertTrue("throw '' || b;");
}
void test_binaryExpression_or_rhs() {
_assertFalse("a || (throw '');");
}
void test_binaryExpression_or_rhs2() {
_assertFalse("true || (throw '');");
}
void test_binaryExpression_or_rhs3() {
_assertTrue("false || (throw '');");
}
void test_block_empty() {
_assertFalse("{}");
}
void test_block_noReturn() {
_assertFalse("{ int i = 0; }");
}
void test_block_return() {
_assertTrue("{ return 0; }");
}
void test_block_returnNotLast() {
_assertTrue("{ return 0; throw 'a'; }");
}
void test_block_throwNotLast() {
_assertTrue("{ throw 0; x = null; }");
}
void test_cascadeExpression_argument() {
_assertTrue("a..b(throw '');");
}
void test_cascadeExpression_index() {
_assertTrue("a..[throw ''];");
}
void test_cascadeExpression_target() {
_assertTrue("throw ''..b();");
}
void test_conditional_ifElse_bothThrows() {
_assertTrue("c ? throw '' : throw '';");
}
void test_conditional_ifElse_elseThrows() {
_assertFalse("c ? i : throw '';");
}
void test_conditional_ifElse_noThrow() {
_assertFalse("c ? i : j;");
}
void test_conditional_ifElse_thenThrow() {
_assertFalse("c ? throw '' : j;");
}
void test_conditionalAccess() {
_assertFalse("a?.b;");
}
void test_conditionalAccess_lhs() {
_assertTrue("(throw '')?.b;");
}
void test_conditionalAccessAssign() {
_assertFalse("a?.b = c;");
}
void test_conditionalAccessAssign_lhs() {
_assertTrue("(throw '')?.b = c;");
}
void test_conditionalAccessAssign_rhs() {
_assertFalse("a?.b = throw '';");
}
void test_conditionalAccessAssign_rhs2() {
_assertFalse("null?.b = throw '';");
}
void test_conditionalAccessIfNullAssign() {
_assertFalse("a?.b ??= c;");
}
void test_conditionalAccessIfNullAssign_lhs() {
_assertTrue("(throw '')?.b ??= c;");
}
void test_conditionalAccessIfNullAssign_rhs() {
_assertFalse("a?.b ??= throw '';");
}
void test_conditionalAccessIfNullAssign_rhs2() {
_assertFalse("null?.b ??= throw '';");
}
void test_conditionalCall() {
_assertFalse("a?.b(c);");
}
void test_conditionalCall_lhs() {
_assertTrue("(throw '')?.b(c);");
}
void test_conditionalCall_rhs() {
_assertFalse("a?.b(throw '');");
}
void test_conditionalCall_rhs2() {
_assertFalse("null?.b(throw '');");
}
void test_creation() {
expect(new ExitDetector(), isNotNull);
}
void test_doStatement_throwCondition() {
_assertTrue("{ do {} while (throw ''); }");
}
void test_doStatement_true_break() {
_assertFalse("{ do { break; } while (true); }");
}
void test_doStatement_true_continue() {
_assertTrue("{ do { continue; } while (true); }");
}
void test_doStatement_true_if_return() {
_assertTrue("{ do { if (true) {return null;} } while (true); }");
}
void test_doStatement_true_noBreak() {
_assertTrue("{ do {} while (true); }");
}
void test_doStatement_true_return() {
_assertTrue("{ do { return null; } while (true); }");
}
void test_emptyStatement() {
_assertFalse(";");
}
void test_forEachStatement() {
_assertFalse("for (element in list) {}");
}
void test_forEachStatement_throw() {
_assertTrue("for (element in throw '') {}");
}
void test_forStatement_condition() {
_assertTrue("for (; throw 0;) {}");
}
void test_forStatement_implicitTrue() {
_assertTrue("for (;;) {}");
}
void test_forStatement_implicitTrue_break() {
_assertFalse("for (;;) { break; }");
}
void test_forStatement_initialization() {
_assertTrue("for (i = throw 0;;) {}");
}
void test_forStatement_true() {
_assertTrue("for (; true; ) {}");
}
void test_forStatement_true_break() {
_assertFalse("{ for (; true; ) { break; } }");
}
void test_forStatement_true_continue() {
_assertTrue("{ for (; true; ) { continue; } }");
}
void test_forStatement_true_if_return() {
_assertTrue("{ for (; true; ) { if (true) {return null;} } }");
}
void test_forStatement_true_noBreak() {
_assertTrue("{ for (; true; ) {} }");
}
void test_forStatement_updaters() {
_assertTrue("for (;; i++, throw 0) {}");
}
void test_forStatement_variableDeclaration() {
_assertTrue("for (int i = throw 0;;) {}");
}
void test_functionExpression() {
_assertFalse("(){};");
}
void test_functionExpression_bodyThrows() {
_assertFalse("(int i) => throw '';");
}
void test_functionExpressionInvocation() {
_assertFalse("f(g);");
}
void test_functionExpressionInvocation_argumentThrows() {
_assertTrue("f(throw '');");
}
void test_functionExpressionInvocation_targetThrows() {
_assertTrue("throw ''(g);");
}
void test_identifier_prefixedIdentifier() {
_assertFalse("a.b;");
}
void test_identifier_simpleIdentifier() {
_assertFalse("a;");
}
void test_if_false_else_return() {
_assertTrue("if (false) {} else { return 0; }");
}
void test_if_false_noReturn() {
_assertFalse("if (false) {}");
}
void test_if_false_return() {
_assertFalse("if (false) { return 0; }");
}
void test_if_noReturn() {
_assertFalse("if (c) i++;");
}
void test_if_return() {
_assertFalse("if (c) return 0;");
}
void test_if_true_noReturn() {
_assertFalse("if (true) {}");
}
void test_if_true_return() {
_assertTrue("if (true) { return 0; }");
}
void test_ifElse_bothReturn() {
_assertTrue("if (c) return 0; else return 1;");
}
void test_ifElse_elseReturn() {
_assertFalse("if (c) i++; else return 1;");
}
void test_ifElse_noReturn() {
_assertFalse("if (c) i++; else j++;");
}
void test_ifElse_thenReturn() {
_assertFalse("if (c) return 0; else j++;");
}
void test_ifNullAssign() {
_assertFalse("a ??= b;");
}
void test_ifNullAssign_rhs() {
_assertFalse("a ??= throw '';");
}
void test_indexExpression() {
_assertFalse("a[b];");
}
void test_indexExpression_index() {
_assertTrue("a[throw ''];");
}
void test_indexExpression_target() {
_assertTrue("throw ''[b];");
}
void test_instanceCreationExpression() {
_assertFalse("new A(b);");
}
void test_instanceCreationExpression_argumentThrows() {
_assertTrue("new A(throw '');");
}
void test_isExpression() {
_assertFalse("A is B;");
}
void test_isExpression_throws() {
_assertTrue("throw '' is B;");
}
void test_labeledStatement() {
_assertFalse("label: a;");
}
void test_labeledStatement_throws() {
_assertTrue("label: throw '';");
}
void test_literal_boolean() {
_assertFalse("true;");
}
void test_literal_double() {
_assertFalse("1.1;");
}
void test_literal_integer() {
_assertFalse("1;");
}
void test_literal_null() {
_assertFalse("null;");
}
void test_literal_String() {
_assertFalse("'str';");
}
void test_methodInvocation() {
_assertFalse("a.b(c);");
}
void test_methodInvocation_argument() {
_assertTrue("a.b(throw '');");
}
void test_methodInvocation_target() {
_assertTrue("throw ''.b(c);");
}
void test_parenthesizedExpression() {
_assertFalse("(a);");
}
void test_parenthesizedExpression_throw() {
_assertTrue("(throw '');");
}
void test_propertyAccess() {
_assertFalse("new Object().a;");
}
void test_propertyAccess_throws() {
_assertTrue("(throw '').a;");
}
void test_rethrow() {
_assertTrue("rethrow;");
}
void test_return() {
_assertTrue("return 0;");
}
void test_superExpression() {
_assertFalse("super.a;");
}
void test_switch_allReturn() {
_assertTrue("switch (i) { case 0: return 0; default: return 1; }");
}
void test_switch_defaultWithNoStatements() {
_assertFalse("switch (i) { case 0: return 0; default: }");
}
void test_switch_fallThroughToNotReturn() {
_assertFalse("switch (i) { case 0: case 1: break; default: return 1; }");
}
void test_switch_fallThroughToReturn() {
_assertTrue("switch (i) { case 0: case 1: return 0; default: return 1; }");
}
void test_switch_noDefault() {
_assertFalse("switch (i) { case 0: return 0; }");
}
void test_switch_nonReturn() {
_assertFalse("switch (i) { case 0: i++; default: return 1; }");
}
void test_thisExpression() {
_assertFalse("this.a;");
}
void test_throwExpression() {
_assertTrue("throw new Object();");
}
void test_tryStatement_noReturn() {
_assertFalse("try {} catch (e, s) {} finally {}");
}
void test_tryStatement_return_catch() {
_assertFalse("try {} catch (e, s) { return 1; } finally {}");
}
void test_tryStatement_return_finally() {
_assertTrue("try {} catch (e, s) {} finally { return 1; }");
}
void test_tryStatement_return_try() {
_assertTrue("try { return 1; } catch (e, s) {} finally {}");
}
void test_variableDeclarationStatement_noInitializer() {
_assertFalse("int i;");
}
void test_variableDeclarationStatement_noThrow() {
_assertFalse("int i = 0;");
}
void test_variableDeclarationStatement_throw() {
_assertTrue("int i = throw new Object();");
}
void test_whileStatement_false_nonReturn() {
_assertFalse("{ while (false) {} }");
}
void test_whileStatement_throwCondition() {
_assertTrue("{ while (throw '') {} }");
}
void test_whileStatement_true_break() {
_assertFalse("{ while (true) { break; } }");
}
void test_whileStatement_true_continue() {
_assertTrue("{ while (true) { continue; } }");
}
void test_whileStatement_true_if_return() {
_assertTrue("{ while (true) { if (true) {return null;} } }");
}
void test_whileStatement_true_noBreak() {
_assertTrue("{ while (true) {} }");
}
void test_whileStatement_true_return() {
_assertTrue("{ while (true) { return null; } }");
}
void test_whileStatement_true_throw() {
_assertTrue("{ while (true) { throw ''; } }");
}
void _assertFalse(String source) {
_assertHasReturn(false, source);
}
void _assertHasReturn(bool expectedResult, String source) {
Statement statement = ParserTestCase.parseStatement(source);
expect(ExitDetector.exits(statement), expectedResult);
}
void _assertTrue(String source) {
_assertHasReturn(true, source);
}
}
/**
* Tests for the [ExitDetector] that require that the AST be resolved.
*
* See [ExitDetectorTest] for tests that do not require the AST to be resolved.
*/
@reflectiveTest
class ExitDetectorTest2 extends ResolverTestCase {
void test_switch_withEnum_false_noDefault() {
Source source = addSource(r'''
enum E { A, B }
String f(E e) {
var x;
switch (e) {
case A:
x = 'A';
case B:
x = 'B';
}
return x;
}
''');
LibraryElement element = resolve2(source);
CompilationUnit unit = resolveCompilationUnit(source, element);
FunctionDeclaration function = unit.declarations.last;
BlockFunctionBody body = function.functionExpression.body;
Statement statement = body.block.statements[1];
expect(ExitDetector.exits(statement), false);
}
void test_switch_withEnum_false_withDefault() {
Source source = addSource(r'''
enum E { A, B }
String f(E e) {
var x;
switch (e) {
case A:
x = 'A';
default:
x = '?';
}
return x;
}
''');
LibraryElement element = resolve2(source);
CompilationUnit unit = resolveCompilationUnit(source, element);
FunctionDeclaration function = unit.declarations.last;
BlockFunctionBody body = function.functionExpression.body;
Statement statement = body.block.statements[1];
expect(ExitDetector.exits(statement), false);
}
void test_switch_withEnum_true_noDefault() {
Source source = addSource(r'''
enum E { A, B }
String f(E e) {
switch (e) {
case A:
return 'A';
case B:
return 'B';
}
}
''');
LibraryElement element = resolve2(source);
CompilationUnit unit = resolveCompilationUnit(source, element);
FunctionDeclaration function = unit.declarations.last;
BlockFunctionBody body = function.functionExpression.body;
Statement statement = body.block.statements[0];
expect(ExitDetector.exits(statement), true);
}
void test_switch_withEnum_true_withDefault() {
Source source = addSource(r'''
enum E { A, B }
String f(E e) {
switch (e) {
case A:
return 'A';
default:
return '?';
}
}
''');
LibraryElement element = resolve2(source);
CompilationUnit unit = resolveCompilationUnit(source, element);
FunctionDeclaration function = unit.declarations.last;
BlockFunctionBody body = function.functionExpression.body;
Statement statement = body.block.statements[0];
expect(ExitDetector.exits(statement), true);
}
}
@reflectiveTest
class FileBasedSourceTest {
void test_equals_false_differentFiles() {
JavaFile file1 = FileUtilities2.createFile("/does/not/exist1.dart");
JavaFile file2 = FileUtilities2.createFile("/does/not/exist2.dart");
FileBasedSource source1 = new FileBasedSource(file1);
FileBasedSource source2 = new FileBasedSource(file2);
expect(source1 == source2, isFalse);
}
void test_equals_false_null() {
JavaFile file = FileUtilities2.createFile("/does/not/exist1.dart");
FileBasedSource source1 = new FileBasedSource(file);
expect(source1 == null, isFalse);
}
void test_equals_true() {
JavaFile file1 = FileUtilities2.createFile("/does/not/exist.dart");
JavaFile file2 = FileUtilities2.createFile("/does/not/exist.dart");
FileBasedSource source1 = new FileBasedSource(file1);
FileBasedSource source2 = new FileBasedSource(file2);
expect(source1 == source2, isTrue);
}
void test_fileReadMode() {
expect(FileBasedSource.fileReadMode('a'), 'a');
expect(FileBasedSource.fileReadMode('a\n'), 'a\n');
expect(FileBasedSource.fileReadMode('ab'), 'ab');
expect(FileBasedSource.fileReadMode('abc'), 'abc');
expect(FileBasedSource.fileReadMode('a\nb'), 'a\nb');
expect(FileBasedSource.fileReadMode('a\rb'), 'a\rb');
expect(FileBasedSource.fileReadMode('a\r\nb'), 'a\r\nb');
}
void test_fileReadMode_changed() {
FileBasedSource.fileReadMode = (String s) => s + 'xyz';
expect(FileBasedSource.fileReadMode('a'), 'axyz');
expect(FileBasedSource.fileReadMode('a\n'), 'a\nxyz');
expect(FileBasedSource.fileReadMode('ab'), 'abxyz');
expect(FileBasedSource.fileReadMode('abc'), 'abcxyz');
FileBasedSource.fileReadMode = (String s) => s;
}
void test_fileReadMode_normalize_eol_always() {
FileBasedSource.fileReadMode =
PhysicalResourceProvider.NORMALIZE_EOL_ALWAYS;
expect(FileBasedSource.fileReadMode('a'), 'a');
// '\n' -> '\n' as first, last and only character
expect(FileBasedSource.fileReadMode('\n'), '\n');
expect(FileBasedSource.fileReadMode('a\n'), 'a\n');
expect(FileBasedSource.fileReadMode('\na'), '\na');
// '\r\n' -> '\n' as first, last and only character
expect(FileBasedSource.fileReadMode('\r\n'), '\n');
expect(FileBasedSource.fileReadMode('a\r\n'), 'a\n');
expect(FileBasedSource.fileReadMode('\r\na'), '\na');
// '\r' -> '\n' as first, last and only character
expect(FileBasedSource.fileReadMode('\r'), '\n');
expect(FileBasedSource.fileReadMode('a\r'), 'a\n');
expect(FileBasedSource.fileReadMode('\ra'), '\na');
FileBasedSource.fileReadMode = (String s) => s;
}
void test_getEncoding() {
SourceFactory factory = new SourceFactory([new FileUriResolver()]);
String fullPath = "/does/not/exist.dart";
JavaFile file = FileUtilities2.createFile(fullPath);
FileBasedSource source = new FileBasedSource(file);
expect(factory.fromEncoding(source.encoding), source);
}
void test_getFullName() {
String fullPath = "/does/not/exist.dart";
JavaFile file = FileUtilities2.createFile(fullPath);
FileBasedSource source = new FileBasedSource(file);
expect(source.fullName, file.getAbsolutePath());
}
void test_getShortName() {
JavaFile file = FileUtilities2.createFile("/does/not/exist.dart");
FileBasedSource source = new FileBasedSource(file);
expect(source.shortName, "exist.dart");
}
void test_hashCode() {
JavaFile file1 = FileUtilities2.createFile("/does/not/exist.dart");
JavaFile file2 = FileUtilities2.createFile("/does/not/exist.dart");
FileBasedSource source1 = new FileBasedSource(file1);
FileBasedSource source2 = new FileBasedSource(file2);
expect(source2.hashCode, source1.hashCode);
}
void test_isInSystemLibrary_contagious() {
JavaFile sdkDirectory = DirectoryBasedDartSdk.defaultSdkDirectory;
expect(sdkDirectory, isNotNull);
DartSdk sdk = new DirectoryBasedDartSdk(sdkDirectory);
UriResolver resolver = new DartUriResolver(sdk);
SourceFactory factory = new SourceFactory([resolver]);
// resolve dart:core
Source result =
resolver.resolveAbsolute(parseUriWithException("dart:core"));
expect(result, isNotNull);
expect(result.isInSystemLibrary, isTrue);
// system libraries reference only other system libraries
Source partSource = factory.resolveUri(result, "num.dart");
expect(partSource, isNotNull);
expect(partSource.isInSystemLibrary, isTrue);
}
void test_isInSystemLibrary_false() {
JavaFile file = FileUtilities2.createFile("/does/not/exist.dart");
FileBasedSource source = new FileBasedSource(file);
expect(source, isNotNull);
expect(source.fullName, file.getAbsolutePath());
expect(source.isInSystemLibrary, isFalse);
}
void test_issue14500() {
// see https://code.google.com/p/dart/issues/detail?id=14500
FileBasedSource source = new FileBasedSource(
FileUtilities2.createFile("/some/packages/foo:bar.dart"));
expect(source, isNotNull);
expect(source.exists(), isFalse);
}
void test_resolveRelative_dart_fileName() {
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source =
new FileBasedSource(file, parseUriWithException("dart:test"));
expect(source, isNotNull);
Uri relative = source.resolveRelativeUri(parseUriWithException("lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "dart:test/lib.dart");
}
void test_resolveRelative_dart_filePath() {
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source =
new FileBasedSource(file, parseUriWithException("dart:test"));
expect(source, isNotNull);
Uri relative =
source.resolveRelativeUri(parseUriWithException("c/lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "dart:test/c/lib.dart");
}
void test_resolveRelative_dart_filePathWithParent() {
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source = new FileBasedSource(
file, parseUriWithException("dart:test/b/test.dart"));
expect(source, isNotNull);
Uri relative =
source.resolveRelativeUri(parseUriWithException("../c/lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "dart:test/c/lib.dart");
}
void test_resolveRelative_file_fileName() {
if (OSUtilities.isWindows()) {
// On Windows, the URI that is produced includes a drive letter,
// which I believe is not consistent across all machines that might run
// this test.
return;
}
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source = new FileBasedSource(file);
expect(source, isNotNull);
Uri relative = source.resolveRelativeUri(parseUriWithException("lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "file:///a/b/lib.dart");
}
void test_resolveRelative_file_filePath() {
if (OSUtilities.isWindows()) {
// On Windows, the URI that is produced includes a drive letter,
// which I believe is not consistent across all machines that might run
// this test.
return;
}
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source = new FileBasedSource(file);
expect(source, isNotNull);
Uri relative =
source.resolveRelativeUri(parseUriWithException("c/lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "file:///a/b/c/lib.dart");
}
void test_resolveRelative_file_filePathWithParent() {
if (OSUtilities.isWindows()) {
// On Windows, the URI that is produced includes a drive letter, which I
// believe is not consistent across all machines that might run this test.
return;
}
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source = new FileBasedSource(file);
expect(source, isNotNull);
Uri relative =
source.resolveRelativeUri(parseUriWithException("../c/lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "file:///a/c/lib.dart");
}
void test_resolveRelative_package_fileName() {
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source =
new FileBasedSource(file, parseUriWithException("package:b/test.dart"));
expect(source, isNotNull);
Uri relative = source.resolveRelativeUri(parseUriWithException("lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "package:b/lib.dart");
}
void test_resolveRelative_package_fileNameWithoutPackageName() {
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source =
new FileBasedSource(file, parseUriWithException("package:test.dart"));
expect(source, isNotNull);
Uri relative = source.resolveRelativeUri(parseUriWithException("lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "package:lib.dart");
}
void test_resolveRelative_package_filePath() {
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source =
new FileBasedSource(file, parseUriWithException("package:b/test.dart"));
expect(source, isNotNull);
Uri relative =
source.resolveRelativeUri(parseUriWithException("c/lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "package:b/c/lib.dart");
}
void test_resolveRelative_package_filePathWithParent() {
JavaFile file = FileUtilities2.createFile("/a/b/test.dart");
FileBasedSource source = new FileBasedSource(
file, parseUriWithException("package:a/b/test.dart"));
expect(source, isNotNull);
Uri relative =
source.resolveRelativeUri(parseUriWithException("../c/lib.dart"));
expect(relative, isNotNull);
expect(relative.toString(), "package:a/c/lib.dart");
}
void test_system() {
JavaFile file = FileUtilities2.createFile("/does/not/exist.dart");
FileBasedSource source =
new FileBasedSource(file, parseUriWithException("dart:core"));
expect(source, isNotNull);
expect(source.fullName, file.getAbsolutePath());
expect(source.isInSystemLibrary, isTrue);
}
}
@reflectiveTest
class FileUriResolverTest {
void test_creation() {
expect(new FileUriResolver(), isNotNull);
}
void test_resolve_file() {
UriResolver resolver = new FileUriResolver();
Source result = resolver
.resolveAbsolute(parseUriWithException("file:/does/not/exist.dart"));
expect(result, isNotNull);
expect(result.fullName,
FileUtilities2.createFile("/does/not/exist.dart").getAbsolutePath());
}
void test_resolve_nonFile() {
UriResolver resolver = new FileUriResolver();
Source result =
resolver.resolveAbsolute(parseUriWithException("dart:core"));
expect(result, isNull);
}
void test_restore() {
UriResolver resolver = new FileUriResolver();
Uri uri = parseUriWithException('file:///foo/bar.dart');
Source source = resolver.resolveAbsolute(uri);
expect(source, isNotNull);
expect(resolver.restoreAbsolute(source), uri);
expect(
resolver.restoreAbsolute(
new NonExistingSource(source.fullName, null, null)),
uri);
}
}
/**
* Instances of the class `MockDartSdk` implement a [DartSdk].
*/
class MockDartSdk implements DartSdk {
@override
AnalysisContext get context => null;
@override
List<SdkLibrary> get sdkLibraries => null;
@override
String get sdkVersion => null;
@override
List<String> get uris => null;
@override
Source fromFileUri(Uri uri) => null;
@override
SdkLibrary getSdkLibrary(String dartUri) => null;
@override
Source mapDartUri(String dartUri) => null;
}
@reflectiveTest
class SDKLibrariesReaderTest extends EngineTestCase {
void test_readFrom_dart2js() {
LibraryMap libraryMap = new SdkLibrariesReader(true).readFromFile(
FileUtilities2.createFile("/libs.dart"),
r'''
final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> {
'first' : const LibraryInfo(
'first/first.dart',
categories: 'Client',
documented: true,
platforms: VM_PLATFORM,
dart2jsPath: 'first/first_dart2js.dart'),
};''');
expect(libraryMap, isNotNull);
expect(libraryMap.size(), 1);
SdkLibrary first = libraryMap.getLibrary("dart:first");
expect(first, isNotNull);
expect(first.category, "Client");
expect(first.path, "first/first_dart2js.dart");
expect(first.shortName, "dart:first");
expect(first.isDart2JsLibrary, false);
expect(first.isDocumented, true);
expect(first.isImplementation, false);
expect(first.isVmLibrary, true);
}
void test_readFrom_empty() {
LibraryMap libraryMap = new SdkLibrariesReader(false)
.readFromFile(FileUtilities2.createFile("/libs.dart"), "");
expect(libraryMap, isNotNull);
expect(libraryMap.size(), 0);
}
void test_readFrom_normal() {
LibraryMap libraryMap = new SdkLibrariesReader(false).readFromFile(
FileUtilities2.createFile("/libs.dart"),
r'''
final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> {
'first' : const LibraryInfo(
'first/first.dart',
categories: 'Client',
documented: true,
platforms: VM_PLATFORM),
'second' : const LibraryInfo(
'second/second.dart',
categories: 'Server',
documented: false,
implementation: true,
platforms: 0),
};''');
expect(libraryMap, isNotNull);
expect(libraryMap.size(), 2);
SdkLibrary first = libraryMap.getLibrary("dart:first");
expect(first, isNotNull);
expect(first.category, "Client");
expect(first.path, "first/first.dart");
expect(first.shortName, "dart:first");
expect(first.isDart2JsLibrary, false);
expect(first.isDocumented, true);
expect(first.isImplementation, false);
expect(first.isVmLibrary, true);
SdkLibrary second = libraryMap.getLibrary("dart:second");
expect(second, isNotNull);
expect(second.category, "Server");
expect(second.path, "second/second.dart");
expect(second.shortName, "dart:second");
expect(second.isDart2JsLibrary, false);
expect(second.isDocumented, false);
expect(second.isImplementation, true);
expect(second.isVmLibrary, false);
}
}
@reflectiveTest
class UriKindTest {
void test_fromEncoding() {
expect(UriKind.fromEncoding(0x64), same(UriKind.DART_URI));
expect(UriKind.fromEncoding(0x66), same(UriKind.FILE_URI));
expect(UriKind.fromEncoding(0x70), same(UriKind.PACKAGE_URI));
expect(UriKind.fromEncoding(0x58), same(null));
}
void test_getEncoding() {
expect(UriKind.DART_URI.encoding, 0x64);
expect(UriKind.FILE_URI.encoding, 0x66);
expect(UriKind.PACKAGE_URI.encoding, 0x70);
}
}