Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ruby folder added

  • Loading branch information...
commit 4721224f10fa7e1b4786fdbc49feaa06399fcc42 1 parent a8f39d9
Alina authored
3  .gitignore
View
@@ -30,4 +30,5 @@ _ReSharper*/
*.[Pp]ublish.xml
*.resharper
*.local.xml
-workspace.xml
+workspace.xml
+.idea*/
114 Kata/StringCalculator/StringCalculator/StringCalculator.cs
View
@@ -7,117 +7,71 @@
namespace StringCalculator
{
- public class CalculatorTests
+ public class StringCalculator
{
- private readonly Calculator calculator = new Calculator();
- private string inputString;
- private int result;
-
- private void Act()
- {
- result = calculator.Add(inputString);
- }
-
- [Fact]
- public void Add_EmptyString_Returns0()
+ public int Add(string number)
{
- inputString = String.Empty;
- Act();
- result.Should().Be(0);
+ if (String.IsNullOrEmpty(number))
+ return 0;
+ const string pattern = @"(?<val>\d+)";
+ var values = (from Match m in new Regex(pattern).Matches(number) where m.Success select Int32.Parse(m.Groups["val"].Value)).ToList();
+ return values.Sum();
}
+ }
- [Fact]
- public void Add_OneValue_ReturnsThisValue()
- {
- inputString = "2";
- Act();
- result.Should().Be(2);
- }
+ public class StringCalculatorTests
+ {
+ private readonly StringCalculator calculator = new StringCalculator();
- [Fact]
- public void Add_TwoValues_ReturnsSum()
+ private int Act(string input)
{
- inputString = "2,3";
- Act();
- result.Should().Be(5);
+ return calculator.Add(input);
}
[Fact]
- public void Add_RandomNumberOfValues_ReturnsSum()
- {
- List<int> values = GetValues();
- inputString = string.Join(",", values);
- int expected = 0;
- values.ForEach(val => expected += val);
-
- Act();
- result.Should().Be(expected);
- }
-
- private List<int> GetValues()
+ public void Add_NullValue_Returns0()
{
- var rand = new Random(Guid.NewGuid().GetHashCode());
- return Enumerable.Range(0, rand.Next(0, 100)).Select(val => rand.Next(0, 100)).ToList();
+ var result = Act(null);
+ result.Should().Be(0);
}
[Fact]
- public void Test_random()
+ public void Add_EmptyString_Returns0()
{
- var ar1 = GetValues();
- var ar2 = GetValues();
- ar1.Should().NotBeEquivalentTo(ar2);
+ var result = Act(String.Empty);
+ result.Should().Be(0);
}
[Fact]
- public void Add_NewLineSeparator_ReturnsSum()
+ public void Add_OnValue_ReturnsThisValue()
{
- inputString = "2\n3,4";
- Act();
- result.Should().Be(9);
+ var result = Act("7");
+ result.Should().Be(7);
}
[Fact]
- public void Add_OneCharDelimiter_ReturnsSum()
+ public void Add_TwoValues_ReturnsTheirSum()
{
- inputString = "//;\n2;3;4";
- Act();
- result.Should().Be(9);
+ var result = Act("2,3");
+ result.Should().Be(5);
}
[Fact]
- public void Add_NegativeValues_Throws()
+ public void Add_RandomAmountOfValues_ReturnsTheirSum()
{
- inputString = "2,-3,4,-5";
- calculator.Invoking(calc => calc.Add(inputString)).ShouldThrow<Exception>(
- "negatives are not allowed: -3, -5");
+ var rand = new Random();
+ var values = Enumerable.Range(0, rand.Next(0, 100)).ToArray();
+ var number = string.Join(",", values);
+ var expected = values.Aggregate((a, b) => a + b);
+ var result = calculator.Add(number);
+ result.Should().Be(expected);
}
[Fact]
- public void Add_ValuesGreaterThen1000_Ignored()
+ public void Add_NewLineSeparator_ReturnsSum()
{
- inputString = "2,3,1002,5,1005";
- Act();
+ var result = calculator.Add("2\n3,4\n1");
result.Should().Be(10);
}
-
- }
-
- public class Calculator
- {
- public int Add(string number)
- {
- var separators = new[] {",", "\n"};
- if (number.StartsWith("//"))
- {
- separators = new[] {number.Substring(2, 1)};
- number = Regex.Replace(number, "//(.*?)\n", string.Empty);
- }
- var values = number.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- var negatives = values.ToList().Where(val => val.StartsWith("-"));
- if (negatives.Any())
- throw new Exception(String.Format("negatives are not allowed: {0}", String.Join(" ,", negatives)));
-
- return values.Select(Int32.Parse).Where(v => v <= 1000).Sum();
- }
}
}
11 Ruby/Kata/string_calculator.rb
View
@@ -0,0 +1,11 @@
+class StringCalculator
+ def add(number)
+ if number.to_s == ''
+ return 0
+ else
+ values = []
+ number.split(',').each {|s| values.push(s.to_i)}
+ return values.inject(:+)
+ end
+ end
+end
34 Ruby/Kata/string_calculator_test.rb
View
@@ -0,0 +1,34 @@
+require "test/unit"
+require "./string_calculator.rb"
+
+class StringCalculatorTests < Test::Unit::TestCase
+
+ def setup
+ @calculator = StringCalculator.new
+ end
+
+ def test_nil_string_returns_0
+ result = @calculator.add nil
+ self.assert_equal(0, result)
+ end
+
+ def test_empty_string_returns_0
+ result = @calculator.add ''
+ self.assert_equal(0, result)
+ end
+
+ def test_one_value_returns_this_value
+ result = @calculator.add '5'
+ self.assert_equal(5, result)
+ end
+
+ def test_two_values_returns_their_sum
+ result = @calculator.add '5,7'
+ self.assert_equal(12, result)
+ end
+
+ def test_unknown_amount_of_vals_returns_sum
+ values = (1..rand(100)).to_a.each{rand(100)}.to_a
+ puts values
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.