summaryrefslogtreecommitdiff
path: root/tests/get-them
blob: 7e03a7de5afc6bb3503ad8b322fc9a36bd6f8adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/sh
# Extract all examples from the manual source.
# Copyright (C) 1992 Free Software Foundation, Inc.

# This script is for use with GNU awk.

FILE=${1-/dev/null}

rm -f generated-tests/*.test 2>/dev/null

${AWK-gawk} '

BEGIN {
  node = "";
  seq = -1;
  count = 0;
  file = "NONE";
}

/^@node / {
  if (seq > 0)
    printf(" --  %d test%s", seq, seq == 1 ? "" : "s");
  if (seq >= 0)
    printf("\n");

  split($0, tmp, ",");
  Node = substr(tmp[1], 7);
  node = tolower(Node);
  gsub(" ", "", node);
  if (length(node) > 8)
    printf("Node: %s - truncated", node);
  else
    printf("Node: %s ", node);
  node = substr(node, 1, 8);
  seq = 0;
}

/^@comment ignore$/ {
  getline;
  next;
}

/^@example$/, /^@end example$/ {
  if (seq < 0)
    next;
  if ($0 ~ /^@example$/) {
    if (count > 0)
      close (file);
    seq++;
    count++;
    o = 0; 
    e = 0;
    error_flag = 0;
    i = 0;
    include_flag = 0;
    file = sprintf("generated-tests/%s.%d.test", node, seq);
    printf ("#!/bin/sh\n\n") > file;
    printf ("# %s is part of the GNU m4 testsuite\n", file) >> file;
    printf ("# generated from example in %s line %d\n\n", FILENAME, NR) >>file;
    printf (". ${srcdir}/defs\n") >> file;
    next;
  }
  if ($0 ~ /^@end example$/) {
    printthem(input, i, "in");
    printthem(output, o, "ok");
    printthem(error, e, "okerr");
    printf ("\n") >> file;
    if (include_flag == 1) printf ("M4PATH=$srcdir ") >> file;
    printf ("$M4 -d in >out") >> file;
    if (error_flag == 1) printf (" 2>err") >> file;
    if (error_flag == 1) printf ("\nsed -e \"s, ../../src/m4:, m4:,\" err >sederr && mv sederr err") >> file;
    printf ("\n\n$CMP -s out ok") >> file;
    if (error_flag == 1) printf (" && $CMP -s err okerr") >> file;
    printf ("\n\n") >> file;
    next;
  }
  if ($0 ~ /^\^D$/)
    next;
  if ($0 ~ /^@result\{\}/) {
    gsub(/^@result\{\}/, "", $0);
    output[o++] = $0;
  }
  else if ($0 ~ /^@error\{\}/) {
    gsub(/^@error\{\}/, "", $0);
    error[e++] = $0;
    error_flag = 1;
  }
  else {
    input[i++] = $0;
    if ($0 ~ /s*include\(/) include_flag = 1;
  }
}

END {
  printf("\n");
}

function printthem(thearray, thecounter, thefile) {
  if ((thecounter>0) || (thefile=="ok")) {
    printf ("\ncat <<\\EOF >%s\n", thefile) >> file;
    for (j=0; j<thecounter; j++) {
      gsub("@{", "{", thearray[j]);
      gsub("@}", "}", thearray[j]);
      gsub("@@", "@", thearray[j]);
      gsub("@comment.*", "", thearray[j]);
      printf ("%s\n", thearray[j]) >> file;
    }
    printf ("EOF\n") >> file;
  }
}

' $FILE >/dev/null

chmod +x generated-tests/*.test