
/*
 *  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*/
package test;

import groovy.lang.GroovyShell;
import junit.framework.Assert;
import oracle.sample.StringUtils;
import org.junit.Test;

import java.io.File;

/**
 * <p>
 * </p>
 *
 * @author velsubra
 */
public class TesterTest {

    @Test
    public void testParseSimple() {
        String script = StringUtils.expressionToCall("unix", "ls");
        System.out.println(script);
        Assert.assertEquals("unix.ls()", script);

    }

    @Test
    public void testParseWithOptions() {
        String script = StringUtils.expressionToCall("unix", "ls -ltr");
        System.out.println(script);
        Assert.assertEquals("unix.ls(Ls.Options.t.r.l)", script);

    }

    @Test
    public void testParseWithArgs() {
        String script = StringUtils.expressionToCall("unix", "ls test");
        System.out.println(script);
        Assert.assertEquals("unix.ls(\"test\")", script);

    }

    @Test
    public void testParseWithArgsWithSpace() {
        String script = StringUtils.expressionToCall("unix", "ls \"test dir\"");
        System.out.println(script);
        Assert.assertEquals("unix.ls(\"test dir\")", script);

    }

    @Test
    public void testParseWithArgsWithOptions() {
        String script = StringUtils.expressionToCall("unix", "ls  -tr \"test dir\"");
        System.out.println(script);
        Assert.assertEquals("unix.ls(Ls.Options.t.r,\"test dir\")", script);

    }

    @Test
    public void testParseWithPipe() {
        String script = StringUtils.expressionToCall("unix", "ls | wc -l");
        System.out.println(script);
        Assert.assertEquals("unix.ls().wc(Wc.Options.l)", script);

    }

    @Test
    public void testParseWithPipeandArgsEscape() {
        String script = StringUtils.expressionToCall("unix", "ls \"|\" | wc -l");
        System.out.println(script);
        Assert.assertEquals("unix.ls(\"|\").wc(Wc.Options.l)", script);

    }

    @Test
    public void testParseWithPipeandArgs() {
        String script = StringUtils.expressionToCall("unix", "ls test | wc -l");
        System.out.println(script);
        Assert.assertEquals("unix.ls(\"test\").wc(Wc.Options.l)", script);

    }

    @Test
     public void testCD() {
        String script = StringUtils.commandToScript("cd", new File(".").getAbsolutePath());
        System.out.println(script);
        GroovyShell engine = new GroovyShell();
        engine.evaluate(script);
        String pwd = engine.getContext().getVariable("pwd").toString();
        Assert.assertEquals(pwd, System.getProperty("user.home"));

    }

    @Test
    public void testCDParent() throws  Exception{
        String script = StringUtils.commandToScript("cd ..", new File(".").getAbsolutePath());
        System.out.println(script);
        GroovyShell engine = new GroovyShell();
        engine.evaluate(script);
        String pwd = engine.getContext().getVariable("pwd").toString();

        Assert.assertEquals(pwd,  new File(".").getCanonicalFile().getParentFile().getCanonicalPath());

    }

    @Test
    public void testLSltr() throws  Exception{
        String script = StringUtils.commandToScript("ls -ltr", new File(".").getCanonicalPath());
        System.out.println(script);
        GroovyShell engine = new GroovyShell();
        String res = engine.evaluate(script).toString();
        System.out.println(res);


    }
}
