Skip to content

Commit

Permalink
refactor!: Change the test API for scripts
Browse files Browse the repository at this point in the history
This change generalize the `test` function for script, allowing to have multiple test functions.
Also remove the Knut.Test module, not needed anymore.

This makes the whole testing more streamline, and easier to work with.

- remove the `Knut.Test` module and `TestCase` item
- add `compare` and `verify` to `Script` and `ScriptDialog`
- run all methods starting with `test_` in test mode
- don't run `test` anymore (breaking change here), as it does not make sense to have a special case for this.

Related to #49
  • Loading branch information
narnaud committed Jul 10, 2024
1 parent 501b1c0 commit cf7f514
Show file tree
Hide file tree
Showing 28 changed files with 401 additions and 585 deletions.
40 changes: 0 additions & 40 deletions docs/API/knut.test/testcase.md

This file was deleted.

42 changes: 0 additions & 42 deletions docs/API/knut.test/testutil.md

This file was deleted.

7 changes: 4 additions & 3 deletions docs/API/knut/qtuiwidget.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Knut

| | Name |
|-|-|
||**[addProperty](#addProperty)**(string name, var value, object attributes = {})|
||**[addProperty](#addProperty)**(string name, var value, object attributes = {}, bool userProperty = false)|
|var |**[getProperty](#getProperty)**(string name)|

## Property Documentation
Expand All @@ -37,16 +37,17 @@ Name of the widget.

## Method Documentation

#### <a name="addProperty"></a>**addProperty**(string name, var value, object attributes = {})
#### <a name="addProperty"></a>**addProperty**(string name, var value, object attributes = {}, bool userProperty = false)

Adds a new property with the given `name`, `value` and `attributes`.
Adds a new property with the given `name`, `value`, `attributes` and `userProperty`.

Attributes is a has<string, string> object, where the key is the attribute name and the value is the attribute value.
For example:

```
widget.setProperty("text", "My text", { "comment": "some comment for translation" });
```
Set userProperty to true if you doesn't want to generate property.

#### <a name="getProperty"></a>var **getProperty**(string name)

Expand Down
61 changes: 61 additions & 0 deletions docs/API/knut/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Script object for writing non visual scripts. [More...](#detailed-description)
import Knut
```

## Methods

| | Name |
|-|-|
||**[compare](#compare)**(var actual, var expected, string message = {})|
||**[verify](#verify)**(bool value, string message = {})|

## Detailed Description

The `Script` is the base class for all creatable items in QML. It is needed as a `QtObject`
Expand All @@ -18,3 +25,57 @@ Script {
// ...
}
```

You can also integrate unit-tests as part of the script dialog. This is done by adding methods prefixed with `test_`.
Those methods will be executed when the script is run in test mode (using `--test` command line option). If a test is
failing, the script will show a critical log message, and the script will return a non-zero exit code.

```qml
import Knut
Script {
function test_foo() {
compare(1, 2, "test should failed")
}
function test_bar() {
verify(1 == 1, "test should pass")
}
}
```

## Method Documentation

#### <a name="compare"></a>**compare**(var actual, var expected, string message = {})

Compare `actual` with `expected` and log an error `message` if they are not equal.

This method will increment the number of failed test internally, and when the script is finished, the test runner
will return the number of failed tests.

This is usually used in tests method like that:

```qml
Script {
function test_foo() {
compare(1, 2, "1 should be equal to 2"); // This will log an error
}
}
```

#### <a name="verify"></a>**verify**(bool value, string message = {})

Compare `actual` with `expected` and log an error `message` if they are not equal.

This method will increment the number of failed test internally, and when the script is finished, the test runner
will return the number of failed tests.

This is usually used in tests method like that:

```qml
Script {
function test_foo() {
let answerToEverything = 42;
verify(answerToEverything == 42, "What else?");
}
}
```
54 changes: 54 additions & 0 deletions docs/API/knut/scriptdialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import Knut

| | Name |
|-|-|
||**[compare](#compare)**(var actual, var expected, string message = {})|
||**[firstStep](#firstStep)**(string firstStep)|
||**[nextStep](#nextStep)**(string title)|
||**[runSteps](#runSteps)**(function generator)|
||**[setStepCount](#setStepCount)**(int stepCount)|
||**[verify](#verify)**(bool value, string message = {})|

## Signals

Expand Down Expand Up @@ -69,6 +71,23 @@ ScriptDialog {
}
```

You can also integrate unit-tests as part of the script dialog. This is done by adding methods prefixed with `test_`.
Those methods will be executed when the script is run in test mode (using `--test` command line option). If a test is
failing, the script will show a critical log message, and the script will return a non-zero exit code.

```qml
import Knut
ScriptDialog {
function test_foo() {
compare(1, 2, "test should failed")
}
function test_bar() {
verify(1 == 1, "test should pass")
}
}
```

## Property Documentation

#### <a name="data"></a>var **data**
Expand All @@ -88,6 +107,23 @@ Number of steps to display in the progress bar.

## Method Documentation

#### <a name="compare"></a>**compare**(var actual, var expected, string message = {})

Compare `actual` with `expected` and log an error `message` if they are not equal.

This method will increment the number of failed test internally, and when the script is finished, the test runner
will return the number of failed tests.

This is usually used in tests method like that:

```qml
ScriptDialog {
function test_foo() {
compare(1, 2, "1 should be equal to 2"); // This will log an error
}
}
```

#### <a name="firstStep"></a>**firstStep**(string firstStep)

Starts a progress bar with the given `firstStep` title.
Expand Down Expand Up @@ -142,6 +178,24 @@ Sets the number of steps to show in the progress bar.
By default the value is 0, meaning there are no steps set. This will show an indeterminate progress bar. You can use
the `stepCount` property to set the number of steps too.

#### <a name="verify"></a>**verify**(bool value, string message = {})

Compare `actual` with `expected` and log an error `message` if they are not equal.

This method will increment the number of failed test internally, and when the script is finished, the test runner
will return the number of failed tests.

This is usually used in tests method like that:

```qml
ScriptDialog {
function test_foo() {
let answerToEverything = 42;
verify(answerToEverything == 42, "What else?");
}
}
```

## Signal Documentation

#### <a name="onAccepted"></a>**onAccepted**()
Expand Down
2 changes: 1 addition & 1 deletion examples/ex_gui_interactive.qml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ScriptDialog {

// This is to test the script automatically, to be used with `knut --test ex-gui-interactive.qml`
// It will run the script automatically without interaction, even if it's the same method as `onAccepted`
function test() {
function test_script() {
runSteps(stepGeneratorFunc());
close();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ex_gui_progressbar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ScriptDialog {

// This is to test the script automatically, to be used with `knut --test ex-gui-interactive.qml`
// It will run the script automatically without interaction
function test() {
function test_script() {
showProgress();
close();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ex_script_dialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ScriptDialog {
}
// Function to automatically test the script, useful for automated testing
// It runs the script without user interaction
function test() {
function test_script() {
showData();
close();
}
Expand Down
3 changes: 0 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ nav:
- Utils: API/knut/utils.md
- QDirValueType: API/knut/qdirvaluetype.md
- QFileInfoValueType: API/knut/qfileinfovaluetype.md
- Knut.Test Module:
- TestCase: API/knut.test/testcase.md
- TestUtil: API/knut.test/testutil.md
# <--

markdown_extensions:
Expand Down
2 changes: 0 additions & 2 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ set(PROJECT_SOURCES
slintdocument.cpp
symbol.h
symbol.cpp
testutil.h
testutil.cpp
textdocument.h
textdocument.cpp
textdocument_p.h
Expand Down
2 changes: 0 additions & 2 deletions src/core/core.qrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<RCC>
<qresource prefix="/">
<file>core/settings.json</file>
<file>qml/Knut/Test/qmldir</file>
<file>qml/Knut/Test/TestCase.qml</file>
</qresource>
</RCC>
Loading

0 comments on commit cf7f514

Please sign in to comment.