web_expression_compiler_test.dart 3.02 KB
Newer Older
1 2 3 4 5
// 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.

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

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

void main() {
15
  late FileSystem fileSystem;
16 17

  setUp(() {
18
    fileSystem = MemoryFileSystem.test();
19 20
  });

21 22 23 24
  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);
25 26 27

    final ExpressionCompilationResult result =
      await expressionCompiler.compileExpressionToJs(
28
        '', '', 1, 1, <String, String>{}, <String, String>{}, '', '');
29 30

    expectResult(result, false, 'a');
31
  });
32

33 34 35 36
  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);
37 38 39

    final ExpressionCompilationResult result =
      await expressionCompiler.compileExpressionToJs(
40
        '', '', 1, 1, <String, String>{}, <String, String>{}, '', '');
41 42

    expectResult(result, true, 'Error: a');
43
  });
44

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

    final ExpressionCompilationResult result =
      await expressionCompiler.compileExpressionToJs(
51
        '', '', 1, 1, <String, String>{}, <String, String>{}, '', 'a');
52

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

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

64 65 66
class FakeResidentCompiler extends Fake implements ResidentCompiler {
  FakeResidentCompiler(this.output);

67
  final CompilerOutput? output;
68 69

  @override
70
  Future<CompilerOutput?> compileExpressionToJs(
71 72 73 74 75 76 77 78 79 80 81
    String libraryUri,
    int line,
    int column,
    Map<String, String> jsModules,
    Map<String, String> jsFrameValues,
    String moduleName,
    String expression,
  ) async {
    return output;
  }
}