aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/virtuals/virtuals_repository_TEST.cc
blob: 3750bd4a87ed3c65e845eb946012268e60bbc1e4 (plain)
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
151
152
153
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 2008 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <test/test_runner.hh>
#include <test/test_framework.hh>

#include <paludis/repositories/fake/fake_repository.hh>
#include <paludis/repositories/fake/fake_installed_repository.hh>
#include <paludis/repositories/fake/fake_package_id.hh>
#include <paludis/repositories/virtuals/virtuals_repository.hh>
#include <paludis/environments/test/test_environment.hh>
#include <paludis/package_database.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/selection.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/options.hh>
#include <paludis/user_dep_spec.hh>

using namespace test;
using namespace paludis;

namespace test_cases
{
    struct VirtualsRepositoryTest : TestCase
    {
        VirtualsRepositoryTest() : TestCase("virtuals repository") { }

        void run()
        {
            TestEnvironment env;
            std::tr1::shared_ptr<VirtualsRepository> virtuals(new VirtualsRepository(&env));
            std::tr1::shared_ptr<FakeRepository> repo(new FakeRepository(&env, RepositoryName("repo")));
            std::tr1::shared_ptr<FakeInstalledRepository> installed(new FakeInstalledRepository(&env, RepositoryName("installed")));

            TEST_CHECK((*repo)[k::virtuals_interface()]);

            env.package_database()->add_repository(2, virtuals);
            env.package_database()->add_repository(3, repo);
            env.package_database()->add_repository(4, installed);

            repo->add_version("cat", "pkg", "1")->provide_key()->set_from_string("virtual/pkg");
            repo->add_version("cat", "pkg", "2")->provide_key()->set_from_string("virtual/pkg");
            repo->add_virtual_package(QualifiedPackageName("virtual/pkg"), make_shared_ptr(new PackageDepSpec(
                            parse_user_package_dep_spec(">=cat/pkg-2", &env, UserPackageDepSpecOptions()))));

            TEST_CHECK(repo->virtual_packages());
            TEST_CHECK_EQUAL(std::distance(repo->virtual_packages()->begin(), repo->virtual_packages()->end()), 1);

            TEST_CHECK(virtuals->has_category_named(CategoryNamePart("virtual")));
            TEST_CHECK(virtuals->has_package_named(QualifiedPackageName("virtual/pkg")));

            std::tr1::shared_ptr<const PackageIDSequence> r(env[selection::AllVersionsSorted(generator::All())]);
            TEST_CHECK_STRINGIFY_EQUAL(join(indirect_iterator(r->begin()), indirect_iterator(r->end()), " | "),
                    "cat/pkg-1:0::repo | cat/pkg-2:0::repo | virtual/pkg-2::virtuals (virtual for cat/pkg-2:0::repo)");
        }
    } test_virtuals_repository;

    struct VirtualsRepositoryDuplicatesTest : TestCase
    {
        VirtualsRepositoryDuplicatesTest() : TestCase("virtuals repository duplicates") { }

