//
//  OmFileTests.cpp
//  OmUtil
//
//  Created by David Van Brink on 3/31/15.
//  Copyright (c) 2015 omino.com. All rights reserved.
//

#include "OmAsserts.h"
#include "Om.h"
#include <stdlib.h>
#include "OmFileTests.h"

OMTEST(testReadWriteFile)
{
    std::string fileName = "tempUnitTestFile.txt";
    std::string contents = "the value is ";
    contents += rr(0,100);
    
    OmFile::writeFile(fileName, contents);
    std::string contentsRead = OmFile::readFile(fileName);
    ASSERT_EQUALS_STRING("read it back?", contents, contentsRead);
    
    bool b1 = OmFile::fileExists(fileName);
    bool b2 = OmFile::fileExists("blahdeblloop.krkk");
    
    ASSERT_TRUE("file exists", b1);
    ASSERT_FALSE("file exists", b2);
}

OMTEST(testPaths)
{
    ASSERT_EQUALS_STRING("path", "/the/dir/", OmFile::getDirectory("/the/dir/foo.txt"));
    ASSERT_EQUALS_STRING("path", "/the/", OmFile::getParent("/the/dir/foo.txt"));
    ASSERT_EQUALS_STRING("path", "/the/", OmFile::getParent("/the/dir/"));
    
    ASSERT_EQUALS_STRING("tidy", "/the/dir/subdir/foo.txt", OmFile::tidy("/the/dir/subdir/foo.txt"));
    ASSERT_EQUALS_STRING("tidy", "/the/dir/subdir/foo.txt", OmFile::tidy("/the/dir//subdir/foo.txt"));
    ASSERT_EQUALS_STRING("tidy", "/the/dir/subdir/foo.txt", OmFile::tidy("/./the/dir//.//./subdir/foo.txt"));
    ASSERT_EQUALS_STRING("tidy", "./foo/bar.xml", OmFile::tidy("./foo/bar.xml"));
    ASSERT_EQUALS_STRING("tidy", "/the/dir/subdir/foo.txt", OmFile::tidy("/the/dir/wrongway/./../subdir/./foo.txt"));
    ASSERT_EQUALS_STRING("tidy", "/pathos/baz.json", OmFile::tidy("/pathos/a/./b/c/d/e/../../..////./././//../../baz.json"));
    ASSERT_EQUALS_STRING("tidy", "./pathos/baz.json", OmFile::tidy("goo/gar/../../pathos/a/./b/c/d/e/../../..////./././//../../baz.json"));
    ASSERT_EQUALS_STRING("tidy", "./pathos/", OmFile::tidy("goo/gar/../../pathos/a/./b/c/d/e/../../..////./././//../../"));
    ASSERT_EQUALS_STRING("tidy", "./pathos/", OmFile::tidy("goo/gar/../../pathos/a/./b/c/d/e/../../..////./././//../.."));

    ASSERT_EQUALS_STRING("tidy", "C:/pathos/foo.txt", OmFile::tidy("c:\\pathos\\x/y/..\\..\\foo.txt"));
}

static std::string rrS()
{
    std::string s = ssprintf("%g", rr(0,1));
    return s;
}


OMTEST(testIncludes)
{
    std::string rr1 = rrS();
    std::string rr2 = rrS();
    
    std::string s1 = "abc\n#include \"file2.txt\"\nghi\n" + rr1 + "\n";
    OmFile::writeFile("file1.txt", s1);
    
    std::string s2 = "def " + rr2 + "\n";
    OmFile::writeFile("file2.txt", s2);
    
    std::vector<std::string> missing;
    std::string contents = OmFile::readFileWithIncludes("file1.txt", missing);
    
    std::string expected = "abc\ndef " + rr2 + "\n\nghi\n" + rr1 + "\n\n";
    ASSERT_EQUALS_STRING("includes", expected, contents);
}

OMTEST(testGetName)
{
    ASSERT_EQUALS_STRING("getName", "foo", OmFile::getName("foo"));

    ASSERT_EQUALS_STRING("getName", "foo.txt", OmFile::getName("foo.txt"));
    ASSERT_EQUALS_STRING("getName", "foo", OmFile::getName("foo.txt", true));

    ASSERT_EQUALS_STRING("getName", "bar.foo.txt", OmFile::getName("bar.foo.txt"));
    ASSERT_EQUALS_STRING("getName", "bar.foo", OmFile::getName("bar.foo.txt", true));

    ASSERT_EQUALS_STRING("getName", "foo", OmFile::getName("./foo"));
    ASSERT_EQUALS_STRING("getName", "foo.txt", OmFile::getName("./foo.txt"));
    ASSERT_EQUALS_STRING("getName", "foo", OmFile::getName("./foo.txt", true));

    ASSERT_EQUALS_STRING("getName", "boo.foo.txt", OmFile::getName("./boo.foo.txt"));
    ASSERT_EQUALS_STRING("getName", "boo.foo", OmFile::getName("./boo.foo.txt", true));
}

OMTEST(testJoin)
{
    ASSERT_EQUALS_STRING("join", "/a/b/c", OmFile::join("/a/b/", "c"));
    ASSERT_EQUALS_STRING("join", "/a/c", OmFile::join("/a/b.txt", "c")); // no trailing slash means take the directory the plain file is in
    ASSERT_EQUALS_STRING("join", "./c", OmFile::join("", "c")); // no trailing slash means take the directory the plain file is in
    ASSERT_EQUALS_STRING("join", "./c", OmFile::join("", "./c")); // no trailing slash means take the directory the plain file is in
    ASSERT_EQUALS_STRING("join", "./c", OmFile::join("././", "./c")); // no trailing slash means take the directory the plain file is in
    
    ASSERT_EQUALS_STRING("join", "/d/c", OmFile::join("/a/b/", "/d/c")); // already absolute
    ASSERT_EQUALS_STRING("join", "A:/d/c/b.txt", OmFile::join("A:\\d/", "c/b.txt")); // already absolute
    ASSERT_EQUALS_STRING("join", "A:/d/c/b.txt", OmFile::join("A:/d/", "c/b.txt")); // already absolute
    ASSERT_EQUALS_STRING("join", "B:/c/b.txt", OmFile::join("A:\\d/", "B:\\c/b.txt")); // already absolute
    ASSERT_EQUALS_STRING("join", "B:/c/b.txt", OmFile::join("A:\\d/", "B:/c/b.txt")); // already absolute
}

void doFileTests()
{
    testReadWriteFile();
    testPaths();
    testIncludes();
    testGetName();
    testJoin();
    
    OMTESTEND();
}
