How to use syntaxError method in wpt

Best JavaScript code snippet using wpt

test_syntax.py

Source:test_syntax.py Github

copy

Full Screen

1"""This module tests SyntaxErrors.2Here's an example of the sort of thing that is tested.3>>> def f(x):4... global x5Traceback (most recent call last):6SyntaxError: name 'x' is local and global7The tests are all raise SyntaxErrors. They were created by checking8each C call that raises SyntaxError. There are several modules that9raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and10symtable.c.11The parser itself outlaws a lot of invalid syntax. None of these12errors are tested here at the moment. We should add some tests; since13there are infinitely many programs with invalid syntax, we would need14to be judicious in selecting some.15The compiler generates a synthetic module name for code executed by16doctest. Since all the code comes from the same module, a suffix like17[1] is appended to the module name, As a consequence, changing the18order of tests in this module means renumbering all the errors after19it. (Maybe we should enable the ellipsis option for these tests.)20In ast.c, syntax errors are raised by calling ast_error().21Errors from set_context():22TODO(jhylton): "assignment to None" is inconsistent with other messages23>>> obj.None = 124Traceback (most recent call last):25SyntaxError: assignment to None (<doctest test.test_syntax[1]>, line 1)26>>> None = 127Traceback (most recent call last):28SyntaxError: assignment to None (<doctest test.test_syntax[2]>, line 1)29It's a syntax error to assign to the empty tuple. Why isn't it an30error to assign to the empty list? It will always raise some error at31runtime.32>>> () = 133Traceback (most recent call last):34SyntaxError: can't assign to () (<doctest test.test_syntax[3]>, line 1)35>>> f() = 136Traceback (most recent call last):37SyntaxError: can't assign to function call (<doctest test.test_syntax[4]>, line 1)38>>> del f()39Traceback (most recent call last):40SyntaxError: can't delete function call (<doctest test.test_syntax[5]>, line 1)41>>> a + 1 = 242Traceback (most recent call last):43SyntaxError: can't assign to operator (<doctest test.test_syntax[6]>, line 1)44>>> (x for x in x) = 145Traceback (most recent call last):46SyntaxError: can't assign to generator expression (<doctest test.test_syntax[7]>, line 1)47>>> 1 = 148Traceback (most recent call last):49SyntaxError: can't assign to literal (<doctest test.test_syntax[8]>, line 1)50>>> "abc" = 151Traceback (most recent call last):52SyntaxError: can't assign to literal (<doctest test.test_syntax[9]>, line 1)53>>> `1` = 154Traceback (most recent call last):55SyntaxError: can't assign to repr (<doctest test.test_syntax[10]>, line 1)56If the left-hand side of an assignment is a list or tuple, an illegal57expression inside that contain should still cause a syntax error.58This test just checks a couple of cases rather than enumerating all of59them.60>>> (a, "b", c) = (1, 2, 3)61Traceback (most recent call last):62SyntaxError: can't assign to literal (<doctest test.test_syntax[11]>, line 1)63>>> [a, b, c + 1] = [1, 2, 3]64Traceback (most recent call last):65SyntaxError: can't assign to operator (<doctest test.test_syntax[12]>, line 1)66>>> a if 1 else b = 167Traceback (most recent call last):68SyntaxError: can't assign to conditional expression (<doctest test.test_syntax[13]>, line 1)69From compiler_complex_args():70>>> def f(None=1):71... pass72Traceback (most recent call last):73SyntaxError: assignment to None (<doctest test.test_syntax[14]>, line 1)74From ast_for_arguments():75>>> def f(x, y=1, z):76... pass77Traceback (most recent call last):78SyntaxError: non-default argument follows default argument (<doctest test.test_syntax[15]>, line 1)79>>> def f(x, None):80... pass81Traceback (most recent call last):82SyntaxError: assignment to None (<doctest test.test_syntax[16]>, line 1)83>>> def f(*None):84... pass85Traceback (most recent call last):86SyntaxError: assignment to None (<doctest test.test_syntax[17]>, line 1)87>>> def f(**None):88... pass89Traceback (most recent call last):90SyntaxError: assignment to None (<doctest test.test_syntax[18]>, line 1)91From ast_for_funcdef():92>>> def None(x):93... pass94Traceback (most recent call last):95SyntaxError: assignment to None (<doctest test.test_syntax[19]>, line 1)96From ast_for_call():97>>> def f(it, *varargs):98... return list(it)99>>> L = range(10)100>>> f(x for x in L)101[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]102>>> f(x for x in L, 1)103Traceback (most recent call last):104SyntaxError: Generator expression must be parenthesized if not sole argument (<doctest test.test_syntax[23]>, line 1)105>>> f((x for x in L), 1)106[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]107>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,108... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,109... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,110... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,111... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,112... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,113... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,114... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,115... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,116... i100, i101, i102, i103, i104, i105, i106, i107, i108,117... i109, i110, i111, i112, i113, i114, i115, i116, i117,118... i118, i119, i120, i121, i122, i123, i124, i125, i126,119... i127, i128, i129, i130, i131, i132, i133, i134, i135,120... i136, i137, i138, i139, i140, i141, i142, i143, i144,121... i145, i146, i147, i148, i149, i150, i151, i152, i153,122... i154, i155, i156, i157, i158, i159, i160, i161, i162,123... i163, i164, i165, i166, i167, i168, i169, i170, i171,124... i172, i173, i174, i175, i176, i177, i178, i179, i180,125... i181, i182, i183, i184, i185, i186, i187, i188, i189,126... i190, i191, i192, i193, i194, i195, i196, i197, i198,127... i199, i200, i201, i202, i203, i204, i205, i206, i207,128... i208, i209, i210, i211, i212, i213, i214, i215, i216,129... i217, i218, i219, i220, i221, i222, i223, i224, i225,130... i226, i227, i228, i229, i230, i231, i232, i233, i234,131... i235, i236, i237, i238, i239, i240, i241, i242, i243,132... i244, i245, i246, i247, i248, i249, i250, i251, i252,133... i253, i254, i255)134Traceback (most recent call last):135SyntaxError: more than 255 arguments (<doctest test.test_syntax[25]>, line 1)136The actual error cases counts positional arguments, keyword arguments,137and generator expression arguments separately. This test combines the138three.139>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,140... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,141... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,142... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,143... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,144... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,145... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,146... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,147... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,148... i100, i101, i102, i103, i104, i105, i106, i107, i108,149... i109, i110, i111, i112, i113, i114, i115, i116, i117,150... i118, i119, i120, i121, i122, i123, i124, i125, i126,151... i127, i128, i129, i130, i131, i132, i133, i134, i135,152... i136, i137, i138, i139, i140, i141, i142, i143, i144,153... i145, i146, i147, i148, i149, i150, i151, i152, i153,154... i154, i155, i156, i157, i158, i159, i160, i161, i162,155... i163, i164, i165, i166, i167, i168, i169, i170, i171,156... i172, i173, i174, i175, i176, i177, i178, i179, i180,157... i181, i182, i183, i184, i185, i186, i187, i188, i189,158... i190, i191, i192, i193, i194, i195, i196, i197, i198,159... i199, i200, i201, i202, i203, i204, i205, i206, i207,160... i208, i209, i210, i211, i212, i213, i214, i215, i216,161... i217, i218, i219, i220, i221, i222, i223, i224, i225,162... i226, i227, i228, i229, i230, i231, i232, i233, i234,163... i235, i236, i237, i238, i239, i240, i241, i242, i243,164... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,165... i252=1, i253=1, i254=1, i255=1)166Traceback (most recent call last):167SyntaxError: more than 255 arguments (<doctest test.test_syntax[26]>, line 1)168>>> f(lambda x: x[0] = 3)169Traceback (most recent call last):170SyntaxError: lambda cannot contain assignment (<doctest test.test_syntax[27]>, line 1)171The grammar accepts any test (basically, any expression) in the172keyword slot of a call site. Test a few different options.173>>> f(x()=2)174Traceback (most recent call last):175SyntaxError: keyword can't be an expression (<doctest test.test_syntax[28]>, line 1)176>>> f(a or b=1)177Traceback (most recent call last):178SyntaxError: keyword can't be an expression (<doctest test.test_syntax[29]>, line 1)179>>> f(x.y=1)180Traceback (most recent call last):181SyntaxError: keyword can't be an expression (<doctest test.test_syntax[30]>, line 1)182From ast_for_expr_stmt():183>>> (x for x in x) += 1184Traceback (most recent call last):185SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_syntax[31]>, line 1)186>>> None += 1187Traceback (most recent call last):188SyntaxError: assignment to None (<doctest test.test_syntax[32]>, line 1)189>>> f() += 1190Traceback (most recent call last):191SyntaxError: illegal expression for augmented assignment (<doctest test.test_syntax[33]>, line 1)192Test continue in finally in weird combinations.193continue in for loop under finally shouuld be ok.194 >>> def test():195 ... try:196 ... pass197 ... finally:198 ... for abc in range(10):199 ... continue200 ... print abc201 >>> test()202 9203Start simple, a continue in a finally should not be allowed.204 >>> def test():205 ... for abc in range(10):206 ... try:207 ... pass208 ... finally:209 ... continue210 ...211 Traceback (most recent call last):212 ...213 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[36]>, line 6)214This is essentially a continue in a finally which should not be allowed.215 >>> def test():216 ... for abc in range(10):217 ... try:218 ... pass219 ... finally:220 ... try:221 ... continue222 ... except:223 ... pass224 Traceback (most recent call last):225 ...226 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[37]>, line 7)227 >>> def foo():228 ... try:229 ... pass230 ... finally:231 ... continue232 Traceback (most recent call last):233 ...234 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[38]>, line 5)235 >>> def foo():236 ... for a in ():237 ... try: pass238 ... finally: continue239 Traceback (most recent call last):240 ...241 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[39]>, line 4)242 >>> def foo():243 ... for a in ():244 ... try: pass245 ... finally:246 ... try:247 ... continue248 ... finally: pass249 Traceback (most recent call last):250 ...251 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[40]>, line 6)252 >>> def foo():253 ... for a in ():254 ... try: pass255 ... finally:256 ... try:257 ... pass258 ... except:259 ... continue260 Traceback (most recent call last):261 ...262 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[41]>, line 8)263There is one test for a break that is not in a loop. The compiler264uses a single data structure to keep track of try-finally and loops,265so we need to be sure that a break is actually inside a loop. If it266isn't, there should be a syntax error.267 >>> try:268 ... print 1269 ... break270 ... print 2271 ... finally:272 ... print 3273 Traceback (most recent call last):274 ...275 SyntaxError: 'break' outside loop (<doctest test.test_syntax[42]>, line 3)276This should probably raise a better error than a SystemError (or none at all).277In 2.5 there was a missing exception and an assert was triggered in a debug278build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514279 >>> while 1:280 ... while 2:281 ... while 3:282 ... while 4:283 ... while 5:284 ... while 6:285 ... while 8:286 ... while 9:287 ... while 10:288 ... while 11:289 ... while 12:290 ... while 13:291 ... while 14:292 ... while 15:293 ... while 16:294 ... while 17:295 ... while 18:296 ... while 19:297 ... while 20:298 ... while 21:299 ... while 22:300 ... break301 Traceback (most recent call last):302 ...303 SystemError: too many statically nested blocks304This tests assignment-context; there was a bug in Python 2.5 where compiling305a complex 'if' (one with 'elif') would fail to notice an invalid suite,306leading to spurious errors.307 >>> if 1:308 ... x() = 1309 ... elif 1:310 ... pass311 Traceback (most recent call last):312 ...313 SyntaxError: can't assign to function call (<doctest test.test_syntax[44]>, line 2)314 >>> if 1:315 ... pass316 ... elif 1:317 ... x() = 1318 Traceback (most recent call last):319 ...320 SyntaxError: can't assign to function call (<doctest test.test_syntax[45]>, line 4)321 >>> if 1:322 ... x() = 1323 ... elif 1:324 ... pass325 ... else:326 ... pass327 Traceback (most recent call last):328 ...329 SyntaxError: can't assign to function call (<doctest test.test_syntax[46]>, line 2)330 >>> if 1:331 ... pass332 ... elif 1:333 ... x() = 1334 ... else:335 ... pass336 Traceback (most recent call last):337 ...338 SyntaxError: can't assign to function call (<doctest test.test_syntax[47]>, line 4)339 >>> if 1:340 ... pass341 ... elif 1:342 ... pass343 ... else:344 ... x() = 1345 Traceback (most recent call last):346 ...347 SyntaxError: can't assign to function call (<doctest test.test_syntax[48]>, line 6)348"""349import re350import unittest351import warnings352from test import test_support353class SyntaxTestCase(unittest.TestCase):354 def _check_error(self, code, errtext,355 filename="<testcase>", mode="exec", subclass=None):356 """Check that compiling code raises SyntaxError with errtext.357 errtest is a regular expression that must be present in the358 test of the exception raised. If subclass is specified it359 is the expected subclass of SyntaxError (e.g. IndentationError).360 """361 try:362 compile(code, filename, mode)363 except SyntaxError, err:364 if subclass and not isinstance(err, subclass):365 self.fail("SyntaxError is not a %s" % subclass.__name__)366 mo = re.search(errtext, str(err))367 if mo is None:368 self.fail("SyntaxError did not contain '%r'" % (errtext,))369 else:370 self.fail("compile() did not raise SyntaxError")371 def test_assign_call(self):372 self._check_error("f() = 1", "assign")373 def test_assign_del(self):374 self._check_error("del f()", "delete")375 def test_global_err_then_warn(self):376 # Bug tickler: The SyntaxError raised for one global statement377 # shouldn't be clobbered by a SyntaxWarning issued for a later one.378 source = re.sub('(?m)^ *:', '', """\379 :def error(a):380 : global a # SyntaxError381 :def warning():382 : b = 1383 : global b # SyntaxWarning384 :""")385 warnings.filterwarnings(action='ignore', category=SyntaxWarning)386 self._check_error(source, "global")387 warnings.filters.pop(0)388 def test_break_outside_loop(self):389 self._check_error("break", "outside loop")390 def test_delete_deref(self):391 source = re.sub('(?m)^ *:', '', """\392 :def foo(x):393 : def bar():394 : print x395 : del x396 :""")397 self._check_error(source, "nested scope")398 def test_unexpected_indent(self):399 self._check_error("foo()\n bar()\n", "unexpected indent",400 subclass=IndentationError)401 def test_no_indent(self):402 self._check_error("if 1:\nfoo()", "expected an indented block",403 subclass=IndentationError)404 def test_bad_outdent(self):405 self._check_error("if 1:\n foo()\n bar()",406 "unindent does not match .* level",407 subclass=IndentationError)408 def test_kwargs_last(self):409 self._check_error("int(base=10, '2')", "non-keyword arg")410def test_main():411 test_support.run_unittest(SyntaxTestCase)412 from test import test_syntax413 test_support.run_doctest(test_syntax, verbosity=True)414if __name__ == "__main__":...

