~larryprice/libertine-scope/preview-2

« back to all changes in this revision

Viewing changes to tests/scope/store/test_package.cpp

  • Committer: Larry Price
  • Date: 2016-07-25 14:10:52 UTC
  • Revision ID: larry.price@canonical.com-20160725141052-r9rwwjgolx2t8060
tests for the package class

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU General Public License, version 3, as published by the
 
6
 * Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
#include "scope/store/package.h"
 
17
 
 
18
#include <gtest/gtest.h>
 
19
#include <gmock/gmock.h>
 
20
 
 
21
 
 
22
namespace
 
23
{
 
24
using namespace Libertine::Store;
 
25
 
 
26
 
 
27
static QVariantMap
 
28
attribute_map(QString const& name, QString const& summary, QString const& description,
 
29
              QString const& icon, QString const& publisher, QString const& website,
 
30
              QString const& license, QString const& id, double rating)
 
31
{
 
32
  QVariantMap attributes;
 
33
  attributes["name"] = name;
 
34
  attributes["summary"] = summary;
 
35
  attributes["rating"] = rating;
 
36
  attributes["description"] = description;
 
37
  attributes["icon"] = icon;
 
38
  attributes["publisher"] = publisher;
 
39
  attributes["website"] = website;
 
40
  attributes["license"] = license;
 
41
  attributes["id"] = id;
 
42
  return attributes;
 
43
}
 
44
 
 
45
 
 
46
static void
 
47
package_matches_attributes(Package actual, std::string const& name, std::string const& summary,
 
48
                           std::string const& description, std::string const& icon, std::string const& publisher,
 
49
                           std::string const& website, std::string const& license, std::string const& id, double rating)
 
50
{
 
51
  EXPECT_EQ(name, actual.name);
 
52
  EXPECT_EQ(summary, actual.summary);
 
53
  EXPECT_EQ(rating, actual.rating);
 
54
  EXPECT_EQ(description, actual.description);
 
55
  EXPECT_EQ(icon, actual.icon);
 
56
  EXPECT_EQ(publisher, actual.publisher);
 
57
  EXPECT_EQ(website, actual.website);
 
58
  EXPECT_EQ(license, actual.license);
 
59
  EXPECT_EQ(id, actual.id);
 
60
}
 
61
 
 
62
 
 
63
TEST(PackageTest, FromMapCreatesSinglePackage)
 
64
{
 
65
  auto attributeMap = attribute_map("Harry", "The Philosopher's Stone",
 
66
                                    "Boy with head injury seeks magic rock",
 
67
                                    "harry.png", "JK", "pottermore.com", "Private",
 
68
                                    "com.pottermore.harry", 3.4);
 
69
  auto package = Package::from_map(attributeMap);
 
70
  package_matches_attributes(package, "Harry", "The Philosopher's Stone",
 
71
                             "Boy with head injury seeks magic rock",
 
72
                             "harry.png", "JK", "pottermore.com", "Private",
 
73
                             "com.pottermore.harry", 3.4);
 
74
}
 
75
 
 
76
 
 
77
TEST(PackageTest, FromMapCreatesListOfPackages)
 
78
{
 
79
  auto attributePkg1Map = attribute_map("Harry", "The Philosopher's Stone",
 
80
                                    "Boy with head injury seeks magic rock",
 
81
                                    "harry.png", "JK", "pottermore.com", "Private",
 
82
                                    "com.pottermore.harry", 3.4);
 
83
  auto attributePkg2Map = attribute_map("Dune", "Classic science fiction",
 
84
                                    "The spice must flow", "worms.png",
 
85
                                    "Herbert", "dunebook.net", "Other",
 
86
                                    "net.dunebook.dune", 4.1);
 
87
  auto packages = Package::from_map(QList<QVariantMap>{attributePkg1Map, attributePkg2Map});
 
88
  ASSERT_EQ(2, packages.size());
 
89
  package_matches_attributes(packages[0], "Harry", "The Philosopher's Stone",
 
90
                             "Boy with head injury seeks magic rock",
 
91
                             "harry.png", "JK", "pottermore.com", "Private",
 
92
                             "com.pottermore.harry", 3.4);
 
93
  package_matches_attributes(packages[1], "Dune", "Classic science fiction",
 
94
                             "The spice must flow", "worms.png",
 
95
                             "Herbert", "dunebook.net", "Other",
 
96
                             "net.dunebook.dune", 4.1);
 
97
}
 
98
 
 
99
 
 
100
TEST(PackageTest, RenderRatingReturnsMinimum0EmptyStars)
 
101
{
 
102
  Package p;
 
103
  p.rating = -5;
 
104
  EXPECT_EQ("☆☆☆☆☆", p.render_rating());
 
105
}
 
106
 
 
107
 
 
108
TEST(PackageTest, RenderRatingReturnsMaximum5FilledStars)
 
109
{
 
110
  Package p;
 
111
  p.rating = 6;
 
112
  EXPECT_EQ("★★★★★", p.render_rating());
 
113
}
 
114
 
 
115
 
 
116
TEST(PackageTest, RenderRatingReturnsRoundedUpStars)
 
117
{
 
118
  Package p;
 
119
  p.rating = 3.49;
 
120
  EXPECT_EQ("★★★☆☆", p.render_rating());
 
121
}
 
122
 
 
123
 
 
124
TEST(PackageTest, RenderRatingReturnsRoundedDownStars)
 
125
{
 
126
  Package p;
 
127
  p.rating = 3.5;
 
128
  EXPECT_EQ("★★★★☆", p.render_rating());
 
129
}
 
130
}