-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjgivengherkin
executable file
·119 lines (98 loc) · 2.42 KB
/
jgivengherkin
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
115
116
117
118
119
#!/bin/sh
arbitrary_long_name==0 "exec" "/usr/bin/env" "gawk" "-f" "$0" "$@"
BEGIN {
INDENTATION = " ";
prev = "";
output = "";
allowPreviousAppending = 0;
prevVariable = "";
}
# We convert everything to lowercase so we don't have to worry about case in the Gherkin files.
# We also remove the trailing whitespace from all lines.
{ $0 = tolower($0);
remove_trailing_whitespace()
}
function remove_keyword() {
$1 = ""
remove_trailing_whitespace()
}
function remove_trailing_whitespace() {
sub(/^[ \t]+/, "")
}
function process_line(beginning, previousEnding, allowAppending) {
extract_string_variables();
extract_float_variables(); # must stand first
extract_int_variables();
convert_to_snake_case();
if (allowPreviousAppending) {
output = prev previousEnding;
} else {
output = prev
}
prev = beginning $0"(" prevVariable ")";
allowPreviousAppending = allowAppending;
prevVariable = "";
}
function extract_string_variables() {
if (match($0, /"/)) {
number = split($0, variables, "\"");
prevVariable = "\"" variables[2] "\"";
sub(/".*?"/, "")
}
}
function extract_float_variables() {
if (match($0, /[0-9*]\.[0-9*]/, variables)) {
prevVariable = variables[0];
sub(/[0-9*]\.[0-9*]/, "$");
}
}
function extract_int_variables() {
if (match($0, /[0-9*]/, variables)) {
prevVariable = variables[0];
sub(/[0-9*]/, "$");
}
}
function convert_to_snake_case() {
remove_keyword();
gsub(/ /, "_");
}
function convert_to_camel_case() {
remove_keyword();
capitalize();
gsub(/ /, "");
}
function capitalize() {
for (i=1; i<=NF; ++i) {
$i=toupper(substr($i,1,1)) tolower(substr($i,2));
}
}
/^feature: / {
convert_to_camel_case()
print "import org.junit.Test;";
print "import com.tngtech.jgiven.junit.ScenarioTest;\n";
print "public class " $0 "Test extends";
print INDENTATION "ScenarioTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {";
}
/^scenario: / {
beginning = "\n" INDENTATION "@Test\n" INDENTATION "public void "
ending = ";\n" INDENTATION "}"
process_line(beginning, ending, 0);
prev = prev " {\n"
}
/^(given|when|then)/ {
process_line(INDENTATION $1"().", ";\n", 1)
}
/^and/ {
process_line(INDENTATION INDENTATION $1"().", ".", 1)
}
# Ignore comments and empty lines.
/^(#|\s*$)/ { next }
{ if (output != "") {
print INDENTATION output
}
}
END {
print INDENTATION prev ";"
print INDENTATION "}"
print "}"
}