The best way to test your scripts is by running them against a local instance of Elasticsearch.
Painless doesn't have a REPL or online sandbox or anything, partly because context is so important to the scripts and partly because we haven't built one. By context I mean that a script that works in an update may not work in filter query or a script field. They have very different things available to them.
OTOH examples like your function would run fine in an "empty" context. This falls more into the category of "we haven't built it". And we may never build it because of the context issue.
I think the simplest way to test painless is with an index with a single document, a search that matches all documents, and a script field. The script field lets you return a value and you can see it in the results.