Home | History | Annotate | Line # | Download | only in indent
t_misc.sh revision 1.13
      1   1.1  rillig #! /bin/sh
      2  1.13  rillig # $NetBSD: t_misc.sh,v 1.13 2021/11/07 15:01:50 rillig Exp $
      3   1.1  rillig #
      4   1.1  rillig # Copyright (c) 2021 The NetBSD Foundation, Inc.
      5   1.1  rillig # All rights reserved.
      6   1.1  rillig #
      7   1.1  rillig # Redistribution and use in source and binary forms, with or without
      8   1.1  rillig # modification, are permitted provided that the following conditions
      9   1.1  rillig # are met:
     10   1.1  rillig # 1. Redistributions of source code must retain the above copyright
     11   1.1  rillig #    notice, this list of conditions and the following disclaimer.
     12   1.1  rillig # 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  rillig #    notice, this list of conditions and the following disclaimer in the
     14   1.1  rillig #    documentation and/or other materials provided with the distribution.
     15   1.1  rillig #
     16   1.1  rillig # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  rillig # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  rillig # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  rillig # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  rillig # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  rillig # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  rillig # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  rillig # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  rillig # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  rillig # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  rillig # POSSIBILITY OF SUCH DAMAGE.
     27   1.1  rillig #
     28   1.1  rillig # $FreeBSD$
     29   1.1  rillig 
     30   1.1  rillig # Tests for indent that do not follow the input-profile-output scheme that is
     31   1.1  rillig # used in t_indent.
     32   1.1  rillig 
     33   1.1  rillig indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
     34   1.2  rillig nl='
     35   1.2  rillig '
     36   1.2  rillig 
     37   1.2  rillig atf_test_case 'in_place'
     38   1.2  rillig in_place_body()
     39   1.2  rillig {
     40   1.2  rillig 	cat <<-\EOF > code.c
     41   1.2  rillig 		int decl;
     42   1.2  rillig 	EOF
     43   1.2  rillig 	cat <<-\EOF > code.c.exp
     44   1.2  rillig 		int		decl;
     45   1.2  rillig 	EOF
     46   1.2  rillig 	cp code.c code.c.orig
     47   1.2  rillig 
     48   1.2  rillig 	atf_check \
     49   1.2  rillig 	    env SIMPLE_BACKUP_SUFFIX=".bak" "$indent" code.c
     50   1.2  rillig 	atf_check -o 'file:code.c.exp' \
     51   1.2  rillig 	    cat code.c
     52   1.2  rillig 	atf_check -o 'file:code.c.orig' \
     53   1.2  rillig 	    cat code.c.bak
     54   1.2  rillig }
     55   1.1  rillig 
     56   1.1  rillig atf_test_case 'verbose_profile'
     57   1.1  rillig verbose_profile_body()
     58   1.1  rillig {
     59   1.1  rillig 	cat <<-\EOF > .indent.pro
     60   1.1  rillig 		-/* comment */bacc
     61   1.1  rillig 		-v
     62   1.1  rillig 		-fc1
     63   1.1  rillig 	EOF
     64   1.1  rillig 	cat <<-\EOF > before.c
     65   1.1  rillig 		int decl;
     66   1.1  rillig 	EOF
     67   1.1  rillig 	cat <<-\EOF > after.c.exp
     68   1.1  rillig 		int		decl;
     69   1.1  rillig 	EOF
     70   1.1  rillig 	cat <<-\EOF > stdout.exp
     71   1.1  rillig 		profile: -fc1
     72   1.1  rillig 		profile: -bacc
     73   1.1  rillig 		profile: -v
     74   1.1  rillig 		profile: -fc1
     75   1.1  rillig 		There were 1 output lines and 0 comments
     76   1.1  rillig 		(Lines with comments)/(Lines with code):  0.000
     77   1.1  rillig 	EOF
     78   1.1  rillig 
     79   1.1  rillig 	# The code in args.c function set_profile suggests that options from
     80   1.1  rillig 	# profile files are echoed to stdout during startup. But since the
     81   1.1  rillig 	# command line options are handled after the profile files, a '-v' in
     82   1.1  rillig 	# the command line has no effect. That's why '-bacc' is not listed
     83   1.1  rillig 	# in stdout, but '-fc1' is. The second round of '-bacc', '-v', '-fc1'
     84   1.1  rillig 	# is listed because when running ATF, $HOME equals $PWD.
     85   1.1  rillig 
     86   1.1  rillig 	atf_check \
     87   1.1  rillig 	    -o 'file:stdout.exp' \
     88   1.1  rillig 	    "$indent" -v before.c after.c
     89   1.1  rillig 	atf_check \
     90   1.1  rillig 	     -o 'file:after.c.exp' \
     91   1.1  rillig 	     cat after.c
     92   1.1  rillig }
     93   1.1  rillig 
     94   1.3  rillig atf_test_case 'nested_struct_declarations'
     95   1.3  rillig nested_struct_declarations_body()
     96   1.3  rillig {
     97   1.3  rillig 	# Trigger the warning about nested struct declarations.
     98   1.3  rillig 
     99   1.3  rillig 	cat <<-\EOF > code.c
    100   1.3  rillig 		struct s01 { struct s02 { struct s03 { struct s04 {
    101   1.3  rillig 		struct s05 { struct s06 { struct s07 { struct s08 {
    102   1.3  rillig 		struct s09 { struct s10 { struct s11 { struct s12 {
    103   1.3  rillig 		struct s13 { struct s14 { struct s15 { struct s16 {
    104   1.3  rillig 		struct s17 { struct s18 { struct s19 { struct s20 {
    105   1.3  rillig 		struct s21 { struct s22 { struct s23 { struct s24 {
    106   1.3  rillig 		};};};};
    107   1.3  rillig 		};};};};
    108   1.3  rillig 		};};};};
    109   1.3  rillig 		};};};};
    110   1.3  rillig 		};};};};
    111   1.3  rillig 		};};};};
    112   1.3  rillig 	EOF
    113   1.3  rillig 	cat <<-\EOF > expected.out
    114   1.3  rillig 		struct s01 {
    115   1.3  rillig 		 struct s02 {
    116   1.3  rillig 		  struct s03 {
    117   1.3  rillig 		   struct s04 {
    118   1.3  rillig 		    struct s05 {
    119   1.3  rillig 		     struct s06 {
    120   1.3  rillig 		      struct s07 {
    121   1.3  rillig 		       struct s08 {
    122   1.3  rillig 		        struct s09 {
    123   1.3  rillig 		         struct s10 {
    124   1.3  rillig 		          struct s11 {
    125   1.3  rillig 		           struct s12 {
    126   1.3  rillig 		            struct s13 {
    127   1.3  rillig 		             struct s14 {
    128   1.3  rillig 		              struct s15 {
    129   1.3  rillig 		               struct s16 {
    130   1.3  rillig 		                struct s17 {
    131   1.3  rillig 		                 struct s18 {
    132   1.3  rillig 		                  struct s19 {
    133   1.3  rillig 		                   struct s20 {
    134   1.3  rillig 		                    struct s21 {
    135   1.3  rillig 		                     struct s22 {
    136   1.3  rillig 		                      struct s23 {
    137   1.3  rillig 		                       struct s24 {
    138   1.3  rillig 		                       };
    139   1.3  rillig 		                      };
    140   1.3  rillig 		                     };
    141   1.3  rillig 		                    };
    142   1.3  rillig 		                   };
    143   1.3  rillig 		                  };
    144   1.3  rillig 		                 };
    145   1.3  rillig 		                };
    146   1.3  rillig 		               };
    147   1.3  rillig 		              };
    148   1.3  rillig 		             };
    149   1.3  rillig 		            };
    150   1.3  rillig 		           };
    151   1.3  rillig 		          };
    152   1.3  rillig 		         };
    153   1.3  rillig 		        };
    154   1.3  rillig 		       };
    155   1.3  rillig 		      };
    156   1.3  rillig 		     };
    157   1.3  rillig 		    };
    158   1.3  rillig 		   };
    159   1.3  rillig 		  };
    160   1.3  rillig 		 };
    161   1.3  rillig 		};
    162   1.3  rillig 	EOF
    163   1.3  rillig 	cat <<-\EOF > expected.err
    164   1.8  rillig 		warning: Standard Input:5: Reached internal limit of 20 struct levels
    165   1.8  rillig 		warning: Standard Input:6: Reached internal limit of 20 struct levels
    166   1.8  rillig 		warning: Standard Input:6: Reached internal limit of 20 struct levels
    167   1.8  rillig 		warning: Standard Input:6: Reached internal limit of 20 struct levels
    168   1.8  rillig 		warning: Standard Input:6: Reached internal limit of 20 struct levels
    169   1.3  rillig 	EOF
    170   1.3  rillig 
    171   1.3  rillig 	atf_check -o 'file:expected.out' -e 'file:expected.err' \
    172   1.3  rillig 	    "$indent" -i1 -nut < 'code.c'
    173   1.3  rillig }
    174   1.3  rillig 
    175   1.4  rillig atf_test_case 'option_P_in_profile_file'
    176   1.4  rillig option_P_in_profile_file_body()
    177   1.4  rillig {
    178   1.4  rillig 	# Mentioning another profile via -P has no effect since only a single
    179   1.4  rillig 	# profile can be specified on the command line, and there is no
    180   1.4  rillig 	# 'include' option.
    181   1.4  rillig 
    182   1.4  rillig 	# It's syntactically possible to specify a profile file inside another
    183   1.4  rillig 	# profile file.  Such a profile file is ignored since only a single
    184   1.4  rillig 	# profile file is ever loaded.
    185   1.4  rillig 	printf '%s\n' '-P/nonexistent' > .indent.pro
    186   1.4  rillig 
    187   1.4  rillig 	echo 'syntax # error' > code.c
    188   1.4  rillig 
    189   1.4  rillig 	atf_check -o 'inline:syntax'"$nl"'#error'"$nl" \
    190   1.4  rillig 	    "$indent" < code.c
    191   1.4  rillig }
    192   1.4  rillig 
    193  1.13  rillig atf_test_case 'option_without_hyphen'
    194  1.13  rillig option_without_hyphen_body()
    195  1.13  rillig {
    196  1.13  rillig 	# TODO: Options in profile files should be required to start with
    197  1.13  rillig 	#  '-', just like in the command line arguments.
    198  1.13  rillig 
    199  1.13  rillig 	printf ' -i3 xi5 +di0\n' > .indent.pro
    200  1.13  rillig 
    201  1.13  rillig 	printf '%s\n' 'int var[] = {' '1,' '}' > code.c
    202  1.13  rillig 	printf '%s\n' 'int var[] = {' '     1,' '}' > code.exp
    203  1.13  rillig 
    204  1.13  rillig 	atf_check -o 'file:code.exp' \
    205  1.13  rillig 	    "$indent" < code.c
    206  1.13  rillig }
    207  1.13  rillig 
    208   1.5  rillig atf_test_case 'opt'
    209   1.5  rillig opt_body()
    210   1.5  rillig {
    211   1.5  rillig 	# Test parsing of command line options from a profile file.
    212   1.5  rillig 
    213   1.5  rillig 	cat <<-\EOF > code.c
    214   1.5  rillig 		int global_var;
    215   1.5  rillig 
    216   1.5  rillig 		int function(int expr) {
    217   1.5  rillig 		switch (expr) { case 1: return 1; default: return 0; }
    218   1.5  rillig 		}
    219   1.5  rillig 	EOF
    220   1.5  rillig 
    221   1.5  rillig 	cat << \EOF > .indent.pro
    222   1.5  rillig /* The latter of the two options wins. */
    223   1.5  rillig -di5
    224   1.5  rillig -di12
    225   1.5  rillig 
    226   1.5  rillig /*
    227   1.5  rillig  * It is possible to embed comments in the middle of an option, but nobody
    228   1.5  rillig  * does that.
    229   1.5  rillig  */
    230   1.5  rillig -/* comment */bacc
    231   1.5  rillig -T/* define
    232   1.5  rillig a type */custom_type
    233   1.5  rillig 
    234   1.9  rillig /* For int options, trailing garbage would be an error. */
    235   1.5  rillig -i3
    236   1.5  rillig 
    237   1.9  rillig /*For float options, trailing garbage would be an error. */
    238   1.9  rillig -cli3.5
    239   1.5  rillig 
    240   1.5  rillig -b/*/acc	/* The comment is '/' '*' '/', making the option '-bacc'. */
    241   1.5  rillig EOF
    242   1.5  rillig 
    243   1.5  rillig 	sed '/[$]/d' << \EOF > code.exp
    244   1.5  rillig /* $ The variable name is indented by 12 characters due to -di12. */
    245   1.5  rillig int	    global_var;
    246   1.5  rillig 
    247   1.5  rillig int
    248   1.5  rillig function(int expr)
    249   1.5  rillig {
    250   1.5  rillig    switch (expr) {
    251   1.5  rillig /* $ The indentation is 3 + (int)(3.5 * 3), so 3 + 10.5, so 13. */
    252   1.5  rillig /* $ See parse.c, function parse, 'case switch_expr'. */
    253   1.5  rillig 	     case 1:
    254   1.5  rillig /* $ The indentation is 3 + (int)3.5 * 3 + 3, so 3 + 9 + 3, so 15. */
    255   1.5  rillig /* $ See parse.c, function parse, 'case switch_expr'. */
    256   1.5  rillig 	       return 1;
    257   1.5  rillig 	     default:
    258   1.5  rillig 	       return 0;
    259   1.5  rillig    }
    260   1.5  rillig }
    261   1.5  rillig EOF
    262   1.5  rillig 
    263   1.5  rillig 	atf_check -o 'file:code.exp' \
    264   1.5  rillig 	    "$indent" code.c -st
    265   1.5  rillig }
    266   1.5  rillig 
    267   1.5  rillig atf_test_case 'opt_npro'
    268   1.5  rillig opt_npro_body()
    269   1.5  rillig {
    270   1.5  rillig 	# Mentioning the option -npro in a .pro file has no effect since at
    271   1.5  rillig 	# that point, indent has already decided to load the .pro file, and
    272   1.5  rillig 	# it only decides once.
    273   1.5  rillig 
    274   1.5  rillig 	echo ' -npro -di8' > .indent.pro
    275   1.5  rillig 	echo 'int var;' > code.c
    276   1.5  rillig 	printf 'int\tvar;\n' > code.exp
    277   1.5  rillig 
    278   1.5  rillig 	atf_check -o 'file:code.exp' \
    279   1.5  rillig 	    "$indent" code.c -st
    280   1.5  rillig }
    281   1.5  rillig 
    282   1.5  rillig atf_test_case 'opt_U'
    283   1.5  rillig opt_U_body()
    284   1.5  rillig {
    285   1.5  rillig 	# From each line of this file, the first word is taken to be a type
    286   1.5  rillig 	# name.
    287   1.5  rillig 	#
    288   1.5  rillig 	# Since neither '/*' nor '' are syntactically valid type names, this
    289   1.5  rillig 	# means that all kinds of comments are effectively ignored.  When a
    290   1.5  rillig 	# type name is indented by whitespace, it is ignored as well.
    291   1.5  rillig 	#
    292   1.5  rillig 	# Since only the first word of each line is relevant, any remaining
    293   1.5  rillig 	# words can be used for comments.
    294   1.5  rillig 	cat <<-\EOF > code.types
    295   1.5  rillig 		/* Comments are effectively ignored since they never match. */
    296   1.5  rillig 		# This comment is ignored as well.
    297   1.5  rillig 		; So is this comment.
    298   1.5  rillig 		# The following line is empty and adds a type whose name is empty.
    299   1.5  rillig 
    300   1.5  rillig 		size_t			from stddef.h
    301   1.5  rillig 		off_t			for file offsets
    302   1.5  rillig  		 ignored_t		is ignored since it is indented
    303   1.5  rillig 	EOF
    304   1.5  rillig 
    305   1.5  rillig 	cat <<-\EOF > code.c
    306   1.5  rillig 		int known_1 = (size_t)   *   arg;
    307   1.5  rillig 		int known_2 = (off_t)   *   arg;
    308   1.5  rillig 		int ignored = (ignored_t)   *   arg;
    309   1.5  rillig 	EOF
    310   1.5  rillig 	cat <<-\EOF > code.exp
    311   1.5  rillig 		int known_1 = (size_t)*arg;
    312   1.5  rillig 		int known_2 = (off_t)*arg;
    313   1.5  rillig 		int ignored = (ignored_t) * arg;
    314   1.5  rillig 	EOF
    315   1.5  rillig 
    316   1.5  rillig 	atf_check -o 'file:code.exp' \
    317   1.5  rillig 	    "$indent" -Ucode.types code.c -di0 -st
    318   1.5  rillig }
    319   1.5  rillig 
    320   1.6  rillig atf_test_case 'line_no_counting'
    321   1.6  rillig line_no_counting_body()
    322   1.6  rillig {
    323   1.7  rillig 	# Before NetBSD indent.c 1.147 from 2021-10-24, indent reported the
    324   1.7  rillig 	# warning in line 2 instead of the correct line 3.
    325   1.7  rillig 
    326   1.6  rillig 	cat <<-\EOF > code.c
    327   1.6  rillig 		void line_no_counting(void)
    328   1.6  rillig 		{
    329   1.6  rillig 			())
    330   1.6  rillig 		}
    331   1.6  rillig 	EOF
    332   1.6  rillig 
    333   1.6  rillig 	cat <<-\EOF > code.err
    334  1.10  rillig 		warning: code.c:3: Extra ')'
    335   1.6  rillig 	EOF
    336   1.6  rillig 
    337   1.6  rillig 	atf_check -o 'ignore' -e 'file:code.err' \
    338   1.6  rillig 	    "$indent" code.c -st
    339   1.6  rillig }
    340   1.6  rillig 
    341  1.11  rillig atf_test_case 'default_backup_extension'
    342  1.11  rillig default_backup_extension_body()
    343  1.11  rillig {
    344  1.11  rillig 	echo 'int var;' > code.c
    345  1.11  rillig 	echo 'int var;' > code.c.orig
    346  1.11  rillig 
    347  1.11  rillig 	atf_check \
    348  1.11  rillig 	    "$indent" code.c
    349  1.11  rillig 	atf_check -o 'file:code.c.orig' \
    350  1.11  rillig 	    cat code.c.BAK
    351  1.11  rillig }
    352  1.11  rillig 
    353  1.12  rillig atf_test_case 'several_profiles'
    354  1.12  rillig several_profiles_body()
    355  1.12  rillig {
    356  1.12  rillig 	# If the option '-P' occurs several times, only the last of the
    357  1.12  rillig 	# profiles is loaded, the others are ignored.
    358  1.12  rillig 
    359  1.12  rillig 	echo ' --invalid-option' > error.pro
    360  1.12  rillig 	echo '' > last.pro
    361  1.12  rillig 	echo '' > code.c
    362  1.12  rillig 
    363  1.12  rillig 	atf_check \
    364  1.12  rillig 	    "$indent" -Pnonexistent.pro -Perror.pro -Plast.pro code.c -st
    365  1.12  rillig }
    366  1.12  rillig 
    367   1.1  rillig atf_init_test_cases()
    368   1.1  rillig {
    369   1.2  rillig 	atf_add_test_case 'in_place'
    370   1.1  rillig 	atf_add_test_case 'verbose_profile'
    371   1.3  rillig 	atf_add_test_case 'nested_struct_declarations'
    372   1.4  rillig 	atf_add_test_case 'option_P_in_profile_file'
    373  1.13  rillig 	atf_add_test_case 'option_without_hyphen'
    374   1.5  rillig 	atf_add_test_case 'opt'
    375   1.5  rillig 	atf_add_test_case 'opt_npro'
    376   1.5  rillig 	atf_add_test_case 'opt_U'
    377   1.6  rillig 	atf_add_test_case 'line_no_counting'
    378  1.11  rillig 	atf_add_test_case 'default_backup_extension'
    379  1.12  rillig 	atf_add_test_case 'several_profiles'
    380   1.1  rillig }
    381