Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RSpec to minitest

  • Loading branch information...
commit 0819822cc5c33ee841ea5eabebf8c1d1415a6b50 1 parent 0fd4557
@txus authored
View
30 Gemfile.lock
@@ -6,35 +6,13 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- configuration (1.2.0)
- diff-lcs (1.1.2)
- guard (0.2.2)
- open_gem (~> 1.4.2)
- thor (~> 0.14.3)
- guard-rspec (0.1.9)
- guard (>= 0.2.2)
- launchy (0.3.7)
- configuration (>= 0.0.5)
- rake (>= 0.8.1)
- open_gem (1.4.2)
- launchy (~> 0.3.5)
- rake (0.8.7)
- rspec (2.4.0)
- rspec-core (~> 2.4.0)
- rspec-expectations (~> 2.4.0)
- rspec-mocks (~> 2.4.0)
- rspec-core (2.4.0)
- rspec-expectations (2.4.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.4.0)
- thor (0.14.6)
+ minitest (2.0.2)
+ mocha (0.9.12)
PLATFORMS
ruby
DEPENDENCIES
- bundler (~> 1.0.7)
- guard
- guard-rspec
+ minitest
+ mocha
rpn!
- rspec (~> 2.4.0)
View
15 Guardfile
@@ -1,15 +0,0 @@
-# A sample Guardfile
-# More info at http://github.com/guard/guard#readme
-
-guard 'rspec', :version => 2 do
- watch('^spec/(.*)_spec.rb')
- watch('^lib/(.*)\.rb') { |m| "spec/lib/#{m[1]}_spec.rb" }
- watch('^spec/spec_helper.rb') { "spec" }
-
- # Rails example
- watch('^app/(.*)\.rb') { |m| "spec/#{m[1]}_spec.rb" }
- # watch('^lib/(.*)\.rb') { |m| "spec/lib/#{m[1]}_spec.rb" }
- watch('^config/routes.rb') { "spec/routing" }
- watch('^app/controllers/application_controller.rb') { "spec/controllers" }
- watch('^spec/factories.rb') { "spec/models" }
-end
View
19 Rakefile
@@ -1,10 +1,19 @@
require 'bundler'
Bundler::GemHelper.install_tasks
-require 'rspec/core'
-require 'rspec/core/rake_task'
-RSpec::Core::RakeTask.new(:spec) do |spec|
- spec.pattern = FileList['spec/**/*_spec.rb']
+require 'rake/testtask'
+Rake::TestTask.new do |t|
+ t.libs << "test"
+ t.test_files = FileList['test/**/*_test.rb'] - FileList['test/acceptance/**/*_test.rb']
+ t.verbose = true
end
-task :default => :spec
+require 'rake/testtask'
+desc 'Run acceptance tests'
+Rake::TestTask.new :acceptance do |t|
+ t.libs << "test"
+ t.test_files = FileList['test/acceptance/**/*_test.rb']
+ t.verbose = true
+end
+
+task :default => [:test, :acceptance]
View
6 rpn.gemspec
@@ -14,10 +14,8 @@ Gem::Specification.new do |s|
s.rubyforge_project = "rpn"
- s.add_development_dependency 'bundler', '~> 1.0.7'
- s.add_development_dependency 'rspec', '~> 2.4.0'
- s.add_development_dependency 'guard'
- s.add_development_dependency 'guard-rspec'
+ s.add_development_dependency 'minitest'
+ s.add_development_dependency 'mocha'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
17 spec/acceptance/acceptance_spec.rb
@@ -1,17 +0,0 @@
-require 'spec_helper'
-
-module RPN
- describe Calculator do
-
- it 'works like a charm' do
- subject.solve("2 3 +").should == 5
- subject.solve("90 3 -").should == 87
- subject.solve("10 4 3 + 2 * -").should == -4
- subject.solve("10 4 3 + 2 * - 2 /").should == -2
- subject.solve("90 34 12 33 55 66 + * - + -").should == 4037
- subject.solve("2 3 ^").should == 8
- end
-
- end
-
-end
View
47 spec/rpn/calculator_spec.rb
@@ -1,47 +0,0 @@
-require 'spec_helper'
-
-module RPN
- describe Calculator do
- describe "#initialize" do
- context "with no arguments" do
- it 'sets default arity' do
- Stack.should_receive(:new).with(2)
- Calculator.new
- end
- it 'sets default delimiter' do
- Parser.should_receive(:new).with(' ')
- Calculator.new
- end
- end
- it 'accepts a custom arity' do
- Stack.should_receive(:new).with(3)
- Calculator.new :arity => 3
- end
- it 'accepts a custom delimiter' do
- Parser.should_receive(:new).with(',')
- Calculator.new :delimiter => ','
- end
- it 'creates an accessible stack' do
- calculator = Calculator.new
- calculator.stack.should be_kind_of(Stack)
- end
- it 'creates an accessible parser' do
- calculator = Calculator.new
- calculator.parser.should be_kind_of(Parser)
- end
- end
-
- describe "#solve" do
- it 'passes the string to the parser' do
- subject.stack.stub(:solve)
- subject.parser.should_receive(:parse).with "3 4 +"
- subject.solve "3 4 +"
- end
- it 'delegates solving to the stack' do
- subject.parser.stub(:parse).and_return ["3", "4", "+"]
- subject.stack.should_receive(:solve).with ["3", "4", "+"]
- subject.solve "3 4 +"
- end
- end
- end
-end
View
60 spec/rpn/parser_spec.rb
@@ -1,60 +0,0 @@
-require 'spec_helper'
-
-module RPN
- describe Parser do
- describe "#initialize" do
- it 'sets the delimiter' do
- parser = Parser.new ' '
- parser.delimiter.should == ' '
- end
- describe "makes the parser raise an InvalidDelimiterError" do
- %w{. + - * / ^ 19 425 0}.each do |invalid_delimiter|
- it "when using #{invalid_delimiter} as a delimiter" do
- expect {
- Parser.new invalid_delimiter
- }.to raise_error(Parser::InvalidDelimiterError)
- end
- end
- end
- end
-
- describe "#parse" do
- context "with a space delimiter" do
- subject { Parser.new ' ' }
-
- it 'returns a tokenized array' do
- subject.parse("1 4 5 + -").should == [1, 4, 5, :+, :-]
- subject.parse("1 2 3 4 5 6 7 8 + - * / ^").should == [1, 2, 3, 4, 5, 6, 7, 8, :+, :-, :*, :/, :**]
- end
- describe "edge cases" do
- it 'strips any extra spaces' do
- subject.parse("1 4 5 + -").should == [1, 4, 5, :+, :-]
- end
- end
- end
- context "with a comma delimiter" do
- subject { Parser.new ',' }
-
- it 'returns a tokenized array' do
- subject.parse("1,4,5.3,+,-").should == [1, 4, 5.3, :+, :-]
- subject.parse("1,2,3,4,5,6,7,8,+,-,*,/,^").should == [1,2,3,4,5,6,7,8,:+,:-,:*,:/,:**]
- end
- describe "edge cases" do
- it 'strips any extra spaces and delimiter' do
- subject.parse("1,, 4 , 5.3 ,,,+, -").should == [1, 4, 5.3, :+, :-]
- end
- end
- end
- context "with inconsistent or malformed strings" do
- subject { Parser.new ',' }
-
- it 'raises a MalformedStringError' do
- expect {
- subject.parse "1,4.5,&+,-"
- }.to raise_error(Parser::MalformedStringError, "Offending token: &+")
- end
- end
- end
-
- end
-end
View
92 spec/rpn/stack_spec.rb
@@ -1,92 +0,0 @@
-require 'spec_helper'
-
-module RPN
- describe Stack do
- subject { Stack.new 2 }
-
- describe "#initialize" do
- it 'sets the delimiter' do
- stack = Stack.new 3
- stack.arity.should == 3
- end
- end
-
- describe "#solve", "iterates the tokens" do
- context 'when the token is a value' do
- it 'pushes it to the stack' do
- subject.should_receive(:push).with 3
- subject.solve [3]
- end
- end
- context 'when the token is an operator' do
- context 'if the size of the stack is below the arity' do
- it 'raises an error' do
- subject.stub(:size).and_return 1
- expect {
- subject.solve [:+]
- }.to raise_error(Stack::InsufficientValuesAvailable)
- end
- end
- context 'otherwise' do
- it 'pops n elements from the stack and pushes the result' do
- subject.stub(:size).and_return 3, 1
- elements = [1,2]
- subject.should_receive(:pop).with(subject.arity).and_return elements
- subject.should_receive(:push).with(3)
-
- subject.solve [:+]
- end
- end
- end
- context 'when at the end there is more than one result' do
- it 'raises an error' do
- subject.stub(:size).and_return 2
- expect {
- subject.solve [:+]
- }.to raise_error(Stack::UnsolvableExpressionError)
- end
- end
- context 'when everything is fine' do
- it 'returns the result' do
- subject.stub(:size).and_return 3, 1
- elements = [1,2]
- subject.stub(:pop).with(subject.arity).and_return elements
-
- subject.solve([:+]).should == 3
- end
- end
-
- it 'clears the stack before each call' do
- subject.solve [3]
- subject.solve [4]
- subject.elements.should == [4]
- end
- end
-
- describe "#push" do
- it 'pushes a token to the elements collection' do
- subject.elements.should_receive(:push).with 3
- subject.push 3
- end
- end
- describe "#pop" do
- it 'pushes a token to the elements collection' do
- subject.elements.should_receive(:pop).with 2
- subject.pop 2
- end
- end
- describe "#size" do
- it 'pushes a token to the elements collection' do
- subject.elements.should_receive(:size)
- subject.size
- end
- end
- describe "#clear" do
- it 'clears the stack' do
- subject.elements.should_receive(:clear)
- subject.clear
- end
- end
-
- end
-end
View
17 spec/spec_helper.rb
@@ -1,17 +0,0 @@
-require 'bundler'
-begin
- Bundler.setup(:default, :development)
-rescue Bundler::BundlerError => e
- $stderr.puts e.message
- $stderr.puts "Run `bundle install` to install missing gems"
- exit e.status_code
-end
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-
-require 'rpn'
-require 'rspec'
-
-# Requires supporting files with custom matchers and macros, etc,
-# in ./support/ and its subdirectories.
-Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
View
19 test/acceptance/acceptance_test.rb
@@ -0,0 +1,19 @@
+require 'test_helper'
+
+module RPN
+ class CalculatorAcceptanceTest < MiniTest::Unit::TestCase
+
+ def setup
+ @calculator = Calculator.new
+ end
+
+ def test_it_works
+ assert_equal 5, @calculator.solve("2 3 +")
+ assert_equal 87, @calculator.solve("90 3 -")
+ assert_equal -4, @calculator.solve("10 4 3 + 2 * -")
+ assert_equal -2, @calculator.solve("10 4 3 + 2 * - 2 /")
+ assert_equal 4037, @calculator.solve("90 34 12 33 55 66 + * - + -")
+ assert_equal 8, @calculator.solve("2 3 ^")
+ end
+ end
+end
View
51 test/rpn/calculator_test.rb
@@ -0,0 +1,51 @@
+require 'test_helper'
+
+module RPN
+ class CalculatorTest < MiniTest::Unit::TestCase
+
+ def test_initialize_sets_default_arity
+ Stack.expects(:new).with(2)
+ Calculator.new
+ end
+
+ def test_initialize_sets_default_delimiter
+ Parser.expects(:new).with(' ')
+ Calculator.new
+ end
+
+ def test_accepts_custom_arity
+ Stack.expects(:new).with(3)
+ Calculator.new :arity => 3
+ end
+
+ def test_accepts_custom_delimiter
+ Parser.expects(:new).with(',')
+ Calculator.new :delimiter => ','
+ end
+
+ def test_creates_an_accessible_stack
+ assert_kind_of Stack, Calculator.new.stack
+ end
+
+ def test_creates_an_accessible_stack
+ assert_kind_of Parser, Calculator.new.parser
+ end
+
+ def test_solve_passes_the_string_onto_the_parser
+ calculator = Calculator.new
+ calculator.stack.stubs(:solve)
+ calculator.parser.expects(:parse).with "3 4 +"
+
+ calculator.solve "3 4 +"
+ end
+
+ def test_solve_elegates_solving_to_the_stack
+ calculator = Calculator.new
+ calculator.parser.stubs(:parse).returns ["3", "4", "+"]
+ calculator.stack.expects(:solve).with ["3", "4", "+"]
+
+ calculator.solve "3 4 +"
+ end
+
+ end
+end
View
45 test/rpn/parser_test.rb
@@ -0,0 +1,45 @@
+require 'test_helper'
+
+module RPN
+ class ParserTest < MiniTest::Unit::TestCase
+
+ def test_initialize_sets_the_delimiter
+ assert_equal ' ', Parser.new(' ').delimiter
+ end
+
+ def test_invalid_delimiters
+ %w{. + - * / ^ 19 425 0}.each do |invalid_delimiter|
+ assert_raises Parser::InvalidDelimiterError do
+ Parser.new invalid_delimiter
+ end
+ end
+ end
+
+ def test_parser_with_a_space_delimiter_returns_a_tokenized_array
+ parser = Parser.new(' ')
+ assert_equal [1, 4, 5, :+, :-],
+ parser.parse("1 4 5 + -")
+ assert_equal [1, 2, 3, 4, 5, 6, 7, 8, :+, :-, :*, :/, :**],
+ parser.parse("1 2 3 4 5 6 7 8 + - * / ^")
+ assert_equal [1, 4, 5, :+, :-],
+ parser.parse("1 4 5 + -")
+ end
+
+ def test_parser_with_a_comma_delimiter_returns_a_tokenized_array
+ parser = Parser.new(',')
+ assert_equal [1, 4, 5.3, :+, :-],
+ parser.parse("1,4,5.3,+,-")
+ assert_equal [1, 2, 3, 4, 5, 6, 7, 8, :+, :-, :*, :/, :**],
+ parser.parse("1,2,3,4,5,6,7,8,+,-,*,/,^")
+ assert_equal [1, 4, 5, :+, :-],
+ parser.parse("1,,, 4 ,, 5, + ,-")
+ end
+
+ def test_parser_with_inconsistent_or_malformed_strings
+ assert_raises Parser::MalformedStringError do
+ Parser.new(' ').parse("1 4.5 &+ -")
+ end
+ end
+
+ end
+end
View
76 test/rpn/stack_test.rb
@@ -0,0 +1,76 @@
+require 'test_helper'
+
+module RPN
+ class StackTest < MiniTest::Unit::TestCase
+ def setup
+ @stack = Stack.new(2)
+ end
+
+ def test_initialize_sets_the_arity
+ assert_equal 3, Stack.new(3).arity
+ end
+
+ def test_solve_pushes_a_value_token
+ @stack.expects(:push).with 3
+ @stack.solve [3]
+ end
+
+ def test_solve_with_operator_token_raises_if_the_size_is_below_arity
+ @stack.stubs(:size).returns 1
+ assert_raises Stack::InsufficientValuesAvailable do
+ @stack.solve [:+]
+ end
+ end
+
+ def test_solve_with_operator_token_pops_n_elements_and_pushes_result
+ @stack.stubs(:size).returns 3, 1
+ elements = [1, 2]
+
+ @stack.expects(:pop).with(2).returns(elements)
+ @stack.expects(:push).with(3)
+
+ @stack.solve [:+]
+ end
+
+ def test_solve_raises_error_if_more_than_one_result
+ @stack.stubs(:size).returns(2)
+ assert_raises Stack::UnsolvableExpressionError do
+ @stack.solve [:+]
+ end
+ end
+
+ def test_solve_returns_the_result_if_everything_is_ok
+ @stack.stubs(:size).returns 3, 1
+ elements = [1, 2]
+ @stack.stubs(:pop).with(2).returns elements
+
+ assert_equal 3, @stack.solve([:+])
+ end
+
+ def test_solve_clears_the_stack_before_each_call
+ @stack.solve [3]
+ @stack.solve [4]
+ assert_equal [4], @stack.elements
+ end
+
+ def test_push_delegates_to_elements
+ @stack.elements.expects(:push).with 3
+ @stack.push 3
+ end
+
+ def test_pop_delegates_to_elements
+ @stack.elements.expects(:pop).with 2
+ @stack.pop 2
+ end
+
+ def test_size_delegates_to_elements
+ @stack.elements.expects(:size)
+ @stack.size
+ end
+
+ def test_clear_clears_the_stack
+ @stack.elements.expects(:clear)
+ @stack.clear
+ end
+ end
+end
View
8 test/test_helper.rb
@@ -0,0 +1,8 @@
+require 'rubygems' if RUBY_VERSION == '1.8.7'
+
+gem 'minitest'
+require 'minitest/unit'
+require 'minitest/autorun'
+require 'mocha'
+
+require 'rpn'
Please sign in to comment.
Something went wrong with that request. Please try again.