Skip to content

Commit

Permalink
- refactor tests and add autotest initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsalter committed Aug 27, 2009
1 parent 62e06d7 commit 123a339
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 218 deletions.
36 changes: 36 additions & 0 deletions .autotest
@@ -0,0 +1,36 @@
class Autotest
##
# Convert a path in a string, s, into a class name, changing
# underscores to CamelCase, etc.

def path_to_classname(s)
sep = File::SEPARATOR
f = s.sub(/^test#{sep}/, '').sub(/\.rb$/, '').split(sep)
f = f.map { |path| path.split(/_|(\d+)/).map { |seg| seg.capitalize }.join }
f = f.map { |path| path =~ /Test$/ ? path : "#{path}Test" }
f.join('::')
end
end

Autotest.add_hook :initialize do |at|
unless ARGV.empty?
if ARGV[0] == '-d'
at.find_directories = ARGV[1..-1].dup
else
at.find_directories = []
at.extra_files = ARGV.dup
end
end

# doesn't seem to work
# at.clear_mappings

at.add_mapping(/^lib\/.*\.rb$/) do |filename, _|
possible = File.basename(filename, 'rb').gsub '_', '_?'
files_matching %r%^test/.*#{possible}_test\.rb$%
end

at.add_mapping(/^test.*\/.*test\.rb$/) do |filename, _|
filename
end
end
220 changes: 2 additions & 218 deletions test/permalink_fu_test.rb
@@ -1,221 +1,5 @@
require 'test/unit'
require File.join(File.dirname(__FILE__), '../lib/permalink_fu')

begin
require 'rubygems'
require 'ruby-debug'
Debugger.start
rescue LoadError
puts "no ruby debugger"
end

gem 'activesupport'
require 'active_support/core_ext/blank'

class FauxColumn < Struct.new(:limit)
end

class BaseModel
def self.columns_hash
@columns_hash ||= {'permalink' => FauxColumn.new(100)}
end

def self.inherited(base)
subclasses << base
end

extend PermalinkFu::PluginMethods
attr_accessor :id
attr_accessor :title
attr_accessor :extra
attr_reader :permalink
attr_accessor :foo

class << self
attr_accessor :validation, :subclasses
end
self.subclasses = []

def self.generated_methods
@generated_methods ||= []
end

def self.primary_key
:id
end

def self.logger
nil
end

def self.define_attribute_methods
return unless generated_methods.empty?
true
end

# ripped from AR
def self.evaluate_attribute_method(attr_name, method_definition, method_name=attr_name)

unless method_name.to_s == primary_key.to_s
generated_methods << method_name
end

begin
class_eval(method_definition, __FILE__, __LINE__)
rescue SyntaxError => err
generated_methods.delete(attr_name)
if logger
logger.warn "Exception occurred during reader method compilation."
logger.warn "Maybe #{attr_name} is not a valid Ruby identifier?"
logger.warn "#{err.message}"
end
end
end

def self.exists?(*args)
false
end

def self.before_validation(method)
self.validation = method
end

def validate
send self.class.validation if self.class.validation
permalink
end

def new_record?
@id.nil?
end

def write_attribute(key, value)
instance_variable_set "@#{key}", value
end

def read_attribute(key)
instance_variable_get "@#{key}"
end
end

class MockModel < BaseModel
def self.exists?(conditions)
if conditions[1] == 'foo' || conditions[1] == 'bar' ||
(conditions[1] == 'bar-2' && conditions[2] != 2)
true
else
false
end
end

has_permalink :title
end

class PermalinkChangeableMockModel < BaseModel
def self.exists?(conditions)
if conditions[1] == 'foo'
true
else
false
end
end

has_permalink :title

def permalink_changed?
@permalink_changed
end

def permalink_will_change!
@permalink_changed = true
end
end

class CommonMockModel < BaseModel
def self.exists?(conditions)
false # oh noes
end

has_permalink :title, :unique => false
end

class ScopedModel < BaseModel
def self.exists?(conditions)
if conditions[1] == 'foo' && conditions[2] != 5
true
else
false
end
end

has_permalink :title, :scope => :foo
end

class ScopedModelForNilScope < BaseModel
def self.exists?(conditions)
(conditions[0] == 'permalink = ? and foo IS NULL') ? (conditions[1] == 'ack') : false
end

has_permalink :title, :scope => :foo
end

class OverrideModel < BaseModel
has_permalink :title

def permalink
'not the permalink'
end
end

class ChangedWithoutUpdateModel < BaseModel
has_permalink :title
def title_changed?; true; end
end

class ChangedWithUpdateModel < BaseModel
has_permalink :title, :update => true
def title_changed?; true; end
end

class NoChangeModel < BaseModel
has_permalink :title, :update => true
def title_changed?; false; end
end

class IfProcConditionModel < BaseModel
has_permalink :title, :if => Proc.new { |obj| false }
end

class IfMethodConditionModel < BaseModel
has_permalink :title, :if => :false_method

def false_method; false; end
end

class IfStringConditionModel < BaseModel
has_permalink :title, :if => 'false'
end

class UnlessProcConditionModel < BaseModel
has_permalink :title, :unless => Proc.new { |obj| false }
end

class UnlessMethodConditionModel < BaseModel
has_permalink :title, :unless => :false_method

def false_method; false; end
end

class UnlessStringConditionModel < BaseModel
has_permalink :title, :unless => 'false'
end

class MockModelExtra < BaseModel
has_permalink [:title, :extra]
end

# trying to be like ActiveRecord, define the attribute methods manually
BaseModel.subclasses.each { |c| c.send :define_attribute_methods }
# encoding: UTF-8
require File.join(File.dirname(__FILE__), 'test_helper')

class PermalinkFuTest < Test::Unit::TestCase
@@samples = {
Expand Down

0 comments on commit 123a339

Please sign in to comment.