TDD and Cro in Perl6 land
Tags: perl, perl6, tdd
Published: 2018-08-21 09:14:43

---
title: TDD and Cro in Perl6 land
published: true
tags: perl,perl6,tdd
canonical_url: https://lancewicks.com/2018/05/12/tdd-and-cro-in-perl6-land/#utm_source=rss&utm_medium=rss
---

Recently I have been writing some Perl6, porting an app from Bailador to Cro. Lots of which if you have been watching the livestream on https://twitch.tv/lancew?utm\_source=rss&utm\_medium=rss I have been doing without tests. So today I sorted that out; with the Cro::HTTP::Test module recently released (v 0.7.5).

Here is a really simple example, which tests that the home page loads ok:

> use Cro::HTTP::Test;  
> use Routes;
> 
> test-service routes(), {  
> test get(‘/’),  
> status =\> 200,  
> content-type =\> ‘text/html’,  
> body =\> /\/register/;  
> }
> 
> done-testing;

This very simply does a GET on the “/” application; then checks the status is 200, content type is HTML and the work “register” appears in the body. So you can easily start development of a new feature/service by writing a test for the route and working forwards from there. I got a little side tracked while working here by getting into thoughts about redesigning my logic to simplify the presentation layer… but I stopped as my skills working with objects in Perl6 is not as sharp as I’d like.

Here is a more interesting example where I GET a route that you have to be logged in to access:

> use Cro::HTTP::Test;  
> use Cro::HTTP::Auth;
> 
> use Routes;
> 
> test-service routes(), fake-auth =\> UserSession.new(:username(‘lancew’)), {  
> test get(‘/user/lancew’),  
> status =\> 200,  
> content-type =\> ‘text/html’,  
> body =\> /lancew/;  
> }
> 
> done-testing;

 

As you can see, it is pretty similar, the main difference is the “**fake-auth =\> UserSession.new(:username(‘lancew’)),**“, which lets the controller have a auth object to assess and react to. So that short snippet gives enough to access logged in pages, really nice! Perl6 (like Perl5) has great test tooling. Test is build in (not a additional module) which is great. I need to sharpen my skills there I admit.

Working with Cro has been really enjoyable so far, so I suggest taking it for a spin. It is small and seems a nice tool should you need to build a back-end service (or two). I am interested in looking at the  Cro::OpenAPI::RoutesFromDefinition module which looks like it could be helpful when working with OpenAPI projects.

If you have tried cro then let me know as I’d like to know how it worked for you. I have not looked very far into the linking of the services you can create; I’d like to see what areal project implemented with cro looks like.

The one area that still disapoints in Perl6 is the lack of a perlcritic and perltidy. Linting and formatting is a suprise to be missing perhaps. I have not looked; but hope it is on the roadmap as it’s reaching the point across languages now that a linter and code formatter has become the norm not the exception (I think). Perl5 lead the way with Perlcritic and PerlTidy; but now JS, PHP, Go and so forth all have good tools here that Perl6 seems to lack.

Cro really does feel like something I might reach for if I was asked to put a small backend together now. More so than something like Bailador. Possibly more so than JS (express) or Perl5 with Mojo or Dancer2. I’d like to explore the concurrency and linking is detail; stay tuned.