Full Screen

Full Screen

test_strlit.py

Source:test_strlit.py Github

copy

Full Screen

1r"""Test correct treatment of various string literals by the parser.2There are four types of string literals:3 'abc' -- normal str4 r'abc' -- raw str5 b'xyz' -- normal bytes6 br'xyz' | rb'xyz' -- raw bytes7The difference between normal and raw strings is of course that in a8raw string, \ escapes (while still used to determine the end of the9literal) are not interpreted, so that r'\x00' contains four10characters: a backslash, an x, and two zeros; while '\x00' contains a11single character (code point zero).12The tricky thing is what should happen when non-ASCII bytes are used13inside literals. For bytes literals, this is considered illegal. But14for str literals, those bytes are supposed to be decoded using the15encoding declared for the file (UTF-8 by default).16We have to test this with various file encodings. We also test it with17exec()/eval(), which uses a different code path.18This file is really about correct treatment of encodings and19backslashes. It doesn't concern itself with issues like single20vs. double quotes or singly- vs. triply-quoted strings: that's dealt21with elsewhere (I assume).22"""23import os24import sys25import shutil26import tempfile27import unittest28import test.support29TEMPLATE = r"""# coding: %s30a = 'x'31assert ord(a) == 12032b = '\x01'33assert ord(b) == 134c = r'\x01'35assert list(map(ord, c)) == [92, 120, 48, 49]36d = '\x81'37assert ord(d) == 0x8138e = r'\x81'39assert list(map(ord, e)) == [92, 120, 56, 49]40f = '\u1881'41assert ord(f) == 0x188142g = r'\u1881'43assert list(map(ord, g)) == [92, 117, 49, 56, 56, 49]44h = '\U0001d120'45assert ord(h) == 0x1d12046i = r'\U0001d120'47assert list(map(ord, i)) == [92, 85, 48, 48, 48, 49, 100, 49, 50, 48]48"""49def byte(i):50 return bytes([i])51class TestLiterals(unittest.TestCase):52 def setUp(self):53 self.save_path = sys.path[:]54 self.tmpdir = tempfile.mkdtemp()55 sys.path.insert(0, self.tmpdir)56 def tearDown(self):57 sys.path[:] = self.save_path58 shutil.rmtree(self.tmpdir, ignore_errors=True)59 def test_template(self):60 # Check that the template doesn't contain any non-printables61 # except for \n.62 for c in TEMPLATE:63 assert c == '\n' or ' ' <= c <= '~', repr(c)64 def test_eval_str_normal(self):65 self.assertEqual(eval(""" 'x' """), 'x')66 self.assertEqual(eval(r""" '\x01' """), chr(1))67 self.assertEqual(eval(""" '\x01' """), chr(1))68 self.assertEqual(eval(r""" '\x81' """), chr(0x81))69 self.assertEqual(eval(""" '\x81' """), chr(0x81))70 self.assertEqual(eval(r""" '\u1881' """), chr(0x1881))71 self.assertEqual(eval(""" '\u1881' """), chr(0x1881))72 self.assertEqual(eval(r""" '\U0001d120' """), chr(0x1d120))73 self.assertEqual(eval(""" '\U0001d120' """), chr(0x1d120))74 def test_eval_str_incomplete(self):75 self.assertRaises(SyntaxError, eval, r""" '\x' """)76 self.assertRaises(SyntaxError, eval, r""" '\x0' """)77 self.assertRaises(SyntaxError, eval, r""" '\u' """)78 self.assertRaises(SyntaxError, eval, r""" '\u0' """)79 self.assertRaises(SyntaxError, eval, r""" '\u00' """)80 self.assertRaises(SyntaxError, eval, r""" '\u000' """)81 self.assertRaises(SyntaxError, eval, r""" '\U' """)82 self.assertRaises(SyntaxError, eval, r""" '\U0' """)83 self.assertRaises(SyntaxError, eval, r""" '\U00' """)84 self.assertRaises(SyntaxError, eval, r""" '\U000' """)85 self.assertRaises(SyntaxError, eval, r""" '\U0000' """)86 self.assertRaises(SyntaxError, eval, r""" '\U00000' """)87 self.assertRaises(SyntaxError, eval, r""" '\U000000' """)88 self.assertRaises(SyntaxError, eval, r""" '\U0000000' """)89 def test_eval_str_raw(self):90 self.assertEqual(eval(""" r'x' """), 'x')91 self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01')92 self.assertEqual(eval(""" r'\x01' """), chr(1))93 self.assertEqual(eval(r""" r'\x81' """), '\\' + 'x81')94 self.assertEqual(eval(""" r'\x81' """), chr(0x81))95 self.assertEqual(eval(r""" r'\u1881' """), '\\' + 'u1881')96 self.assertEqual(eval(""" r'\u1881' """), chr(0x1881))97 self.assertEqual(eval(r""" r'\U0001d120' """), '\\' + 'U0001d120')98 self.assertEqual(eval(""" r'\U0001d120' """), chr(0x1d120))99 def test_eval_bytes_normal(self):100 self.assertEqual(eval(""" b'x' """), b'x')101 self.assertEqual(eval(r""" b'\x01' """), byte(1))102 self.assertEqual(eval(""" b'\x01' """), byte(1))103 self.assertEqual(eval(r""" b'\x81' """), byte(0x81))104 self.assertRaises(SyntaxError, eval, """ b'\x81' """)105 self.assertEqual(eval(r""" b'\u1881' """), b'\\' + b'u1881')106 self.assertRaises(SyntaxError, eval, """ b'\u1881' """)107 self.assertEqual(eval(r""" b'\U0001d120' """), b'\\' + b'U0001d120')108 self.assertRaises(SyntaxError, eval, """ b'\U0001d120' """)109 def test_eval_bytes_incomplete(self):110 self.assertRaises(SyntaxError, eval, r""" b'\x' """)111 self.assertRaises(SyntaxError, eval, r""" b'\x0' """)112 def test_eval_bytes_raw(self):113 self.assertEqual(eval(""" br'x' """), b'x')114 self.assertEqual(eval(""" rb'x' """), b'x')115 self.assertEqual(eval(r""" br'\x01' """), b'\\' + b'x01')116 self.assertEqual(eval(r""" rb'\x01' """), b'\\' + b'x01')117 self.assertEqual(eval(""" br'\x01' """), byte(1))118 self.assertEqual(eval(""" rb'\x01' """), byte(1))119 self.assertEqual(eval(r""" br'\x81' """), b"\\" + b"x81")120 self.assertEqual(eval(r""" rb'\x81' """), b"\\" + b"x81")121 self.assertRaises(SyntaxError, eval, """ br'\x81' """)122 self.assertRaises(SyntaxError, eval, """ rb'\x81' """)123 self.assertEqual(eval(r""" br'\u1881' """), b"\\" + b"u1881")124 self.assertEqual(eval(r""" rb'\u1881' """), b"\\" + b"u1881")125 self.assertRaises(SyntaxError, eval, """ br'\u1881' """)126 self.assertRaises(SyntaxError, eval, """ rb'\u1881' """)127 self.assertEqual(eval(r""" br'\U0001d120' """), b"\\" + b"U0001d120")128 self.assertEqual(eval(r""" rb'\U0001d120' """), b"\\" + b"U0001d120")129 self.assertRaises(SyntaxError, eval, """ br'\U0001d120' """)130 self.assertRaises(SyntaxError, eval, """ rb'\U0001d120' """)131 self.assertRaises(SyntaxError, eval, """ bb'' """)132 self.assertRaises(SyntaxError, eval, """ rr'' """)133 self.assertRaises(SyntaxError, eval, """ brr'' """)134 self.assertRaises(SyntaxError, eval, """ bbr'' """)135 self.assertRaises(SyntaxError, eval, """ rrb'' """)136 self.assertRaises(SyntaxError, eval, """ rbb'' """)137 def test_eval_str_u(self):138 self.assertEqual(eval(""" u'x' """), 'x')139 self.assertEqual(eval(""" U'\u00e4' """), 'ä')140 self.assertEqual(eval(""" u'\N{LATIN SMALL LETTER A WITH DIAERESIS}' """), 'ä')141 self.assertRaises(SyntaxError, eval, """ ur'' """)142 self.assertRaises(SyntaxError, eval, """ ru'' """)143 self.assertRaises(SyntaxError, eval, """ bu'' """)144 self.assertRaises(SyntaxError, eval, """ ub'' """)145 def check_encoding(self, encoding, extra=""):146 modname = "xx_" + encoding.replace("-", "_")147 fn = os.path.join(self.tmpdir, modname + ".py")148 f = open(fn, "w", encoding=encoding)149 try:150 f.write(TEMPLATE % encoding)151 f.write(extra)152 finally:153 f.close()154 __import__(modname)155 del sys.modules[modname]156 def test_file_utf_8(self):157 extra = "z = '\u1234'; assert ord(z) == 0x1234\n"158 self.check_encoding("utf-8", extra)159 def test_file_utf_8_error(self):160 extra = "b'\x80'\n"161 self.assertRaises(SyntaxError, self.check_encoding, "utf-8", extra)162 def test_file_utf8(self):163 self.check_encoding("utf-8")164 def test_file_iso_8859_1(self):165 self.check_encoding("iso-8859-1")166 def test_file_latin_1(self):167 self.check_encoding("latin-1")168 def test_file_latin9(self):169 self.check_encoding("latin9")170def test_main():171 test.support.run_unittest(__name__)172if __name__ == "__main__":...

Full Screen

Full Screen

block-let-declaration.js

Source:block-let-declaration.js Github

copy

Full Screen

1// Copyright 2011 the V8 project authors. All rights reserved.2// Redistribution and use in source and binary forms, with or without3// modification, are permitted provided that the following conditions are4// met:5//6// * Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// * Redistributions in binary form must reproduce the above9// copyright notice, this list of conditions and the following10// disclaimer in the documentation and/or other materials provided11// with the distribution.12// * Neither the name of Google Inc. nor the names of its13// contributors may be used to endorse or promote products derived14// from this software without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27// Flags: --harmony-scoping28// Test let declarations in various settings.29// TODO(ES6): properly activate extended mode30"use strict";31// Global32let x;33let y = 2;34const z = 4;35// Block local36{37 let y;38 let x = 3;39 const z = 5;40}41assertEquals(undefined, x);42assertEquals(2,y);43assertEquals(4,z);44if (true) {45 let y;46 assertEquals(undefined, y);47}48// Invalid declarations are early errors in harmony mode and thus should trigger49// an exception in eval code during parsing, before even compiling or executing50// the code. Thus the generated function is not called here.51function TestLocalThrows(str, expect) {52 assertThrows("(function(){ 'use strict'; " + str + "})", expect);53}54function TestLocalDoesNotThrow(str) {55 assertDoesNotThrow("(function(){ 'use strict'; " + str + "})()");56}57// Test let declarations in statement positions.58TestLocalThrows("if (true) let x;", SyntaxError);59TestLocalThrows("if (true) {} else let x;", SyntaxError);60TestLocalThrows("do let x; while (false)", SyntaxError);61TestLocalThrows("while (false) let x;", SyntaxError);62TestLocalThrows("label: let x;", SyntaxError);63TestLocalThrows("for (;false;) let x;", SyntaxError);64TestLocalThrows("switch (true) { case true: let x; }", SyntaxError);65TestLocalThrows("switch (true) { default: let x; }", SyntaxError);66// Test const declarations with initialisers in statement positions.67TestLocalThrows("if (true) const x = 1;", SyntaxError);68TestLocalThrows("if (true) {} else const x = 1;", SyntaxError);69TestLocalThrows("do const x = 1; while (false)", SyntaxError);70TestLocalThrows("while (false) const x = 1;", SyntaxError);71TestLocalThrows("label: const x = 1;", SyntaxError);72TestLocalThrows("for (;false;) const x = 1;", SyntaxError);73TestLocalThrows("switch (true) { case true: const x = 1; }", SyntaxError);74TestLocalThrows("switch (true) { default: const x = 1; }", SyntaxError);75// Test const declarations without initialisers.76TestLocalThrows("const x;", SyntaxError);77TestLocalThrows("const x = 1, y;", SyntaxError);78TestLocalThrows("const x, y = 1;", SyntaxError);79// Test const declarations without initialisers in statement positions.80TestLocalThrows("if (true) const x;", SyntaxError);81TestLocalThrows("if (true) {} else const x;", SyntaxError);82TestLocalThrows("do const x; while (false)", SyntaxError);83TestLocalThrows("while (false) const x;", SyntaxError);84TestLocalThrows("label: const x;", SyntaxError);85TestLocalThrows("for (;false;) const x;", SyntaxError);86TestLocalThrows("switch (true) { case true: const x; }", SyntaxError);87TestLocalThrows("switch (true) { default: const x; }", SyntaxError);88// Test var declarations in statement positions.89TestLocalDoesNotThrow("if (true) var x;");90TestLocalDoesNotThrow("if (true) {} else var x;");91TestLocalDoesNotThrow("do var x; while (false)");92TestLocalDoesNotThrow("while (false) var x;");93TestLocalDoesNotThrow("label: var x;");94TestLocalDoesNotThrow("for (;false;) var x;");95TestLocalDoesNotThrow("switch (true) { case true: var x; }");96TestLocalDoesNotThrow("switch (true) { default: var x; }");97// Test function declarations in source element and98// non-strict statement positions.99function f() {100 // Non-strict source element positions.101 function g0() {102 "use strict";103 // Strict source element positions.104 function h() { }105 {106 function h1() { }107 }108 }109 {110 function g1() { }111 }112}113f();114// Test function declarations in statement position in strict mode.115TestLocalThrows("function f() { if (true) function g() {}", SyntaxError);116TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError);117TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError);118TestLocalThrows("function f() { while (false) function g() {}", SyntaxError);119TestLocalThrows("function f() { label: function g() {}", SyntaxError);120TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError);121TestLocalThrows("function f() { switch (true) { case true: function g() {} }", SyntaxError);...

Full Screen

Full Screen

NASHORN-207.js

Source:NASHORN-207.js Github

copy

Full Screen

1/*2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4 *5 * This code is free software; you can redistribute it and/or modify it6 * under the terms of the GNU General Public License version 2 only, as7 * published by the Free Software Foundation.8 *9 * This code is distributed in the hope that it will be useful, but WITHOUT10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12 * version 2 for more details (a copy is included in the LICENSE file that13 * accompanied this code).14 *15 * You should have received a copy of the GNU General Public License version16 * 2 along with this work; if not, write to the Free Software Foundation,17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18 *19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20 * or visit www.oracle.com if you need additional information or have any21 * questions.22 */23/**24 * NASHORN-207 : implement strict mode25 *26 * @test27 * @run28 */29'use strict';30// cannot delete a variable31try {32 eval("var fooVar = 3; delete fooVar;");33 fail("#1 should have thrown SyntaxError");34} catch (e) {35 if (! (e instanceof SyntaxError)) {36 fail("#2 SyntaxError expected but got " + e);37 }38}39// cannot delete function parameter variable40try {41 eval("function func(a) { delete a; }; func(2);");42 fail("#3 should have thrown SyntaxError");43} catch(e) {44 if (! (e instanceof SyntaxError)) {45 fail("#4 SyntaxError expected but got " + e);46 }47}48// assignment can't be used to define as new variable49try {50 bar = 3;51 fail("#5 should have thrown ReferenceError");52} catch (e) {53 if (! (e instanceof ReferenceError)) {54 fail("#6 ReferenceError expected but got " + e);55 }56}57// repeated property definition is not allowed in an object literal58try {59 eval("var obj = { foo: 33, foo: 44 }");60 fail("#7 should have thrown SyntaxError");61} catch(e) {62 if (! (e instanceof SyntaxError)) {63 fail("#8 SyntaxError expected but got " + e);64 }65}66// can't assign to "eval"67try {68 eval("var eval = 3");69 fail("#9 should have thrown SyntaxError");70} catch(e) {71 if (! (e instanceof SyntaxError)) {72 fail("#10 SyntaxError expected but got " + e);73 }74}75// can't assign to 'arguments'76try {77 eval("function func() { arguments = 34; }");78 fail("#11 should have thrown SyntaxError");79} catch(e) {80 if (! (e instanceof SyntaxError)) {81 fail("#12 SyntaxError expected but got " + e);82 }83}84// can't delete non-configurable properties85try {86 delete Object.prototype;87 fail("#13 should have thrown TypeError");88} catch (e) {89 if (! (e instanceof TypeError)) {90 fail("#14 TypeError expected but got " + e);91 }92}93// cannot re-use function parameter name94try {95 eval("function func(a, a) {}");96 fail("#15 should have thrown SyntaxError");97} catch (e) {98 if (! (e instanceof SyntaxError)) {99 fail("#16 SyntaxError expected but got " + e);100 }101}102// strict eval creates own scope and caller's scope is untouched!103eval("var myVar = 4;");104if (typeof myVar !== 'undefined') {105 fail("#17 eval var 'myVar' created in global scope");106}107eval("function myNewFunc() {}");108if (typeof myNewFunc !== 'undefined') {109 fail("#18 eval function 'myNewFunc' created in global scope");110}111// no octal in strict mode112try {113 eval("var x = 010;");114 fail("#19 should have thrown SyntaxError");115} catch (e) {116 if (! (e instanceof SyntaxError)) {117 fail("#20 SyntaxError expected but got " + e);118 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var wpt = new wpt('API_KEY');3 if(err) return console.error(err);4 wpt.testStatus(data.data.testId, function(err, data) {5 if(err) return console.error(err);6 if(data.statusCode == 200) {7 wpt.testResults(data.data.testId, function(err, data) {8 if(err) return console.error(err);9 console.log(data);10 });11 }12 });13});14var wpt = require('wpt');15var wpt = new wpt('API_KEY');16 if(err) return console.error(err);17 wpt.testStatus(data.data.testId, function(err, data) {18 if(err) return console.error(err);19 if(data.statusCode == 200) {20 wpt.testResults(data.data.testId, function(err, data) {21 if(err) return console.error(err);22 console.log(data);23 });24 }25 });26});27var wpt = require('wpt');28var wpt = new wpt('API_KEY');29 if(err) return console.error(err);30 wpt.testStatus(data.data.testId, function(err, data) {31 if(err) return console.error(err);32 if(data.statusCode == 200) {33 wpt.testResults(data.data.testId, function(err, data) {34 if(err) return console.error(err);35 console.log(data);36 });37 }38 });39});40var wpt = require('wpt');41var wpt = new wpt('API_KEY');42 if(err) return console.error(err);

