//
//  OmCommandLineTests.cpp
//  OmUtil
//
//  Created by David Van Brink on 1/8/17.
//  Copyright (c) 2017 omino.com. All rights reserved.
//

#include "OmCommandLineTests.h"
#include "OmCommandLine.h"
#include "OmAsserts.h"
void commandLineTests1()
{
    OmCommandLine ocl;
    
    ocl.addSwitch("foo", "just a foo tool");
    std::string h = ocl.getHelp();
    
    ASSERT_NOT_EQUALS_INT("blank?", 0, h.length());
    printf("%s", h.c_str());
}

void commandLineTests2()
{
    char *args[] = {"--a=fish", "--b=dog"};
    OmCommandLine cl;
    
    cl.setName("Example Tool");
    cl.addDescription("this is the first paragraph. "
                      "you can do a lot of things with it"
                      " and. ssssss ssssss ssssssssssss ssssss ssssssssssss ssssss ssssssssssss. ssssss ssssss ssssss.");
    
    cl.addSwitch("source", "the incoming thingie would be good here").setKind(OSK_File);
    cl.addSwitch("width", "output width in pixies", false).setKind(OSK_Int);
    cl.addSwitch("bolo", "this should be on or off").setKind(OSK_Bool);
    std::string help = cl.getHelp();
    
    cl.setArgs(2, args);
    
    std::string v = cl.validationAndHelp();
    printf("%s", v.c_str());
    
    cl.setArgs(std::vector<std::string>{"--a=1","--b=-2","--c=23d4"});
    ASSERT_EQUALS_INT("getValueInt", +1, cl.getValueInt("a"));
    ASSERT_EQUALS_INT("getValueInt", -2, cl.getValueInt("b"));
    ASSERT_EQUALS_INT("getValueInt", 23, cl.getValueInt("c"));
    
    ASSERT_TRUE("has arg", cl.hasArg("a"));
    ASSERT_FALSE("has arg", cl.hasArg("z"));
    
    
    cl.addCredit("Made in USA.");
    cl.addCredit("Uses Fish from http://fishie.com.");
    cl.setArgs("");
    std::string a = cl.validationAndHelp();
    printf("\n%s\n", a.c_str());
    
    cl = OmCommandLine();
    cl.setArgs("--bolo=ya");
    printf("%s", cl.validationAndHelp().c_str());
    
    cl.setArgs("--bolo=yes");
    printf("%s", cl.validationAndHelp().c_str());
    printf("bolo = %d\n", cl.getValueBool("bolo"));
    
    cl.setArgs("--bolo=no");
    printf("%s", cl.validationAndHelp().c_str());
    printf("bolo = %d\n", cl.getValueBool("bolo"));
    
    cl.setArgs("--bolo");
    printf("%s", cl.validationAndHelp().c_str());
    printf("bolo = %d\n", cl.getValueBool("bolo"));
    
    cl.addSwitch("f", "a float").setKind(OSK_Float);
}

OMTEST(testCommandLineSurpriseArgsError)
{
    OmCommandLine cl;
    
    cl.addSwitch("source", "the incoming thingie would be good here",false).setKind(OSK_File);
    cl.addSwitch("width", "output width in pixies", false).setKind(OSK_Int);
    cl.addSwitch("bolo", "this should be on or off",false).setKind(OSK_Bool);
    
    cl.setArgs(std::vector<std::string>{"--a=1","--b=-2","--c=23d4"});
    std::string s;
    
    s = cl.validationAndHelp();
    ASSERT_EQUALS_STRING("surprise params AOK", "", s);
    
    cl.setReportSurpriseParams(true);
    
    s = cl.validationAndHelp();
    ASSERT_TRUE("surprise params not ok", s.length() > 10);
    
    cl.setArgs(std::vector<std::string>{"--bolo=yes"});
    s = cl.validationAndHelp();
    ASSERT_EQUALS_STRING("surprise params AOK", "", s);
}


void allOmCommandLineTests()
{
    commandLineTests1();
    commandLineTests2();
    testCommandLineSurpriseArgsError();
}