1votos

Kata String Calculator en JavaScript

por alberto hace 4 años

Mi primera Kata en javascript. ¿Me la criticas?

Fantástica kata para comenzar a practicar el testeo unitario y el avanzar paso a paso, escribiendo el mínimo código necesario. El nivel de complejidad del código irá aumentando poco a poco.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var assert = require("assert"); 
 
function StringCalculator() {} 
 
StringCalculator.prototype.add = function(s) { 
  return sum(values(s || "0")); 
 
  function sum(values) { 
    guardAgainstNegatives(values); 
    return values.reduce(function(previous, current) { 
      return previous + current; 
    }); 
 
  function guardAgainstNegatives(values) { 
    var n = negatives(values); 
    if (n.length > 0) { 
      throw new Error("negatives not allowed " + n); 
 
  function values(s) { 
    var d = delimiters_expression(s); 
    return positive(numbers(s, d)); 
 
  function numbers(s, delimiters) { 
    return string_with_numbers(s).split(delimiters).map(function(value){ 
      return +value; 
    }); 
 
  function positive(numbers) { 
    return numbers.filter(function(value){ 
      return value <= 1000; 
    }); 
 
  function string_with_numbers(s) { 
    if (s.indexOf('//') === 0) { 
      return s.substr(s.indexOf('\n') +1) 
    return s; 
 
  function negatives(values) { 
    return values.filter(function(value) { 
      return value < 0; 
    }); 
 
  function delimiters_expression(s) { 
    return new RegExp(delimiters(s).join('|')); 
 
  function delimiters(s) { 
    return defaultDelimiters().concat(customDelimiters(s)); 
 
  function defaultDelimiters() { 
    return [',', '\n']; 
 
  function customDelimiters(s) { 
    if (s.indexOf('//') === -1) { 
      return []; 
    return [s[2]]; 
 
 
describe('StringCalculator', function(){ 
  describe('#add()', function(){ 
    it('should return 0 when no values present', function(){ 
      assert.equal(0, new StringCalculator().add()); 
    }) 
 
    it('should return 0 when passing ""', function(){ 
      assert.equal(0, new StringCalculator().add("")); 
    }) 
 
    it('should return 1 when passing "1"', function(){ 
      assert.equal(1, new StringCalculator().add("1")); 
    }) 
 
    it('should return 3 when passing "1,2"', function(){ 
      assert.equal(3, new StringCalculator().add("1,2")); 
    }) 
 
    it('should return 6 when passing "1,2,3"', function(){ 
      assert.equal(6, new StringCalculator().add("1,2,3")); 
    }) 
 
    it('should return 6 when passing "1\\n2,3"', function(){ 
      assert.equal(6, new StringCalculator().add("1\n2,3")); 
    }) 
 
    it('should return 3 when passing "//;\\n1;2"', function(){ 
      assert.equal(3, new StringCalculator().add("//;\n1;2")); 
    }) 
 
    it('should raise when passing "//1,-2"', function(){ 
      assert.throws( function() { 
        new StringCalculator().add("//;\n1;-2"); 
      }, Error); 
    }) 
 
    it('includes negatives in error message', function(){ 
      try { 
        new StringCalculator().add("//;\n-1;-2"); 
      catch(e) { 
        assert.equal(e.message, "negatives not allowed -1,-2") 
    }) 
 
    it('excludes number bigger than 1000', function(){ 
      assert.equal(2, new StringCalculator().add("2,1001")); 
    }) 
  }) 
}) 

Comenta la solución

Tienes que identificarte para poder publicar tu comentario.