|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'nokogiri' |
| 4 | +require 'rspec' |
| 5 | +require 'spec_helper' |
| 6 | + |
| 7 | +ENV['APP_ENV'] = 'test' |
| 8 | + |
| 9 | +require './fibscale' |
| 10 | + |
| 11 | +RSpec.describe 'FibScale' do |
| 12 | + def app |
| 13 | + Sinatra::Application |
| 14 | + end |
| 15 | + |
| 16 | + it 'shows the home page' do |
| 17 | + get '/' |
| 18 | + expect(last_response).to be_ok |
| 19 | + |
| 20 | + page = Nokogiri::HTML.parse(last_response.body) |
| 21 | + expect(page.css('title').text).to eq('FibScale') |
| 22 | + expect(page.css('#fib-number').first['value']).to eq('0') |
| 23 | + end |
| 24 | + |
| 25 | + it 'shows the home page without error even if the configured delay is invalid' do |
| 26 | + get '/', delay: 'foo' |
| 27 | + expect(last_response).to be_ok |
| 28 | + |
| 29 | + page = Nokogiri::HTML.parse(last_response.body) |
| 30 | + expect(page.css('title').text).to eq('FibScale') |
| 31 | + expect(page.css('#fib-number').first['value']).to eq('0') |
| 32 | + end |
| 33 | + |
| 34 | + it 'shows an error on the home page if the delay is badly configured' do |
| 35 | + get '/', delay: '-1' |
| 36 | + expect(last_response).to be_ok |
| 37 | + expect(last_response.body).to include('Delay must be greater than or equal to zero.') |
| 38 | + end |
| 39 | + |
| 40 | + %w[recursive iterative].each do |algorithm| |
| 41 | + [{ n: 0, label: '0th', fib: 0 }, { n: 1, label: '1st', fib: 1 }, { n: 2, label: '2nd', fib: 1 }, |
| 42 | + { n: 3, label: '3rd', fib: 2 }, { n: 4, label: '4th', fib: 3 }, { n: 5, label: '5th', fib: 5 }, |
| 43 | + { n: 6, label: '6th', fib: 8 }, { n: 7, label: '7th', fib: 13 }, { n: 8, label: '8th', fib: 21 }, |
| 44 | + { n: 9, label: '9th', fib: 34 }, { n: 10, label: '10th', fib: 55 }].each do |params| |
| 45 | + it "computes the #{params[:label]} fibonacci number with the #{algorithm} algorithm" do |
| 46 | + post '/', fib: params[:n], algorithm: algorithm |
| 47 | + expect(last_response).to be_ok |
| 48 | + expect(last_response.body).to include("The #{params[:label]} fibonacci number is:") |
| 49 | + |
| 50 | + page = Nokogiri::HTML.parse(last_response.body) |
| 51 | + expect(page.css('.fib-result').text).to eq(params[:fib].to_s) |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + it 'computes the 100th fibonacci number with the iterative algorithm' do |
| 57 | + post '/', fib: 100, algorithm: 'iterative' |
| 58 | + expect(last_response).to be_ok |
| 59 | + expect(last_response.body).to include('The 100th fibonacci number is:') |
| 60 | + |
| 61 | + page = Nokogiri::HTML.parse(last_response.body) |
| 62 | + expect(page.css('.fib-result').text).to eq('354,224,848,179,261,915,075') |
| 63 | + end |
| 64 | + |
| 65 | + it 'cannot compute a non-numeric fibonacci number' do |
| 66 | + post '/', fib: 'foo' |
| 67 | + expect(last_response).to be_ok |
| 68 | + expect(last_response.body).to include('Fibonacci number must be an integer.') |
| 69 | + end |
| 70 | + |
| 71 | + it 'cannot compute a negative fibonacci number' do |
| 72 | + post '/', fib: -1 |
| 73 | + expect(last_response).to be_ok |
| 74 | + expect(last_response.body).to include('Fibonacci number must be greater than or equal to zero.') |
| 75 | + end |
| 76 | + |
| 77 | + it 'cannot compute a fibonacci number with an unknown algorithm' do |
| 78 | + post '/', fib: 1, algorithm: 'foo' |
| 79 | + expect(last_response).to be_ok |
| 80 | + expect(last_response.body).to include('Fibonacci algorithm must be one of: recursive, iterative.') |
| 81 | + end |
| 82 | + |
| 83 | + it 'cannot compute a fibonacci number higher than 40 with the recursive algorithm by default' do |
| 84 | + post '/', fib: 41 |
| 85 | + expect(last_response).to be_ok |
| 86 | + expect(last_response.body).to include( |
| 87 | + 'Cannot compute fibonacci numbers beyond 40 with the recursive algorithm (it would be too slow).' |
| 88 | + ) |
| 89 | + end |
| 90 | + |
| 91 | + it 'cannot compute a fibonacci number higher than 10000 with the iterative algorithm by default' do |
| 92 | + post '/', fib: 10_001, algorithm: 'iterative' |
| 93 | + expect(last_response).to be_ok |
| 94 | + expect(last_response.body).to include('Cannot compute fibonacci numbers beyond 10000.') |
| 95 | + end |
| 96 | + |
| 97 | + it 'cannot compute a fibonacci number with a negative delay' do |
| 98 | + post '/', fib: 1, delay: -2 |
| 99 | + expect(last_response).to be_ok |
| 100 | + expect(last_response.body).to include('Delay must be greater than or equal to zero.') |
| 101 | + end |
| 102 | +end |
0 commit comments