ci: fix `$tmpl` regression detected by gitly tests (after a73e146)

pull/13090/head
Delyan Angelov 2022-01-07 19:19:31 +02:00
parent 3e9c1c1a3a
commit 5717066147
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 40 additions and 16 deletions

View File

@ -96,21 +96,22 @@ fn vweb_tmpl_${fn_name}() string {
mut tline_number := -1 // keep the original line numbers, even after insert/delete ops on lines; `i` changes
for i := 0; i < lines.len; i++ {
line := lines[i]
trimmed_line := line.trim_space()
tline_number++
start_of_line_pos = end_of_line_pos
end_of_line_pos += line.len + 1
$if trace_tmpl ? {
eprintln('>>> tfile: $template_file, spos: ${start_of_line_pos:6}, epos:${end_of_line_pos:6}, fi: ${tline_number:5}, i: ${i:5}, line: $line')
}
if is_html_open_tag('style', line) {
state = .css
} else if line == '</style>' {
} else if trimmed_line == '</style>' {
state = .html
} else if is_html_open_tag('script', line) {
state = .js
} else if line == '</script>' {
} else if trimmed_line == '</script>' {
state = .html
}
$if trace_tmpl ? {
eprintln('>>> tfile: $template_file, spos: ${start_of_line_pos:6}, epos:${end_of_line_pos:6}, fi: ${tline_number:5}, i: ${i:5}, state: ${state:10}, line: $line')
}
if line.contains('@header') {
position := line.index('@header') or { 0 }
p.error_with_error(errors.Error{

View File

@ -1,11 +1,20 @@
// V won't parse that
<script>
var non_interpolated_labels = @benchmark_plot_data.dates;
var non_interpolated_values = @benchmark_plot_data.numerical_result;
</script>
// V will parse that
<script>
var real_labels = @benchmark_plot_data.dates; //V_TEMPLATE
var real_values = @benchmark_plot_data.numerical_result; //V_TEMPLATE
</script>
This should be interpolated:
Username: @username
// V will NOT do interpolation inside <script> tags by default:
<script>
var non_interpolated_labels = @benchmark_plot_data.dates;
var non_interpolated_values = @benchmark_plot_data.numerical_result;
</script>
// V will interpolate the values here:
<script>
var real_labels = @benchmark_plot_data.dates; //V_TEMPLATE
var real_values = @benchmark_plot_data.numerical_result; //V_TEMPLATE
</script>
This should be interpolated too, because it is outside the script tag:
Year: @year
myint: @{benchmark_plot_data.myint(1)}
mydate: @{benchmark_plot_data.mydate()}

View File

@ -5,12 +5,26 @@ struct PlotData {
numerical_result []int
}
fn (pd PlotData) mydate() string {
return pd.dates[0]
}
fn (pd PlotData) myint(idx int) int {
return pd.numerical_result[idx]
}
fn test_template_interpolation_can_be_selectively_turned_on_in_script_tags() {
benchmark_plot_data := PlotData{['2012-11-30', '2022-12-29'], [5, 6, 7, 1]}
username := 'abcd'
year := 2022
text := $tmpl('tmpl/selective_interpolation_in_script_tag.html')
// dump(text)
dump(text)
assert text.contains('Username: abcd')
assert text.contains('var non_interpolated_labels = @benchmark_plot_data.dates;')
assert text.contains('var non_interpolated_values = @benchmark_plot_data.numerical_result;')
assert text.contains("var real_labels = ['2012-11-30', '2022-12-29']; //V_TEMPLATE")
assert text.contains('var real_values = [5, 6, 7, 1]; //V_TEMPLATE')
assert text.contains('Year: 2022')
assert text.contains('myint: 6')
assert text.contains('mydate: 2012-11-30')
}