Skip to content

Commit

Permalink
first import
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrec1 committed May 7, 2009
0 parents commit 7dc6255
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
h2. INSTALLATION:

<pre>
<code>
script/plugin install git://github.com/spiderman/remarkable_you_name_it.git
</code>
</pre>

h2. USAGE:

<pre>
<code>
describe Spiderman do
should_have_a_name :peter
end
</code>
</pre>

h2. LICENSE:

(The MIT License)

Copyright (c) 2009

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 changes: 49 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'rubygems'
require 'rubygems/specification'
require 'rake'
require 'rake/gempackagetask'
require 'spec/rake/spectask'

GEM = "remarkable_you_name_it"
GEM_VERSION = "0.2.0"
SUMMARY = "YouNameIt Remarkable Matchers"
AUTHOR = "Spiderman"
EMAIL = "spiderman@marvel.com"
HOMEPAGE = "http://spiderman.marvel.com"


spec = Gem::Specification.new do |s|
s.name = GEM
s.version = GEM_VERSION
s.platform = Gem::Platform::RUBY
s.summary = SUMMARY
s.require_paths = ['lib']
s.files = FileList['lib/**/*.rb' '[A-Z]*'].to_a

s.author = AUTHOR
s.email = EMAIL
s.homepage = HOMEPAGE

s.rubyforge_project = GEM # GitHub bug, gem isn't being build when this miss
end

Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = %w(-fs --color)
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
end

desc "Install the gem locally"
task :install => [:package] do
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
end

desc "Create a gemspec file"
task :make_spec do
File.open("#{GEM}.gemspec", "w") do |file|
file.puts spec.to_ruby
end
end
12 changes: 12 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if RAILS_ENV == "test"

require 'remarkable_activerecord'

require File.join(File.dirname(__FILE__), "lib", "remarkable_you_name_it")

require 'spec'
require 'spec/rails'

Remarkable.include_matchers!(Remarkable::YouNameIt, Spec::Rails::Example::ModelExampleGroup)

end
7 changes: 7 additions & 0 deletions lib/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
en:
remarkable:
you_name_it:
have_a_name:
description: "should have a name"
expectations:
has_a_name: "{{subject_name}} should have a name {{attribute}}"
7 changes: 7 additions & 0 deletions lib/pt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pt:
remarkable:
you_name_it:
have_a_name:
description: "deve ter um nome"
expectations:
has_a_name: "{{subject_name}} deve ter un nome {{attribute}}"
20 changes: 20 additions & 0 deletions lib/remarkable_you_name_it.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Remarkable
module YouNameIt
module Matchers
class HaveANameMatcher < Remarkable::ActiveRecord::Base
arguments :name

assertion :has_a_name?

def has_a_name?
@subject.names.include? @name.to_s
end
end

def have_a_name(attribute)
HaveANameMatcher.new(attribute).spec(self)
end

end
end
end
100 changes: 100 additions & 0 deletions spec/model_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This is based on Remarkable based on Shoulda model builder for Test::Unit.
#
module ModelBuilder
def self.included(base)
return unless base.name =~ /^Spec/

base.class_eval do
after(:each) do
if @defined_constants
@defined_constants.each do |class_name|
Object.send(:remove_const, class_name)
end
end

if @created_tables
@created_tables.each do |table_name|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{table_name}")
end
end
end
end

base.extend ClassMethods
end

def create_table(table_name, &block)
connection = ActiveRecord::Base.connection

begin
connection.execute("DROP TABLE IF EXISTS #{table_name}")
connection.create_table(table_name, &block)
@created_tables ||= []
@created_tables << table_name
connection
rescue Exception => e
connection.execute("DROP TABLE IF EXISTS #{table_name}")
raise e
end
end

def define_constant(class_name, base, &block)
class_name = class_name.to_s.camelize

klass = Class.new(base)
Object.const_set(class_name, klass)

klass.class_eval(&block) if block_given?

@defined_constants ||= []
@defined_constants << class_name

klass
end

def define_model_class(class_name, &block)
define_constant(class_name, ActiveRecord::Base, &block)
end

def define_model(name, columns = {}, &block)
class_name = name.to_s.pluralize.classify
table_name = class_name.tableize

table = columns.delete(:table) || lambda {|table|
columns.each do |name, type|
table.column name, *type
end
}

create_table(table_name, &table)

klass = define_model_class(class_name, &block)
instance = klass.new

self.class.subject { instance } if self.class.respond_to?(:subject)
instance
end

module ClassMethods
# This is a macro to run validations of boolean optionals such as :allow_nil
# and :allow_blank. This macro tests all scenarios. The specs must have a
# define_and_validate method defined.
#
def create_optional_boolean_specs(optional, base, options={})
base.describe "with #{optional} option" do
it { should define_and_validate(options.merge(optional => true)).send(optional) }
it { should define_and_validate(options.merge(optional => false)).send(optional, false) }
it { should_not define_and_validate(options.merge(optional => true)).send(optional, false) }
it { should_not define_and_validate(options.merge(optional => false)).send(optional) }
end
end

def create_message_specs(base)
base.describe "with message option" do
it { should define_and_validate(:message => 'valid_message').message('valid_message') }
it { should_not define_and_validate(:message => 'not_valid').message('valid_message') }
end
end
end

end
23 changes: 23 additions & 0 deletions spec/remarkable_you_name_it_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.join(File.dirname(__FILE__), "spec_helper.rb")

require 'paperclip'

create_table "people" do end

class Person < ActiveRecord::Base
def names
["johny", "vitto"]
end
end

describe Remarkable::YouNameIt do
describe "have_a_name" do
it "should validate that a model has a name" do
have_a_name(:johny).matches?(Person.new).should be_true
end

it "should validate that a model doens't has a name" do
have_a_name(:marcus).matches?(Person.new).should be_false
end
end
end
25 changes: 25 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rubygems'

RAILS_ENV = "test"
RAILS_VERSION = ENV['RAILS_VERSION'] || '2.3.2'

gem 'activesupport', RAILS_VERSION
require 'active_support'

gem 'activerecord', RAILS_VERSION
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)

require 'remarkable_activerecord'

dir = File.dirname(__FILE__)
require File.join(dir, "..", "lib", "remarkable_you_name_it")

require File.join(dir, "model_builder")
include ModelBuilder

Remarkable.include_matchers!(Remarkable::YouNameIt, Spec::Example::ExampleGroup)

0 comments on commit 7dc6255

Please sign in to comment.