# Python code coverage for Lib/test/test_augassign.py

# | count | content |
---|---|---|

1 | n/a | # Augmented assignment test. |

2 | n/a | |

3 | n/a | import unittest |

4 | n/a | |

5 | n/a | |

6 | n/a | class AugAssignTest(unittest.TestCase): |

7 | n/a | def testBasic(self): |

8 | n/a | x = 2 |

9 | n/a | x += 1 |

10 | n/a | x *= 2 |

11 | n/a | x **= 2 |

12 | n/a | x -= 8 |

13 | n/a | x //= 5 |

14 | n/a | x %= 3 |

15 | n/a | x &= 2 |

16 | n/a | x |= 5 |

17 | n/a | x ^= 1 |

18 | n/a | x /= 2 |

19 | n/a | self.assertEqual(x, 3.0) |

20 | n/a | |

21 | n/a | def test_with_unpacking(self): |

22 | n/a | self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec") |

23 | n/a | |

24 | n/a | def testInList(self): |

25 | n/a | x = [2] |

26 | n/a | x[0] += 1 |

27 | n/a | x[0] *= 2 |

28 | n/a | x[0] **= 2 |

29 | n/a | x[0] -= 8 |

30 | n/a | x[0] //= 5 |

31 | n/a | x[0] %= 3 |

32 | n/a | x[0] &= 2 |

33 | n/a | x[0] |= 5 |

34 | n/a | x[0] ^= 1 |

35 | n/a | x[0] /= 2 |

36 | n/a | self.assertEqual(x[0], 3.0) |

37 | n/a | |

38 | n/a | def testInDict(self): |

39 | n/a | x = {0: 2} |

40 | n/a | x[0] += 1 |

41 | n/a | x[0] *= 2 |

42 | n/a | x[0] **= 2 |

43 | n/a | x[0] -= 8 |

44 | n/a | x[0] //= 5 |

45 | n/a | x[0] %= 3 |

46 | n/a | x[0] &= 2 |

47 | n/a | x[0] |= 5 |

48 | n/a | x[0] ^= 1 |

49 | n/a | x[0] /= 2 |

50 | n/a | self.assertEqual(x[0], 3.0) |

51 | n/a | |

52 | n/a | def testSequences(self): |

53 | n/a | x = [1,2] |

54 | n/a | x += [3,4] |

55 | n/a | x *= 2 |

56 | n/a | |

57 | n/a | self.assertEqual(x, [1, 2, 3, 4, 1, 2, 3, 4]) |

58 | n/a | |

59 | n/a | x = [1, 2, 3] |

60 | n/a | y = x |

61 | n/a | x[1:2] *= 2 |

62 | n/a | y[1:2] += [1] |

63 | n/a | |

64 | n/a | self.assertEqual(x, [1, 2, 1, 2, 3]) |

65 | n/a | self.assertTrue(x is y) |

66 | n/a | |

67 | n/a | def testCustomMethods1(self): |

68 | n/a | |

69 | n/a | class aug_test: |

70 | n/a | def __init__(self, value): |

71 | n/a | self.val = value |

72 | n/a | def __radd__(self, val): |

73 | n/a | return self.val + val |

74 | n/a | def __add__(self, val): |

75 | n/a | return aug_test(self.val + val) |

76 | n/a | |

77 | n/a | class aug_test2(aug_test): |

78 | n/a | def __iadd__(self, val): |

79 | n/a | self.val = self.val + val |

80 | n/a | return self |

81 | n/a | |

82 | n/a | class aug_test3(aug_test): |

83 | n/a | def __iadd__(self, val): |

84 | n/a | return aug_test3(self.val + val) |

85 | n/a | |

86 | n/a | class aug_test4(aug_test3): |

87 | n/a | """Blocks inheritance, and fallback to __add__""" |

88 | n/a | __iadd__ = None |

89 | n/a | |

90 | n/a | x = aug_test(1) |

91 | n/a | y = x |

92 | n/a | x += 10 |

93 | n/a | |

94 | n/a | self.assertIsInstance(x, aug_test) |

95 | n/a | self.assertTrue(y is not x) |

96 | n/a | self.assertEqual(x.val, 11) |

97 | n/a | |

98 | n/a | x = aug_test2(2) |

99 | n/a | y = x |

100 | n/a | x += 10 |

101 | n/a | |

102 | n/a | self.assertTrue(y is x) |

103 | n/a | self.assertEqual(x.val, 12) |

104 | n/a | |

