web_expression_compiler_test.dart 2.97 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:dwds/dwds.dart';
8 9
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/compile.dart';
11
import 'package:flutter_tools/src/isolated/devfs_web.dart';
12
import 'package:test/fake.dart';
13 14 15 16

import '../../src/common.dart';

void main() {
17
  FileSystem fileSystem;
18 19

  setUp(() {
20
    fileSystem = MemoryFileSystem.test();
21 22
  });

23 24 25 26
  testWithoutContext('WebExpressionCompiler handles successful expression compilation', () async {
    fileSystem.file('compilerOutput').writeAsStringSync('a');
    final ResidentCompiler residentCompiler = FakeResidentCompiler(const CompilerOutput('compilerOutput', 0, <Uri>[]));
    final ExpressionCompiler expressionCompiler = WebExpressionCompiler(residentCompiler, fileSystem: fileSystem);
27 28 29 30 31 32

    final ExpressionCompilationResult result =
      await expressionCompiler.compileExpressionToJs(
        null, null, 1, 1, null, null, null, null);

    expectResult(result, false, 'a');
33
  });
34

35 36 37 38
  testWithoutContext('WebExpressionCompiler handles compilation error', () async {
    fileSystem.file('compilerOutput').writeAsStringSync('Error: a');
    final ResidentCompiler residentCompiler = FakeResidentCompiler(const CompilerOutput('compilerOutput', 1, <Uri>[]));
    final ExpressionCompiler expressionCompiler = WebExpressionCompiler(residentCompiler, fileSystem: fileSystem);
39 40 41 42 43 44

    final ExpressionCompilationResult result =
      await expressionCompiler.compileExpressionToJs(
        null, null, 1, 1, null, null, null, null);

    expectResult(result, true, 'Error: a');
45
  });
46

47 48 49
  testWithoutContext('WebExpressionCompiler handles internal error', () async {
    final ResidentCompiler residentCompiler = FakeResidentCompiler(null);
    final ExpressionCompiler expressionCompiler = WebExpressionCompiler(residentCompiler, fileSystem: fileSystem);
50 51 52 53 54

    final ExpressionCompilationResult result =
      await expressionCompiler.compileExpressionToJs(
        null, null, 1, 1, null, null, null, 'a');

55
    expectResult(result, true, "InternalError: frontend server failed to compile 'a'");
56
  });
57 58 59 60 61 62
}

void expectResult(ExpressionCompilationResult result, bool isError, String value) {
  expect(result,
    const TypeMatcher<ExpressionCompilationResult>()
      .having((ExpressionCompilationResult instance) => instance.isError, 'isError', isError)
63
      .having((ExpressionCompilationResult instance) => instance.result, 'result', value));
64 65
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
class FakeResidentCompiler extends Fake implements ResidentCompiler {
  FakeResidentCompiler(this.output);

  final CompilerOutput output;

  @override
  Future<CompilerOutput> compileExpressionToJs(
    String libraryUri,
    int line,
    int column,
    Map<String, String> jsModules,
    Map<String, String> jsFrameValues,
    String moduleName,
    String expression,
  ) async {
    return output;
  }
}