To support a demonstration I'm going to be doing, I need a short C# program that can be unit tested. I decided to make a FizzBuzz split into a few methods:
using System;
namespace FizzBuzz
{
class FizzBuzzer
{
public static void Main()
{
FizzBuzzer fizzBuzzer = new FizzBuzzer();
int n = Convert.ToInt32(Console.ReadLine());
fizzBuzzer.StartLoop(n);
}
public int StartLoop(int n)
{
int fizzBuzzes = 0;
for(int i = 0; i < n; i++)
{
string output = getOutputForI(i);
if(output == "FizzBuzz") {
fizzBuzzes++;
}
Console.WriteLine(output);
}
return fizzBuzzes;
}
public string getOutputForI(int i)
{
string s = "";
if(i % 3 == 0)
{
s += "Fizz";
}
if(i % 5 == 0)
{
s += "Buzz";
}
if(s.Length == 0)
{
s = "" + i;
}
return s;
}
}
}
I want people to focus on the point of my demonstration, and not on potential mistakes in my code. As someone new to C#, I can't evaluate my code very well. Are there any beginner mistakes that would distract pedantic C# experts?
I am of course open to any type feedback, I mainly just want people to not laugh at or get annoyed by my code.
Edit:
here are example unit tests
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FizzBuzz.Tests
{
[TestClass()]
public class FizzBuzzerTests
{
[TestMethod()]
public void StartLoopTest()
{
Assert.AreEqual(6, FizzBuzzer.StartLoop(100));
}
[TestMethod()]
public void GetOutputFor0()
{
Assert.AreEqual("FizzBuzz", FizzBuzzer.getOutputForI(0));
}
[TestMethod()]
public void GetOutputFor4()
{
Assert.AreEqual("4", FizzBuzzer.getOutputForI(4));
}
}
}
and for context, here is the updated code (based on Ron Beyer's answer) that those tests are run against:
using System;
namespace FizzBuzz
{
public static class FizzBuzzer
{
public static void Main() { }
public static int CountFizzBuzzes(int max)
{
int fizzBuzzes = 0;
for (int i = 1; i <= max; i++)
{
string result = FizzBuzz(i);
if (result == "FizzBuzz")
{
fizzBuzzes++;
}
Console.WriteLine(result);
}
return fizzBuzzes;
}
public static string FizzBuzz(int input)
{
string s = "";
if (input % 3 == 0)
{
s += "Fizz";
}
if (input % 5 == 0)
{
s += "Buzz";
}
if (s.Length == 0)
{
s = "" + input;
}
return s;
}
}
}