<commit_before>#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;
use Storable qw(lock_retrieve);
use Tie::IxHash;
use File::Basename qw(dirname);
use Text::Xslate;
use Data::Section::Simple;

my $classdef = lock_retrieve(dirname(__FILE__) . "/.idl2jsx.bin");

my @classes;
my %used_type;

# prepare
while(my($class, $def) = each %{$classdef}) {
    next if $def->{skip};
    next if $def->{alias};

    my $i = 0;
    push @classes, $def;

    my @tests;
    foreach my $member(@{$def->{members}}) {
        next unless ref $member;

        my $receiver;
        if($member->{static}) {
            $receiver = $class;
        }
        else {
            $receiver = "o";
        }

        my $decl = $member->{decl};
        $decl =~ s{/\* .*? \*/}{}xmsg;

        if(exists $member->{type}) {
            push @tests, sprintf "var v%d : %s = %s.%s;",
                ++$i, $member->{type},
                $receiver, $member->{id};

            $used_type{type2id($member->{type})} = $member->{type};
        }
        elsif($member->{id} eq 'constructor') {
            # TODO
        }
        else {
            my $ret_type    = $member->{ret_type_decl};
            my $param_types = $member->{param_types};

            $ret_type =~ s/\A \s* : \s*//xms;

            if($ret_type eq 'void') {
                push @tests, sprintf "%s.%s(%s);",
                    $receiver, $member->{id}, make_args($param_types);

                $used_type{type2id($_)} = $_ for @{$param_types};
            }
            else {
                push @tests, sprintf "var f%d : %s = %s.%s(%s);",
                    ++$i, $ret_type,
                    $receiver, $member->{id}, make_args($param_types);

                $used_type{type2id($_)} = $_ for @{$param_types};
                $used_type{type2id($ret_type)} = $ret_type;
            }
        }
    }

    $def->{tests} = \@tests;

}
# build
my $xslate = Text::Xslate->new(
    type => "text",
    path => [ Data::Section::Simple->new->get_data_section ],
    function => {
        type2id => \&type2id,
    },
);

print $xslate->render("web.jsx", {
        classes => \@classes,
        types   => \%used_type,
    });

exit;

sub type2id {
    my($type) = @_;
    $type =~ s/\s+//xmsg;
    $type =~ s{/\* .*? \*/}{}xmsg;
    $type =~ s/ \W /_/xmsg;
    return $type;
}

sub value_of {
    my($type) = @_;
    return sprintf 'X.get%s()', type2id($type);
}

sub make_args {<commit_msg>remove duplicated tests and add tests for constructors in 010.web.jsx<commit_after>#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;
use Storable qw(lock_retrieve);
use Tie::IxHash;
use File::Basename qw(dirname);
use Text::Xslate;
use Data::Dumper;
use Data::Section::Simple;

my $classdef = lock_retrieve(dirname(__FILE__) . "/.idl2jsx.bin");

my @classes;
my %used_type;


sub use_type {
    my($type) = @_;
    $used_type{type2id($type)} = trim_comment($type);
    return;
}

# prepare
while(my($class, $def) = each %{$classdef}) {
    next if $def->{skip};
    next if $def->{alias};

    my $c = 0; # for constructors
    my $v = 0; # for member variables
    my $f = 0; # for member functions
    push @classes, $def;

    my %seen;

    my @tests;
    foreach my $member(@{$def->{members}}) {
        next unless ref $member;
        next if $seen{$member->{decl}}++;

        my $receiver;
        if($member->{static}) {
            $receiver = $class;
        }
        else {
            $receiver = "o";
        }

        if(exists $member->{type}) {
            push @tests, sprintf "var v%d : %s = %s.%s;",
                ++$v, $member->{type},
                $receiver, $member->{id};

            use_type($member->{type});
        }
        else {
            my $ret_type    = $member->{ret_type_decl};
            my $param_types = $member->{param_types};

            $ret_type =~ s/\A \s* : \s*//xms;

            if($member->{id} eq 'constructor') {
                push @tests, sprintf "var c%d = new %s(%s);",
                    ++$c, $class, make_args($param_types);
            }
            elsif($ret_type eq 'void') {
                push @tests, sprintf "%s.%s(%s);",
                    $receiver, $member->{id}, make_args($param_types);
            }
            else {
                push @tests, sprintf "var f%d : %s = %s.%s(%s);",
                    ++$f, $ret_type,
                    $receiver, $member->{id}, make_args($param_types);

                use_type($ret_type);
            }
            use_type($_) for @{$param_types};
        }
    }

    $def->{tests} = \@tests;

}
# build
my $xslate = Text::Xslate->new(
    type => "text",
    path => [ Data::Section::Simple->new->get_data_section ],
);

print $xslate->render("web.jsx", {
        classes => \@classes,
        types   => \%used_type,
    });

exit;

sub trim_comment {
    my($t) = @_;
    $t =~ s{/\* .*? \*/}{}xmsg;
    return $t;
}

sub type2id {
    my($type) = @_;
    $type = trim_comment($type);
    $type =~ s/\s+//xmsg;
    $type =~ s/ \W /_/xmsg;
    return $type;
}

sub value_of {
    my($type) = @_;
    return sprintf 'X.get%s()', type2id($type);
}

sub make_args {
{'fraction_non_alphanumeric': 0.19076266252668347, 'fraction_numerical': 0.005433727925480303, 'mean_word_length': 1.8857782754759238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4582970', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1828, 'n_words': 382}