about summary refs log blame commit diff stats
path: root/tests/test_parser.h
blob: 51d768fec610d38f17c8d9e17444d1193c88c0ea (plain) (tree)








































                                                                          






                                                                

                                                                   
void parse_null_returns_null(void **state);
void parse_empty_returns_null(void **state);
void parse_space_returns_null(void **state);
void parse_cmd_no_args_returns_null(void **state);
void parse_cmd_with_space_returns_null(void **state);
void parse_cmd_with_too_few_returns_null(void **state);
void parse_cmd_with_too_many_returns_null(void **state);
void parse_cmd_one_arg(void **state);
void parse_cmd_two_args(void **state);
void parse_cmd_three_args(void **state);
void parse_cmd_three_args_with_spaces(void **state);
void parse_cmd_with_freetext(void **state);
void parse_cmd_one_arg_with_freetext(void **state);
void parse_cmd_two_args_with_freetext(void **state);
void parse_cmd_min_zero(void **state);
void parse_cmd_min_zero_with_freetext(void **state);
void parse_cmd_with_quoted(void **state);
void parse_cmd_with_quoted_and_space(void **state);
void parse_cmd_with_quoted_and_many_spaces(void **state);
void parse_cmd_with_many_quoted_and_many_spaces(void **state);
void parse_cmd_freetext_with_quoted(void **state);
void parse_cmd_freetext_with_quoted_and_space(void **state);
void parse_cmd_freetext_with_quoted_and_many_spaces(void **state);
void parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state);
void parse_cmd_with_quoted_freetext(void **state);
void parse_cmd_with_third_arg_quoted_0_min_3_max(void **state);
void parse_cmd_with_second_arg_quoted_0_min_3_max(void **state);
void parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state);
void count_one_token(void **state);
void count_one_token_quoted_no_whitespace(void **state);
void count_one_token_quoted_with_whitespace(void **state);
void count_two_tokens(void **state);
void count_two_tokens_first_quoted(void **state);
void count_two_tokens_second_quoted(void **state);
void count_two_tokens_both_quoted(void **state);
void get_first_of_one(void **state);
void get_first_of_two(void **state);
void get_first_two_of_three(void **state);
void get_first_two_of_three_first_quoted(void **state);
void get_first_two_of_three_second_quoted(void **state);
void get_first_two_of_three_first_and_second_quoted(void **state);
void parse_options_when_none_returns_empty_hasmap(void **state);
void parse_options_when_opt1_no_val_sets_error(void **state);
void parse_options_when_one_returns_map(void **state);
void parse_options_when_opt2_no_val_sets_error(void **state);
void parse_options_when_two_returns_map(void **state);
void parse_options_when_opt3_no_val_sets_error(void **state);
void parse_options_when_three_returns_map(void **state);
void parse_options_when_unknown_opt_sets_error(void **state);
void parse_options_with_duplicated_option_sets_error(void **state);
' href='#n91'>91 92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120





                  
                   

                

                                           










































                                                                     


                                        


                                                                       
            
                                                                            

      
                                                         
 

                                
                    
                                                              
                                                            

                       
                                                 
                               
                                 
                                                                
                                   
                                 
                                                           
                                                                 






                                                       
                                          


                          
                                             
                            
 





                                                           
                                                           


                                            
                                                     

                                

                  
                   
                                   

                   
                     




                                                   
#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use DateTime qw( );
use Path::Tiny;
use File::Fetch;
use JSON::MaybeXS qw( decode_json );
use Text::Table::Tiny qw( generate_table );

use OpenBSD::Unveil;

# Unveil @INC.
foreach my $path (@INC) {
    unveil( $path, 'rx' ) or
        die "Unable to unveil: $!";
}

# %unveil contains list of paths to unveil with their permissions.
my %unveil = (
    "/" => "rx", # Unveil "/", remove this later after profiling with
                 # ktrace.
    "/home" => "", # Veil "/home", we don't want to read it.
    "/tmp" => "rwc",
    "/dev/null" => "rw",
    );

# Unveil each path from %unveil.
keys %unveil;
while( my( $path, $permission ) = each %unveil ) {
    unveil( $path, $permission ) or
        die "Unable to unveil: $!";
}

# Fetch latest data from api.
my $url = 'https://api.covid19india.org/data.json';
my $ff = File::Fetch->new(uri => $url);

# Save the api response under /tmp.
my $file = $ff->fetch( to => '/tmp' ) or
    die $ff->error;

# Slurp api response to $file_data.
my $file_data = path($file)->slurp;

# Block further unveil calls.
unveil() or
    die "Unable to lock unveil: $!";

# Decode $file_data to $json_data.
my $json_data = decode_json($file_data);

# Get statewise information.
my $statewise = $json_data->{statewise};

# Map month number to Months.
my @months = qw( lol Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );

my $rows = [
    ['State', 'Confirmed', 'Active', 'Recovered', 'Deaths', 'Last Updated'],
    ];

my $today = DateTime->now( time_zone => 'Asia/Kolkata' );

# Add first 37 entries to $rows.
foreach my $i (0...37) {
    my $update_info;
    my $lastupdatedtime = $statewise->[$i]{'lastupdatedtime'};
    my $last_update_dmy = substr( $lastupdatedtime, 0, 10 );

    # Add $update_info.
    if ( $last_update_dmy eq $today->dmy('/') ) {
        $update_info = "Today";
    } elsif ( $last_update_dmy eq
              $today->clone->subtract( days => 1 )->dmy('/') ) {
        $update_info = "Yesterday";
    } elsif ( $last_update_dmy eq
              $today->clone->add( days => 1 )->dmy('/') ) {
        $update_info = "Tomorrow"; # Hopefully we don't see this.
    } else {
        $update_info =
            $months[substr( $lastupdatedtime, 3, 2 )] .
            " " .
            substr( $lastupdatedtime, 0, 2 );
    }

    my $state = $statewise->[$i]{'state'};
    $state = "India" if
        $state eq "Total";

    $state = $statewise->[$i]{'statecode'} if
        length($state) > 16;

    # Add delta only if it was updated Today.
    my $confirmed = "$statewise->[$i]{confirmed}";
    $confirmed .= " (+$statewise->[$i]{deltaconfirmed})" if
        $update_info eq "Today";

    my $recovered = "$statewise->[$i]{recovered}";
    $recovered .= " (+$statewise->[$i]{deltarecovered})" if
        $update_info eq "Today";

    my $deaths = "$statewise->[$i]{deaths}";
    $deaths .= " (+$statewise->[$i]{deltadeaths})" if
        $update_info eq "Today";

    push @$rows, [
        $state,
        $confirmed,
        $statewise->[$i]{'active'},
        $recovered,
        $deaths,
        $update_info,
        ];
}

# Generate table.
say generate_table(rows => $rows, header_row => 1);