Full Screen

Using AI Code Generation

copy

Full Screen

1wpt.syntaxError('test.js');2wpt.syntaxError('test.js', function(err, data) {3 console.log(data);4});5wpt.syntaxError('test.js', function(err, data) {6 console.log(data);7}, true);8wpt.syntaxError('test.js', function(err, data) {9 console.log(data);10}, true, true);11wpt.syntaxError('test.js', function(err, data) {12 console.log(data);13}, true, true, true);14wpt.syntaxError('test.js', function(err, data) {15 console.log(data);16}, true, true, true, true);17wpt.syntaxError('test.js', function(err, data) {18 console.log(data);19}, true, true, true, true, true);20wpt.syntaxError('test.js', function(err, data) {21 console.log(data);22}, true, true, true, true, true, true);23wpt.syntaxError('test.js', function(err, data) {24 console.log(data);25}, true, true, true, true, true, true, true);26wpt.syntaxError('test.js', function(err, data) {27 console.log(data);28}, true, true, true, true, true, true, true, true);29wpt.syntaxError('test.js', function(err, data) {30 console.log(data);31}, true, true, true, true, true, true, true, true, true);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var wpt = new wpt('API_KEY');3wpt.syntaxError(function(err, data) {4 console.log(data);5});6var wpt = require('wpt');7var wpt = new wpt('API_KEY');8wpt.testInfo('test_id', function(err, data) {9 console.log(data);10});11var wpt = require('wpt');12var wpt = new wpt('API_KEY');13wpt.testStatus('test_id', function(err, data) {14 console.log(data);15});16var wpt = require('wpt');17var wpt = new wpt('API_KEY');18wpt.testResults('test_id', function(err, data) {19 console.log(data);20});21var wpt = require('wpt');22var wpt = new wpt('API_KEY');23wpt.getLocations(function(err, data) {24 console.log(data);25});26var wpt = require('wpt');27var wpt = new wpt('API_KEY');28wpt.getTesters(function(err, data) {29 console.log(data);30});31var wpt = require('wpt');32var wpt = new wpt('API_KEY');33wpt.getLocations(function(err, data) {34 console.log(data);35});36var wpt = require('wpt');37var wpt = new wpt('API_KEY');38wpt.getTesters(function(err, data) {39 console.log(data);40});41var wpt = require('wpt');42var wpt = new wpt('API_KEY');43wpt.getLocations(function(err, data) {44 console.log(data);45});

