| 1 | #!/usr/bin/env python2
 | 
| 2 | # Copyright 2016 Andy Chu. All rights reserved.
 | 
| 3 | # Licensed under the Apache License, Version 2.0 (the "License");
 | 
| 4 | # you may not use this file except in compliance with the License.
 | 
| 5 | # You may obtain a copy of the License at
 | 
| 6 | #
 | 
| 7 | #   http://www.apache.org/licenses/LICENSE-2.0
 | 
| 8 | """
 | 
| 9 | cmd_eval_test.py: Tests for cmd_eval.py
 | 
| 10 | """
 | 
| 11 | 
 | 
| 12 | import unittest
 | 
| 13 | 
 | 
| 14 | from _devbuild.gen.id_kind_asdl import Id
 | 
| 15 | from _devbuild.gen.syntax_asdl import (BracedVarSub, suffix_op, CompoundWord)
 | 
| 16 | from core import test_lib
 | 
| 17 | from frontend.lexer import DummyToken as Tok
 | 
| 18 | 
 | 
| 19 | 
 | 
| 20 | def InitEvaluator():
 | 
| 21 |     word_ev = test_lib.InitWordEvaluator()
 | 
| 22 |     test_lib.SetLocalString(word_ev.mem, 'x', 'xxx')
 | 
| 23 |     test_lib.SetLocalString(word_ev.mem, 'y', 'yyy')
 | 
| 24 |     return word_ev
 | 
| 25 | 
 | 
| 26 | 
 | 
| 27 | class ExpansionTest(unittest.TestCase):
 | 
| 28 | 
 | 
| 29 |     def testBraceExpand(self):
 | 
| 30 |         arena = test_lib.MakeArena('<cmd_eval_test.py>')
 | 
| 31 |         c_parser = test_lib.InitCommandParser('echo _{a,b}_', arena=arena)
 | 
| 32 |         node = c_parser._ParseCommandLine()
 | 
| 33 |         print(node)
 | 
| 34 | 
 | 
| 35 |         cmd_ev = test_lib.InitCommandEvaluator(arena=arena)
 | 
| 36 |         #print(cmd_ev.Execute(node))
 | 
| 37 |         #print(cmd_ev._ExpandWords(node.words))
 | 
| 38 | 
 | 
| 39 | 
 | 
| 40 | class VarOpTest(unittest.TestCase):
 | 
| 41 | 
 | 
| 42 |     def testVarOps(self):
 | 
| 43 |         ev = InitEvaluator()  # initializes x=xxx and y=yyy
 | 
| 44 |         left = None
 | 
| 45 |         unset_sub = BracedVarSub.CreateNull(alloc_lists=True)
 | 
| 46 |         unset_sub.left = left
 | 
| 47 |         unset_sub.token = Tok(Id.VSub_Name, 'unset')
 | 
| 48 |         unset_sub.var_name = 'unset'
 | 
| 49 | 
 | 
| 50 |         part_vals = []
 | 
| 51 |         ev._EvalWordPart(unset_sub, part_vals, 0)
 | 
| 52 |         print(part_vals)
 | 
| 53 | 
 | 
| 54 |         set_sub = BracedVarSub.CreateNull(alloc_lists=True)
 | 
| 55 |         set_sub.left = left
 | 
| 56 |         set_sub.token = Tok(Id.VSub_Name, 'x')
 | 
| 57 |         set_sub.var_name = 'x'
 | 
| 58 | 
 | 
| 59 |         part_vals = []
 | 
| 60 |         ev._EvalWordPart(set_sub, part_vals, 0)
 | 
| 61 |         print(part_vals)
 | 
| 62 | 
 | 
| 63 |         # Now add some ops
 | 
| 64 |         part = Tok(Id.Lit_Chars, 'default')
 | 
| 65 |         arg_word = CompoundWord([part])
 | 
| 66 |         op_tok = Tok(Id.VTest_ColonHyphen, ':-')
 | 
| 67 |         test_op = suffix_op.Unary(op_tok, arg_word)
 | 
| 68 |         unset_sub.suffix_op = test_op
 | 
| 69 |         set_sub.suffix_op = test_op
 | 
| 70 | 
 | 
| 71 |         part_vals = []
 | 
| 72 |         ev._EvalWordPart(unset_sub, part_vals, 0)
 | 
| 73 |         print(part_vals)
 | 
| 74 | 
 | 
| 75 |         part_vals = []
 | 
| 76 |         ev._EvalWordPart(set_sub, part_vals, 0)
 | 
| 77 |         print(part_vals)
 | 
| 78 | 
 | 
| 79 | 
 | 
| 80 | if __name__ == '__main__':
 | 
| 81 |     unittest.main()
 |