Discover the cappuccino tools : objj and ojunit

In the tutorial about cappuccino 0.7 beta, we have learned of to use "nib2cib" to convert a cocoa "nib" file to a cappuccino "cib" file. We have also learned how to use "capp" to create a startup project. We will use, in this tutorial, "objj" which is a command line Objective-J interpreter and interactive console. Once your are familiar with this tool, we will use it to run Objective-J unit tests.

How to use objj, the Objective-J interpreter and interactive console

If you have installed cappuccino 0.7 beta, you have "objj" already in your path. objj will need to have access to the cappuccino framework. So first create a startup application to have a copy of the "Foundation" and "Objective-J" frameworks. Change to this new directory and launch the interpreter.

$ capp test_objj $ cd test_objj

objj seems to be a java program.

$ objj java -classpath /usr/local/share/objj/lib:/usr/local/share/objj/lib/js.jar:/usr/local/share/objj/lib/objj: main Setting up Rhino utilties objj>

Just play a little with the interactive console. You must first import the classes before using them. You can see that unrecognized selector are detected. You can display or send message to objects. To quit the interpreter, use control C.

objj> @import <Foundation/CPString.j> objj> var message = [CPString stringWithString:@"Hello cappuccino"]; objj> message Hello cappuccino objj> [message reverse]; - [CPString reverse] unrecognized selector sent to instance 0x000017 objj> var uppercaseMessage = [message uppercaseString]; objj> uppercaseMessage HELLO CAPPUCCINO objj> ^C

You can also create a Objective-J file, for example "test_objj.j", and interpret it. You can see that we used "@import <Foundation/Foundation.j>" because you import this way all the different classes available in the "Foundation" framework. We have also setup the log to see the result in the terminal.

$ cat test_objj.j @import <Foundation/Foundation.j> CPLogRegister(CPLogPrint); var message = [CPString stringWithString:@"Hello cappuccino"]; var uppercaseMessage = [message uppercaseString]; CPLog.warn("Greatings: " + uppercaseMessage); print(args);

You can now use your work like this:

$ objj test_objj.j arg1 arg2 java -classpath /usr/local/share/objj/lib:/usr/local/share/objj/lib/js.jar:/usr/local/share/objj/lib/objj: main test_objj.j arg1 arg2 Setting up Rhino utilties 2009-02-31 23:43:42.765 Cappuccino [warn]: Greatings: HELLO CAPPUCCINO arg1,arg2

You can use arguments to your programs (they are passed as a CPArray).

I have found lately how to get rid of the need to use capp to have access to the Frameworks. You can use the flag "-I/usr/local/share/objj/lib/Frameworks" to include the path to the framework. You can do it either for the interactive or for the interpreter usage.

$ objj -I/usr/local/share/objj/lib/Frameworks $ objj -I/usr/local/share/objj/lib/Frameworks test_objj.j arg1 arg2

How to use objj to run Objective-J unit tests.

If you have installed cappuccino 0.7 beta, you have some tools available:

$ cd /usr/local/bin $ ls -lsa objj -> /usr/local/share/objj/bin/objj ojtest -> /usr/local/share/objj/bin/ojtest

In fact "ojtest" is just a link, but the real tool is not installed by default.

$ cd /usr/local/share/objj/bin $ ls bake blend capp cplutil nib2cib objj objjc press

Once we are here, you can see that the tools a shell scripts (plain text file).

$ file capp capp: Bourne shell script text executable $ cat objj

Installing ojunit

To install "OJUnit", you need to use either "git" or to download a zip/tar file. I clicked on the "download" button on the page http://github.com/280north/ojunit/tree/master.

git clone git://github.com/280north/ojunit.git

As I choosed to download a zip file, I had to manually copy the files from the directory "280north-ojunit" to the directory "cappuccino_07beta/280north-cappuccino-07b/Tools/ojunit".

$ cd /path/to/cappuccino_07beta/280north-cappuccino-07b/Tools/ojunit $ ls OJTestCase.j OJTestSuite.j main.j OJTestFailure.j README ojtest OJTestListenerText.j Rakefile OJTestResult.j build.xml

Since I had forget to setup "OBJJ_HOME" and "STEAM_BUILD" environment varaiables, I decided to upgrade my ".bash_profile" file.

$ cd ~ $ cat .bash_profile export PATH=$PATH:/Users/philippe/Desktop/cappuccino_07beta/280north-cappuccino-07b/Build/Release/env/bin:/usr/local/mysql/bin:/usr/local/bin:. export OBJJ_HOME=/usr/local/share/objj export STEAM_BUILD=/Users/philippe/objj_build $ . .bash_profile

You are now ready to build the objecive-J unit test framework.

$ cd /path/to/cappuccino_07beta/280north-cappuccino-07b $ rake release $ rake install Password: Cappuccino Tools Installed

Using ojunit to run Objective-J unit tests.

cappuccino web framework come with some unit tests which are located at "Tests/Objective-J" and "Tests/Foundation" in the main directory "/path/to/cappuccino_07beta/280north-cappuccino-07b".

To run the test, you need to launch "objj" in a folder where the Frameworks are available. We reuse our directory "test_objj". We have to interpret the file "main.j" in the directory "Tools/ojunit" with objj and pass the unit test file as a parameter.

$ cd test_objj $ objj /path/to/cappuccino_07beta/280north-cappuccino-07b/Tools/ojunit/main.j /path/to/cappuccino_07beta/280north-cappuccino-07b/Tests/Objective-J/base64Test.j $ objj /path/to/cappuccino_07beta/280north-cappuccino-07b/Tools/ojunit/main.j /path/to/cappuccino_07beta/280north-cappuccino-07b/Tests/Foundation/CPDateTest.j

Very long command line ? Well, I discovered a shortcut ! It is nammed "ojtest". You can see that it pass the "-I/usr/local/share/objj/lib/Frameworks" include path. It also use the "main.j" from "/usr/local/share/objj/lib/ojtest".

$ cd /path/to/cappuccino_07beta/280north-cappuccino-07b/Tests/Objective-J $ ojtest base64Test.j java -classpath /usr/local/share/objj/lib:/usr/local/share/objj/lib/js.jar:/usr/local/share/objj/lib/objj: main -I/usr/local/share/objj/lib/Frameworks /usr/local/share/objj/lib/ojtest/main.j base64Test.j Setting up Rhino utilties base64Test.j base64Test 2009-03-01 00:24:40.453 Cappuccino [info]: startTest test=[base64Test test_base64_encode_string] 2009-03-01 00:24:40.468 Cappuccino [info]: endTest test=[base64Test test_base64_encode_string]

Building Objective-J unit tests.

The best way to learn how to build Objective-J unit test is to read the source of existing ones. So check the one already availables in the directory "Tests". You can also read the source of ojunit in the directory "Tools/ojunit".

import <Foundation/CPString.j> @implementation CPStringTest : OJTestCase - (void)testStringByReplacingOccurrencesOfStringWithString { var expectedString = @"hello world. A new world!"; var dummyString = @"hello woold. A new woold!"; var actualString = [dummyString stringByReplacingOccurrencesOfString:@"woold" withString:@"world"]; [self assertTrue:(expectedString === actualString) message:"stringByAppendingFormat: expected:" + expectedString + " actual:" + actualString]; } @end
Copyright © 2009 - Philippe Laval. Cappuccino and Objective-J are registered Trademarks of 280 North.