Skip to content

collapse all other-data-type format tests together #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2021

Conversation

karenetheridge
Copy link
Member

This doesn't remove any checks, just folds them together consistently.

@karenetheridge karenetheridge requested a review from a team July 4, 2021 00:51
@karenetheridge karenetheridge force-pushed the ether/streamlined-format-tests branch 3 times, most recently from 646849e to 6c9a1b4 Compare July 6, 2021 01:07
"schema": {"format": "email"},
"description": "validation of string formats",
"schema": {
"allOf": [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like the previous way more personally if I'm honest.

Or, I don't really have a strong opinion either way on putting these in one place or in each format, but I slightly prefer we don't complicate the schema -- i.e. use allOf.

(I think basically the simpler each schema can be in the test suite, the better -- we don't mean to test interaction between allOf and format in these tests, so it's better if we don't use it, essentially)

Also, previously someone would get one test per format if you map each test case to a test case in your language's testing framework. But now it's one test for all formats, which likely will fail in a more obscure way if one format is broken.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency is good though -- e.g. it'd be nice certainly to have all of these kinds of tests looking the same way (using the same description, etc.).

@karenetheridge karenetheridge force-pushed the ether/streamlined-format-tests branch from 6c9a1b4 to e940330 Compare July 9, 2021 02:55
@karenetheridge
Copy link
Member Author

Ok, I've kept the good bits of this PR - that is, normalizing all the tests, formatting and adding consistent data items.

@karenetheridge karenetheridge force-pushed the ether/streamlined-format-tests branch from e940330 to e000145 Compare July 9, 2021 04:28
@karenetheridge karenetheridge force-pushed the ether/streamlined-format-tests branch from e000145 to dadf73d Compare July 9, 2021 16:12
@Julian
Copy link
Member

Julian commented Jul 9, 2021

Thanks! This looks good to me now, though CI is failing, but it's because of the previous PR: #498 (comment)

The generating script:

----8< perl >8----

use strict;
use warnings;
use JSON::PP ();
use JSON::MaybeXS;
use Path::Tiny;

my %format_by_draft = (
  draft3 => [ qw(email ip-address ipv6 host-name date-time regex date time color uri) ],
  draft4 => [ qw(email ipv4 ipv6 hostname date-time uri) ],
  draft6 => [ qw(email ipv4 ipv6 hostname date-time json-pointer uri uri-reference uri-template) ],
  draft7 => [ qw(email idn-email regex ipv4 ipv6 idn-hostname hostname date date-time time json-pointer relative-json-pointer iri iri-reference uri uri-reference uri-template) ],
  'draft2019-09' => [ qw(email idn-email regex ipv4 ipv6 idn-hostname hostname date date-time time json-pointer relative-json-pointer iri iri-reference uri uri-reference uri-template uuid duration) ],
  'draft2020-12' => [ qw(email idn-email regex ipv4 ipv6 idn-hostname hostname date date-time time json-pointer relative-json-pointer iri iri-reference uri uri-reference uri-template uuid duration) ],
  'draft-future' => [ qw(email idn-email regex ipv4 ipv6 idn-hostname hostname date date-time time json-pointer relative-json-pointer iri iri-reference uri uri-reference uri-template uuid duration) ],
);

my %type_to_data = (
  integer => 12,
  float => 13.7,
  object => {},
  array => [],
  boolean => JSON::PP::false,
  null => undef,
);

my %format_string_data = (
  email => '2962',
  'ip-address' => '127.0.0.0.1',
  ipv6 => '12345::',
  'host-name' => '-a-host-name-that-starts-with--',
  'date-time' => '1990-02-31T15:59:60.123-08:00',
  regex => '^(abc]',
  date => '06/19/1963',
  time => '08:30:06 PST',
  uri => '//foo.bar/?baz=qux#quux',
  ipv4 => '127.0.0.0.1',
  hostname => '-a-host-name-that-starts-with--',
  'json-pointer' => '/foo/bar~',
  'uri-reference' => "\\\\WINDOWS\\fileshare",
  'uri-template' => 'http://example.com/dictionary/{term:1}/{term',
  'idn-email' => '2962',
  'idn-hostname' => "〮실례.테스트",
  'relative-json-pointer' => '/foo/bar',
  iri => 'http://2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  'iri-reference' => "\\\\WINDOWS\\filëßåré",
  uuid => '2eb8aa08-aa98-11ea-b4aa-73b441d1638',
  duration => 'PT1D',
);

my $encoder = JSON::MaybeXS->new(canonical => 1, pretty => 1, indent_length => 4);

foreach my $draft (keys %format_by_draft) {
  my $data = [
    map +{
      description => $_.' format',
      schema => { format => $_ },
      tests => [
        (map +{
          description => 'all string formats ignore '.$_.'s',
          data => $type_to_data{$_},
          valid => JSON::PP::true,
        }, qw(integer float object array boolean null)),
        $draft !~ /^draft[467]/ && $format_string_data{$_} ? +{
            description => 'invalid '.$_.' string is only an annotation by default',
            data => $format_string_data{$_},
            valid => JSON::PP::true,
        } : (),
      ],
    }, @{$format_by_draft{$draft}}
  ];

  my $str = $encoder->encode($data);
  $str =~ s/("data" : .+)(\n\s+)("description" : "[^"]+",)/$3$2$1/mg;
  $str =~ s/ : /: /g;
  $str =~ s/"schema": \{\n\s+("format": "[^"]+")\n\s+/"schema": { $1 /mg;
  path('tests', $draft, 'format.json')->spew_raw($str);
}
@karenetheridge karenetheridge force-pushed the ether/streamlined-format-tests branch from dadf73d to ba1f1a7 Compare July 9, 2021 17:21
@karenetheridge karenetheridge requested review from Julian and a team July 10, 2021 01:34
@Julian
Copy link
Member

Julian commented Jul 10, 2021

lgtm now thanks for all the work!

@Julian Julian merged commit 35bab68 into master Jul 10, 2021
@karenetheridge karenetheridge deleted the ether/streamlined-format-tests branch July 11, 2021 16:02
Julian added a commit that referenced this pull request Jul 13, 2021
In older (pre-draft7) drafts, implementations are indeed free to treat
format as an assertion by default.

(ref #497 (comment))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants