|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/src/error/codes.dart'; |
| 6 | +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 7 | + |
| 8 | +import '../dart/resolution/context_collection_resolution.dart'; |
| 9 | + |
| 10 | +main() { |
| 11 | + defineReflectiveSuite(() { |
| 12 | + defineReflectiveTests(ConstEvalPrimitiveEqualityTest); |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +@reflectiveTest |
| 17 | +class ConstEvalPrimitiveEqualityTest extends PubPackageResolutionTest { |
| 18 | + test_equal_double_object() async { |
| 19 | + await assertNoErrorsInCode(r''' |
| 20 | +const a = 0.1; |
| 21 | +const b = a == Object(); |
| 22 | +'''); |
| 23 | + } |
| 24 | + |
| 25 | + test_equal_int_object() async { |
| 26 | + await assertNoErrorsInCode(r''' |
| 27 | +const a = 0; |
| 28 | +const b = a == Object(); |
| 29 | +'''); |
| 30 | + } |
| 31 | + |
| 32 | + test_equal_int_userClass() async { |
| 33 | + await assertNoErrorsInCode(r''' |
| 34 | +class A { |
| 35 | + const A(); |
| 36 | +} |
| 37 | +
|
| 38 | +const a = 0; |
| 39 | +const b = a == A(); |
| 40 | +'''); |
| 41 | + } |
| 42 | + |
| 43 | + test_equal_null_object() async { |
| 44 | + await assertNoErrorsInCode(r''' |
| 45 | +const a = null; |
| 46 | +const b = a == Object(); |
| 47 | +'''); |
| 48 | + } |
| 49 | + |
| 50 | + test_equal_string_object() async { |
| 51 | + await assertNoErrorsInCode(r''' |
| 52 | +const a = 'foo'; |
| 53 | +const b = a == Object(); |
| 54 | +'''); |
| 55 | + } |
| 56 | + |
| 57 | + test_equal_userClass_int_hasEqEq() async { |
| 58 | + await assertErrorsInCode(r''' |
| 59 | +class A { |
| 60 | + const A(); |
| 61 | + bool operator ==(other) => false; |
| 62 | +} |
| 63 | +
|
| 64 | +const a = A(); |
| 65 | +const b = a == 0; |
| 66 | +''', [ |
| 67 | + error(CompileTimeErrorCode.CONST_EVAL_PRIMITIVE_EQUALITY, 87, 6), |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + test_equal_userClass_int_hasHashCode() async { |
| 72 | + await assertErrorsInCode(r''' |
| 73 | +class A { |
| 74 | + const A(); |
| 75 | + int get hashCode => 0; |
| 76 | +} |
| 77 | +
|
| 78 | +const a = A(); |
| 79 | +const b = a == 0; |
| 80 | +''', [ |
| 81 | + error(CompileTimeErrorCode.CONST_EVAL_PRIMITIVE_EQUALITY, 76, 6), |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + test_equal_userClass_int_hasPrimitiveEquality() async { |
| 86 | + await assertNoErrorsInCode(r''' |
| 87 | +class A { |
| 88 | + const A(); |
| 89 | +} |
| 90 | +
|
| 91 | +const a = A(); |
| 92 | +const b = a == 0; |
| 93 | +'''); |
| 94 | + } |
| 95 | + |
| 96 | + test_notEqual_double_object() async { |
| 97 | + await assertNoErrorsInCode(r''' |
| 98 | +const a = 0.1; |
| 99 | +const b = a != Object(); |
| 100 | +'''); |
| 101 | + } |
| 102 | + |
| 103 | + test_notEqual_int_object() async { |
| 104 | + await assertNoErrorsInCode(r''' |
| 105 | +const a = 0; |
| 106 | +const b = a != Object(); |
| 107 | +'''); |
| 108 | + } |
| 109 | + |
| 110 | + test_notEqual_int_userClass() async { |
| 111 | + await assertNoErrorsInCode(r''' |
| 112 | +class A { |
| 113 | + const A(); |
| 114 | +} |
| 115 | +
|
| 116 | +const a = 0; |
| 117 | +const b = a != A(); |
| 118 | +'''); |
| 119 | + } |
| 120 | + |
| 121 | + test_notEqual_null_object() async { |
| 122 | + await assertNoErrorsInCode(r''' |
| 123 | +const a = null; |
| 124 | +const b = a != Object(); |
| 125 | +'''); |
| 126 | + } |
| 127 | + |
| 128 | + test_notEqual_string_object() async { |
| 129 | + await assertNoErrorsInCode(r''' |
| 130 | +const a = 'foo'; |
| 131 | +const b = a != Object(); |
| 132 | +'''); |
| 133 | + } |
| 134 | + |
| 135 | + test_notEqual_userClass_int_hasEqEq() async { |
| 136 | + await assertErrorsInCode(r''' |
| 137 | +class A { |
| 138 | + const A(); |
| 139 | + bool operator ==(other) => false; |
| 140 | +} |
| 141 | +
|
| 142 | +const a = A(); |
| 143 | +const b = a != 0; |
| 144 | +''', [ |
| 145 | + error(CompileTimeErrorCode.CONST_EVAL_PRIMITIVE_EQUALITY, 87, 6), |
| 146 | + ]); |
| 147 | + } |
| 148 | + |
| 149 | + test_notEqual_userClass_int_hasHashCode() async { |
| 150 | + await assertErrorsInCode(r''' |
| 151 | +class A { |
| 152 | + const A(); |
| 153 | + int get hashCode => 0; |
| 154 | +} |
| 155 | +
|
| 156 | +const a = A(); |
| 157 | +const b = a != 0; |
| 158 | +''', [ |
| 159 | + error(CompileTimeErrorCode.CONST_EVAL_PRIMITIVE_EQUALITY, 76, 6), |
| 160 | + ]); |
| 161 | + } |
| 162 | + |
| 163 | + test_notEqual_userClass_int_hasPrimitiveEquality() async { |
| 164 | + await assertNoErrorsInCode(r''' |
| 165 | +class A { |
| 166 | + const A(); |
| 167 | +} |
| 168 | +
|
| 169 | +const a = A(); |
| 170 | +const b = a != 0; |
| 171 | +'''); |
| 172 | + } |
| 173 | +} |
0 commit comments