main.cpp 4.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
22
23
24
25
26

#if HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "Tester.h"
28

29
static std::string TestDirectory = SRC_DIR "/test/samples/";
30
bool Verbose = false;
31
bool ExtraVerbose = false;
32

33
static const char* testCases[] = {
34
    "featuring",
35
    "parse_video",
36
    "parse_audio",
37
    "same_album_name_different_artist",
38
    "same_album_name_same_artist",
39
    "compilation",
40
    "compilation_no_albumartist",
41
    "release_year_same",
42
43
    "notags",
    "multi_cd",
44
    "no_album_artist",
45
    "utf8",
46
47
48
49
50
51
52
53
54
55
56
57
58
59
};

class TestEnv : public ::testing::Environment
{
    public:
        virtual void SetUp()
        {
            // Always clean the DB in case a previous test crashed
            unlink("test.db");
        }
};

TEST_P( Tests, Parse )
{
60
    auto casePath = TestDirectory + "testcases/" + GetParam() + ".json";
61
62
63
64
65
66
67
68
69
70
71
72
73
    std::unique_ptr<FILE, int(*)(FILE*)> f( fopen( casePath.c_str(), "rb" ), &fclose );
    ASSERT_NE( nullptr, f );
    char buff[65536]; // That's how ugly I am!
    auto ret = fread( buff, sizeof(buff[0]), sizeof(buff), f.get() );
    ASSERT_NE( 0u, ret );
    buff[ret] = 0;
    rapidjson::Document doc;
    doc.Parse( buff );

    ASSERT_TRUE( doc.HasMember( "input" ) );
    const auto& input = doc["input"];
    for ( auto i = 0u; i < input.Size(); ++i )
    {
74
        // Quick and dirty check to ensure we're discovering something that exists
75
        auto samplesDir = TestDirectory + "samples/" + input[i].GetString();
76
77
        struct stat s;
        auto res = stat( samplesDir.c_str(), &s );
78
        ASSERT_EQ( 0, res );
79
80

        m_ml->discover( samplesDir );
81
    }
82
83
84
85
86
87
88
89
90
91
    ASSERT_TRUE( m_cb->waitForParsingComplete() );

    if ( doc.HasMember( "expected" ) == false )
    {
        // That's a lousy test case with no assumptions, but ok.
        return;
    }
    const auto& expected = doc["expected"];

    if ( expected.HasMember( "albums" ) == true )
92
    {
93
        checkAlbums( expected["albums" ], m_ml->albums( SortingCriteria::Default, false ) );
94
    }
95
96
97
98
    if ( expected.HasMember( "media" ) == true )
        checkMedias( expected["media"] );
    if ( expected.HasMember( "nbVideos" ) == true )
    {
99
        const auto videos = m_ml->videoFiles( SortingCriteria::Default, false );
100
101
102
103
        ASSERT_EQ( expected["nbVideos"].GetUint(), videos.size() );
    }
    if ( expected.HasMember( "nbAudios" ) == true )
    {
104
        const auto audios = m_ml->audioFiles( SortingCriteria::Default, false );
105
106
        ASSERT_EQ( expected["nbAudios"].GetUint(), audios.size() );
    }
107
108
    if ( expected.HasMember( "artists" ) )
    {
109
        checkArtists( expected["artists"], m_ml->artists( SortingCriteria::Default, false ) );
110
    }
111
112
}

113
114
115
int main(int ac, char** av)
{
    ::testing::InitGoogleTest(&ac, av);
116
    const std::string verboseArg = "-v";
117
    const std::string extraVerboseArg = "-vv";
118
119
    for ( auto i = 1; i < ac; ++i )
    {
120
        if ( av[i] == verboseArg )
121
            Verbose = true;
122
123
        else if ( av[i] == extraVerboseArg )
            ExtraVerbose = true;
124
125
126
127
    }
    return RUN_ALL_TESTS();
}

128
INSTANTIATE_TEST_CASE_P(SamplesTests, Tests,
129
                        ::testing::ValuesIn(testCases) );
130
131

::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new TestEnv);