example_code_parser_test.dart 1.63 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:typed_data';
6 7

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

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

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

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

21
    final String? codeSnippet3 = await getExampleCode('test_2_windows_breaks', bundle);
22
    expect(codeSnippet3, 'windows test 2 0\nwindows test 2 1');
23 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 45 46
  Future<ByteData> load(String key) async {
    return ByteData.sublistView(Uint8List(0));
  }
47 48

  @override
49
  Future<String> loadString(String key, { bool cache = true }) async {
50
    if (key == 'lib/gallery/example_code.dart')
51
      return testCodeFile;
52
    return '';
53 54 55
  }

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

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