dart-sdk/tests/language/const_functions/const_functions_simple_invocations_test.dart
Kallen Tu c906133967 [cfe] Multi-call const function tests to existing behavior.
Change-Id: Id03250e5eeb38d9c69b629c239e573a1d838820f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/190980
Reviewed-by: Jake Macdonald <jakemac@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2021-03-15 18:09:13 +00:00

85 lines
2.8 KiB
Dart

// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Tests function invocations that immediately return simple expressions.
// SharedOptions=--enable-experiment=const-functions
import "package:expect/expect.dart";
const binary = binaryFn(2, 1);
// ^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int binaryFn(int a, int b) => a - b;
const optional = optionalFn(2);
// ^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
const optional1 = optionalFn(2, 1);
// ^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int optionalFn(int c, [int d = 0]) => c + d;
const named = namedFn(2, f: 2);
// ^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
const named1 = namedFn(2);
// ^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int namedFn(int e, {int f = 3}) => e + f;
const type = typeFn(6);
// ^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
T typeFn<T>(T x) => x;
const str = stringFn("str");
// ^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
String stringFn(String s) => s + "ing";
const eq = equalFn(2, 2);
// ^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
bool equalFn(int a, int b) => a == b;
const neg = unary(2);
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int unary(int a) => -a;
const boolean = boolFn(true, false);
// ^^^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
bool boolFn(bool a, bool b) => a || b;
const doub = doubleFn(2.2, 2);
// ^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
double doubleFn(double a, double b) => a * b;
const multi = multiFn(1);
// ^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
const multi2 = multiFn(2);
// ^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
int multiFn(int a) => a + 1;
void main() {
Expect.equals(binary, 1);
Expect.equals(optional, 2);
Expect.equals(optional1, 3);
Expect.equals(named, 4);
Expect.equals(named1, 5);
Expect.equals(type, 6);
Expect.equals(str, "string");
Expect.equals(eq, true);
Expect.equals(neg, -2);
Expect.equals(boolean, true);
Expect.equals(doub, 4.4);
Expect.equals(multi, 2);
Expect.equals(multi2, 3);
}