example_code_parser_test.dart 1.38 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 21 22 23
    expect(codeSnippet1, 'test 1 0\ntest 1 1');
  });
}

24
const String testCodeFile = '''// A fake test file
25 26 27 28 29 30 31 32 33 34
// START test_0
test 0 0
test 0 1
// END

// Some comments
// START test_1
test 1 0
test 1 1
// END
35
''';
36 37 38

class TestAssetBundle extends AssetBundle {
  @override
39
  Future<ByteData> load(String key) => null;
40 41

  @override
42
  Future<String> loadString(String key, { bool cache: true }) {
43 44
    if (key == 'lib/gallery/example_code.dart')
      return new Future<String>.value(testCodeFile);
45 46 47 48
    return null;
  }

  @override
49
  Future<T> loadStructuredData<T>(String key, Future<T> parser(String value)) async {
50 51
    return parser(await loadString(key));
  }
52 53 54 55

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