|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.test.generator; |
| 42 | + |
| 43 | +import org.junit.Ignore; |
| 44 | +import org.junit.Test; |
| 45 | + |
| 46 | +import com.oracle.graal.python.test.PythonTests; |
| 47 | + |
| 48 | +public class YieldExpressionTests { |
| 49 | + @Test |
| 50 | + public void testYieldExprAnd1() { |
| 51 | + String source = "list((lambda: str(print('x')) and str((yield)) and str(print('y')))())"; |
| 52 | + PythonTests.assertPrints("x\ny\n", source); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testYieldExprAnd2() { |
| 57 | + String source = "list((lambda: str(print('x')) and (yield) and str(print('y')))())"; |
| 58 | + PythonTests.assertPrints("x\n", source); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testYieldExprOr1() { |
| 63 | + String source = "list((lambda: print('x') or (yield) or print('y'))())"; |
| 64 | + PythonTests.assertPrints("x\ny\n", source); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testYieldExprOr2() { |
| 69 | + String source = "list((lambda: print('x') or str((yield)) or print('y'))())"; |
| 70 | + PythonTests.assertPrints("x\n", source); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testYieldExprAdd() { |
| 75 | + String source = "list((lambda: str(print('x')) + str((yield)) + str(print('y')))())"; |
| 76 | + PythonTests.assertPrints("x\ny\n", source); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testYieldExprSub() { |
| 81 | + String source = "list((lambda: int(print('x') or 5) - int((yield) or 5) - int(print('y') or 5))())"; |
| 82 | + PythonTests.assertPrints("x\ny\n", source); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testYieldExprMul() { |
| 87 | + String source = "list((lambda: int(print('x') or 5) * int((yield) or 5) * int(print('y') or 5))())"; |
| 88 | + PythonTests.assertPrints("x\ny\n", source); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testYieldExprFloordiv() { |
| 93 | + String source = "list((lambda: int(print('x') or 5) // int((yield) or 5) // int(print('y') or 5))())"; |
| 94 | + PythonTests.assertPrints("x\ny\n", source); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testYieldExprTruediv() { |
| 99 | + String source = "list((lambda: int(print('x') or 5) / int((yield) or 5) / int(print('y') or 5))())"; |
| 100 | + PythonTests.assertPrints("x\ny\n", source); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testYieldExprMod() { |
| 105 | + String source = "list((lambda: int(print('x') or 5) % int((yield) or 5) % int(print('y') or 5))())"; |
| 106 | + PythonTests.assertPrints("x\ny\n", source); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testYieldExprPow() { |
| 111 | + String source = "list((lambda: int(print('x') or 5) ** int((yield) or 5) ** int(print('y') or 5))())"; |
| 112 | + PythonTests.assertPrints("x\ny\n", source); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testYieldExprBitAnd() { |
| 117 | + String source = "list((lambda: int(print('x') or 5) & int((yield) or 5) & int(print('y') or 5))())"; |
| 118 | + PythonTests.assertPrints("x\ny\n", source); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testYieldExprBitOr() { |
| 123 | + String source = "list((lambda: int(print('x') or 5) | int((yield) or 5) | int(print('y') or 5))())"; |
| 124 | + PythonTests.assertPrints("x\ny\n", source); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testYieldExprBitXor() { |
| 129 | + String source = "list((lambda: int(print('x') or 5) ^ int((yield) or 5) ^ int(print('y') or 5))())"; |
| 130 | + PythonTests.assertPrints("x\ny\n", source); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testYieldExprBitLShift() { |
| 135 | + String source = "list((lambda: int(print('x') or 5) << int((yield) or 5) << int(print('y') or 5))())"; |
| 136 | + PythonTests.assertPrints("x\ny\n", source); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testYieldExprBitRShift() { |
| 141 | + String source = "list((lambda: int(print('x') or 5) >> int((yield) or 5) >> int(print('y') or 5))())"; |
| 142 | + PythonTests.assertPrints("x\ny\n", source); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testYieldExprEq() { |
| 147 | + String source = "list((lambda: int(print('x') or 5) == int((yield) or 5))())"; |
| 148 | + PythonTests.assertPrints("x\n", source); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testYieldExprNe() { |
| 153 | + String source = "list((lambda: int(print('x') or 5) != int((yield) or 5))())"; |
| 154 | + PythonTests.assertPrints("x\n", source); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testYieldExprGt() { |
| 159 | + String source = "list((lambda: int(print('x') or 5) > int((yield) or 5))())"; |
| 160 | + PythonTests.assertPrints("x\n", source); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testYieldExprGe() { |
| 165 | + String source = "list((lambda: int(print('x') or 5) >= int((yield) or 5))())"; |
| 166 | + PythonTests.assertPrints("x\n", source); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testYieldExprLt() { |
| 171 | + String source = "list((lambda: int(print('x') or 5) < int((yield) or 5))())"; |
| 172 | + PythonTests.assertPrints("x\n", source); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void testYieldExprLe() { |
| 177 | + String source = "list((lambda: int(print('x') or 5) <= int((yield) or 5))())"; |
| 178 | + PythonTests.assertPrints("x\n", source); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testYieldExprIs() { |
| 183 | + String source = "list((lambda: int(print('x') or 5) is int((yield) or 5))())"; |
| 184 | + PythonTests.assertPrints("x\n", source); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testYieldExprIn() { |
| 189 | + String source = "list((lambda: int(print('x') or 5) in [(yield) or 5])())"; |
| 190 | + PythonTests.assertPrints("x\n", source); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testYieldExprTuple() { |
| 195 | + String source = "list((lambda: (print('x'), (yield), print('y')))())"; |
| 196 | + PythonTests.assertPrints("x\ny\n", source); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void testYieldExprList() { |
| 201 | + String source = "list((lambda: [print('x'), (yield), print('y')])())"; |
| 202 | + PythonTests.assertPrints("x\ny\n", source); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testYieldExprSet() { |
| 207 | + String source = "list((lambda: {print('x'), (yield), print('y')})())"; |
| 208 | + PythonTests.assertPrints("x\ny\n", source); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void testYieldExprDict() { |
| 213 | + String source = "list((lambda: {'a': print('x'), 'b': (yield), 'c': print('y')})())"; |
| 214 | + PythonTests.assertPrints("x\ny\n", source); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + @Ignore // TODO |
| 219 | + public void testYieldExprCall() { |
| 220 | + String source = "list((lambda: dict({'a': print('x')}, b=(yield), c=print('y'), d=(yield)))())"; |
| 221 | + PythonTests.assertPrints("x\ny\n", source); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + @Ignore // TODO |
| 226 | + public void testYieldExprDef() { |
| 227 | + String source = "def gen():\n" + |
| 228 | + " def inner(a=print('x'), b=(yield), c=print('y')):\n" + |
| 229 | + " pass\n" + |
| 230 | + "list(gen())"; |
| 231 | + PythonTests.assertPrints("x\ny\n", source); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + @Ignore // TODO |
| 236 | + public void testYieldExprSlice() { |
| 237 | + String source = "list((lambda: [][print('x') or 1:(yield) or 1:print('y') or 1])())"; |
| 238 | + PythonTests.assertPrints("x\ny\n", source); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + @Ignore // TODO |
| 243 | + public void testYieldExprTernaryIf1() { |
| 244 | + String source = "list((lambda: (yield) if print('x') else print('y'))())"; |
| 245 | + PythonTests.assertPrints("x\ny\n", source); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + @Ignore // TODO |
| 250 | + public void testYieldExprTernaryIf2() { |
| 251 | + String source = "list((lambda: print('y') if print('x') else (yield))())"; |
| 252 | + PythonTests.assertPrints("x\n", source); |
| 253 | + } |
| 254 | + |
| 255 | + @Test |
| 256 | + public void testYieldExprWith() { |
| 257 | + String source = "class cm:\n" + |
| 258 | + " def __init__(self):\n" + |
| 259 | + " print('init')\n" + |
| 260 | + " def __enter__(self, *args):\n" + |
| 261 | + " print('enter')\n" + |
| 262 | + " def __exit__(self, *args):\n" + |
| 263 | + " print('exit')\n" + |
| 264 | + "def gen():\n" + |
| 265 | + " with cm() as x, ((yield) or cm()) as y:\n" + |
| 266 | + " pass\n" + |
| 267 | + "list(gen())"; |
| 268 | + PythonTests.assertPrints("init\nenter\ninit\nenter\nexit\nexit\n", source); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + @Ignore // TODO |
| 273 | + public void testYieldExprFString() { |
| 274 | + String source = "list((lambda: f\"{print('x')}{(yield)}{print('y')}\")())"; |
| 275 | + PythonTests.assertPrints("x\ny\n", source); |
| 276 | + } |
| 277 | +} |
0 commit comments