Monday, February 28, 2011

Work on Mocketry

Mocketry is a Mocketry is Smalltalk mock object framework.

To load it in Pharo:
Gofer it
  squeaksource: 'MetacelloRepository';
  package: 'ConfigurationOfMocketry';
  load.

(Smalltalk at:#ConfigurationOfMocketry) project latestVersion load.


I've done the first part of the Picasa screencast in a TDD way using Mocketry to prevent external HTTP requests.
Gofer it
 squeaksource: 'LaurentLSandbox';
 package: 'Picasa';
 load.

As the requests are done using HTTPSocket class>>httpGet:, one way is to give a mock to PicasaSearch so we can check (and stub) the HTTP request:
PicasaSearchTwoRoughSeaTest>>setUp
  [:mockHTTPSocketClass|
    [photos := PicasaSearch new
        httpSocketClass: mockHTTPSocketClass;
        addKeyword: 'rough';
        addKeyword: 'sea';  
        maxResult: 2;
        photos.] 
 
     should strictly satisfy: [
        (mockHTTPSocketClass httpGet: 
          'http://picasaweb.google.com/data/feed/api/all?q=rough+sea&max-results=2')
        willReturn: self fixtureXMLResponseForTwoRoughSea] 
  ] runScenario.

#fixtureXMLResponseForTwoRoughSea will return an XML string and test methods will check that it is correctly parsed.

In PicasaSearch:
httpGetDocument 
  |url| 
  url := String streamContents: [:aStream|
  aStream 
    nextPutAll: 'http://picasaweb.google.com/data/feed/api/all?q=';
    nextPutAll: ('+'  join: self keywords);
    nextPutAll: '&max-results=';
    nextPutAll: self maxResult asString.    
  ].  

  ^ (self httpSocketClass httpGet: url).


See that mocketry extends BlockClosure to create mocks:
[:myFirstMock :mySecondMock|
"do stuff with mocks"
] runScenario

and set up expectations:
[:myFirstMock :mySecondMock|
  [ "do stuff with mocks" ] 
  should strictly satisfy: 
  [ "what is expected on mocks" ]  
] runScenario

See HelpSystem book loaded with Mocketry for several examples.

Comments and better code propositions are welcome.

Monday, February 21, 2011

XML Browser with Pharo

I've written a little tool to help me browse xml. If you want to try it:
Gofer new
 squeaksource: 'LaurentLSandbox';
 package: 'XML-GUI';
 load.
 
((Smalltalk at:#XMLBrowser) labelled: 'Picasa search')
  browseAtUrl: 
   'http://picasaweb.google.com/data/feed/api/all?q=lighthouse&max-results=10'.



See XMLBrowser comment for examples.

Friday, February 18, 2011

FizzBuzz Kata: minimal solution

Trying to find a minimal solution for the FizzBuzz Kata in Smalltalk:

fb := [:counter| |rules|
       rules := {15->'FizzBuzz'. 5->'Buzz'. 3->'Fizz'. 1->counter}.
       rightRule := rules detect: [:aRule| counter \\ aRule key == 0].
       rightRule value].  

self assert: (fb value: 7) == 7.
self assert: (fb value: 3) == 'Fizz'.
self assert: (fb value: 5) == 'Buzz'.
self assert: (fb value: 15) == 'FizzBuzz'.

1 to: 100 do: [:counter | 
               Transcript 
                 show: (fb value: counter) asString;
                 cr]


Update: Little variant from Alexandre:
rightRule := rules detect: [:aRule| counter isDivisibleBy: aRule key]

Tuesday, February 8, 2011

Smalltalk tiny exercise for TDD newbies

Done in course:

Create the category Exercise in the Browser. All classes you create will be placed in this category.

Implement the class Operation (using Test-Driven Development) which should work like this:
  1. a := Operation new.
    a result.
    
    Should return 0.
  2. b := Operation new.
    b add: 2.
    b add: 3.
    b result.
    
    Should return 5.
  3. Operation new
      add: 2;
      add: 4;
      divideBy: 2;
      result.
    
    Should return 3.

Add the message Operation>>#multiplyBy by yourself.

Sunday, February 6, 2011

new funny ProfStef lesson

Update to last ProfStef (on Pharo 1.2 only):


Gofer it
 squeaksource: 'MetacelloRepository';
 package: 'ConfigurationOfProfStef';
 load.
 
ConfigurationOfProfStef project latestVersion load.

ProfStef 
  go;
  tutorial: SmalltalkSyntaxTutorial lesson: #instanciation.

Wednesday, February 2, 2011

Why technology matters

Life is very short. You have a limited amount of time in which to realize your dreams, in which to do your projects. If you’re like me, you probably have dozens and dozens of ideas for apps, sites, portals, that you want to implement. Most of those ideas will suck. But you don’t know which ones, you won’t know until you try them. Your goal, in this life, is to iterate faster. To fail more, to fail quicker, until you get to the brilliant ideas and brilliant projects that can change the world, or at least make you a dollar or two.

From A Secret: Passion and Your Choice of Web Framework by Dmitri Zagidulin