Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored to use newgem

  • Loading branch information...
commit 6e2e6658ae2e3126d6382627ed33325461a8152f 1 parent e855fab
Bobby Norton authored
4 History.txt
View
@@ -0,0 +1,4 @@
+== 0.5.0 2009-06-15
+
+* 1 major enhancement:
+ * Refactored to use newgem / rubigen file generation.
25 Manifest.txt
View
@@ -0,0 +1,25 @@
+History.txt
+MIT-LICENSE
+Manifest.txt
+PostInstall.txt
+README.rdoc
+Rakefile
+app_generators/tbox/USAGE
+app_generators/tbox/tbox_generator.rb
+app_generators/tbox/templates/README
+app_generators/tbox/templates/file.txt
+app_generators/tbox/templates/rakefile.rb
+app_generators/tbox/templates/right_triangle.rb
+app_generators/tbox/templates/tbox.rb
+app_generators/tbox/templates/test_helper.rb
+app_generators/tbox/templates/test_io.rb
+app_generators/tbox/templates/test_right_triangle.rb
+bin/tbox
+lib/tbox.rb
+script/console
+script/destroy
+script/generate
+tbox.gemspec
+test/test_generator_helper.rb
+test/test_helper.rb
+test/test_tbox_generator.rb
6 PostInstall.txt
View
@@ -0,0 +1,6 @@
+
+For more information on tbox, see http://github.com/bobbyno/tbox/tree/master
+
+
+
+
47 README.rdoc
View
@@ -0,0 +1,47 @@
+= tbox - A test-driven learning sandbox creation tool
+
+Project Home: http://github.com/bobbyno/tbox/tree/master
+More on Test-Driven Learning: http://www.bobbynorton.com/?p=36
+
+== DESCRIPTION:
+
+tbox lowers the barrier to entry for test-driven learning* by providing a basic infrastructure in which
+to start coding: A directory structure, some classes and unit tests to get started, and a Rakefile to run
+all tests. For now, tbox is geared toward Ruby, though the technique is applicable to any language that
+lends itself to TDD.
+
+== BASIC USAGE:
+
+Go to the folder where you want to create your new test sandbox, and run the <code>tbox</code> command to generate your test scaffolding.
+
+ <code>
+ $ cd ~/ruby_projects
+ $ tbox learning_ruby
+ $ rake
+ </code>
+
+== SYNOPSIS:
+
+tbox is built using newgem. Although tbox was initially designed with Ruby and Test::Unit in mind, new generator
+scripts can easily be added to support other testing techniques in multiple languages. tbox can therefore become
+a starting point for test-driven learning in any language with any dependencies.
+
+== REQUIREMENTS:
+
+* newgem
+
+== INSTALL:
+
+The <code>tbox</code> application is distributed as a RubyGem and is available immediately after installation.
+
+ <code>
+ $ gem sources -a http://gems.github.com (you only have to do this once)
+ $ sudo gem install bobbyno-tbox
+ </code>
+
+Alternately, download the gem and install manually from github.
+
+== CREDITS:
+
+Bobby Norton, DRW Trading Group
+Dr. Nic Williams for newgem and rubigen: http://rubyconf2007.confreaks.com/d3t1p1_rubigen.html
31 Rakefile
View
@@ -0,0 +1,31 @@
+require 'rubygems' unless ENV['NO_RUBYGEMS']
+%w[rake rake/clean fileutils newgem rubigen].each { |f| require f }
+require File.dirname(__FILE__) + '/lib/tbox'
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+$hoe = Hoe.new('tbox', Tbox::VERSION) do |p|
+ p.developer('Bobby Norton', 'codeculturist@gmail.com')
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
+ p.rubyforge_name = p.name # TODO this is default value
+ # p.extra_deps = [
+ # ['activesupport','>= 2.0.2'],
+ # ]
+ p.extra_dev_deps = [
+ ['newgem', ">= #{::Newgem::VERSION}"]
+ ]
+
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
+ p.rsync_args = '-av --delete --ignore-errors'
+end
+
+require 'newgem/tasks' # load /tasks/*.rake
+Dir['tasks/**/*.rake'].each { |t| load t }
+
+# TODO - want other tests/tasks run by default? Add them to the list
+# task :default => [:spec, :features]
+
+task :package => [:gemspec]
12 app_generators/tbox/USAGE
View
@@ -0,0 +1,12 @@
+Description:
+
+tbox lowers the barrier to entry for test-driven learning by providing a basic infrastructure in which
+to start coding: A directory structure, some classes and unit tests to get started, and a Rakefile to run
+all tests. For now, tbox is geared toward Ruby, though the technique is applicable to any language that
+lends itself to TDD.
+
+Usage:
+
+$ cd ~/ruby_projects
+$ tbox learning_ruby
+$ rake
84 app_generators/tbox/tbox_generator.rb
View
@@ -0,0 +1,84 @@
+class TboxGenerator < RubiGen::Base
+
+ DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
+ Config::CONFIG['ruby_install_name'])
+
+ default_options :author => nil
+
+ attr_reader :name
+
+ def initialize(runtime_args, runtime_options = {})
+ super
+ usage if args.empty?
+ @destination_root = File.expand_path(args.shift)
+ @name = base_name
+ extract_options
+ end
+
+ def manifest
+ record do |m|
+ # Ensure appropriate folder(s) exists
+ m.directory ''
+ BASEDIRS.each { |path| m.directory path }
+
+ m.file "rakefile.rb", "rakefile.rb"
+ m.file "README", "README"
+ m.file "../../../MIT-LICENSE", "MIT-LICENSE"
+ # Create stubs
+ # m.template "template.rb", "some_file_after_erb.rb"
+ # m.template_copy_each ["template.rb", "template2.rb"]
+ # m.file "file", "some_file_copied"
+ # m.file_copy_each ["path/to/file", "path/to/file2"]
+ m.directory "lib/samples"
+ m.file "right_triangle.rb", "lib/samples/right_triangle.rb"
+ m.file "tbox.rb", "lib/tbox.rb"
+ m.directory "test/samples"
+ m.directory "test/data"
+ m.file "test_right_triangle.rb", "test/samples/test_right_triangle.rb"
+ m.file "test_io.rb", "test/test_io.rb"
+ m.file "file.txt", "test/data/file.txt"
+ m.file "test_helper.rb", "test/test_helper.rb"
+
+ m.dependency "install_rubigen_scripts", [destination_root, 'tbox'],
+ :shebang => options[:shebang], :collision => :force
+ end
+ end
+
+ protected
+ def banner
+ <<-EOS
+Creates a test-driven learning environment: A directory structure, some sample classes and unit tests,
+and a Rakefile to run all tests.
+
+USAGE: #{spec.name} dir_name
+EOS
+ end
+
+ def add_options!(opts)
+ opts.separator ''
+ opts.separator 'Options:'
+ # For each option below, place the default
+ # at the top of the file next to "default_options"
+ # opts.on("-a", "--author=\"Your Name\"", String,
+ # "Some comment about this option",
+ # "Default: none") { |o| options[:author] = o }
+ opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
+ end
+
+ def extract_options
+ # for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
+ # Templates can access these value via the attr_reader-generated methods, but not the
+ # raw instance variable value.
+ # @author = options[:author]
+ end
+
+ # Installation skeleton. Intermediate directories are automatically
+ # created so don't sweat their absence here.
+ BASEDIRS = %w(
+ lib
+ log
+ script
+ test
+ tmp
+ )
+end
45 app_generators/tbox/templates/README
View
@@ -0,0 +1,45 @@
+= tbox - A test-driven learning sandbox creation tool
+
+* See http://www.bobbynorton.com/?p=36 for more on tbox and test-driven learning.
+
+== Description:
+
+tbox lowers the barrier to entry for test-driven learning by providing a basic infrastructure in which
+to start coding: A directory structure, some classes and unit tests to get started, and a Rakefile to run
+all tests. For now, tbox is geared toward Ruby, though the technique is applicable to any language that
+lends itself to TDD.
+
+== Learning Ruby by Testing:
+
+If you're completely new to Ruby, you should start with "Ruby in Twenty Minutes" -
+http://www.ruby-lang.org/en/documentation/quickstart/. This will help you get Ruby installed and will
+explain the basics of Ruby programming.
+
+If you already have Ruby and rake (http://rake.rubyforge.org/files/README.html) installed and you're
+ready to start, then fire up a command prompt and run the starter tests that ship with tbox:
+
+ $ cd ~/ruby_projects
+ $ tbox learning_ruby
+ $ rake
+
+This uses the rake TestTask to run all the unit tests in your test folder. If everything goes as planned,
+running this command should produce something like:
+
+ Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader
+ Started
+ ...
+ Finished in 0.000706 seconds.
+
+ 3 tests, 4 assertions, 0 failures, 0 errors
+
+Open up the test cases in your favorite editor and have a look at what's going on. Change the tests. Add
+some new cases. Run them again every time you want to experiment with something new. Make sure you run
+all the tests in your suite before you check in at the end of your session.
+
+If you don't yet have a copy of Pickaxe, get one (http://www.pragprog.com/titles/ruby/programming-ruby).
+Add to your tbox repository as you read through the book to code up examples of what you're reading.
+
+== Credits:
+
+Bobby Norton, DRW Trading Group
+Dr. Nic Williams for newgem and rubigen: http://rubyconf2007.confreaks.com/d3t1p1_rubigen.html
3  app_generators/tbox/templates/file.txt
View
@@ -0,0 +1,3 @@
+Salam, azizam!
+
+Welcome to Expo.
9 app_generators/tbox/templates/rakefile.rb
View
@@ -0,0 +1,9 @@
+require 'rake/testtask'
+
+task :default => :test
+
+Rake::TestTask.new do |t|
+ t.libs << "test"
+ t.test_files = FileList['test/**/test*.rb']
+ t.verbose = true
+end
24 app_generators/tbox/templates/right_triangle.rb
View
@@ -0,0 +1,24 @@
+# Encapsulates the geometry of a 3-sided polygon with a right angle.
+# http://mathworld.wolfram.com/RightTriangle.html
+class RightTriangle
+
+ def initialize(base, height)
+ @a, @b = base, height
+ @c = hypotenuse
+ end
+
+ def hypotenuse
+ Math.sqrt((@a**2) + (@b**2))
+ end
+
+ def area
+ 0.5 * @a * @b
+ end
+
+private
+
+ def s
+ semiperimeter
+ end
+
+end
6 app_generators/tbox/templates/tbox.rb
View
@@ -0,0 +1,6 @@
+$:.unshift(File.dirname(__FILE__)) unless
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+module Tbox
+
+end
9 app_generators/tbox/templates/test_helper.rb
View
@@ -0,0 +1,9 @@
+require 'stringio'
+require 'test/unit'
+require File.dirname(__FILE__) + '/../lib/tbox'
+
+module Constants
+
+ TEST_DATA_DIR = File.join(File.expand_path(File.dirname(__FILE__)), "data")
+
+end
15 app_generators/tbox/templates/test_io.rb
View
@@ -0,0 +1,15 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+# Expo usage note: Keeping an inline bookmark to the rdocs for the class under study
+# makes it easy to continue your learning later.
+
+# Exploring the methods of the IO class: http://www.ruby-doc.org/core/classes/IO.html
+class TestIO < Test::Unit::TestCase
+
+ def test_readlines
+ contents = IO.readlines(File.join(Constants::TEST_DATA_DIR, "file.txt"))
+ # See String Match for =~ syntax: http://www.ruby-doc.org/core/classes/String.html#M000792
+ assert(contents[0] =~ /Salam/)
+ end
+
+end
21 app_generators/tbox/templates/test_right_triangle.rb
View
@@ -0,0 +1,21 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'samples/right_triangle'
+
+# An example of testing a class in the lib folder.
+class TestRightTriangle < Test::Unit::TestCase
+
+ def setup
+ @small = RightTriangle.new(3, 4)
+ @big = RightTriangle.new(120, 160)
+ end
+
+ def test_hypotenuse
+ assert_equal(5, @small.hypotenuse)
+ assert_equal(200, @big.hypotenuse)
+ end
+
+ def test_area
+ assert_equal(6, @small.area)
+ end
+
+end
17 bin/tbox
View
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'rubigen'
+
+if %w(-v --version).include? ARGV.first
+ require File.dirname(__FILE__) + "/../lib/tbox"
+ puts "#{File.basename($0)} #{Tbox::VERSION}"
+ exit(0)
+end
+
+require 'rubigen/scripts/generate'
+source = RubiGen::PathSource.new(:application,
+ File.join(File.dirname(__FILE__), "../app_generators"))
+RubiGen::Base.reset_sources
+RubiGen::Base.append_sources source
+RubiGen::Scripts::Generate.new.run(ARGV, :generator => 'tbox')
6 lib/tbox.rb
View
@@ -0,0 +1,6 @@
+$:.unshift(File.dirname(__FILE__)) unless
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+module Tbox
+ VERSION = '0.5.0'
+end
10 script/console
View
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# File: script/console
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+
+libs = " -r irb/completion"
+# Perhaps use a console_lib to store any extra methods I may want available in the cosole
+# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
+libs << " -r #{File.dirname(__FILE__) + '/../lib/tbox.rb'}"
+puts "Loading tbox gem"
+exec "#{irb} #{libs} --simple-prompt"
14 script/destroy
View
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
14 script/generate
View
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
41 tbox.gemspec
View
@@ -0,0 +1,41 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{tbox}
+ s.version = "0.5.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Bobby Norton"]
+ s.date = %q{2009-06-15}
+ s.default_executable = %q{tbox}
+ s.description = %q{tbox lowers the barrier to entry for test-driven learning* by providing a basic infrastructure in which to start coding: A directory structure, some classes and unit tests to get started, and a Rakefile to run all tests. For now, tbox is geared toward Ruby, though the technique is applicable to any language that lends itself to TDD.}
+ s.email = ["codeculturist@gmail.com"]
+ s.executables = ["tbox"]
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "app_generators/tbox/templates/file.txt"]
+ s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "app_generators/tbox/USAGE", "app_generators/tbox/tbox_generator.rb", "app_generators/tbox/templates/README", "app_generators/tbox/templates/file.txt", "app_generators/tbox/templates/rakefile.rb", "app_generators/tbox/templates/right_triangle.rb", "app_generators/tbox/templates/tbox.rb", "app_generators/tbox/templates/test_helper.rb", "app_generators/tbox/templates/test_io.rb", "app_generators/tbox/templates/test_right_triangle.rb", "bin/tbox", "lib/tbox.rb", "script/console", "script/destroy", "script/generate", "tbox.gemspec", "test/test_generator_helper.rb", "test/test_helper.rb", "test/test_tbox_generator.rb"]
+ s.has_rdoc = true
+ s.homepage = %q{Project Home: http://github.com/bobbyno/tbox/tree/master}
+ s.post_install_message = %q{PostInstall.txt}
+ s.rdoc_options = ["--main", "README.rdoc"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{tbox}
+ s.rubygems_version = %q{1.3.1}
+ s.summary = %q{tbox lowers the barrier to entry for test-driven learning* by providing a basic infrastructure in which to start coding: A directory structure, some classes and unit tests to get started, and a Rakefile to run all tests}
+ s.test_files = ["test/test_generator_helper.rb", "test/test_helper.rb", "test/test_tbox_generator.rb"]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<newgem>, [">= 1.4.1"])
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
+ else
+ s.add_dependency(%q<newgem>, [">= 1.4.1"])
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
+ end
+ else
+ s.add_dependency(%q<newgem>, [">= 1.4.1"])
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
+ end
+end
29 test/test_generator_helper.rb
View
@@ -0,0 +1,29 @@
+begin
+ require File.dirname(__FILE__) + '/test_helper'
+rescue LoadError
+ require 'test/unit'
+end
+require 'fileutils'
+
+# Must set before requiring generator libs.
+TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
+PROJECT_NAME = "myproject" unless defined?(PROJECT_NAME)
+app_root = File.join(TMP_ROOT, PROJECT_NAME)
+if defined?(APP_ROOT)
+ APP_ROOT.replace(app_root)
+else
+ APP_ROOT = app_root
+end
+if defined?(RAILS_ROOT)
+ RAILS_ROOT.replace(app_root)
+else
+ RAILS_ROOT = app_root
+end
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/helpers/generator_test_helper'
4 test/test_helper.rb
View
@@ -0,0 +1,4 @@
+require 'stringio'
+require 'test/unit'
+require File.dirname(__FILE__) + '/../lib/tbox'
+
63 test/test_tbox_generator.rb
View
@@ -0,0 +1,63 @@
+require File.join(File.dirname(__FILE__), "test_generator_helper.rb")
+require 'fileutils'
+
+class TestTboxGenerator < Test::Unit::TestCase
+ include RubiGen::GeneratorTestHelper
+
+ def setup
+ bare_setup
+ end
+
+ def teardown
+ bare_teardown
+ end
+
+ # Some generator-related assertions:
+ # assert_generated_file(name, &block) # block passed the file contents
+ # assert_directory_exists(name)
+ # assert_generated_class(name, &block)
+ # assert_generated_module(name, &block)
+ # assert_generated_test_for(name, &block)
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
+ #
+ # Other helper methods are:
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
+
+ def test_generator_without_options
+ run_generator('tbox', [APP_ROOT], sources)
+
+ assert_generated_file "rakefile.rb"
+ assert_generated_file "README"
+ assert_generated_file "MIT-LICENSE"
+ assert_directory_exists "lib/samples"
+ assert_generated_file "lib/samples/right_triangle.rb"
+ assert_generated_file "lib/tbox.rb"
+ assert_directory_exists "test/samples"
+ assert_generated_file "test/samples/test_right_triangle.rb"
+ assert_directory_exists "test/data"
+ assert_generated_file "test/data/file.txt"
+ assert_generated_file "test/test_io.rb"
+ assert_generated_file "test/test_helper.rb"
+
+ FileUtils::cd(APP_ROOT) do
+ puts "Running generated rakefile in #{FileUtils::pwd}"
+ `rake`
+ raise "Error occured while running generated rakefile" if ($?.to_s != "0")
+ end
+
+ p app_root_files
+ end
+
+ private
+ def sources
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__),"..", generator_path))
+ ]
+ end
+
+ def generator_path
+ "app_generators"
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.