Example #1
0
 def test_unidirectional(self):
     proc = ArgProc()
     proc.rule('$left => $right')
     assert proc.process({'left': 10}) == {'right': 10}
     assert proc.reverse({'right': 10}) == {}
     proc = ArgProc()
     proc.rule('$left <= $right')
     assert proc.process({'left': 10}) == {}
     assert proc.reverse({'right': 10}) == {'left': 10}
Example #2
0
 def test_negated_tag(self):
     proc = ArgProc()
     proc.rule('$left => $right @!tag')
     assert proc.process({'left': 10}) == {'right': 10}
     proc = ArgProc(tags=['tg'])
     proc.rule('$left => $right @!tag')
     assert proc.process({'left': 10}) == {'right': 10}
     proc = ArgProc(tags=['tag'])
     proc.rule('$left => $right @!tag')
     assert proc.process({'left': 10}) == {}
Example #3
0
 def test_multiple_tags(self):
     proc = ArgProc(tags=['tag2'])
     proc.rule('$left => $right @tag1,@tag2')
     assert proc.process({'left': 10}) == {'right': 10}
     proc = ArgProc(tags=[])
     proc.rule('$left => $right @tag1,@tag2')
     assert proc.process({'left': 10}) == {}
     proc = ArgProc()
     proc.rule('$left => $right @tag1,@tag2')
     assert proc.process({'left': 10}) == {'right': 10}
Example #4
0
 def test_global_name(self):
     global verify
     verify = 'test'
     proc = ArgProc()
     proc.rule('$left:verify => $right *')
     assert proc.process({'left': 'test'}) == {'right': 'test'}
     assert_raises(Error, proc.process, {'left': 'value'})
Example #5
0
 def test_simple(self):
     proc = ArgProc()
     proc.rule('$left <=> $right')
     assert proc.process({'left': 10}) == {'right': 10}
     assert proc.process({'right': 10}) == {}
     assert proc.reverse({'right': 10}) == {'left': 10}
     assert proc.reverse({'left': 10}) == {}
Example #6
0
 def test_comment(self):
     proc = ArgProc()
     proc.rules("""
             $left1 => $right1  # comment
             #$left2 => $right2
     """)
     assert proc.process({'left1': 10}) == {'right1': 10}
     assert proc.process({'left2': 20}) == {}
Example #7
0
 def test_multiple_rules(self):
     proc = ArgProc()
     proc.rules("""
         $left1 <=> $right1
         $left2 <=> $right2
         """)
     right = proc.process({'left1': 1, 'left2': 2})
     assert right == {'right1': 1, 'right2': 2}
Example #8
0
 def test_optional(self):
     proc = ArgProc()
     proc.rule('$left <=> $right')
     assert proc.process({}) == {}
Example #9
0
 def test_mandatory(self):
     proc = ArgProc()
     proc.rule('$left <=> $right *')
     assert_raises(Error, proc.process, {})
Example #10
0
 def test_attribute_reference(self):
     proc = ArgProc()
     proc.rule('int.__class__ => $right')
     assert proc.process({}) == {'right': type}
Example #11
0
 def test_float(self):
     proc = ArgProc()
     proc.rule('3.14 => $right')
     assert proc.process({}) == {'right': 3.14}
Example #12
0
 def test_false(self):
     proc = ArgProc()
     proc.rule('False => $right')
     assert proc.process({}) == {'right': False}
Example #13
0
 def test_none(self):
     proc = ArgProc()
     proc.rule('None => $right')
     assert proc.process({}) == {'right': None}
Example #14
0
 def test_validator_function_call(self):
     proc = ArgProc()
     proc.rule('$left:set((1,2)) => $right')
     assert proc.process({'left': 1}) == {'right': 1}
     assert_raises(Error, proc.process, {'left': 3})
Example #15
0
 def test_validator_list(self):
     proc = ArgProc()
     proc.rule('$left:[1,2,3] => $right')
     assert proc.process({'left': 2}) == {'right': 2}
     assert_raises(Error, proc.process, {'left': 4})
Example #16
0
 def test_validator_literal(self):
     proc = ArgProc()
     proc.rule('$left:"value" => $right')
     assert proc.process({'left': 'value'}) == {'right': 'value'}
     assert_raises(Error, proc.process, {'left': 'val'})
Example #17
0
 def test_validator_callable(self):
     proc = ArgProc()
     proc.rule('$left:int => $right')
     assert proc.process({'left': 10}) == {'right': 10}
     assert_raises(Error, proc.process, {'left': '10a'})
Example #18
0
 def test_slicing(self):
     proc = ArgProc()
     proc.rule('[1,2,3][1:2] => $right')
     assert proc.process({}) == {'right': [2]}
Example #19
0
 def test_subscription(self):
     proc = ArgProc()
     proc.rule('[1,2,3][1] => $right')
     assert proc.process({}) == {'right': 2}
Example #20
0
 def test_function_with_validator(self):
     proc = ArgProc()
     proc.rule('int($left:int) => $right')
     assert proc.process({'left': 1}) == {'right': 1}
Example #21
0
 def test_dict(self):
     proc = ArgProc()
     proc.rule('{1:2, 3:4} => $right')
     assert proc.process({}) == {'right': {1: 2, 3: 4}}
Example #22
0
 def test_str_double_quoted(self):
     proc = ArgProc()
     proc.rule('"test" => $right')
     assert proc.process({}) == {'right': 'test'}
Example #23
0
 def test_true(self):
     proc = ArgProc()
     proc.rule('True => $right')
     assert proc.process({}) == {'right': True}
Example #24
0
 def test_list(self):
     proc = ArgProc()
     proc.rule('[1,2,3] => $right')
     assert proc.process({}) == {'right': [1, 2, 3]}
Example #25
0
 def test_int(self):
     proc = ArgProc()
     proc.rule('10 => $right')
     assert proc.process({}) == {'right': 10}
Example #26
0
 def test_function_call(self):
     proc = ArgProc()
     proc.rule('int($left) => $right')
     assert proc.process({'left': '10'}) == {'right': 10}
Example #27
0
 def test_str_single_quoted(self):
     proc = ArgProc()
     proc.rule('\'test\' => $right')
     assert proc.process({}) == {'right': 'test'}
Example #28
0
 def test_function_call_with_multiple_arguments(self):
     proc = ArgProc()
     proc.rule('max($left, 2) => $right')
     assert proc.process({'left': 1}) == {'right': 2}
Example #29
0
 def test_tuple(self):
     proc = ArgProc()
     proc.rule('(1,2,3) => $right')
     assert proc.process({}) == {'right': (1, 2, 3)}
Example #30
0
 def test_tuple_with_one_entry(self):
     proc = ArgProc()
     proc.rule('(1,) => $right')
     assert proc.process({}) == {'right': (1, )}