NAME

Test::Format - test files if they match format

VERSION

version 1.0.0

SYNOPSIS

In t/format.t file:

use strict;
use warnings FATAL => 'all';

use Test::More tests => 1;
use Test::Format;

test_format(
    files => [
        'data/countries.json',
    ],
    format => 'pretty_json',
);

It will check file 'data/countries.json' that it is in pretty json format.

And you can prettify all the files that test checks if you run test with SELF_UPDATE environment variable:

SELF_UPDATE=1 prove t/format.t

You can also write custom format checker:

test_format(
    files => [
        'data/file.asdf',
    ],
    format_sub => sub {
        my ($content) = @_;

        # Your custom code that creates pretty $expected_content from ugly $content

        return $expected_content;
    },
);

test_format

Sub test_format checks all the files that are specified that they match specified format.

test_format(
    files => [
        'data/cities/*.json',
        'data/countries/*.json',
    ],
    format => 'pretty_json',
);

or

test_format(
    files => [
        $file_name,
    ],
    format_sub => \&prettifier,
);

You must specify `files` option and one of two options: `format` or `format_sub`.

Option `files` is a ARRAYREF with list of files to be checked. If you specify relative path it it relative of how you run your test, not the position of the test. You can use wildcard characters in file names (internaly it is implemented with the `glob` function).

The value of `format` must be string. Now the only valid value is 'pretty_json'. Maybe in the future there some other values will be added.

The value of `format_sub` must be reference to a sub. This sub gets contents of every file the test checks and it must return the prettified version of the content. The $content that sub gets is chars, not bytes.

sub {
	my ($content) = @_;

	...

	return $expected_content;
}

Sub test_format behaviour depends of the environment variable SELF_UPDATE. If it is not set, or have a false value, the sub just cheks all the files. If the variable SELF_UPDATE is set to a true value the sub will fix the files that do not have expected content - it will write expected content to the files.

SOURCE CODE

The source code for this module is hosted on GitHub https://github.com/bessarabov/Test-Format

BUGS

Please report any bugs or feature requests in GitHub Issues https://github.com/bessarabov/Test-Format/issues

AUTHOR

Ivan Bessarabov <ivan@bessarabov.ru>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Ivan Bessarabov.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.