1.3. FeedEntryCollection
FeedEntryCollection as you can see in the class diagram of Figure 1, has an ArrayCollection underneath. A large part of the class in Listing 4 deals with housekeeping of read items – the functions setMarked, getMarked and getMarkedNumber.
Listing 4: Source of FeedEntryCollection.as
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 | package net.ivanidris.flexreader { import mx.collections.ArrayCollection; public class FeedEntryCollection { private var entries:ArrayCollection; public function FeedEntryCollection() { this.entries = new ArrayCollection(); } public function add(feed:FeedEntry):void { if(!this.entries.contains(feed) ) { this.entries.addItem(feed); } } public function len():uint { return this.entries.length; } public function getUrl(i:int):String { return this.entries.getItemAt(i).url; } public function getTitle(i:int):String { return this.entries.getItemAt(i).title; } public function getContent(i:int):String { return this.entries.getItemAt(i).content; } public function getFeedLink(i:int):String { return this.entries.getItemAt(i).feedLink; } public function getMarked(i:int):Boolean { return this.entries.getItemAt(i).marked; } public function setMarked(i:int):Boolean { return this.entries.getItemAt(i).marked = true; } public function getMarkedNumber():uint { var number:uint = 0; for(var i:int = 0; i < this.entries.length; i++) { if(getMarked(i)) { number++; } } return number; } public function contains(feed:FeedEntry):Boolean { for(var i:int = 0; i < this.entries.length; i++) { if(getUrl(i) == feed.url && getTitle(i) == feed.title) { return true; } } return false; } public function toString():String { var s:String = ''; for(var i:uint; i < this.entries.length; i++) { s += getUrl(i) + 'n'; } return s; } } } |
FeedEntryCollection is a very simple wrapper of ArrayCollection. A very testable piece of code as well. In the next section, I will show you just that – how to test this class.
1.4 Flex unit testing
I make use of the open source FlexUnit framework for my unit tests. In Listing 5 and 6 is an example test case and test runner. FlexUnit follows the example of JUnit, but only has a graphical Flash test runner, in contrast to JUnit having a Swing and a console test runner.
Listing 5: Example of a unit test
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 | package net.ivanidris.flexreader { import flexunit.framework.Assert; import flexunit.framework.TestCase; public class FeedEntryCollectionTest extends TestCase { public function testContains():void { var collection:FeedEntryCollection = new FeedEntryCollection(); var feed:FeedEntry = new FeedEntry(); feed.url = 'url1'; feed.title = 'title1'; collection.add(feed); var feed2:FeedEntry = new FeedEntry(); feed2.url = 'url1'; feed2.title = 'title1'; Assert.assertTrue('Collection contains this feed', collection.contains(feed2)); } } } |
The test class extends TestCase. It has 1 test testing the contains method of FeedEntryCollection. First, an entry is added to an empty FeedEntryCollection, then the test checks whether contains returns true, if it is queried for a FeedEntry with the same url and title.
Listing 6: Example FlexUnit test runner.
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 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" xmlns:flexunit="flexunit.flexui.*" creationComplete="onCreationComplete()"> <mx:Script> <![CDATA[ import flexunit.framework.TestSuite; import net.ivanidris.flexreader.FeedEntryCollectionTest; // Create the test suite and run the tests private function onCreationComplete():void { testRunner.test = createSuite(); testRunner.startTest(); } // Creates the test suite to run private function createSuite():TestSuite { var testSuite:TestSuite = new TestSuite(); testSuite.addTestSuite( FeedEntryCollectionTest ); return testSuite; } ]]> </mx:Script> <!-- FlexUnit GUI Component --> <flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" /> </mx:Application> |
The test runner is setup as a separate Flex application, just like FlexReader. A special namespace is declared for FlexUnit. At the moment the test runner application is created, onCreationComplete() is triggered. Its responsibility is to set the testRunner test property. The property is set to the createSuite() method, whose responsibility is to produce a test suite with tests.
More From ivanidris
ivanidris Recommends
- Seven Characteristics of Stepper Motors | Solder In The Veins (Solder In The Veins)
- 10 Questions with Facebook Research Engineer – Andrei Alexandrescu (Server-Side Magazine)
