DSL with Ruby : how to get a step ahead ? (make it written in French, for my colleagues’ sake)
DSL is the acronym for Domain Specific Language. A DSL is a language designed for a particular purpose, for an intended audience who may not feel fine with a complex programming language. For example, Microsoft Excel macros are part of a DSL.
I’m currently working on an application which exports data into a text file, formatted with given tags and given parameters, then send it to another remote software, which will handle it (I skip here the whole process, which isn’t interesting at all for those who don’t work on it) to finally have the data printed and sent by mail.
WinDEV doesn’t offer built-in testing, so I made the application write a checksum like log, to verify the number of each type of data exported. Well.
But as we’ve got several undocumented (yeah, I know, real developers don’t write neither documentation nor comments, I should just read the code…) data handling rules, some of the corrections I add last month made errors appear in other parts of the file generated.
At this point, it gets enough freaky for me. I NEED REAL TESTS !
So, I decided to write, on my spare time, a tool to validate the file (if fields are all filled, if the values make sense, if the email field really contains an email address, and so on). Playing with metaprogramming and code injection, I got this skeleton of testing code (hiding all the automagical thingies in FileAnalysis.rb) :
#!/usr/local/bin/ruby -w
require "FileAnalysis"
include FileAnalysis
trap('SIGINT') { puts "\n\nAborting tests...\nBe sure to run *ALL* the tests at once from time to time \n\n"
exit
}
FileForPrinting.is "MMMMMMMM_data_2006_11.txt"
FileForPrinting.analyze do |line|
line.store? do |mag|
store.should_have_all_fields_filled
store.should_have_a_valid_email_address
end
line.cust? do |cust|
# here goes the tests for customers lines
end
line.art? do |art|
# here go the tests for article lines
end
end
This looks like a DSL to me (and it’s pretty like “Creating DSLs with Ruby” on Artima.com). Tests are quick to write, and fully understandable (okay, for those who know which tags are in the file, and which tests could apply to those lines).
Unfortunatly, my colleagues, who’ll have to deal with this piece of code, sooner or later, aren’t… English-friendly
For one thing, we don’t write code in English at work (WinDEV is set to “I code in French, pal” mode), and they’re not used to read and write this language. On the other hand, I don’t feel comfortable with writing tests in English for something I work on daily, in French. I mean : why the hell would I write line.store?
instead of ligne.magasin ?
So I’ve the idea of pusching the DSL work one step further, turning it into a French written test tool, which would look like :
FichierAImprimer.est "MMMMMMMM_data_2006_11.txt"
FichierAImprimer.analyse ligne par ligne
ligne de type magasin ?
la ligne devrait avoir tous les champs remplis
le magasin devrait avoir une adresse electronique valide
ligne de type virement ?
...
fin des tests
Okay, that would be very cool. Of course, I might end up writing slightly different code lines, to save some time working on that, but I think you get the idea. I suppose I’ll call a method, just after starting the program, as the first line of the code, to parse the file (or, maybe another one which will contain the tests) to replace some strings by another, according to rules I’ll set up (delete a blank space if it is immediately followed by a “?”, “le” and “la” at beginning of the line should be deleted as well, and so on…).
This would mix the two categories of DSL : the ones which are new dedicated languages to serve one goal, and the ones which are evolution of existing programming languages.
At this point, I have only two questions to you, readers : have you ever written a DSL to be used by non-English people, and what’s your opinion about all this ?