| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library descriptor_test; | 5 library descriptor_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:pathos/path.dart' as path; | 10 import 'package:pathos/path.dart' as path; |
| 11 import 'package:scheduled_test/descriptor.dart' as d; | 11 import 'package:scheduled_test/descriptor.dart' as d; |
| 12 import 'package:scheduled_test/scheduled_test.dart'; | 12 import 'package:scheduled_test/scheduled_test.dart'; |
| 13 | 13 |
| 14 import 'metatest.dart'; | 14 import 'metatest.dart'; |
| 15 import 'utils.dart'; |
| 15 | 16 |
| 16 String sandbox; | 17 String sandbox; |
| 17 | 18 |
| 18 void main() { | 19 void main() { |
| 19 expectTestsPass('file().create() creates a file', () { | 20 expectTestsPass('file().create() creates a file', () { |
| 20 test('test', () { | 21 test('test', () { |
| 21 scheduleSandbox(); | 22 scheduleSandbox(); |
| 22 | 23 |
| 23 d.file('name.txt', 'contents').create(); | 24 d.file('name.txt', 'contents').create(); |
| 24 | 25 |
| (...skipping 829 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 "'-- file2.txt")); | 855 "'-- file2.txt")); |
| 855 }); | 856 }); |
| 856 }); | 857 }); |
| 857 | 858 |
| 858 expectTestsPass("directory().describe() with no contents returns the " | 859 expectTestsPass("directory().describe() with no contents returns the " |
| 859 "directory name", () { | 860 "directory name", () { |
| 860 test('test', () { | 861 test('test', () { |
| 861 expect(d.dir('dir').describe(), equals('dir')); | 862 expect(d.dir('dir').describe(), equals('dir')); |
| 862 }); | 863 }); |
| 863 }); | 864 }); |
| 865 |
| 866 expectTestsPass("async().create() forwards to file().create", () { |
| 867 test('test', () { |
| 868 scheduleSandbox(); |
| 869 |
| 870 d.async(pumpEventQueue().then((_) { |
| 871 return d.file('name.txt', 'contents'); |
| 872 })).create(); |
| 873 |
| 874 d.file('name.txt', 'contents').validate(); |
| 875 }); |
| 876 }); |
| 877 |
| 878 expectTestsPass("async().create() forwards to directory().create", () { |
| 879 test('test', () { |
| 880 scheduleSandbox(); |
| 881 |
| 882 d.async(pumpEventQueue().then((_) { |
| 883 return d.dir('dir', [ |
| 884 d.file('file1.txt', 'contents1'), |
| 885 d.file('file2.txt', 'contents2') |
| 886 ]); |
| 887 })).create(); |
| 888 |
| 889 d.dir('dir', [ |
| 890 d.file('file1.txt', 'contents1'), |
| 891 d.file('file2.txt', 'contents2') |
| 892 ]).validate(); |
| 893 }); |
| 894 }); |
| 895 |
| 896 expectTestsPass("async().validate() forwards to file().validate", () { |
| 897 test('test', () { |
| 898 scheduleSandbox(); |
| 899 |
| 900 d.file('name.txt', 'contents').create(); |
| 901 |
| 902 d.async(pumpEventQueue().then((_) { |
| 903 return d.file('name.txt', 'contents'); |
| 904 })).validate(); |
| 905 }); |
| 906 }); |
| 907 |
| 908 expectTestsPass("async().validate() fails if file().validate fails", () { |
| 909 var errors; |
| 910 test('test 1', () { |
| 911 scheduleSandbox(); |
| 912 |
| 913 currentSchedule.onException.schedule(() { |
| 914 errors = currentSchedule.errors; |
| 915 }); |
| 916 |
| 917 d.async(pumpEventQueue().then((_) { |
| 918 return d.file('name.txt', 'contents'); |
| 919 })).validate(); |
| 920 }); |
| 921 |
| 922 test('test 2', () { |
| 923 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 924 expect(errors.length, equals(1)); |
| 925 expect(errors.first.error, |
| 926 matches(r"^File not found: '[^']+[\\/]name\.txt'\.$")); |
| 927 }); |
| 928 }, passing: ['test 2']); |
| 929 |
| 930 expectTestsPass("async().validate() forwards to directory().validate", () { |
| 931 test('test', () { |
| 932 scheduleSandbox(); |
| 933 |
| 934 d.dir('dir', [ |
| 935 d.file('file1.txt', 'contents1'), |
| 936 d.file('file2.txt', 'contents2') |
| 937 ]).create(); |
| 938 |
| 939 d.async(pumpEventQueue().then((_) { |
| 940 return d.dir('dir', [ |
| 941 d.file('file1.txt', 'contents1'), |
| 942 d.file('file2.txt', 'contents2') |
| 943 ]); |
| 944 })).validate(); |
| 945 }); |
| 946 }); |
| 947 |
| 948 expectTestsPass("async().create() fails if directory().create fails", () { |
| 949 var errors; |
| 950 test('test 1', () { |
| 951 scheduleSandbox(); |
| 952 |
| 953 currentSchedule.onException.schedule(() { |
| 954 errors = currentSchedule.errors; |
| 955 }); |
| 956 |
| 957 d.async(pumpEventQueue().then((_) { |
| 958 return d.dir('dir', [ |
| 959 d.file('file1.txt', 'contents1'), |
| 960 d.file('file2.txt', 'contents2') |
| 961 ]); |
| 962 })).validate(); |
| 963 }); |
| 964 |
| 965 test('test 2', () { |
| 966 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 967 expect(errors.length, equals(1)); |
| 968 expect(errors.first.error, |
| 969 matches(r"^Directory not found: '[^']+[\\/]dir'\.$")); |
| 970 }); |
| 971 }, passing: ['test 2']); |
| 972 |
| 973 expectTestsPass("async().load() fails", () { |
| 974 test('test', () { |
| 975 scheduleSandbox(); |
| 976 |
| 977 expect(d.async(new Future.immediate(d.file('name.txt'))) |
| 978 .load('path').toList(), |
| 979 throwsA(equals("Async descriptors don't support load()."))); |
| 980 }); |
| 981 }); |
| 982 |
| 983 expectTestsPass("async().read() fails", () { |
| 984 test('test', () { |
| 985 scheduleSandbox(); |
| 986 |
| 987 expect(d.async(new Future.immediate(d.file('name.txt'))).read().toList(), |
| 988 throwsA(equals("Async descriptors don't support read()."))); |
| 989 }); |
| 990 }); |
| 864 } | 991 } |
| 865 | 992 |
| 866 void scheduleSandbox() { | 993 void scheduleSandbox() { |
| 867 schedule(() { | 994 schedule(() { |
| 868 return new Directory('').createTemp().then((dir) { | 995 return new Directory('').createTemp().then((dir) { |
| 869 sandbox = dir.path; | 996 sandbox = dir.path; |
| 870 d.defaultRoot = sandbox; | 997 d.defaultRoot = sandbox; |
| 871 }); | 998 }); |
| 872 }); | 999 }); |
| 873 | 1000 |
| 874 currentSchedule.onComplete.schedule(() { | 1001 currentSchedule.onComplete.schedule(() { |
| 875 d.defaultRoot = null; | 1002 d.defaultRoot = null; |
| 876 if (sandbox == null) return; | 1003 if (sandbox == null) return; |
| 877 var oldSandbox = sandbox; | 1004 var oldSandbox = sandbox; |
| 878 sandbox = null; | 1005 sandbox = null; |
| 879 return new Directory(oldSandbox).delete(recursive: true); | 1006 return new Directory(oldSandbox).delete(recursive: true); |
| 880 }); | 1007 }); |
| 881 } | 1008 } |
| 882 | 1009 |
| 883 Future<List<int>> byteStreamToList(Stream<List<int>> stream) { | 1010 Future<List<int>> byteStreamToList(Stream<List<int>> stream) { |
| 884 return stream.reduce(<int>[], (buffer, chunk) { | 1011 return stream.reduce(<int>[], (buffer, chunk) { |
| 885 buffer.addAll(chunk); | 1012 buffer.addAll(chunk); |
| 886 return buffer; | 1013 return buffer; |
| 887 }); | 1014 }); |
| 888 } | 1015 } |
| 889 | 1016 |
| 890 Future<String> byteStreamToString(Stream<List<int>> stream) => | 1017 Future<String> byteStreamToString(Stream<List<int>> stream) => |
| 891 byteStreamToList(stream).then((bytes) => new String.fromCharCodes(bytes)); | 1018 byteStreamToList(stream).then((bytes) => new String.fromCharCodes(bytes)); |
| OLD | NEW |