105 | n/a | x = aug_test3(3) |

106 | n/a | y = x |

107 | n/a | x += 10 |

108 | n/a | |

109 | n/a | self.assertIsInstance(x, aug_test3) |

110 | n/a | self.assertTrue(y is not x) |

111 | n/a | self.assertEqual(x.val, 13) |

112 | n/a | |

113 | n/a | x = aug_test4(4) |

114 | n/a | with self.assertRaises(TypeError): |

115 | n/a | x += 10 |

116 | n/a | |

117 | n/a | |

118 | n/a | def testCustomMethods2(test_self): |

119 | n/a | output = [] |

120 | n/a | |

121 | n/a | class testall: |

122 | n/a | def __add__(self, val): |

123 | n/a | output.append("__add__ called") |

124 | n/a | def __radd__(self, val): |

125 | n/a | output.append("__radd__ called") |

126 | n/a | def __iadd__(self, val): |

127 | n/a | output.append("__iadd__ called") |

128 | n/a | return self |

129 | n/a | |

130 | n/a | def __sub__(self, val): |

131 | n/a | output.append("__sub__ called") |

132 | n/a | def __rsub__(self, val): |

133 | n/a | output.append("__rsub__ called") |

134 | n/a | def __isub__(self, val): |

135 | n/a | output.append("__isub__ called") |

136 | n/a | return self |

137 | n/a | |

138 | n/a | def __mul__(self, val): |

139 | n/a | output.append("__mul__ called") |

140 | n/a | def __rmul__(self, val): |

141 | n/a | output.append("__rmul__ called") |

142 | n/a | def __imul__(self, val): |

143 | n/a | output.append("__imul__ called") |

144 | n/a | return self |

145 | n/a | |

146 | n/a | def __matmul__(self, val): |

147 | n/a | output.append("__matmul__ called") |

148 | n/a | def __rmatmul__(self, val): |

149 | n/a | output.append("__rmatmul__ called") |

150 | n/a | def __imatmul__(self, val): |

151 | n/a | output.append("__imatmul__ called") |

152 | n/a | return self |

153 | n/a | |

154 | n/a | def __floordiv__(self, val): |

155 | n/a | output.append("__floordiv__ called") |

156 | n/a | return self |

157 | n/a | def __ifloordiv__(self, val): |

158 | n/a | output.append("__ifloordiv__ called") |

159 | n/a | return self |

160 | n/a | def __rfloordiv__(self, val): |

161 | n/a | output.append("__rfloordiv__ called") |

162 | n/a | return self |

163 | n/a | |

164 | n/a | def __truediv__(self, val): |

165 | n/a | output.append("__truediv__ called") |

166 | n/a | return self |

167 | n/a | def __rtruediv__(self, val): |

168 | n/a | output.append("__rtruediv__ called") |

169 | n/a | return self |

170 | n/a | def __itruediv__(self, val): |

171 | n/a | output.append("__itruediv__ called") |

172 | n/a | return self |

173 | n/a | |

174 | n/a | def __mod__(self, val): |

175 | n/a | output.append("__mod__ called") |

176 | n/a | def __rmod__(self, val): |

177 | n/a | output.append("__rmod__ called") |

178 | n/a | def __imod__(self, val): |

179 | n/a | output.append("__imod__ called") |

180 | n/a | return self |

181 | n/a | |

182 | n/a | def __pow__(self, val): |

183 | n/a | output.append("__pow__ called") |

184 | n/a | def __rpow__(self, val): |

185 | n/a | output.append("__rpow__ called") |

186 | n/a | def __ipow__(self, val): |

187 | n/a | output.append("__ipow__ called") |

188 | n/a | return self |

189 | n/a | |

190 | n/a | def __or__(self, val): |

191 | n/a | output.append("__or__ called") |

192 | n/a | def __ror__(self, val): |

193 | n/a | output.append("__ror__ called") |

194 | n/a | def __ior__(self, val): |

195 | n/a | output.append("__ior__ called") |

196 | n/a | return self |

197 | n/a | |

198 | n/a | def __and__(self, val): |

199 | n/a | output.append("__and__ called") |

200 | n/a | def __rand__(self, val): |

201 | n/a | output.append("__rand__ called") |

202 | n/a | def __iand__(self, val): |

203 | n/a | output.append("__iand__ called") |

204 | n/a | return self |

205 | n/a | |

206 | n/a | def __xor__(self, val): |

207 | n/a | output.append("__xor__ called") |

208 | n/a | def __rxor__(self, val): |

