Skip to content

Commit

Permalink
Fix Rakefile to add __tag__: code for ~lambda test data
Browse files Browse the repository at this point in the history
This was broken when Ruby switched YAML engines from Syck to Psych
around Ruby 1.9.3. Syck was removed completely around version 2.0.
The code didn't fail, because Psych also implements the add_builtin_type
routine, but it does not recognize `!code` as a builtin type (because it
isn't; builtins look like !!str or !!map).

Instead, Psych requires a custom class to handle decoding the tagged
YAML item to Ruby.

And then, to get JSON to actually display the object, it is easiest to
just inherit from `Hash`.
  • Loading branch information
softmoth committed Mar 30, 2021
1 parent 1d59723 commit 42ac090
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ require 'json'
require 'yaml'

# Our custom YAML tags must retain their magic.
%w[ code ].each do |tag|
YAML::add_builtin_type(tag) { |_,val| val.merge(:__tag__ => tag) }
class TaggedMap < Hash
yaml_tag '!code'
def init_with(psych_coder)
self.replace({:__tag__ => 'code'}.merge(psych_coder.map))
end
end

YAML::add_tag('code', TaggedMap)

desc 'Build all alternate versions of the specs.'
multitask :build => [ 'build:json' ]

Expand Down
10 changes: 10 additions & 0 deletions specs/~lambdas.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"desc": "A lambda's return value should be interpolated.",
"data": {
"lambda": {
"__tag__": "code",
"ruby": "proc { \"world\" }",
"raku": "sub { \"world\" }",
"perl": "sub { \"world\" }",
Expand All @@ -26,6 +27,7 @@
"data": {
"planet": "world",
"lambda": {
"__tag__": "code",
"ruby": "proc { \"{{planet}}\" }",
"raku": "sub { q+{{planet}}+ }",
"perl": "sub { \"{{planet}}\" }",
Expand All @@ -45,6 +47,7 @@
"data": {
"planet": "world",
"lambda": {
"__tag__": "code",
"ruby": "proc { \"|planet| => {{planet}}\" }",
"raku": "sub { q+|planet| => {{planet}}+ }",
"perl": "sub { \"|planet| => {{planet}}\" }",
Expand All @@ -63,6 +66,7 @@
"desc": "Interpolated lambdas should not be cached.",
"data": {
"lambda": {
"__tag__": "code",
"ruby": "proc { $calls ||= 0; $calls += 1 }",
"raku": "sub { state $calls += 1 }",
"perl": "sub { no strict; $calls += 1 }",
Expand All @@ -81,6 +85,7 @@
"desc": "Lambda results should be appropriately escaped.",
"data": {
"lambda": {
"__tag__": "code",
"ruby": "proc { \">\" }",
"raku": "sub { \">\" }",
"perl": "sub { \">\" }",
Expand All @@ -100,6 +105,7 @@
"data": {
"x": "Error!",
"lambda": {
"__tag__": "code",
"ruby": "proc { |text| text == \"{{x}}\" ? \"yes\" : \"no\" }",
"raku": "sub { $^section eq q+{{x}}+ ?? \"yes\" !! \"no\" }",
"perl": "sub { $_[0] eq \"{{x}}\" ? \"yes\" : \"no\" }",
Expand All @@ -119,6 +125,7 @@
"data": {
"planet": "Earth",
"lambda": {
"__tag__": "code",
"ruby": "proc { |text| \"#{text}{{planet}}#{text}\" }",
"raku": "sub { $^section ~ q+{{planet}}+ ~ $^section }",
"perl": "sub { $_[0] . \"{{planet}}\" . $_[0] }",
Expand All @@ -138,6 +145,7 @@
"data": {
"planet": "Earth",
"lambda": {
"__tag__": "code",
"ruby": "proc { |text| \"#{text}{{planet}} => |planet|#{text}\" }",
"raku": "sub { $^section ~ q+{{planet}} => |planet|+ ~ $^section }",
"perl": "sub { $_[0] . \"{{planet}} => |planet|\" . $_[0] }",
Expand All @@ -156,6 +164,7 @@
"desc": "Lambdas used for sections should not be cached.",
"data": {
"lambda": {
"__tag__": "code",
"ruby": "proc { |text| \"__#{text}__\" }",
"raku": "sub { \"__\" ~ $^section ~ \"__\" }",
"perl": "sub { \"__\" . $_[0] . \"__\" }",
Expand All @@ -175,6 +184,7 @@
"data": {
"static": "static",
"lambda": {
"__tag__": "code",
"ruby": "proc { |text| false }",
"raku": "sub { 0 }",
"perl": "sub { 0 }",
Expand Down

0 comments on commit 42ac090

Please sign in to comment.