Skip to content

SoftwareCraftersMurcia/string-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Base para hacer tests

Configuración básica para empezar a hacer una kata o aprender a hacer tests en los siguientes lenguajes:

  • PHP y PHPUnit
  • Javascript con Jest
  • Java, Junit y Mockito

Configuración específica por lenguaje

PHP

  1. Instalar composer curl -sS https://getcomposer.org/installer | php
  2. composer install (estando en la carpeta php)
  3. ./vendor/bin/phpunit

Javascript

  1. Instalar Node
  2. npm install (Estando en la carpeta javascript)
  3. npm test

Java

  1. Instalar las dependencias y tests con Maven [mvn test]
  2. Ejecutar los tests con el IDE

Documentación

Javascript

Jest

PHP

PHPUnit

Prophecy para dobles de prueba

Java

JUnit

Mockito

How to solve the kata

This classic kata guides you step by step through the implementation of a calculator that receives a String as input. It is a good exercise on refactoring and incremental implementation. It is also a good candidate for practising TDD.

First step

Create a function add that takes a String and returns a String:

String add(String number)
  • The method can take 0, 1 or 2 numbers separated by comma, and returns their sum.
  • An empty string will return "0".
  • Example of inputs: "", "1", "1.1,2.2".

Many numbers

Allow the add method to handle an unknow number of arguments.

Newline as separator

Allow the add method to handle newlines as separators:

  • "1\n2,3" should return "6".
  • "175.2,\n35" is invalid and should return the message "Number expected but '\n' found at position 6."

Missing number in last position

Don't allow the input to end in a separator.

"1,3," is invalid and should return the message Number expected but EOF found.

Custom separators

Allow the add method to handle a different delimiter. To change the delimiter, the beginning of the input will contain a separate line that looks like this:

//[delimiter]\n[numbers]
  • "//;\n1;2" should return "3"
  • "//|\n1|2|3" should return "6"
  • "//sep\n2sep3" should return "5"
  • "//|\n1|2,3" is invalid and should return the message "'|' expected but ',' found at position 3."

All existing scenarios should work as before.

Negative numbers

Calling add with negative numbers will return the message "Negative not allowed : " listing all negative numbers that were in the list of numbers.

  • "-1,2" is invalid and should return the message "Negative not allowed : -1"
  • "2,-4,-5" is invalid and should return the message "Negative not allowed : -4, -5"

Multiple errors

Calling add with multiple errors will return all error messages separated by newlines.

"-1,,2" is invalid and return the message "Negative not allowed : -1\nNumber expected but ',' found at position 3."

Credits to: Roy Osherove

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published