Skip to content

Copy over tests and add .travis.yml #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: dart
sudo: false
dart:
- dev
- stable
- 1.22.1
dart_task:
- test
- dartfmt
- dartanalyzer
matrix:
exclude:
- dart: 1.22.1
dart_task: dartfmt
- dart: 1.22.1
dart_task: dartanalyzer

# Only building master means that we don't run two builds for each pull request.
branches:
only: [master]

cache:
directories:
- $HOME/.pub-cache
114 changes: 114 additions & 0 deletions test/analysis_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2017, 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.

import 'dart:async';
import 'dart:io';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart' hide File;
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/source/pub_package_map_provider.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk;
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:cli_util/cli_util.dart' as cli;
import 'package:path/path.dart' as p;

/// [foundFiles] is the list of files to consider for the context.
Future<AnalysisContext> getAnalysisContextForProjectPath(
String projectPath, List<String> foundFiles) async {
// TODO: fail more clearly if this...fails
var sdkPath = cli.getSdkDir().path;

var resourceProvider = PhysicalResourceProvider.INSTANCE;
DartSdk sdk = new FolderBasedDartSdk(
resourceProvider, resourceProvider.getFolder(sdkPath));

var packageResolver = _getPackageResolver(projectPath, sdk);

var resolvers = [
new DartUriResolver(sdk),
new ResourceUriResolver(PhysicalResourceProvider.INSTANCE),
packageResolver
];

AnalysisEngine.instance.processRequiredPlugins();

var options = new AnalysisOptionsImpl()..analyzeFunctionBodies = false;

var context = AnalysisEngine.instance.createAnalysisContext()
..analysisOptions = options
..sourceFactory = new SourceFactory(resolvers);

// ensures all libraries defined by the set of files are resolved
_getLibraryElements(foundFiles, context).toList();

return context;
}

UriResolver _getPackageResolver(String projectPath, DartSdk sdk) {
var dotPackagesPath = p.join(projectPath, '.packages');

if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
throw new StateError('A package configuration file was not found at the '
'expectetd location. $dotPackagesPath');
}

var pubPackageMapProvider =
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
var packageMapInfo = pubPackageMapProvider.computePackageMap(
PhysicalResourceProvider.INSTANCE.getResource(projectPath));
var packageMap = packageMapInfo.packageMap;
if (packageMap == null) {
throw new StateError('An error occurred getting the package map.');
}

return new PackageMapUriResolver(
PhysicalResourceProvider.INSTANCE, packageMap);
}

LibraryElement getLibraryElementForSourceFile(
AnalysisContext context, String sourcePath) {
Source source = new FileBasedSource(new JavaFile(sourcePath));

var libs = context.getLibrariesContaining(source);

if (libs.length > 1) {
throw new Exception("We don't support multiple libraries for a source.");
}

if (libs.isEmpty) {
return null;
}

var libSource = libs.single;

// using `getLibraryElement` because the library should already be computed
// If not, it's a bug in usage
return context.getLibraryElement(libSource);
}

// may return `null` if [path] doesn't refer to a library.
/// [dartFiles] is a [Stream] of paths to [.dart] files.
Iterable<LibraryElement> _getLibraryElements(
List<String> dartFiles, AnalysisContext context) =>
dartFiles
.map((path) => _getLibraryElement(path, context))
.where((lib) => lib != null);

LibraryElement _getLibraryElement(String path, AnalysisContext context) {
Source source = new FileBasedSource(new JavaFile(path));
if (context.computeKindOf(source) == SourceKind.LIBRARY) {
return context.computeLibraryElement(source);
}
return null;
}

String getFileBasedSourcePath(FileBasedSource source) {
return p.fromUri(source.uri);
}
108 changes: 108 additions & 0 deletions test/json_serializable_integration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2015, 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.

import 'dart:convert';

import 'package:test/test.dart';

import 'test_files/json_test_example.dart';

void main() {
group('Person', () {
roundTripPerson(Person p) {
_roundTripObject(p, (json) => new Person.fromJson(json));
}

test("null", () {
roundTripPerson(new Person(null, null));
});

test("empty", () {
roundTripPerson(new Person('', '',
middleName: '',
dateOfBirth: new DateTime.fromMillisecondsSinceEpoch(0)));
});

test("now", () {
roundTripPerson(new Person('a', 'b',
middleName: 'c', dateOfBirth: new DateTime.now()));
});

test("now toUtc", () {
roundTripPerson(new Person('a', 'b',
middleName: 'c', dateOfBirth: new DateTime.now().toUtc()));
});

test('empty json', () {
var person = new Person.fromJson({});
expect(person.dateOfBirth, isNull);
roundTripPerson(person);
});
});

group('Order', () {
roundTripOrder(Order p) {
_roundTripObject(p, (json) => new Order.fromJson(json));
}

test("null", () {
roundTripOrder(new Order());
});

test("empty", () {
roundTripOrder(new Order(const [])
..count = 0
..isRushed = false);
});

test("simple", () {
roundTripOrder(new Order(<Item>[
new Item(24)
..itemNumber = 42
..saleDates = [new DateTime.now()]
])
..count = 42
..isRushed = true);
});

test('empty json', () {
var order = new Order.fromJson({});
expect(order.items, isEmpty);
roundTripOrder(order);
});
});

group('Item', () {
roundTripItem(Item p) {
_roundTripObject(p, (json) => new Item.fromJson(json));
}

test('empty json', () {
var item = new Item.fromJson({});
expect(item.saleDates, isNull);
roundTripItem(item);
});
});
}

void _roundTripObject(object, factory(Map<String, dynamic> json)) {
var json = _loudEncode(object);

var person2 = factory(JSON.decode(json) as Map<String, dynamic>);

expect(person2, equals(object));

var json2 = _loudEncode(person2);

expect(json2, equals(json));
}

_loudEncode(object) {
try {
return JSON.encode(object.toJson());
} on JsonUnsupportedObjectError catch (e) {
print(e.cause);
rethrow;
}
}
Loading