        void run()
        {
            TestEnvironment env;
            std::tr1::shared_ptr<VirtualsRepository> virtuals(new VirtualsRepository(&env));
            std::tr1::shared_ptr<FakeRepository> repo1(new FakeRepository(&env, RepositoryName("repo1")));
            std::tr1::shared_ptr<FakeRepository> repo2(new FakeRepository(&env, RepositoryName("repo2")));
            std::tr1::shared_ptr<FakeInstalledRepository> installed(new FakeInstalledRepository(&env, RepositoryName("installed")));

            env.package_database()->add_repository(2, virtuals);
            env.package_database()->add_repository(3, repo1);
            env.package_database()->add_repository(4, repo2);
            env.package_database()->add_repository(5, installed);

            repo1->add_version("cat", "pkg", "1")->provide_key()->set_from_string("virtual/pkg");
            repo1->add_version("cat", "pkg", "2")->provide_key()->set_from_string("virtual/pkg");
            repo1->add_virtual_package(QualifiedPackageName("virtual/pkg"), make_shared_ptr(
                        new PackageDepSpec(parse_user_package_dep_spec(">=cat/pkg-2", &env, UserPackageDepSpecOptions()))));
            repo1->add_virtual_package(QualifiedPackageName("virtual/foo"), make_shared_ptr(
                        new PackageDepSpec(parse_user_package_dep_spec(">=cat/pkg-2", &env, UserPackageDepSpecOptions()))));

            repo2->add_virtual_package(QualifiedPackageName("virtual/pkg"), make_shared_ptr(new PackageDepSpec(
                            parse_user_package_dep_spec(">=cat/pkg-2", &env, UserPackageDepSpecOptions()))));
            repo2->add_virtual_package(QualifiedPackageName("virtual/foo"), make_shared_ptr(new PackageDepSpec(
                            parse_user_package_dep_spec("<=cat/pkg-1", &env, UserPackageDepSpecOptions()))));

            TEST_CHECK(virtuals->has_category_named(CategoryNamePart("virtual")));
            TEST_CHECK(virtuals->has_package_named(QualifiedPackageName("virtual/pkg")));

            std::tr1::shared_ptr<const PackageIDSequence> r(env[selection::AllVersionsSorted(generator::All())]);
            TEST_CHECK_STRINGIFY_EQUAL(join(indirect_iterator(r->begin()), indirect_iterator(r->end()), " | "),
                    "cat/pkg-1:0::repo1 | cat/pkg-2:0::repo1 | "
                    "virtual/foo-1::virtuals (virtual for cat/pkg-1:0::repo1) | "
                    "virtual/foo-2::virtuals (virtual for cat/pkg-2:0::repo1) | "
                    "virtual/pkg-2::virtuals (virtual for cat/pkg-2:0::repo1)");
        }
    } test_virtuals_repository_duplicates;

    struct VirtualsRepositoryRecursionTest : TestCase
    {
        VirtualsRepositoryRecursionTest() : TestCase("virtuals repository recursion") { }

        void run()
        {
            TestEnvironment env;
            std::tr1::shared_ptr<VirtualsRepository> virtuals(new VirtualsRepository(&env));
            std::tr1::shared_ptr<FakeRepository> repo1(new FakeRepository(&env, RepositoryName("repo1")));
            std::tr1::shared_ptr<FakeRepository> repo2(new FakeRepository(&env, RepositoryName("repo2")));

            env.package_database()->add_repository(2, repo1);
            env.package_database()->add_repository(3, repo2);
            env.package_database()->add_repository(4, virtuals);

            repo1->add_version("virtual", "gkp", "1")->provide_key()->set_from_string("virtual/pkg");
            repo1->add_virtual_package(QualifiedPackageName("virtual/pkg"), make_shared_ptr(
                        new PackageDepSpec(parse_user_package_dep_spec("virtual/gkp", &env, UserPackageDepSpecOptions()))));

            repo2->add_version("virtual", "pkg", "2")->provide_key()->set_from_string("virtual/pkg");
            repo2->add_virtual_package(QualifiedPackageName("virtual/pkg"), make_shared_ptr(new PackageDepSpec(
                            parse_user_package_dep_spec("virtual/pkg", &env, UserPackageDepSpecOptions()))));

            TEST_CHECK(virtuals->has_category_named(CategoryNamePart("virtual")));
            TEST_CHECK(virtuals->has_package_named(QualifiedPackageName("virtual/pkg")));

            std::tr1::shared_ptr<const PackageIDSequence> r(env[selection::AllVersionsSorted(generator::All())]);
            TEST_CHECK_STRINGIFY_EQUAL(join(indirect_iterator(r->begin()), indirect_iterator(r->end()), " | "),
                    "virtual/gkp-1:0::repo1 | virtual/pkg-1::virtuals (virtual for virtual/gkp-1:0::repo1) | "
                    "virtual/pkg-2:0::repo2 | virtual/pkg-2::virtuals (virtual for virtual/pkg-2:0::repo2)");
        }
    } test_virtuals_repository_recursion;
}