#!/usr/bin/env python
# Copyright 2016 Andy Chu. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
"""
cmd_exec_test.py: Tests for cmd_exec.py
"""

import unittest

from core import test_lib
from core.meta import syntax_asdl, Id
from osh import state

suffix_op = syntax_asdl.suffix_op
osh_word = syntax_asdl.word
word_part = syntax_asdl.word_part


def InitEvaluator():
  word_ev = test_lib.MakeTestEvaluator()
  state.SetLocalString(word_ev.mem, 'x', 'xxx')
  state.SetLocalString(word_ev.mem, 'y', 'yyy')
  return word_ev


class ExpansionTest(unittest.TestCase):

  def testBraceExpand(self):
    arena = test_lib.MakeArena('<cmd_exec_test.py>')
    c_parser = test_lib.InitCommandParser('echo _{a,b}_', arena=arena)
    node = c_parser._ParseCommandLine()
    print(node)

    ex = test_lib.InitExecutor(arena=arena)
    #print(ex.Execute(node))
    #print(ex._ExpandWords(node.words))


class VarOpTest(unittest.TestCase):

  def testVarOps(self):
    ev = InitEvaluator()  # initializes x=xxx and y=yyy
    unset_sub = word_part.BracedVarSub(syntax_asdl.token(Id.VSub_Name, 'unset'))
    part_vals = []
    ev._EvalWordPart(unset_sub, part_vals)
    print(part_vals)

    set_sub = word_part.BracedVarSub(syntax_asdl.token(Id.VSub_Name, 'x'))
    part_vals = []
    ev._EvalWordPart(set_sub, part_vals)
    print(part_vals)

    # Now add some ops
    part = word_part.LiteralPart(syntax_asdl.token(Id.Lit_Chars, 'default'))
    arg_word = osh_word.CompoundWord([part])
    test_op = suffix_op.StringUnary(Id.VTest_ColonHyphen, arg_word)
    unset_sub.suffix_op = test_op
    set_sub.suffix_op = test_op

    part_vals = []
    ev._EvalWordPart(unset_sub, part_vals)
    print(part_vals)

    part_vals = []
    ev._EvalWordPart(set_sub, part_vals)
    print(part_vals)


if __name__ == '__main__':
  unittest.main()

[((578, 606), 'core.test_lib.MakeTestEvaluator', 'test_lib.MakeTestEvaluator', ([], {}), '()\n', (604, 606), False, 'from core import test_lib\n'), ((609, 654), 'osh.state.SetLocalString', 'state.SetLocalString', (['word_ev.mem', '"""x"""', '"""xxx"""'], {}), "(word_ev.mem, 'x', 'xxx')\n", (629, 654), False, 'from osh import state\n'), ((657, 702), 'osh.state.SetLocalString', 'state.SetLocalString', (['word_ev.mem', '"""y"""', '"""yyy"""'], {}), "(word_ev.mem, 'y', 'yyy')\n", (677, 702), False, 'from osh import state\n'), ((2003, 2018), 'unittest.main', 'unittest.main', ([], {}), '()\n', (2016, 2018), False, 'import unittest\n'), ((804, 844), 'core.test_lib.MakeArena', 'test_lib.MakeArena', (['"""<cmd_exec_test.py>"""'], {}), "('<cmd_exec_test.py>')\n", (822, 844), False, 'from core import test_lib\n'), ((860, 915), 'core.test_lib.InitCommandParser', 'test_lib.InitCommandParser', (['"""echo _{a,b}_"""'], {'arena': 'arena'}), "('echo _{a,b}_', arena=arena)\n", (886, 915), False, 'from core import test_lib\n'), ((982, 1016), 'core.test_lib.InitExecutor', 'test_lib.InitExecutor', ([], {'arena': 'arena'}), '(arena=arena)\n', (1003, 1016), False, 'from core import test_lib\n'), ((1244, 1284), 'core.meta.syntax_asdl.token', 'syntax_asdl.token', (['Id.VSub_Name', '"""unset"""'], {}), "(Id.VSub_Name, 'unset')\n", (1261, 1284), False, 'from core.meta import syntax_asdl, Id\n'), ((1407, 1443), 'core.meta.syntax_asdl.token', 'syntax_asdl.token', (['Id.VSub_Name', '"""x"""'], {}), "(Id.VSub_Name, 'x')\n", (1424, 1443), False, 'from core.meta import syntax_asdl, Id\n'), ((1583, 1625), 'core.meta.syntax_asdl.token', 'syntax_asdl.token', (['Id.Lit_Chars', '"""default"""'], {}), "(Id.Lit_Chars, 'default')\n", (1600, 1625), False, 'from core.meta import syntax_asdl, Id\n')]