No Test::RelaxNG?

Ovid on 2008-03-31T10:42:59

I can't believe there's no Test::RelaxNG or similar on the CPAN. On the other hand, I can't believe there's no Test::YAML out there, either. Oh, wait, there is is Test::YAML, but it not only doesn't do anything particularly useful (that I can tell), it also relies on Spiffy. Jonathan Rockway has Test::YAML::Valid, but it only tests that YAML is valid (well, duh :). It doesn't let me test whether or not the YAML contents are correct. Of course, this is why I wrote Test::JSON, a module which I was astonished hadn't already existed.

Someone (hint, hint!), really needs to take XML, JSON, and YAML and create one giant Test::SomeCleverName module which lets people bundle all of those needs together.

For the curious, here's a rather naive implementation of Test::RelaxNG that I just wrote:

package Bermuda::Data::Test;

use strict;
use warnings;

use Test::Builder;
use XML::LibXML;
use base 'Exporter';
our @EXPORT = 'is_valid_against_rng';

my $TEST = Test::Builder->new;

sub is_valid_against_rng ($$;$) {
    my ( $xml, $schema, $name ) = @_;
    $name ||= 'XML validates against RNG';

    my $parser = XML::LibXML->new;
    my $rng    = XML::LibXML::RelaxNG->new( string => $schema );

    eval {
        $rng->validate($parser->parse_string($xml));
    };

    if ( my $error = $@ ) {
        $TEST->ok( 0, $name );
        $TEST->diag($error);
        return;
    }
    else {
        $TEST->ok( 1, $name );
        return 1;
    }
}

1;


Hmm...

rjray on 2008-04-01T17:19:37

This plants seeds of interesting ideas into my brain. I see that Test::XML exists but seems to be abandoned. Now I must go take some notes before I forget these things...

Re:Hmm...

Ovid on 2008-04-01T21:33:36

I was about to say that it's not abandoned as the author recently accepted a patch from me, but then I noticed that the patch was sent in 2005.

Re:Hmm...

rjray on 2008-04-01T21:52:35

Yeah, I've had some moments like that... (like realizing how long it's been since my book came out)

Cross cutting concern

nothingmuch on 2008-04-02T16:26:04

Test::RelaxNG and Test::YAML (the hypothetical one) are in different categories IMHO.

RelaxNG is XML specific validation

YAML, otoh, is just a format

Validating the data (not the serialization of the data) seems to be what you care about, so why not just use Test::Deep? It's complete, robust, well tested, featureful...

I don't see why a data validation test suite has to be redone for every serliazation format out there.

In fact, I'd much rather see RelaxNG validation become applicable to non XML formats whenever possible, for instance any object tree that implements Tree::XPathEngine, instead of being tied to the plain text format that is XML.

I think that Test::JSON, Test::YAML etc should actually converge on to something more Test::YAML::Valid like, leaving the data validation to something less encumbered with formats.

It's also more informative to fail the "badly formatted" test separately from the "failed validation" test.

Seed Planted

rjray on 2008-04-03T07:17:34

I am actively working on this now. Keep an eye to CPAN for something in the next 2-4 days. Still trying to think of a good base namespace that applies equally to XML, JSON and YAML.

I'm tempted to call it Test::Markup with the full knowledge that the use of the word "Markup" will wind up the YAML guys.

Great work

Ovid on 2008-04-03T07:22:00

Woot! Ask and thou shalt receive. Thanks :)