From e209cbe61aacea60ccff7b806d2a8fab05750e18 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 10 Nov 2019 11:20:21 +0000 Subject: [PATCH 1/5] tests: Include the name of the 'cardinality check' test point in the output --- tests/test-highlighting.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index b2a6db5..b55324c 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -178,7 +178,7 @@ run_test_internal() { details+="have $#expected_region_highlight expectations and $#region_highlight region_highlight entries: " details+="«$(typeset_p expected_region_highlight)» «$(typeset_p region_highlight)»" tap_escape $details; details=$REPLY - print -r -- "not ok $i - $details" "${expected_mismatch:+"# TODO ${(qqq)expected_mismatch}"}" + print -r -- "not ok $i - cardinality check" "$details" "${expected_mismatch:+"# TODO ${(qqq)expected_mismatch}"}" fi } From d5a4a6e1951fb2f4fbbad205405c0f6b1b349187 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 10 Nov 2019 11:29:20 +0000 Subject: [PATCH 2/5] tests: Make $expected_mismatch skip the cardinality check, rather than consider it an expected failure. With this change, if $expected_region_highlight and $region_highlight coincidentally have the same number of elements, the test won't be considered to fail. This is useful in conjunction with the next commit, q.v.. At this time, no tests set $expected_mismatch explicitly. However, the commit after next (this commit's grandchild) will add a test that will set $expected_mismatch implicitly, using the functionality in the next commit (this commit's child). --- tests/README.md | 3 +-- tests/test-highlighting.zsh | 9 ++++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/README.md b/tests/README.md index 89aef14..a413e16 100644 --- a/tests/README.md +++ b/tests/README.md @@ -23,8 +23,7 @@ need not match the order in `$region_highlight`. Normally, tests fail if `$expected_region_highlight` and `$region_highlight` have different numbers of elements. Tests may set `$expected_mismatch` to an -explanation string (like `$todo`) to avoid this and mark the cardinality check -as todo. +explanation string (like `$todo`) to avoid this and skip the cardinality check. **Note**: `$region_highlight` uses the same `"$i $j $style"` syntax but interprets the indexes differently. diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index b55324c..7b32dbc 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -171,14 +171,17 @@ run_test_internal() { unset desc done - if (( $#expected_region_highlight == $#region_highlight )); then - print -r -- "ok $i - cardinality check" "${expected_mismatch:+"# TODO ${(qqq)expected_mismatch}"}" + if [[ -n $expected_mismatch ]]; then + tap_escape $expected_mismatch; expected_mismatch=$REPLY + print "ok $i - cardinality check" "# SKIP $expected_mismatch" + elif (( $#expected_region_highlight == $#region_highlight )); then + print -r -- "ok $i - cardinality check" else local details details+="have $#expected_region_highlight expectations and $#region_highlight region_highlight entries: " details+="«$(typeset_p expected_region_highlight)» «$(typeset_p region_highlight)»" tap_escape $details; details=$REPLY - print -r -- "not ok $i - cardinality check" "$details" "${expected_mismatch:+"# TODO ${(qqq)expected_mismatch}"}" + print -r -- "not ok $i - cardinality check - $details" fi } From 4952325051d41817485a4a3a2dfc9a70dd134c74 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 10 Nov 2019 11:35:21 +0000 Subject: [PATCH 3/5] tests: Skip cardinality tests whenever any test point is expected to fail. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When writing an expected-to-fail test case, the cardinality of $region_highlight at the time the test is written may differ from the cardinality it will have once the bug is fixed. For example, with issue #641.5, the current highlighting is ['nice', 'x=y', 'y', 'ls'] — four elements — but the correct highlighting would have three elements: ['nice', 'x=y', 'ls']. There is no point in reporting a separate test failure for the cardinality check in this case, nor for 'ls' being highlighted as 'command' rather than 'default'. At the same time, in other cases the current and correct highlighting may have the same number of elements (for example, this would be the case for a hypothetical "the command word is highlighted as an alias rather than a function" bug). Thus, the previous commit, q.v.. --- tests/README.md | 1 + tests/test-highlighting.zsh | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/README.md b/tests/README.md index a413e16..ebacae4 100644 --- a/tests/README.md +++ b/tests/README.md @@ -24,6 +24,7 @@ need not match the order in `$region_highlight`. Normally, tests fail if `$expected_region_highlight` and `$region_highlight` have different numbers of elements. Tests may set `$expected_mismatch` to an explanation string (like `$todo`) to avoid this and skip the cardinality check. +`$expected_mismatch` is set implicitly if the `$todo` component is present. **Note**: `$region_highlight` uses the same `"$i $j $style"` syntax but interprets the indexes differently. diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index 7b32dbc..a01df43 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -145,7 +145,10 @@ run_test_internal() { local -a expected_highlight_zone; expected_highlight_zone=( ${(z)expected_region_highlight[i]} ) integer exp_start=$expected_highlight_zone[1] exp_end=$expected_highlight_zone[2] local todo= - (( $+expected_highlight_zone[4] )) && todo="# TODO $expected_highlight_zone[4]" + if (( $+expected_highlight_zone[4] )); then + todo="# TODO $expected_highlight_zone[4]" + : ${expected_mismatch:="cardinality check disabled whilst regular test points are expected to fail"} + fi if ! (( $+region_highlight[i] )); then print -r -- "not ok $i - unmatched expectation ($exp_start $exp_end $expected_highlight_zone[3])" continue From 926c36c1fb3f47f1a21a6a6fd699258011cc5fda Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 10 Nov 2019 11:37:41 +0000 Subject: [PATCH 4/5] Add a test for issue #641.5, using the infrastructure added in the previous commits. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Current output: . # precommand-then-assignment 1..4 ok 1 - [1,4] «nice» not ok 2 - [6,8] «x=y» - expected (6 8 "unknown-token"), observed (6 8 "assign"). # TODO "issue #641.5" not ok 3 - [8,8] «y» - expected (10 11 "default"), observed (8 8 "default"). # TODO "issue #641.5 (fallout)" ok 4 - cardinality check # SKIP cardinality check disabled whilst regular test points are expected to fail --- .../test-data/precommand-then-assignment.zsh | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 highlighters/main/test-data/precommand-then-assignment.zsh diff --git a/highlighters/main/test-data/precommand-then-assignment.zsh b/highlighters/main/test-data/precommand-then-assignment.zsh new file mode 100644 index 0000000..f0bb75f --- /dev/null +++ b/highlighters/main/test-data/precommand-then-assignment.zsh @@ -0,0 +1,37 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2019 zsh-syntax-highlighting contributors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + +BUFFER=$'nice x=y ls' + +expected_region_highlight=( + '1 4 precommand' # nice + '6 8 unknown-token "issue #641.5"' # x=y + '10 11 default "issue #641.5 (fallout)"' # ls +) From b7592e581dad4e8bba0ceb44ca85dd7c9dc293af Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 10 Nov 2019 11:41:31 +0000 Subject: [PATCH 5/5] tests: Minor documentation readability tweak --- tests/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/README.md b/tests/README.md index ebacae4..f35a5c1 100644 --- a/tests/README.md +++ b/tests/README.md @@ -16,8 +16,10 @@ that is, `$i` and `$j` specify a range, 1-indexed, inclusive of both endpoints. `$style` is a key of `$ZSH_HIGHLIGHT_STYLES`. If `$todo` exists, the test point is marked as TODO (the failure of that test point will not fail the test), and `$todo` is used as the explanation. + If a test sets `$skip_test` to a non-empty string, the test will be skipped with the provided string as the reason. + If a test sets `unsorted=1` the order of highlights in `$expected_region_highlight` need not match the order in `$region_highlight`.