aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2007-07-03 20:50:13 +0000
committerAvatar David Leverton <levertond@googlemail.com> 2007-07-03 20:50:13 +0000
commit84dfd198adf05f8beb2056ce62e2157cc5e5e285 (patch)
treec623456c75f3d04790895bd888142d045872b07e
parentcbeb2aecd9bf68cbd29840969a7fcf5b190b96bd (diff)
downloadpaludis-84dfd198adf05f8beb2056ce62e2157cc5e5e285.tar.gz
paludis-84dfd198adf05f8beb2056ce62e2157cc5e5e285.tar.xz
(ruby) Add FakeRepository.
-rw-r--r--ruby/repository.cc162
-rw-r--r--ruby/repository_TEST.rb135
2 files changed, 297 insertions, 0 deletions
diff --git a/ruby/repository.cc b/ruby/repository.cc
index 0141153..81788eb 100644
--- a/ruby/repository.cc
+++ b/ruby/repository.cc
@@ -3,6 +3,7 @@
/*
* Copyright (c) 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
* Copyright (c) 2006, 2007 Richard Brown <rbrown@gentoo.org>
+ * Copyright (c) 2007 David Leverton <levertond@googlemail.com>
*
* 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
@@ -21,6 +22,8 @@
#include <paludis_ruby.hh>
#include <paludis/repository.hh>
#include <paludis/repository_info.hh>
+#include <paludis/repositories/fake/fake_repository.hh>
+#include <paludis/repositories/fake/fake_package_id.hh>
#include <paludis/util/stringify.hh>
#include <libwrapiter/libwrapiter_forward_iterator.hh>
#include <libwrapiter/libwrapiter_output_iterator.hh>
@@ -37,6 +40,8 @@ namespace
static VALUE c_repository_info;
static VALUE c_repository_info_section;
static VALUE c_profiles_desc_line;
+ static VALUE c_fake_repository_base;
+ static VALUE c_fake_repository;
VALUE
profiles_desc_line_to_value(const RepositoryEInterface::ProfilesDescLine & v)
@@ -45,6 +50,21 @@ namespace
return Data_Wrap_Struct(c_profiles_desc_line, 0, &Common<RepositoryEInterface::ProfilesDescLine>::free, vv);
}
+ tr1::shared_ptr<FakeRepositoryBase>
+ value_to_fake_repository_base(VALUE v)
+ {
+ if (rb_obj_is_kind_of(v, c_fake_repository_base))
+ {
+ tr1::shared_ptr<FakeRepositoryBase> * v_ptr;
+ Data_Get_Struct(v, tr1::shared_ptr<FakeRepositoryBase>, v_ptr);
+ return *v_ptr;
+ }
+ else
+ {
+ rb_raise(rb_eTypeError, "Can't convert %s into FakeRepositoryBase", rb_obj_classname(v));
+ }
+ }
+
/*
* call-seq:
* name -> String
@@ -801,6 +821,127 @@ namespace
}
};
+ /*
+ * call-seq:
+ * add_category(category_name) -> Nil
+ *
+ * Add a category.
+ */
+ VALUE
+ fake_repository_base_add_category(VALUE self, VALUE category)
+ {
+ try
+ {
+ tr1::shared_ptr<FakeRepositoryBase> repo(value_to_fake_repository_base(self));
+ std::string cat_str(StringValuePtr(category));
+ repo->add_category(CategoryNamePart(cat_str));
+ return Qnil;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ /*
+ * call-seq:
+ * add_package(qualified_package_name) -> Nil
+ *
+ * Add a package, and a category if necessary.
+ */
+ VALUE
+ fake_repository_base_add_package(VALUE self, VALUE qpn)
+ {
+ try
+ {
+ tr1::shared_ptr<FakeRepositoryBase> repo(value_to_fake_repository_base(self));
+ QualifiedPackageName name(value_to_qualified_package_name(qpn));
+ repo->add_package(name);
+ return Qnil;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ /*
+ * call-seq:
+ * add_version(qualified_package_name, version_spec) -> PackageID
+ * add_version(category_name, package_name, version_string) -> PackageID
+ *
+ * Add a version, and a package and category if necessary, and set some
+ * default values for its metadata, and return said metadata.
+ */
+ VALUE
+ fake_repository_base_add_version(int argc, VALUE* argv, VALUE self)
+ {
+ try
+ {
+ tr1::shared_ptr<FakeRepositoryBase> repo(value_to_fake_repository_base(self));
+ tr1::shared_ptr<PackageID> pkg;
+
+ switch (argc)
+ {
+ case 2: {
+ QualifiedPackageName qpn(value_to_qualified_package_name(argv[0]));
+ VersionSpec ver(value_to_version_spec(argv[1]));
+ pkg = repo->add_version(qpn, ver);
+ break;
+ }
+
+ case 3: {
+ std::string cat(StringValuePtr(argv[0]));
+ std::string name(StringValuePtr(argv[1]));
+ std::string ver(StringValuePtr(argv[2]));
+ pkg = repo->add_version(cat, name, ver);
+ break;
+ }
+
+ default:
+ rb_raise(rb_eArgError, "FakeRepositoryBase.add_version expects two or three arguments, but got %d", argc);
+ }
+
+ return package_id_to_value(pkg);
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ VALUE
+ fake_repository_init(int, VALUE*, VALUE self)
+ {
+ return self;
+ }
+
+ /*
+ * call-seq:
+ * FakeRepository.new(environment, repo_name) -> FakeRepository
+ *
+ * Create a new FakeRepository.
+ */
+ VALUE
+ fake_repository_new(int argc, VALUE* argv, VALUE self)
+ {
+ try
+ {
+ if (2 != argc)
+ rb_raise(rb_eArgError, "FakeRepository.new expects two arguments, but got %d", argc);
+
+ tr1::shared_ptr<FakeRepository> * r = new tr1::shared_ptr<FakeRepository>(new
+ FakeRepository(value_to_environment(argv[0]).get(), RepositoryName(StringValuePtr(argv[1]))));
+ VALUE tdata(Data_Wrap_Struct(self, 0, &Common<tr1::shared_ptr<FakeRepository> >::free, r));
+ rb_obj_call_init(tdata, argc, argv);
+ return tdata;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
void do_register_repository()
{
/*
@@ -895,6 +1036,27 @@ namespace
RUBY_FUNC_CAST((&DescLineValue<std::string,&RepositoryEInterface::ProfilesDescLine::arch>::fetch)), 0);
rb_define_method(c_profiles_desc_line, "status",
RUBY_FUNC_CAST((&DescLineValue<std::string,&RepositoryEInterface::ProfilesDescLine::status>::fetch)), 0);
+
+ /*
+ * Document-class: Paludis::FakeRepositoryBase
+ *
+ * A FakeRepositoryBase is a Repository subclass whose subclasses are used for
+ * various test cases.
+ */
+ c_fake_repository_base = rb_define_class_under(paludis_module(), "FakeRepositoryBase", c_repository);
+ rb_funcall(c_fake_repository_base, rb_intern("private_class_method"), 1, rb_str_new2("new"));
+ rb_define_method(c_fake_repository_base, "add_category", RUBY_FUNC_CAST(&fake_repository_base_add_category), 1);
+ rb_define_method(c_fake_repository_base, "add_package", RUBY_FUNC_CAST(&fake_repository_base_add_package), 1);
+ rb_define_method(c_fake_repository_base, "add_version", RUBY_FUNC_CAST(&fake_repository_base_add_version), -1);
+
+ /*
+ * Document-class: Paludis::FakeRepository
+ *
+ * Fake repository for use in test cases.
+ */
+ c_fake_repository = rb_define_class_under(paludis_module(), "FakeRepository", c_fake_repository_base);
+ rb_define_singleton_method(c_fake_repository, "new", RUBY_FUNC_CAST(&fake_repository_new), -1);
+ rb_define_method(c_fake_repository, "initialize", RUBY_FUNC_CAST(&fake_repository_init), -1);
}
}
diff --git a/ruby/repository_TEST.rb b/ruby/repository_TEST.rb
index 1579837..f91d40f 100644
--- a/ruby/repository_TEST.rb
+++ b/ruby/repository_TEST.rb
@@ -478,5 +478,140 @@ module Paludis
assert_equal 'A test local use flag', repo.describe_use_flag('test2', p)
end
end
+
+ class TestCase_FakeRepository < Test::Unit::TestCase
+ include RepositoryTestCase
+
+ def fake
+ FakeRepository.new(env, 'fake')
+ end
+
+ def test_new
+ f = fake
+
+ assert_kind_of FakeRepository, f
+ assert_kind_of FakeRepositoryBase, f
+ assert_equal 'fake', f.name
+ end
+
+ def test_new_bad
+ assert_raise ArgumentError do FakeRepository.new end
+ assert_raise ArgumentError do FakeRepository.new(env) end
+ assert_raise ArgumentError do FakeRepository.new(env, 'fake', 42) end
+
+ assert_raise TypeError do FakeRepository.new(repo, 'fake') end
+ assert_raise TypeError do FakeRepository.new(env, 42) end
+
+ assert_raise NameError do FakeRepository.new(env, 'f a k e') end
+ end
+
+ def test_add_category
+ f = fake
+
+ assert_equal [], f.category_names
+ f.add_category('foo-bar')
+ assert_equal ['foo-bar'], f.category_names
+ f.add_category('foo-bar')
+ assert_equal ['foo-bar'], f.category_names
+ f.add_category('bar-foo')
+ assert_equal ['bar-foo', 'foo-bar'], f.category_names
+ end
+
+ def test_add_category_bad
+ f = fake
+
+ assert_raise ArgumentError do f.add_category end
+ assert_raise ArgumentError do f.add_category('foo-bar', 'bar-foo') end
+
+ assert_raise TypeError do f.add_category(42) end
+
+ assert_raise CategoryNamePartError do f.add_category('foo bar') end
+ end
+
+ def test_add_package
+ f = fake
+
+ foobar_baz = QualifiedPackageName.new('foo-bar', 'baz')
+ foobar_quux = QualifiedPackageName.new('foo-bar', 'quux')
+ barfoo_xyzzy = QualifiedPackageName.new('bar-foo', 'xyzzy')
+
+ f.add_category('foo-bar')
+ assert_equal [], f.package_names('foo-bar')
+ f.add_package(foobar_baz)
+ assert_equal [foobar_baz], f.package_names('foo-bar')
+ f.add_package('foo-bar/baz')
+ assert_equal [foobar_baz], f.package_names('foo-bar')
+ f.add_package(foobar_quux)
+ assert_equal [foobar_baz, foobar_quux], f.package_names('foo-bar')
+
+ f.add_package(barfoo_xyzzy)
+ assert_equal ['bar-foo', 'foo-bar'], f.category_names
+ assert_equal [barfoo_xyzzy], f.package_names('bar-foo')
+ end
+
+ def test_add_package_bad
+ f = fake
+
+ assert_raise ArgumentError do f.add_package end
+ assert_raise ArgumentError do
+ f.add_package(Paludis::QualifiedPackageName.new('foo-bar', 'baz'), 42)
+ end
+
+ assert_raise TypeError do f.add_package(42) end
+
+ assert_raise NameError do f.add_package('test') end
+ assert_raise CategoryNamePartError do f.add_package('f o o/bar') end
+ assert_raise PackageNamePartError do f.add_package('foo/b a r') end
+ end
+
+ def test_add_version
+ f = fake
+
+ f.add_category('foo-bar')
+ f.add_package('foo-bar/baz')
+ assert_equal [], f.package_ids('foo-bar/baz')
+ pkg = f.add_version('foo-bar/baz', '123')
+ assert_equal [pkg], f.package_ids('foo-bar/baz')
+ assert_kind_of PackageID, pkg
+ assert_equal QualifiedPackageName.new('foo-bar/baz'), pkg.name
+ assert_equal VersionSpec.new('123'), pkg.version
+
+ f.add_version('bar-foo/quux', '42')
+ assert_equal ['bar-foo', 'foo-bar'], f.category_names
+ assert_equal [QualifiedPackageName.new('bar-foo/quux')], f.package_names('bar-foo')
+
+ pkg2 = f.add_version('abc-def', 'ghi', 'scm')
+ assert_kind_of PackageID, pkg2
+ assert_equal QualifiedPackageName.new('abc-def/ghi'), pkg2.name
+ assert_equal VersionSpec.new('scm'), pkg2.version
+ assert_equal ['abc-def', 'bar-foo', 'foo-bar'], f.category_names
+ assert_equal [QualifiedPackageName.new('abc-def/ghi')], f.package_names('abc-def')
+ assert_equal [pkg2], f.package_ids('abc-def/ghi')
+ end
+
+ def test_add_version_bad
+ f = fake
+
+ assert_raise ArgumentError do f.add_version end
+ assert_raise ArgumentError do f.add_version('foo-bar/baz') end
+ assert_raise ArgumentError do f.add_version('foo-bar', 'baz', '3', 'abc') end
+
+ assert_raise TypeError do f.add_version(42, '1') end
+ assert_raise TypeError do f.add_version('foo-bar/baz', []) end
+
+ assert_raise TypeError do f.add_version(proc {}, 'quux', '1.5') end
+ assert_raise TypeError do f.add_version('foo-bar', {}, '9') end
+ assert_raise TypeError do f.add_version('foo-bar', 'baz', Paludis) end
+
+ assert_raise NameError do f.add_version('foo', '42') end
+ assert_raise CategoryNamePartError do f.add_version('f o o/bar', '42') end
+ assert_raise PackageNamePartError do f.add_version('foo/b a r' , '42') end
+ assert_raise BadVersionSpecError do f.add_version('foo/bar', 'abc') end
+
+ assert_raise CategoryNamePartError do f.add_version('f o o', 'bar', '42') end
+ assert_raise PackageNamePartError do f.add_version('foo', 'b a r', '42') end
+ assert_raise BadVersionSpecError do f.add_version('foo', 'bar', 'abc') end
+ end
+ end
end