Skip to content

Commit 8c0b588

Browse files
committed
added tests for operators
1 parent dbaff64 commit 8c0b588

File tree

5 files changed

+367
-4
lines changed

5 files changed

+367
-4
lines changed

src/jast/_jast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ def __init__(self, op: unaryop = None, operand: expr = None, *args, **kwargs):
12211221
if op is None:
12221222
raise ValueError("op is required for UnaryOp")
12231223
if operand is None:
1224-
raise ValueError("value is required for UnaryOp")
1224+
raise ValueError("operand is required for UnaryOp")
12251225
self.op = op
12261226
self.operand = operand
12271227

@@ -1240,7 +1240,7 @@ class PostOp(expr):
12401240
def __init__(self, operand: expr = None, op: postop = None, *args, **kwargs):
12411241
super().__init__(*args, **kwargs)
12421242
if operand is None:
1243-
raise ValueError("value is required for PostOp")
1243+
raise ValueError("operand is required for PostOp")
12441244
if op is None:
12451245
raise ValueError("op is required for PostOp")
12461246
self.operand = operand

src/jast/_parser/_convert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ def visitPostfixExpression(self, ctx: JavaParser.PostfixExpressionContext):
14311431
else:
14321432
op = jast.PostDec()
14331433
return jast.PostOp(
1434-
expression=self.visitPostfixExpression(ctx.postfixExpression()),
1434+
operand=self.visitPostfixExpression(ctx.postfixExpression()),
14351435
op=op,
14361436
level=15,
14371437
**self._get_location_rule(ctx),
@@ -1454,7 +1454,7 @@ def visitPrefixExpression(self, ctx: JavaParser.PrefixExpressionContext):
14541454
else:
14551455
op = jast.Not()
14561456
return jast.UnaryOp(
1457-
expression=self.visitPrefixExpression(ctx.prefixExpression()),
1457+
operand=self.visitPrefixExpression(ctx.prefixExpression()),
14581458
op=op,
14591459
level=14,
14601460
**self._get_location_rule(ctx),

tests/test_jast.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,165 @@ def test_guardedpattern_error(self):
548548
jast.guardedpattern,
549549
conditions=[jast.Constant(jast.BoolLiteral(True))],
550550
)
551+
552+
def test_Or(self):
553+
or_ = jast.Or()
554+
self.assertIsInstance(or_, jast.Or)
555+
self.assertIsInstance(or_, jast.JAST)
556+
self._test_iteration(or_)
557+
558+
def test_And(self):
559+
and_ = jast.And()
560+
self.assertIsInstance(and_, jast.And)
561+
self.assertIsInstance(and_, jast.JAST)
562+
self._test_iteration(and_)
563+
564+
def test_BitOr(self):
565+
bit_or = jast.BitOr()
566+
self.assertIsInstance(bit_or, jast.BitOr)
567+
self.assertIsInstance(bit_or, jast.JAST)
568+
self._test_iteration(bit_or)
569+
570+
def test_BitAnd(self):
571+
bit_and = jast.BitAnd()
572+
self.assertIsInstance(bit_and, jast.BitAnd)
573+
self.assertIsInstance(bit_and, jast.JAST)
574+
self._test_iteration(bit_and)
575+
576+
def test_BitXor(self):
577+
bit_xor = jast.BitXor()
578+
self.assertIsInstance(bit_xor, jast.BitXor)
579+
self.assertIsInstance(bit_xor, jast.JAST)
580+
self._test_iteration(bit_xor)
581+
582+
def test_Eq(self):
583+
equal = jast.Eq()
584+
self.assertIsInstance(equal, jast.Eq)
585+
self.assertIsInstance(equal, jast.JAST)
586+
self._test_iteration(equal)
587+
588+
def test_NotEq(self):
589+
not_eq = jast.NotEq()
590+
self.assertIsInstance(not_eq, jast.NotEq)
591+
self.assertIsInstance(not_eq, jast.JAST)
592+
self._test_iteration(not_eq)
593+
594+
def test_Lt(self):
595+
lt = jast.Lt()
596+
self.assertIsInstance(lt, jast.Lt)
597+
self.assertIsInstance(lt, jast.JAST)
598+
self._test_iteration(lt)
599+
600+
def test_LtE(self):
601+
lte = jast.LtE()
602+
self.assertIsInstance(lte, jast.LtE)
603+
self.assertIsInstance(lte, jast.JAST)
604+
self._test_iteration(lte)
605+
606+
def test_Gt(self):
607+
gt = jast.Gt()
608+
self.assertIsInstance(gt, jast.Gt)
609+
self.assertIsInstance(gt, jast.JAST)
610+
self._test_iteration(gt)
611+
612+
def test_GtE(self):
613+
gte = jast.GtE()
614+
self.assertIsInstance(gte, jast.GtE)
615+
self.assertIsInstance(gte, jast.JAST)
616+
self._test_iteration(gte)
617+
618+
def test_LShift(self):
619+
lshift = jast.LShift()
620+
self.assertIsInstance(lshift, jast.LShift)
621+
self.assertIsInstance(lshift, jast.JAST)
622+
self._test_iteration(lshift)
623+
624+
def test_RShift(self):
625+
rshift = jast.RShift()
626+
self.assertIsInstance(rshift, jast.RShift)
627+
self.assertIsInstance(rshift, jast.JAST)
628+
self._test_iteration(rshift)
629+
630+
def test_URShift(self):
631+
u_rshift = jast.URShift()
632+
self.assertIsInstance(u_rshift, jast.URShift)
633+
self.assertIsInstance(u_rshift, jast.JAST)
634+
self._test_iteration(u_rshift)
635+
636+
def test_Add(self):
637+
add = jast.Add()
638+
self.assertIsInstance(add, jast.Add)
639+
self.assertIsInstance(add, jast.JAST)
640+
self._test_iteration(add)
641+
642+
def test_Sub(self):
643+
sub = jast.Sub()
644+
self.assertIsInstance(sub, jast.Sub)
645+
self.assertIsInstance(sub, jast.JAST)
646+
self._test_iteration(sub)
647+
648+
def test_Mult(self):
649+
mult = jast.Mult()
650+
self.assertIsInstance(mult, jast.Mult)
651+
self.assertIsInstance(mult, jast.JAST)
652+
self._test_iteration(mult)
653+
654+
def test_Div(self):
655+
div = jast.Div()
656+
self.assertIsInstance(div, jast.Div)
657+
self.assertIsInstance(div, jast.JAST)
658+
self._test_iteration(div)
659+
660+
def test_Mod(self):
661+
mod = jast.Mod()
662+
self.assertIsInstance(mod, jast.Mod)
663+
self.assertIsInstance(mod, jast.JAST)
664+
self._test_iteration(mod)
665+
666+
def test_PreInc(self):
667+
pre_inc = jast.PreInc()
668+
self.assertIsInstance(pre_inc, jast.PreInc)
669+
self.assertIsInstance(pre_inc, jast.JAST)
670+
self._test_iteration(pre_inc)
671+
672+
def test_PreDec(self):
673+
pre_dec = jast.PreDec()
674+
self.assertIsInstance(pre_dec, jast.PreDec)
675+
self.assertIsInstance(pre_dec, jast.JAST)
676+
self._test_iteration(pre_dec)
677+
678+
def test_UAdd(self):
679+
u_add = jast.UAdd()
680+
self.assertIsInstance(u_add, jast.UAdd)
681+
self.assertIsInstance(u_add, jast.JAST)
682+
self._test_iteration(u_add)
683+
684+
def test_USub(self):
685+
u_sub = jast.USub()
686+
self.assertIsInstance(u_sub, jast.USub)
687+
self.assertIsInstance(u_sub, jast.JAST)
688+
self._test_iteration(u_sub)
689+
690+
def test_Invert(self):
691+
invert = jast.Invert()
692+
self.assertIsInstance(invert, jast.Invert)
693+
self.assertIsInstance(invert, jast.JAST)
694+
self._test_iteration(invert)
695+
696+
def test_Not(self):
697+
not_ = jast.Not()
698+
self.assertIsInstance(not_, jast.Not)
699+
self.assertIsInstance(not_, jast.JAST)
700+
self._test_iteration(not_)
701+
702+
def test_PostInc(self):
703+
post_inc = jast.PostInc()
704+
self.assertIsInstance(post_inc, jast.PostInc)
705+
self.assertIsInstance(post_inc, jast.JAST)
706+
self._test_iteration(post_inc)
707+
708+
def test_PostDec(self):
709+
post_dec = jast.PostDec()
710+
self.assertIsInstance(post_dec, jast.PostDec)
711+
self.assertIsInstance(post_dec, jast.JAST)
712+
self._test_iteration(post_dec)

tests/test_parse.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,96 @@ def test_guardedpattern(self):
467467
self.assertIsInstance(guardedpattern.conditions[1], jast.Constant)
468468
self.assertIsInstance(guardedpattern.conditions[1].value, jast.IntLiteral)
469469
self.assertEqual(42, guardedpattern.conditions[1].value.value)
470+
471+
def _test_operator(self, tree, operator):
472+
self.assertIsInstance(tree, jast.BinOp)
473+
self.assertIsInstance(tree.op, operator)
474+
475+
def _test_unaryop(self, tree, operator):
476+
self.assertIsInstance(tree, jast.UnaryOp)
477+
self.assertIsInstance(tree.op, operator)
478+
479+
def _test_postop(self, tree, operator):
480+
self.assertIsInstance(tree, jast.PostOp)
481+
self.assertIsInstance(tree.op, operator)
482+
483+
def test_Or(self):
484+
self._test_operator(jast.parse("x || y", jast.ParseMode.EXPR), jast.Or)
485+
486+
def test_And(self):
487+
self._test_operator(jast.parse("x && y", jast.ParseMode.EXPR), jast.And)
488+
489+
def test_BitOr(self):
490+
self._test_operator(jast.parse("x | y", jast.ParseMode.EXPR), jast.BitOr)
491+
492+
def test_BitXor(self):
493+
self._test_operator(jast.parse("x ^ y", jast.ParseMode.EXPR), jast.BitXor)
494+
495+
def test_BitAnd(self):
496+
self._test_operator(jast.parse("x & y", jast.ParseMode.EXPR), jast.BitAnd)
497+
498+
def test_Eq(self):
499+
self._test_operator(jast.parse("x == y", jast.ParseMode.EXPR), jast.Eq)
500+
501+
def test_NotEq(self):
502+
self._test_operator(jast.parse("x != y", jast.ParseMode.EXPR), jast.NotEq)
503+
504+
def test_Lt(self):
505+
self._test_operator(jast.parse("x < y", jast.ParseMode.EXPR), jast.Lt)
506+
507+
def test_LtE(self):
508+
self._test_operator(jast.parse("x <= y", jast.ParseMode.EXPR), jast.LtE)
509+
510+
def test_Gt(self):
511+
self._test_operator(jast.parse("x > y", jast.ParseMode.EXPR), jast.Gt)
512+
513+
def test_GtE(self):
514+
self._test_operator(jast.parse("x >= y", jast.ParseMode.EXPR), jast.GtE)
515+
516+
def test_LShift(self):
517+
self._test_operator(jast.parse("x << y", jast.ParseMode.EXPR), jast.LShift)
518+
519+
def test_RShift(self):
520+
self._test_operator(jast.parse("x >> y", jast.ParseMode.EXPR), jast.RShift)
521+
522+
def test_URShift(self):
523+
self._test_operator(jast.parse("x >>> y", jast.ParseMode.EXPR), jast.URShift)
524+
525+
def test_Add(self):
526+
self._test_operator(jast.parse("x + y", jast.ParseMode.EXPR), jast.Add)
527+
528+
def test_Sub(self):
529+
self._test_operator(jast.parse("x - y", jast.ParseMode.EXPR), jast.Sub)
530+
531+
def test_Mult(self):
532+
self._test_operator(jast.parse("x * y", jast.ParseMode.EXPR), jast.Mult)
533+
534+
def test_Div(self):
535+
self._test_operator(jast.parse("x / y", jast.ParseMode.EXPR), jast.Div)
536+
537+
def test_Mod(self):
538+
self._test_operator(jast.parse("x % y", jast.ParseMode.EXPR), jast.Mod)
539+
540+
def test_PreInc(self):
541+
self._test_unaryop(jast.parse("++x", jast.ParseMode.EXPR), jast.PreInc)
542+
543+
def test_PreDec(self):
544+
self._test_unaryop(jast.parse("--x", jast.ParseMode.EXPR), jast.PreDec)
545+
546+
def test_UAdd(self):
547+
self._test_unaryop(jast.parse("+x", jast.ParseMode.EXPR), jast.UAdd)
548+
549+
def test_USub(self):
550+
self._test_unaryop(jast.parse("-x", jast.ParseMode.EXPR), jast.USub)
551+
552+
def test_Invert(self):
553+
self._test_unaryop(jast.parse("~x", jast.ParseMode.EXPR), jast.Invert)
554+
555+
def test_Not(self):
556+
self._test_unaryop(jast.parse("!x", jast.ParseMode.EXPR), jast.Not)
557+
558+
def test_PostInc(self):
559+
self._test_postop(jast.parse("x++", jast.ParseMode.EXPR), jast.PostInc)
560+
561+
def test_PostDec(self):
562+
self._test_postop(jast.parse("x--", jast.ParseMode.EXPR), jast.PostDec)

0 commit comments

Comments
 (0)