# :title: FizzBuzz v0.2
# :main: FizzBuzz.rb
# Author::    Rikard Edgren
# Copyright:: Copyright (c) 2014 Rikard Edgren
# License::   Distributes under the same terms as Ruby
#
# An exercise for software testers. 
# Inspired by FizzBuzz programming exercise as explained at http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
#
# 		Write a program that prints the numbers from 1 to 100. 
#		But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. 
#		For numbers which are multiples of both three and five print “FizzBuzz”.
# 
# The testing exercise statement is:
#
# 		This program is an exercise for software testers. As input it takes an integer between 1 and 1000, and repeats it as output.
#		If the number is a multiple of three, it should print "Fizz" instead of the number and for the multiples of five print "Buzz". 
#		For numbers which are multiples of both three and five it should give "FizzBuzz" as output.
#
#		The functionality is so straightforward so you will have time to investigate other things as well. 
#		Your testing mission is to find any threats to this being a useful exercise for testers around the world.
#		Test as much as you want, and answer these questions:

#		What would be a good test strategy?
#		What is your test coverage?
#		What are the results from your testing?
#		If you would use this exercise as a teacher, what would you talk about in the debrief?

require "io/console"

def setup
	# Strings for translation. \n is newline, so keep those.
	str_welcome = "\nFizzBuzz for Testers v0.2 by Rikard Edgren.\n\nThis is an exercise for software testers.\nAs input it takes an integer between 1 and 1000.\nIf the number is a multiple of three, it should print Fizz.\nIf the number is a multiple of five should print Buzz.\nFor numbers which are multiples of both three and five it will print FizzBuzz.\nFor other numbers, the number given should be printed."
	str_mission = "\nYour testing mission is to find any threats to this being a useful exercise for testers around the world."
	str_technicalities = "\nYour input will be interpreted to an integer by generous Ruby standards.\nEnter no to quit."
	str_numberentry = "Enter a number between 1 and 1000: "
	$exitcode = "no"

	# calculation variables
	$fizz = 3
	$buzz = 5
	$fizzbuzz = 15

	puts str_welcome
	puts str_mission
	puts str_technicalities
	print str_numberentry
	
	File.open("FizzBuzz.log", 'a') do |f|
		f.puts "FizzBuzz session started at #{Time.now}"
	end
end

def main
	testersinput = gets.chomp
	intinput = testersinput.to_i

	while testersinput != $exitcode
		if  intinput > 1000 || intinput < 1
	# This works since non integers will become 0.
			output = "invalid"
			puts "I could not interpret that as an integer between 1 and 1000."
			print "Try again: "
		else
			output = fizzbuzzeval(intinput, $fizz, $buzz)
			puts output
			print "\nAnother test? "
		end
		File.open("FizzBuzz.log", 'a') do |f|
			f.puts "#{testersinput}\t#{intinput}\t#{output}"
		end
		testersinput = gets.chomp
		intinput = testersinput.chomp.to_i
	end

	if testersinput == $exitcode
		$stdin.iflush
		puts "\nThanks for your effort."
		sleep 1
		$stdin.iflush
		puts "But have you tested enough?"
		print "\nPress Enter to exit..."
		testersinput = gets
	end
end

def fizzbuzzeval(number, fizznumber, buzznumber)
	result = ""
	result << "Fizz" if number%fizznumber == 0
	result << "Buzz" if number%buzznumber == 0
	result << number.to_s if result.length == 0
	return result	
end

setup
main
