HighlightWords

Highlight words inside a given text.

python · Base

Details

IDHighlightWords
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775

README

Highlights words inside a given text.

Script Data


Name Description
Script Type python
Tags -

Inputs


Argument Name Description
text The input text to highlight the words in.
terms Terms to highlight in the text. Can be words or sentences (without commas “,”). Note: if you use a sentences and word, the word shouldn’t be as part of a sentence. For example, “thank,thank you” is an invalid input. (Comma-separated value).

Outputs


There are no outputs for this script.

import demistomock as demisto  # noqa: F401
import pytest
from HighlightWords import main


@pytest.mark.parametrize(
    "args, expected_text",
    [
        ({"terms": "test", "text": "This is a test"}, "this is a **test**"),
        ({"terms": "test", "text": "Test upper case"}, "**test** upper case"),
        ({"terms": "test", "text": "test two test"}, "**test** two **test**"),
    ],
)
def test_highlight_as_expected(mocker, args, expected_text):
    """
    Scenario: Run the script

    Given:
    - args with one word and a text

    Then:
    - assert result is as expected
    """
    mocker.patch.object(demisto, "args", return_value=args)
    mocker.patch.object(demisto, "results")
    main()
    assert demisto.results.call_args[0][0]["Contents"] == expected_text


def test_error_for_sentence_and_word(mocker):
    """
    Scenario: Args has word and a sentence containing the word

    Given:
    - args where there's a word and a sentence containing the word

    Then:
    - Make sure an error is printed
    """
    mocker.patch.object(demisto, "args", return_value={"terms": "test, this is a test", "text": "testing test"})
    error_mock = mocker.patch("HighlightWords.return_error", return_value=None)
    main()
    assert error_mock.call_args.args[0] == 'The word "test" is a substring of the sentance: "this is a test"'