example_code_parser_test.dart 1.6 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. 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';
6
import 'dart:typed_data';
7 8

import 'package:flutter/services.dart';
9
import 'package:flutter_gallery/gallery/example_code_parser.dart';
10 11 12
import 'package:test/test.dart';

void main() {
13
  test('Flutter gallery example code parser test', () async {
14
    final TestAssetBundle bundle = new TestAssetBundle();
15

16
    final String codeSnippet0 = await getExampleCode('test_0', bundle);
17 18
    expect(codeSnippet0, 'test 0 0\ntest 0 1');

19
    final String codeSnippet1 = await getExampleCode('test_1', bundle);
20
    expect(codeSnippet1, 'test 1 0\ntest 1 1');
21 22 23

    final String codeSnippet3 = await getExampleCode('test_2_windows_breaks', bundle);
    expect(codeSnippet3, 'windows test 2 0\nwindows test 2 1');
24 25 26
  });
}

27
const String testCodeFile = '''// A fake test file
28 29 30 31 32 33 34 35 36 37
// START test_0
test 0 0
test 0 1
// END

// Some comments
// START test_1
test 1 0
test 1 1
// END
38 39

// START test_2_windows_breaks\r\nwindows test 2 0\r\nwindows test 2 1\r\n// END
40
''';
41 42 43

class TestAssetBundle extends AssetBundle {
  @override
44
  Future<ByteData> load(String key) => null;
45 46

  @override
47
  Future<String> loadString(String key, { bool cache: true }) {
48 49
    if (key == 'lib/gallery/example_code.dart')
      return new Future<String>.value(testCodeFile);
50 51 52 53
    return null;
  }

  @override
54
  Future<T> loadStructuredData<T>(String key, Future<T> parser(String value)) async {
55 56
    return parser(await loadString(key));
  }
57 58 59 60

  @override
  String toString() => '$runtimeType@$hashCode()';
}