Skip to content
Browse files

Group object is now fully tested

  • Loading branch information...
1 parent 538eb03 commit ec663207eadb4b3de7b79d66c0971a99b81c383a @wingrunr21 wingrunr21 committed Oct 18, 2011
Showing with 147 additions and 12 deletions.
  1. +27 −12 lib/gitolite/config/group.rb
  2. +120 −0 spec/group_spec.rb
View
39 lib/gitolite/config/group.rb
@@ -1,31 +1,46 @@
module Gitolite
class Config
- #Represents a group inside the gitolite configuration. The name and users
- #options are all encapsulated in this class
+ # Represents a group inside the gitolite configuration. The name and users
+ # options are all encapsulated in this class. All users are stored as
+ # Strings!
class Group
- attr_accessor :name, :gl_name, :users
+ attr_accessor :name, :users
def initialize(name)
@name = name
- @gl_name = "@#{name}"
@users = []
end
- def clean_users
- @users = []
+ def empty!
+ @users.clear
+ end
+
+ def add_user(user)
+ return if has_user?(user)
+ @users.push(user.to_s).sort!
end
def add_users(*users)
- users.flatten.each do |user|
- @users << user
- end
- @users.sort!.uniq!
+ fixed_users = users.flatten.map{ |u| u.to_s }
+ @users.concat(fixed_users).sort!.uniq!
+ end
+
+ def rm_user(user)
+ @users.delete(user.to_s)
+ end
+
+ def has_user?(user)
+ @users.include? user.to_s
end
- def remove_user(user)
- @users.delete(user)
+ def size
+ @users.length
end
+ def to_s
+ members = @users.join(' ')
+ "#{@name.ljust(20)}= #{members}\n"
+ end
end
end
end
View
120 spec/group_spec.rb
@@ -0,0 +1,120 @@
+require 'gitolite/config/group'
+require 'spec_helper'
+
+describe Gitolite::Config::Group do
+ describe "#new" do
+ it "should create a new group with an empty list of users" do
+ group = Gitolite::Config::Group.new("@testgroup")
+ group.users.empty?.should be true
+ group.name.should == "@testgroup"
+ end
+ end
+
+ describe "users" do
+ before :each do
+ @group = Gitolite::Config::Group.new('@testgroup')
+ end
+
+ describe "#add_user" do
+ it "should allow adding one user with a string" do
+ @group.add_user("bob")
+ @group.size.should == 1
+ @group.users.first.should == "bob"
+ end
+
+ it "should allow adding one user with a symbol" do
+ @group.add_user(:bob)
+ @group.size.should == 1
+ @group.users.first.should == "bob"
+ end
+
+ it "should not add the same user twice" do
+ @group.add_user("bob")
+ @group.size.should == 1
+ @group.add_user(:bob)
+ @group.size.should == 1
+ @group.users.first.should == "bob"
+ end
+
+ it "should maintain users in sorted order" do
+ @group.add_user("susan")
+ @group.add_user("peyton")
+ @group.add_user("bob")
+ @group.users.first.should == "bob"
+ @group.users.last.should == "susan"
+ end
+ end
+
+ describe "#add_users" do
+ it "should allow adding multiple users at once" do
+ @group.add_users("bob", "joe", "sue", "sam", "dan")
+ @group.size.should == 5
+ end
+
+ it "should allow adding multiple users in nested arrays" do
+ @group.add_users(["bob", "joe", ["sam", "sue", "dan"]], "bill")
+ @group.size.should == 6
+ end
+
+ it "should allow adding users of symbols and strings" do
+ @group.add_users("bob", :joe, :sue, "sam")
+ @group.size.should == 4
+ end
+
+ it "should not add the same user twice" do
+ @group.add_users("bob", :bob, "bob", "sam")
+ @group.size.should == 2
+ end
+ end
+
+ describe "#rm_user" do
+ before :each do
+ @group.add_users("bob", "joe", "susan", "sam", "alex")
+ end
+
+ it "should support removing a user via a String" do
+ @group.rm_user("bob")
+ @group.size.should == 4
+ end
+
+ it "should support removing a user via a Symbol" do
+ @group.rm_user(:bob)
+ @group.size.should == 4
+ end
+ end
+
+ describe "#empty!" do
+ it "should clear all users from the group" do
+ @group.add_users("bob", "joe", "sue", "jim")
+ @group.size.should == 4
+ @group.empty!
+ @group.size.should == 0
+ end
+ end
+
+ describe "#size" do
+ it "should reflect how many users are in the group" do
+ @group.add_users("bob", "joe", "sue", "jim")
+ @group.users.length.should == @group.size
+ end
+ end
+
+ describe "#has_user?" do
+ it "should search for a user via a String" do
+ @group.add_user("bob")
+ @group.has_user?("bob").should be true
+ end
+
+ it "should search for a user via a Symbol" do
+ @group.add_user(:bob)
+ @group.has_user?(:bob).should be true
+ end
+ end
+ end
+
+ describe "#to_s" do
+ group = Gitolite::Config::Group.new("@testgroup")
+ group.add_users("bob", "joe", "sam", "sue")
+ group.to_s.should == "@testgroup = bob joe sam sue\n" #10 spaces after @testgroup
+ end
+end

0 comments on commit ec66320

Please sign in to comment.
Something went wrong with that request. Please try again.