Code Kata: String Calculator in Flex

725 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
725
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Code Kata: String Calculator in Flex

  1. 1. TDD Code Kata 1 StringCalculator
  2. 2. Ground Rules● do not skip ahead● no man left behind● we will only get as far as the whole group gets by 12:50 to allow for discussion at the end● when were out of time, thats it● next time, start from beginning again, not where we left off● if participating, commit and obey the rules● if observing, please do not assist or distract
  3. 3. Review RequirementThere should be a StringCalculator application
  4. 4. Compose Testthere should be a StringCalculator● Launch FlashBuilder● Create an application to run unit tests● Create a new Flex Desktop Project called StringCalculatorTestRunner in your kata directory● Add the FlexUnit swcs to to libs● Add a class named Suite to a package named suite● Add a class named Test to a package named test
  5. 5. Compose Testthere should be a StringCalculator ● add flexui:TestRunnerBase component to the test application ● add creationComplete handler to instantiate, init & run FlexUnitCore<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:flexui="org.flexunit.flexui.*" width="1200" height="900" creationComplete="application1_creationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; import org.flexunit.runner.FlexUnitCore; import suite.Suite; protected function application1_creationCompleteHandler(event:FlexEvent):void{ var core:FlexUnitCore = new FlexUnitCore(); core.addListener(myRunnerBase); core.run(suite.Suite); } ]]> </fx:Script> <flexui:TestRunnerBase id="myRunnerBase" /></s:WindowedApplication>
  6. 6. Compose Testthere should be a StringCalculator● Edit the Suite class package suite{ import tests.Test; [Suite] [RunWith("org.flexunit.runners.Suite")] public class Suite{ public var test:Test; } }
  7. 7. Compose Testthere should be a StringCalculator● Edit the Test class: add a passing test to get started package tests{ import org.flexunit.Assert; public class Test{ [Test] public function Run_App_CompilesWithoutError():void{ var expected:Number = 3; var actual:Number = Math.round(3.4999999); Assert.assertEquals(expected, actual); } } }
  8. 8. Developthere should be a StringCalculator● Create a directory on your computer for this kata project: kata/StringCalculator● Create a new ActionScript web project called StringCalculator in your directory with default settings● In Test Apps Project Properties>Build Path>Source Path, add your StringCalculator projects src directory● Run the test app, resolve any compiler issues● See starter test pass
  9. 9. Review RequirementStringCalculator should have an add() function that:● returns a Number● accepts a String● returns 0 for an empty string
  10. 10. Compose Testadd() should exist and return 0 for empty string● add a test called Add_InputEmptyString_ReturnsZero● instantiate the StringCalculator class● call StringCalculator.add()● Command+1 to create method add● run StringCalculatorTestRunner● see test fail
  11. 11. Developadd() should exist and return 0 for empty string● add a String parameter: add(numbers:String)● add a return type of Number: add():Number● add statements: var sum:Number; return sum;
  12. 12. Compose Testadd() should exist and return 0 for empty string● assert that 0 is returned for an empty string● run StringCalclatorTestRunner● see test fail
  13. 13. Developadd() should exist and return 0 for empty string● add a condition where empty string returns 0● run StringCalclatorTestRunner● see test passCongratulations!Your first requirement is formally specifiedYour applications intent is formally documentedYour application works as expected
  14. 14. Review RequirementThe calculators add function should:● throw exception for more than 2 inputs● return the value of 1 valid input● return the sum of 2 valid inputs
  15. 15. Compose Testthrow exception for more than 2 inputs● move StringCalculator to class and create in [Before]● add test called Add_InputMoreThanTwoValues_ThrowsError● compose the test and its assertion● run StringCalclatorTestRunner● see test fail
  16. 16. Developthrow exception for more than 2 inputs● add a condition where too many inputs throws an error● run StringCalclatorTestRunner● see all tests pass
  17. 17. Compose Testreturn the value of 1 valid input● add test called Add_InputSingleValue_ReturnsInputValue● compose the test and its assertion● run StringCalclatorTestRunner● see test fail
  18. 18. Developreturn the value of 1 valid input● add condition where single input value is returned● run StringCalclatorTestRunner● see all tests pass
  19. 19. Compose Testreturn the sum of 2 valid inputs● add test called Add_InputTwoValues_ReturnsSum● compose the test and its assertion● run StringCalclatorTestRunner● see test fail
  20. 20. Developreturn the sum of 2 valid inputs● add condition where sum of two inputs is returned● run StringCalclatorTestRunner● see all tests pass
  21. 21. Review RequirementThe calculators add function should return the sum of anynumber of inputsHere is a great example of how requirements change on usUnitTests help us to refactor code, safely and with confidence
  22. 22. Compose Testreturn the sum of any number of inputs● [Ignore] Add_InputMoreThanTwoValues_ThrowsError● add test called Add_InputMoreThanTwoValues_ReturnsSum● compose the test and its assertion● run StringCalclatorTestRunner● see test fail
  23. 23. Developreturn the sum of any number of inputs● remove condition where more than two inputs throws an error● add condition and logic to add any number of inputs● run StringCalclatorTestRunner● see all tests pass
  24. 24. Review RequirementThe calculators add function should:● accept the new line character n as a delimeter● n and , are allowed in the same input: add(1n2,3)
  25. 25. Compose Testaccept the new line character n as a delimiter● add test called Add_InputsDelimetedByNewLine_ReturnsSum● compose test and its assertion● run StringCalculatorTestRunner● see test fail
  26. 26. Developaccept the new line character n as a delimiter● solve for requirement (eg. replace n with ,)● run StringCalculatorTestRunner● see all tests pass
  27. 27. Review RequirementThe calculators add function should:● optionally allow for different default delimiters via "//dn"● example: add("//;n3;5;2n10;1) should return 21
  28. 28. Compose Testoptionally allow for different default delimiters via "//dn"● add test called Add_InputOptionalDelimiter_ReturnSum● compose test and its assertion● run StringCalculatorTestRunner● see test fail
  29. 29. Developoptionally allow for different default delimiters via "//dn"● solve for requirement● run StringCalculatorTestRunner● see all tests pass
  30. 30. Review RequirementThe calculators add function should:● throw an exception when passed a negative number● exception message is "negatives not allowed"● include negative in message● if multiple negatives, include them all in message
  31. 31. Compose Testthrow an exception when passed negative number(s)● add test called Add_InputNegativeNumbers_ThrowsException● compose test and its assertion● run StringCalculatorTestRunner● see test fail
  32. 32. Developthrow an exception when passed negative number(s)● solve for requirement● run StringCalculatorTestRunner● see all tests pass
  33. 33. Review RequirementThe calculators add function should:● ignore numbers larger than 1000
  34. 34. Compose Testignore numbers larger than 1000● add test called Add_InputValuesLargerThan1000_ReturnsSumIgnoringLargeNumbers● compose test and its assertion● run StringCalculatorTestRunner● see test fail
  35. 35. Developignore numbers larger than 1000● solve for requirement● run StringCalculatorTestRunner● see all tests pass

×