File: TestExpression.cpp

package info (click to toggle)
amarok 3.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,168 kB
  • sloc: cpp: 195,056; xml: 4,322; ansic: 2,634; javascript: 673; ruby: 528; python: 507; sh: 252; makefile: 12
file content (150 lines) | stat: -rw-r--r-- 6,084 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/***************************************************************************
 *   Copyright (c) 2009 Sven Krohlas <sven@asbest-online.de>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU 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.         *
 ***************************************************************************/

#include "TestExpression.h"

#include "core/support/Debug.h"
#include "core-impl/collections/support/Expression.h"

#include <QTest>

QTEST_GUILESS_MAIN( TestExpression )

//required for Debug.h
QRecursiveMutex Debug::mutex;

TestExpression::TestExpression()
{
}

void TestExpression::testParse()
{
    ParsedExpression result;
    expression_element element;
    result = ExpressionParser::parse( QStringLiteral("") );
    QCOMPARE( result.isEmpty(), true );

    result = ExpressionParser::parse( QStringLiteral("love artist:cure album:\"Best of\" year:<1990 playcount:>2 -score:<50") );
    int i = 6;
    QCOMPARE( result.size(), i );

    while( !result.isEmpty() )
    {
        element = result.takeFirst().takeFirst();

        if( element.text == QStringLiteral("love") )
        {
            QCOMPARE( element.field, QStringLiteral( "" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::Contains );
        }

        else if( element.text == QStringLiteral("cure") )
        {
            QCOMPARE( element.field, QStringLiteral( "artist" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::Contains );
        }

        else if( element.text == QStringLiteral("Best of") )
        {
            QCOMPARE( element.field, QStringLiteral( "album" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::Contains );
        }

        else if( element.text == QStringLiteral("1990") )
        {
            QCOMPARE( element.field, QStringLiteral( "year" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::Less );
        }

        else if( element.text == QStringLiteral("2") )
        {
            QCOMPARE( element.field, QStringLiteral( "playcount" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::More );
        }

        else if( element.text == QStringLiteral("50") )
        {
            QCOMPARE( element.field, QStringLiteral( "score" ) );
            QCOMPARE( element.negate, true );
            QVERIFY( element.match == expression_element::Less );
        }

        i--;
        QCOMPARE( result.size(), i );
    }

    /* another more complex one */
    result = ExpressionParser::parse( QStringLiteral("artist:cure OR album:\"Best of\" OR year:2009") );
    i = 1;
    QCOMPARE( result.size(), i ); // only 1 or_list

    QList<expression_element> elementList;

    if( !result.isEmpty() )
        elementList = result.at(0);

    QCOMPARE( elementList.size(), 3 );

    while( !elementList.isEmpty() )
    {
        element = elementList.takeFirst();

        if( element.text == QStringLiteral("cure") )
        {
            QCOMPARE( element.field, QStringLiteral( "artist" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::Contains );
        }

        else if( element.text == QStringLiteral("Best of") )
        {
            QCOMPARE( element.field, QStringLiteral( "album" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::Contains );
        }

        else if( element.text == QStringLiteral("2009") )
        {
            QCOMPARE( element.field, QStringLiteral( "year" ) );
            QCOMPARE( element.negate, false );
            QVERIFY( element.match == expression_element::Contains );
        }
    }
}

void TestExpression::testIsAdvancedExpression()
{
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("") ), false );

    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("test") ), false );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("foo bar") ), false );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("\"foo bar\"") ), true );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("artist:cure") ), true );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("year:<1990") ), true );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("artist:cure year:<1990") ), true );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("artist:cure AND year:<1990") ), true );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("artist:cure OR year:<1990") ), true );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("-artist:madonna") ), true );
    QCOMPARE( ExpressionParser::isAdvancedExpression( QStringLiteral("album:\"Best of\"") ), true );
}