From 845802532a1b3aba880e71ec15726c254ce2d69f Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 24 Jan 2017 14:05:56 -0500 Subject: [PATCH 1/7] Change rspec output to use documentation format --- .rspec | 1 + 1 file changed, 1 insertion(+) diff --git a/.rspec b/.rspec index 0d786ba..e67c38c 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,4 @@ --color --warnings --require spec_helper +--format documentation From 65ff19ddd38442dc2b45b3f4ce461bed85d776fe Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 24 Jan 2017 14:06:27 -0500 Subject: [PATCH 2/7] Update gems and add some groupings --- Gemfile | 14 +++++++++++--- Gemfile.lock | 15 ++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 2c0786c..4fa89fe 100644 --- a/Gemfile +++ b/Gemfile @@ -4,9 +4,17 @@ gem 'sinatra', '1.4.5' gem 'tilt', '1.4.1' gem 'rack', '1.5.2' gem 'rack-protection', '1.5.0' -gem 'rspec', '3.0.0' -gem 'rspec-core', '3.0.1' gem 'math24', '~>2.0.0' gem 'require_all', '~>1.4.0' gem 'thin', '~>1.7.0' -gem 'newrelic_rpm', '~>3.17.1' + +group :development, :test do + gem 'rspec', '3.0.0' + gem 'rspec-core', '3.0.1' + gem 'rack-test' + gem 'byebug' +end + +group :production do + gem 'newrelic_rpm', '~>3.17.1' +end diff --git a/Gemfile.lock b/Gemfile.lock index 4bce367..50259a7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,17 @@ GEM remote: https://rubygems.org/ specs: + byebug (9.0.6) daemons (1.2.4) - diff-lcs (1.2.5) + diff-lcs (1.3) eventmachine (1.2.1) math24 (2.0.1) - newrelic_rpm (3.17.1.326) + newrelic_rpm (3.17.2.327) rack (1.5.2) rack-protection (1.5.0) rack + rack-test (0.6.3) + rack (>= 1.0) require_all (1.4.0) rspec (3.0.0) rspec-core (~> 3.0.0) @@ -16,12 +19,12 @@ GEM rspec-mocks (~> 3.0.0) rspec-core (3.0.1) rspec-support (~> 3.0.0) - rspec-expectations (3.0.1) + rspec-expectations (3.0.4) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.0.0) - rspec-mocks (3.0.1) + rspec-mocks (3.0.4) rspec-support (~> 3.0.0) - rspec-support (3.0.0) + rspec-support (3.0.4) sinatra (1.4.5) rack (~> 1.4) rack-protection (~> 1.4) @@ -36,10 +39,12 @@ PLATFORMS ruby DEPENDENCIES + byebug math24 (~> 2.0.0) newrelic_rpm (~> 3.17.1) rack (= 1.5.2) rack-protection (= 1.5.0) + rack-test require_all (~> 1.4.0) rspec (= 3.0.0) rspec-core (= 3.0.1) From 42b5e8387a36e0d1c5101b137ff00272d22a89e8 Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 24 Jan 2017 14:07:34 -0500 Subject: [PATCH 3/7] Delete unused lib and bin files --- bin/.keep | 0 bin/math24 | 8 ----- bin/math24solver | 8 ----- lib/.keep | 0 lib/math24.rb | 35 ---------------------- lib/math24gamecli.rb | 24 --------------- lib/math24solver.rb | 68 ------------------------------------------ lib/math24solvercli.rb | 37 ----------------------- lib/math24web.rb | 66 ---------------------------------------- 9 files changed, 246 deletions(-) create mode 100644 bin/.keep delete mode 100755 bin/math24 delete mode 100755 bin/math24solver create mode 100644 lib/.keep delete mode 100644 lib/math24.rb delete mode 100644 lib/math24gamecli.rb delete mode 100644 lib/math24solver.rb delete mode 100644 lib/math24solvercli.rb delete mode 100644 lib/math24web.rb diff --git a/bin/.keep b/bin/.keep new file mode 100644 index 0000000..e69de29 diff --git a/bin/math24 b/bin/math24 deleted file mode 100755 index 029976f..0000000 --- a/bin/math24 +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env ruby - -require 'math24' -require_relative '../lib/math24solver.rb' -require_relative '../lib/math24gamecli.rb' - -cli = Math24GameCli.new -cli.run() \ No newline at end of file diff --git a/bin/math24solver b/bin/math24solver deleted file mode 100755 index 807ef9e..0000000 --- a/bin/math24solver +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env ruby - -require 'math24' -require_relative '../lib/math24solver.rb' -require_relative '../lib/math24solvercli.rb' - -cli = Math24SolverCLI.new -cli.run() \ No newline at end of file diff --git a/lib/.keep b/lib/.keep new file mode 100644 index 0000000..e69de29 diff --git a/lib/math24.rb b/lib/math24.rb deleted file mode 100644 index 61ec37a..0000000 --- a/lib/math24.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'math24solver' - -class Math24 - - attr_accessor :numbers - attr_reader :last_answer - - def initialize - end - - def solution?(solution) - if solution.scan(/\A(\(*(\d{1}[()\s]*[*+-\/]+[()\s]*){3}\d{1}\)*)\z/).flatten.first == solution - @last_answer = instance_eval(solution) - return last_answer == 24 - else - @last_answer = "???" - end - return false - end - - def generate_problem() - loop do - numbers = [] - 4.times do |i| - numbers << rand(1..9).to_s - end - math24 = Math24Solver.new() - math24.numbers = numbers - if math24.solve() != "No solution found" - @numbers = numbers - return numbers - end - end - end -end \ No newline at end of file diff --git a/lib/math24gamecli.rb b/lib/math24gamecli.rb deleted file mode 100644 index fb023e4..0000000 --- a/lib/math24gamecli.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'math24' - -def run() - game = Math24.new() - - problem = game.generate_problem().join(' ') - puts "Solve: #{problem}" - attempt = gets.chomp - - while attempt.downcase != "exit" - solved = game.solution?(attempt) - if solved - puts "Correct!\n\n" - solved = false - problem = game.generate_problem().join(' ') - else - puts "Try again...\n\n" unless problem.empty? - end - puts "Solve: #{problem}" - attempt = gets.chomp - end -end - -run() \ No newline at end of file diff --git a/lib/math24solver.rb b/lib/math24solver.rb deleted file mode 100644 index 7af70a6..0000000 --- a/lib/math24solver.rb +++ /dev/null @@ -1,68 +0,0 @@ -class Math24Solver - attr_accessor :numbers - attr_reader :operators - attr_accessor :target - - def initialize(operators = ["+", "-", "*", "/"]) - @operators = operators - @target = 24 - end - - def solve(problem_numbers = []) - if problem_numbers.empty? - return "Please provide numbers" if @numbers.empty? - else - @numbers = problem_numbers - end - - @numbers.each do |number| - if (number.to_i < 1) || (number.to_i > 9) || (number.to_f != number.to_i) - return "Please only enter integers between 1 and 9" - end - end - - op_permutation = @operators.repeated_permutation(3) - num_permutation = @numbers.permutation(4).to_a.uniq - - num_permutation.each do |numbers| - op_permutation.each do |operators| - begin - forward_result = instance_eval("((#{numbers[0].to_f} #{operators[0]} #{numbers[1]}.to_f) #{operators[1]} #{numbers[2]}.to_f) #{operators[2]} #{numbers[3].to_f}") - rescue ZeroDivisionError - forward_result = 0 - end - begin - alternate_result = instance_eval("(#{numbers[0].to_f} #{operators[0]} #{numbers[1].to_f}) #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f})") - rescue ZeroDivisionError - alternate_result = 0 - end - - begin - reverse_result = instance_eval("#{numbers[0].to_f} #{operators[0]} (#{numbers[1].to_f} #{operators[1]} (#{numbers[2].to_f} #{operators[2]} #{numbers[3].to_f}))") - rescue ZeroDivisionError - reverse_result = 0 - end - - if forward_result == @target - if (operators.include?("+") || operators.include?("-")) && (operators.include?("*") || operators.include?("/")) - #Might need parentheses for order of operations - return "((#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} #{numbers[2]}) #{operators[2]} #{numbers[3]} = 24" - else - return "#{numbers[0]} #{operators[0]} #{numbers[1]} #{operators[1]} #{numbers[2]} #{operators[2]} #{numbers[3]} = 24" - end - elsif alternate_result == @target - return "(#{numbers[0]} #{operators[0]} #{numbers[1]}) #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]}) = 24" - elsif reverse_result == @target - return "#{numbers[0]} #{operators[0]} (#{numbers[1]} #{operators[1]} (#{numbers[2]} #{operators[2]} #{numbers[3]})) = 24" - end - end - end - return "No solution found" - end -end - - - - - - diff --git a/lib/math24solvercli.rb b/lib/math24solvercli.rb deleted file mode 100644 index 2c08f66..0000000 --- a/lib/math24solvercli.rb +++ /dev/null @@ -1,37 +0,0 @@ -class Math24SolverCLI - attr_reader :operators - - def initialize - @operators = ["+", "-", "*", "/"] - end - - def run() - puts "Welcome to Math 24 solver! Please enter your numbers to see solution" - command = "" - solver = Math24Solver.new(operators) - command = gets.chomp - while command.downcase != "exit" - if command.downcase == "help" - puts "Input four numbers, and Math24 solution will be printed" - else - number_entry = command - if number_entry.include?(",") - numbers =number_entry.gsub(" ", "").split(",") - else - number_entry.squeeze(" ") - numbers = number_entry.split(" ") - end - if numbers.size == 4 - solver.numbers = numbers - puts solver.solve() - print "\n" - else - puts "Please enter exactly four numbers" - end - end - - command = gets.chomp - end - puts "Thanks for playing!" - end -end \ No newline at end of file diff --git a/lib/math24web.rb b/lib/math24web.rb deleted file mode 100644 index c27ca62..0000000 --- a/lib/math24web.rb +++ /dev/null @@ -1,66 +0,0 @@ -require "sinatra/base" -require "math24" - -class Math24Web < Sinatra::Base - get '/' do - erb :index - end - - get '/about' do - erb :about - end - - get '/problem' do - math24 = Math24.new - problem = math24.generate_problem.join(" ") - erb :problem, :locals => {:problem => problem} - end - - post '/problem' do - math24 = Math24.new - problem = "#{params[:problem] || ""}" - erb :problem, :locals => {:problem => problem} - end - - get '/solve' do - erb :solve, :locals => {:invalid => false} - end - - post '/solution' do - math24solver = Math24Solver.new - problem = "#{params[:problem] || ""}" - if problem.include?(",") - numbers = problem.gsub(" ","").split(",") - else - numbers = problem.squeeze(" ").split(" ") - end - valid = numbers.all? do |number| - number.to_i > 0 && number.to_i < 10 - end - if valid - solution = math24solver.solve(numbers) - erb :solution, :locals => {:problem => numbers.join(" "), - :solution => solution, - :last_answer => 24} - else - erb :solve, :locals => {:invalid => true} - end - end - - post '/check' do - problem = "#{params[:problem] || ""}" - solution = "#{params[:solution] || ""}" - math24 = Math24.new - math24.numbers = problem.split(" ") - if math24.solution?(solution) - erb :correct, :locals => {:problem => problem, - :solution => solution, - :last_answer => math24.last_answer} - else - erb :incorrect, :locals => {:problem => problem, - :solution => solution, - :last_answer => math24.last_answer} - end - end -end - From 794ac322457e83b1afb140cc7e4bc2a281b41d49 Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 24 Jan 2017 14:07:56 -0500 Subject: [PATCH 4/7] Ignore logging files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 05b56e9..4073378 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /.bundle/ +/log/ math24-*.gem +.byebug_history From 88965d1941b902f87ed4422b7828dcaadcd8baf7 Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 24 Jan 2017 14:08:26 -0500 Subject: [PATCH 5/7] Delete old gem and gemspec files --- math24-1.1.2.gem | Bin 5120 -> 0 bytes math24.gemspec | 14 -------------- 2 files changed, 14 deletions(-) delete mode 100644 math24-1.1.2.gem delete mode 100644 math24.gemspec diff --git a/math24-1.1.2.gem b/math24-1.1.2.gem deleted file mode 100644 index f0a4355a56d3d3ccfb72467754661d0c4fb5d2ba..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5120 zcmeHJX*?8a7awc3EX7bE`!b0!#+u1mWStCVlI4!!W*LbYhHF>$7O$N!mh2JPv&)h_ zp|M>OV+fU9O7pt!{m}jO;e9{V+Xv^%d7j_-Jr*A~V%UX~hx}XZWEC`#dxL#u?7C@MJQvm8Fx9d8TqI=hxATk-o&@7Br+pF}URyt9%uy+-8q z=Yp9m`E5=9W<3ISfgr4w$Qx}FqHIh5;sb|<@~vjPlhLlc?Rrx{f(84mZyH$Au7%T` z?BG|GW64Hzc@kWeI|im-O$lEx6i9FN8^!ge7>-DauV2g_f|PcQi;bC`#ruJGI=o>X z?5yyN4VLo!3#Y`rhE_v9xwwACQ<7qz64=-!70W#_2Jw%J{YB}Xp%_mW`>P~ZTvlkR z-XHjE+Kx7RTp#ywGdV@usB~T4qoMRUAFqBzIY&(0IqHaU%}I=`e*g07>Xdau06J^O zHA{!+Q@Av2FBKfR+xd{h>)+pHxS2xI$uMs6H4!Ep*f~E&#n`6z2lci_UquONelf0) z_Kk63bpP@)uv=8Gv!G=avt-gSp?ZmbL-u3aZkVFrT)3PVhmRm%8;+RC_NV*ABB73( z{_l$X5BCol@PFh#1PW39p8pC;kR$&8jGpn0|EnlBcM)aIMTM633H@Z7S27Ka^ki-W z6H+m;tavogMe{{DU7b~9`dZi`Ew!oggPhC)Jl;I9yn{A#eQzBzG#|n|KOk-qV_C5C z`@qxlC8V|2j|&rLOD!9&SFR7&tX?l6X;Ew32(BH)O*)%-IkeeWAmBY$OY$nY^T2eH zd}$;(N3{(D@Hf7r;af~%@ZU}ac=BZ2EqR2IY0Zl+9kx$1PBwmVW3x-a649AJXt!M;?fPdMUYMkFnlH2k3J!clFZUs0g>YC*ch!RA)nXyTQ zSxJxO8nNTz4Ng&hZ!%%BFu1#nXEFOD)PLctC#|*wBh4tyYL}LaQT=o<6Qe3v^sYi@ zVP7+iH-HcqnBIfzpQnR;a>xqfOiDFU&9ioA*I3qR%I3(p;kR3OMn}~}U8&4Hd|=q? z%b3s2589hAo%o)+8$VCa-j{l_c$c|IzlGSvMiE*Nj^I{|^iXH;XThKlwe*pkG`+m( z+~Hfy8z=bM65#^oXV@667H;Y5tS~Hx)0{g={T-0X5O^fcpQE%MyMb0ymyRK+D>|Rlq z3ChT_lab&`8Or1YTkz^!TsR$N?A)kJ)S<>FS=F)U6n*71y)!61pYMEm*cqFbm#$Gdz0v>IvAwpBp1t(k#Yr_e)T4`)w{tuZrEd zJOemn5g}I!tD3cDs$vWv^@ltH4u`ID@X&2;+bKCa=R+G-d^Juk;klA-UhP&YDcmZQ zl$zkHm#opMnX;!mSSXuTWu=EuFbONg4sL=3BIo^f8;8C0V=0nZN`Ulnde+OH1r|U% zu~!D}ie1Ln_M^kMZ8U1p`N2F{nrglrO#)h4SaJw6kM9Q)RI%9zW!3f2>#bDv)N&+! za(y0WCB&B&*EQl!&uUST-QIRmHUPPHAo~)3u%Vi{HhelP;RBpF@?!jyzp3@=up0#1-6Yjo$b1=IS*t01gFCeuv`cc7lOhyxm%bGuVFP&mR)e|A2#fz=&!K0<_m=GMvpEUV-oXvm|0wz--2 zlE!1g`aNHe*6$=Wl?}#4tH_lBY%3mx`yO$oq0b@|9qaw`B*sBh$d$hR@(k(|m0H|| zT9PaEu-8(lbO5V(tQtV@XEfel#0SR75#x&Ua>L2`qTR4RW()XhHU4k=KU7}vyZH}Q z0xKTP|DRDJ-{!w#th^xbP?W24ist}@4b@=0G2;p&)e84ldcw+0dG>g8*FA~ zZPx?qZ8cWrmN|kx?KRu!@pxf(gs)fXXN%Evg$6T~D>3Ok&8<%eusR@zSZL?lT3=Ad?{c>rz0MVE zy;g5F1uR`#5(0KcRJ*)GRjU<3_spFoG?0w34)*H|io}tZnK*}66EbU!y@0Y{U% Date: Tue, 24 Jan 2017 14:10:14 -0500 Subject: [PATCH 6/7] Delete old test files for gem --- spec/math24_spec.rb | 26 -------------- spec/math24solver_spec.rb | 68 ------------------------------------ spec/math24solvercli_spec.rb | 39 --------------------- 3 files changed, 133 deletions(-) delete mode 100644 spec/math24_spec.rb delete mode 100644 spec/math24solver_spec.rb delete mode 100644 spec/math24solvercli_spec.rb diff --git a/spec/math24_spec.rb b/spec/math24_spec.rb deleted file mode 100644 index f0df815..0000000 --- a/spec/math24_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -require_relative "spec_helper.rb" - -describe Math24 do - let(:math24) { Math24.new } - - it "should generate an array of numbers" do - problem = math24.generate_problem() - expect(problem.class).to eq(Array) - expect(problem.size).to eq(4) - expect(problem[0].class).to eq(String) - end - - it "should generate a different problem each time" do - problem1 = math24.generate_problem - problem2 = math24.generate_problem - - expect(problem1).not_to eq(problem2) - end - - it "should accept the solver's solution" do - problem = math24.generate_problem - solver = Math24Solver.new() - solution = solver.solve(problem).gsub(" = 24", "") - expect(math24.solution?(solution)).to eq(true) - end -end \ No newline at end of file diff --git a/spec/math24solver_spec.rb b/spec/math24solver_spec.rb deleted file mode 100644 index 3db2c8b..0000000 --- a/spec/math24solver_spec.rb +++ /dev/null @@ -1,68 +0,0 @@ -require_relative "spec_helper.rb" - -describe Math24Solver do - let (:math24) { Math24Solver.new() } - - context 'initialize' do - it "sets default operators" do - math24.operators == ["+", "-", "*", "/"] - end - end - - context 'solve' do - it "only accepts integers" do - math24.numbers = ["1", "1", "1", "1.5"] - expect(math24.solve).to eq("Please only enter integers between 1 and 9") - end - - it "does not accept negative numbers" do - math24.numbers = ["1", "1", "1", "-1"] - expect(math24.solve).to eq("Please only enter integers between 1 and 9") - end - - it "does not accept zero" do - math24.numbers = ["1", "1", "1", "0"] - expect(math24.solve).to eq("Please only enter integers between 1 and 9") - end - - it "does not accept numbers greater than 9" do - math24.numbers = ["1", "1", "1", "10"] - expect(math24.solve).to eq("Please only enter integers between 1 and 9") - end - - it "solves a simple addition example" do - math24.numbers = ["6", "6", "6", "6"] - expect(math24.solve).to eq("6 + 6 + 6 + 6 = 24") - end - - it "solves a more complex example" do - math24.numbers = ["9", "8", "3", "2"] - expect(math24.solve).to eq("((9 - 3) * 8) / 2 = 24") - end - - it "solves problems where order of operations matters" do - math24.numbers = ["9", "6", "6", "5"] - expect(math24.solve).to eq("(9 * 6) - (6 * 5) = 24") - end - - it "solves another problem where OOP matters" do - math24.numbers = ["9", "9", "8", "4"] - expect(math24.solve).to eq("8 * (4 - (9 / 9)) = 24") - end - - it "solves a third problem where OOP matters" do - math24.numbers = ["6", "5", "7", "7"] - expect(math24.solve).to eq ("6 * (5 - (7 / 7)) = 24") - end - - it "accepts numbers as arguments" do - math24.solve(["9","8","3","2"]) - expect(math24.numbers).to eq(["9","8","3","2"]) - end - - it "provides feedback if no solution found" do - math24.numbers = ["1", "1", "1", "1"] - expect(math24.solve).to eq("No solution found") - end - end -end \ No newline at end of file diff --git a/spec/math24solvercli_spec.rb b/spec/math24solvercli_spec.rb deleted file mode 100644 index e124322..0000000 --- a/spec/math24solvercli_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -require_relative "spec_helper.rb" - -describe 'Running math24 solver' do - let (:math24) { Math24SolverCLI.new() } - - context 'entering commands' do - describe 'help' do - it 'prints instructions on how to use solver' do - #math24.stub(:gets).and_return('help', 'exit') - allow(math24).to receive(:gets).and_return('help','exit') - help_output = capture_stdout { math24.run } - expect(help_output).to match(/Input four numbers, and Math24 solution will be printed/) - - end - end - - describe 'exit' do - it 'prints a goodbye message' do - allow(math24).to receive(:gets).and_return('exit') - exit_output = capture_stdout { math24.run } - expect(exit_output).to match(/Thanks for playing!/) - end - end - end - - context 'providing numbers' do - it 'prints instructions if too many numbers are given' do - allow(math24).to receive(:gets).and_return('1 2 3 4 5', 'exit') - number_output = capture_stdout { math24.run } - expect(number_output).to match(/Please enter exactly four numbers/) - end - - it 'prints the solution if possible' do - allow(math24).to receive(:gets).and_return('1 2 3 4', 'exit') - number_output = capture_stdout { math24.run } - expect(number_output).to match(/\(\(1 \+ 2\) \+ 3\) \* 4 = 24/) - end - end -end \ No newline at end of file From db1e1d7a2966b82573e64c2a3308ae09cbf3d961 Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 24 Jan 2017 14:35:25 -0500 Subject: [PATCH 7/7] Update app to use new syntax for Math24 gem *Delete unused controller action 'post /problem' *Delete lots of comments *Add tests for controller *Remove dependency on Math24 last_answer instance variable *Fix some whitespace errors *Delete capture_stdout method from spec_helper *Other minor refactoring --- app/controllers/math24_controller.rb | 58 ++++++++------ app/views/math24/correct.erb | 4 +- app/views/math24/solution.erb | 4 +- config/environment.rb | 12 +-- spec/math24_controller_spec.rb | 115 +++++++++++++++++++++++++++ spec/spec_helper.rb | 80 +------------------ 6 files changed, 156 insertions(+), 117 deletions(-) create mode 100644 spec/math24_controller_spec.rb diff --git a/app/controllers/math24_controller.rb b/app/controllers/math24_controller.rb index 9976f3d..9d421bb 100644 --- a/app/controllers/math24_controller.rb +++ b/app/controllers/math24_controller.rb @@ -7,15 +7,8 @@ class Math24Controller < ApplicationController erb :'math24/about' end - get '/problem' do - math24 = Math24.new - problem = math24.generate_problem.join(" ") - erb :'math24/problem', :locals => {:problem => problem} - end - - post '/problem' do - math24 = Math24.new - problem = "#{params[:problem] || ""}" + get '/problem' do + problem = Math24.generate_problem.join(" ") erb :'math24/problem', :locals => {:problem => problem} end @@ -24,20 +17,22 @@ class Math24Controller < ApplicationController end post '/solution' do - math24solver = Math24Solver.new problem = "#{params[:problem] || ""}" if problem.include?(",") - numbers = problem.gsub(" ","").split(",") + numbers = problem.delete(" ").split(",") else - numbers = problem.squeeze(" ").split(" ") + numbers = problem.squeeze(" ").split end + valid = numbers.all? do |number| number.to_i > 0 && number.to_i < 10 end + if valid - solution = math24solver.solve(numbers) - erb :'math24/solution', :locals => {:problem => numbers.join(" "), - :solution => solution, + solution = Math24.solve(numbers) + message = solution ? "#{solution} = 24" : "No solution found" + erb :'math24/solution', :locals => {:problem => numbers.join(" "), + :message => message, :last_answer => 24} else erb :'math24/solve', :locals => {:invalid => true} @@ -47,16 +42,29 @@ class Math24Controller < ApplicationController post '/check' do problem = "#{params[:problem] || ""}" solution = "#{params[:solution] || ""}" - math24 = Math24.new - math24.numbers = problem.split(" ") - if math24.solution?(solution) - erb :'math24/correct', :locals => {:problem => problem, - :solution => solution, - :last_answer => math24.last_answer} + numbers = problem.split + + begin + valid_solution = Math24.check(numbers, solution) + rescue ArgumentError + error = true + valid_solution = false + end + + if valid_solution + erb :'math24/correct', + :locals => { + :problem => problem, + :solution => solution + } + elsif error + erb :'math24/incorrect', :locals => {:problem => problem, + :solution => solution, + :last_answer => '???'} else - erb :'math24/incorrect', :locals => {:problem => problem, - :solution => solution, - :last_answer => math24.last_answer} + erb :'math24/incorrect', :locals => {:problem => problem, + :solution => solution, + :last_answer => instance_eval(solution)} end end -end \ No newline at end of file +end diff --git a/app/views/math24/correct.erb b/app/views/math24/correct.erb index 2b04c6d..6552b67 100644 --- a/app/views/math24/correct.erb +++ b/app/views/math24/correct.erb @@ -12,7 +12,7 @@
<%= problem %>
-
<%= solution %> = <%= last_answer %>!
+
<%= solution %> = 24!
-<%= erb :'layouts/_footer' %> \ No newline at end of file +<%= erb :'layouts/_footer' %> diff --git a/app/views/math24/solution.erb b/app/views/math24/solution.erb index 638c873..12b8427 100644 --- a/app/views/math24/solution.erb +++ b/app/views/math24/solution.erb @@ -12,9 +12,9 @@
<%= problem %>
-
<%= solution %>!
+
<%= message %>!
-<%= erb :'layouts/_footer' %> \ No newline at end of file +<%= erb :'layouts/_footer' %> diff --git a/config/environment.rb b/config/environment.rb index 0c88e3b..0aba26c 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,14 +1,6 @@ -require_relative "../lib/math24solvercli.rb" -#require "math24" - - ENV['SINATRA_ENV'] ||= "development" +ENV['SINATRA_ENV'] ||= "development" require 'bundler/setup' Bundler.require(:default, ENV['SINATRA_ENV']) -# ActiveRecord::Base.establish_connection( -# :adapter => "sqlite3", -# :database => "db/math24#{ENV['SINATRA_ENV']}.sqlite" -# ) - -require_all 'app' \ No newline at end of file +require_all 'app' diff --git a/spec/math24_controller_spec.rb b/spec/math24_controller_spec.rb new file mode 100644 index 0000000..d7a1c43 --- /dev/null +++ b/spec/math24_controller_spec.rb @@ -0,0 +1,115 @@ +def app + Math24Controller +end + +describe Math24Controller, :type => :controller do + describe 'get /' do + it 'renders the home page' do + get '/' + + expect(last_response).to be_ok + end + end + + describe 'get /about' do + it 'renders the about page' do + get '/about' + + expect(last_response).to be_ok + end + end + + describe 'get /problem' do + it 'renders a new problem' do + get '/problem' + + expect(last_response).to be_ok + expect(last_response.body).to match(/^\s*
(\d\s){3}\d\<\/div>$/) + end + + it 'generates a new problem every time' do + get '/problem' + first_problem = problem_markup = /^\s*
(\d\s){3}\d\<\/div>$/.match(last_response.body).to_s + get '/problem' + second_problem = problem_markup = /^\s*
(\d\s){3}\d\<\/div>$/.match(last_response.body).to_s + + expect(first_problem).to_not eq(second_problem) + end + end + + describe 'post /check' do + let(:problem_string) { "6 6 6 6" } + + it "renders 'correct' view if solution equals 24" do + get '/problem' + + solution = "6+6+6+6" + post '/check', {"problem"=>problem_string, "solution"=>solution} + + expect(last_response).to be_ok + expect(last_response.body).to include("#{solution} = 24!") + end + + xit "renders 'incorrect' view if solution does not match problem" do + solution = "(1+2+3)*(4)" + post '/check', {"problem"=>problem_string, "solution"=>solution} + + expect(last_response).to be_ok + expect(last_response.body).to include("#{solution} = 24!") + end + + it "renders 'incorrect' view if solution does not equal 24" do + solution = "6/6+6*6" + post '/check', {"problem"=>problem_string, "solution"=>solution} + + expect(last_response).to be_ok + expect(last_response.body).to include("Sorry, that's not right.") + end + + it 'displays error if solution input is invalid' do + post '/check', {"problem"=>problem_string, "solution"=>'invalid'} + + expect(last_response).to be_ok + expect(last_response.body).to include("= ???") + expect(last_response.body).to include("Sorry, that's not right.") + end + end + + describe 'get /solve' do + it 'renders input form for problem to sovle' do + get '/solve' + + expect(last_response.body).to match(//) + end + end + + describe 'post /solution' do + it 'displays message if problem cannot be solved' do + post '/solution', { "problem" => "1 1 1 1" } + + expect(last_response).to be_ok + expect(last_response.body).to include("No solution found!") + end + + it 'displays solution when it exists' do + post '/solution', { "problem" => "6 6 6 6" } + + expect(last_response).to be_ok + expect(last_response.body).to include("6 + 6 + 6 + 6 = 24!") + end + + it 'supports a comma-separated list' do + post '/solution', { "problem" => "6, 6, 6, 6" } + + expect(last_response).to be_ok + expect(last_response.body).to include("6 + 6 + 6 + 6 = 24!") + end + + it 'displays error if problem input is invalid' do + post '/solution', {"problem" => 'invalid'} + + expect(last_response).to be_ok + expect(last_response.body).to include("Invalid input, please try again") + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f143bcc..3a967dc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,48 +1,8 @@ require_relative "../config/environment.rb" -# This file was generated by the `rspec --init` command. Conventionally, all -# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. -# The generated `.rspec` file contains `--require spec_helper` which will cause this -# file to always be loaded, without a need to explicitly require it in any files. -# -# Given that it is always loaded, you are encouraged to keep this file as -# light-weight as possible. Requiring heavyweight dependencies from this file -# will add to the boot time of your test suite on EVERY test run, even for an -# individual file that may not need all of that loaded. Instead, make a -# separate helper file that requires this one and then use it only in the specs -# that actually need it. -# -# The `.rspec` file also contains a few flags that are not defaults but that -# users commonly want. -# -# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. - - # These two settings work together to allow you to limit a spec run - # to individual examples or groups you care about by tagging them with - # `:focus` metadata. When nothing is tagged with `:focus`, all examples - # get run. - config.filter_run :focus - config.run_all_when_everything_filtered = true - -=begin - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = 'doc' - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 -=end + # enable controller spec methods + config.include Rack::Test::Methods # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing @@ -55,40 +15,4 @@ # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed - -=begin - # rspec-expectations config goes here. You can use an alternate - # assertion/expectation library such as wrong or the stdlib/minitest - # assertions if you prefer. - config.expect_with :rspec do |expectations| - # Enable only the newer, non-monkey-patching expect syntax. - # For more details, see: - # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax - expectations.syntax = :expect - end - - # rspec-mocks config goes here. You can use an alternate test double - # library (such as bogus or mocha) by changing the `mock_with` option here. - config.mock_with :rspec do |mocks| - # Enable only the newer, non-monkey-patching expect syntax. - # For more details, see: - # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - mocks.syntax = :expect - - # Prevents you from mocking or stubbing a method that does not exist on - # a real object. This is generally recommended. - mocks.verify_partial_doubles = true - end -=end -end - -def capture_stdout(&block) - original_stdout = $stdout - $stdout = fake = StringIO.new - begin - yield - ensure - $stdout = original_stdout - end - fake.string end