209 | n/a | output.append("__rxor__ called") |

210 | n/a | def __ixor__(self, val): |

211 | n/a | output.append("__ixor__ called") |

212 | n/a | return self |

213 | n/a | |

214 | n/a | def __rshift__(self, val): |

215 | n/a | output.append("__rshift__ called") |

216 | n/a | def __rrshift__(self, val): |

217 | n/a | output.append("__rrshift__ called") |

218 | n/a | def __irshift__(self, val): |

219 | n/a | output.append("__irshift__ called") |

220 | n/a | return self |

221 | n/a | |

222 | n/a | def __lshift__(self, val): |

223 | n/a | output.append("__lshift__ called") |

224 | n/a | def __rlshift__(self, val): |

225 | n/a | output.append("__rlshift__ called") |

226 | n/a | def __ilshift__(self, val): |

227 | n/a | output.append("__ilshift__ called") |

228 | n/a | return self |

229 | n/a | |

230 | n/a | x = testall() |

231 | n/a | x + 1 |

232 | n/a | 1 + x |

233 | n/a | x += 1 |

234 | n/a | |

235 | n/a | x - 1 |

236 | n/a | 1 - x |

237 | n/a | x -= 1 |

238 | n/a | |

239 | n/a | x * 1 |

240 | n/a | 1 * x |

241 | n/a | x *= 1 |

242 | n/a | |

243 | n/a | x @ 1 |

244 | n/a | 1 @ x |

245 | n/a | x @= 1 |

246 | n/a | |

247 | n/a | x / 1 |

248 | n/a | 1 / x |

249 | n/a | x /= 1 |

250 | n/a | |

251 | n/a | x // 1 |

252 | n/a | 1 // x |

253 | n/a | x //= 1 |

254 | n/a | |

255 | n/a | x % 1 |

256 | n/a | 1 % x |

257 | n/a | x %= 1 |

258 | n/a | |

259 | n/a | x ** 1 |

260 | n/a | 1 ** x |

261 | n/a | x **= 1 |

262 | n/a | |

263 | n/a | x | 1 |

264 | n/a | 1 | x |

265 | n/a | x |= 1 |

266 | n/a | |

267 | n/a | x & 1 |

268 | n/a | 1 & x |

269 | n/a | x &= 1 |

270 | n/a | |

271 | n/a | x ^ 1 |

272 | n/a | 1 ^ x |

273 | n/a | x ^= 1 |

274 | n/a | |

275 | n/a | x >> 1 |

276 | n/a | 1 >> x |

277 | n/a | x >>= 1 |

278 | n/a | |

279 | n/a | x << 1 |

280 | n/a | 1 << x |

281 | n/a | x <<= 1 |

282 | n/a | |

283 | n/a | test_self.assertEqual(output, '''\ |

284 | n/a | __add__ called |

285 | n/a | __radd__ called |

286 | n/a | __iadd__ called |

287 | n/a | __sub__ called |

288 | n/a | __rsub__ called |

289 | n/a | __isub__ called |

290 | n/a | __mul__ called |

291 | n/a | __rmul__ called |

292 | n/a | __imul__ called |

293 | n/a | __matmul__ called |

294 | n/a | __rmatmul__ called |

295 | n/a | __imatmul__ called |

296 | n/a | __truediv__ called |

297 | n/a | __rtruediv__ called |

298 | n/a | __itruediv__ called |

299 | n/a | __floordiv__ called |

300 | n/a | __rfloordiv__ called |

301 | n/a | __ifloordiv__ called |

302 | n/a | __mod__ called |

303 | n/a | __rmod__ called |

304 | n/a | __imod__ called |

305 | n/a | __pow__ called |

306 | n/a | __rpow__ called |

307 | n/a | __ipow__ called |

308 | n/a | __or__ called |

309 | n/a | __ror__ called |

310 | n/a | __ior__ called |

311 | n/a | __and__ called |

312 | n/a | __rand__ called |

313 | n/a | __iand__ called |

314 | n/a | __xor__ called |

315 | n/a | __rxor__ called |

316 | n/a | __ixor__ called |

317 | n/a | __rshift__ called |

318 | n/a | __rrshift__ called |

319 | n/a | __irshift__ called |

320 | n/a | __lshift__ called |

321 | n/a | __rlshift__ called |

322 | n/a | __ilshift__ called |

323 | n/a | '''.splitlines()) |

324 | n/a | |

325 | n/a | if __name__ == '__main__': |

326 | n/a | unittest.main() |