Full Screen

Using AI Code Generation

copy

Full Screen

1wpt.syntaxError('test.js');2wpt.syntaxError('test.js', function(err, data) {3 if(err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9wpt.syntaxError('test.js', function(err, data) {10 if(err) {11 console.log(err);12 } else {13 console.log(data);14 }15}, function(data) {16 console.log(data);17});18wpt.syntaxError('test.js', function(err, data) {19 if(err) {20 console.log(err);21 } else {22 console.log(data);23 }24}, function(data) {25 console.log(data);26}, function(data) {27 console.log(data);28});29wpt.syntaxError('test.js', function(err, data) {30 if(err) {31 console.log(err);32 } else {33 console.log(data);34 }35}, function(data) {36 console.log(data);37}, function(data) {38 console.log(data);39}, function(data) {40 console.log(data);41});42wpt.syntaxError('test.js', function(err, data) {43 if(err) {44 console.log(err);45 } else {46 console.log(data);47 }48}, function(data) {49 console.log(data);50}, function(data) {51 console.log(data);52}, function(data) {53 console.log(data);54}, function(data) {55 console.log(data);56});57wpt.syntaxError('test.js', function(err, data) {58 if(err) {59 console.log(err);60 } else {61 console.log(data);62 }63}, function(data) {64 console.log(data);65}, fun

Full Screen

Using AI Code Generation

copy

Full Screen

1wpt.syntaxError("syntax error");2wpt.syntaxError("syntax error", function (err, data) {3 if (err) {4 console.log("error: ", err);5 }6 console.log("data: ", data);7});8wpt.syntaxError("syntax error", function (err, data) {9 if (err) {10 console.log("error: ", err);11 }12 console.log("data: ", data);13}, function (err, data) {14 if (err) {15 console.log("error: ", err);16 }17 console.log("data: ", data);18});19wpt.syntaxError("syntax error", function (err, data) {20 if (err) {21 console.log("error: ", err);22 }23 console.log("data: ", data);24}, function (err, data) {25 if (err) {26 console.log("error: ", err);27 }28 console.log("data: ", data);29}, {30});31wpt.syntaxError("syntax error", function (err, data) {32 if (err) {33 console.log("error: ", err);34 }35 console.log("data: ", data);36}, function (err, data) {37 if (err) {38 console.log("error: ", err);39 }40 console.log("data: ", data);41}, {42}, {43});44wpt.syntaxError("syntax error", function (err, data) {45 if (err) {46 console.log("error: ", err);47 }48 console.log("data: ", data);49}, function (err, data) {50 if (err) {51 console.log("error: ", err);52 }53 console.log("data: ", data);54}, {55}, {56}, {57});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful