# References - [Basic structure](#basic-structure) - [Example group](#example-group) - [`ExampleGroup` / `Describe` / `Context`](#examplegroup--describe--context) - [Example](#example) - [`Example` / `It` / `Specify`](#example--it--specify) - [Evaluation](#evaluation) - [`When call`](#when-call) - [`When run`](#when-run) - [about calling shell function with run](#about-calling-shell-function-with-run) - [`When run command`](#when-run-command) - [`When run script`](#when-run-script) - [`When run source`](#when-run-source) - [Comparison](#comparison) - [Expectation](#expectation) - [`The` ... `should (not)`](#the--should-not) - [`Assert`](#assert) - [Subjects](#subjects) - [`stdout` (`output`) subject](#stdout-output-subject) - [`stderr` (`error`) subject](#stderr-error-subject) - [`status` subject](#status-subject) - [`line` subject](#line-subject) - [`word` subject](#word-subject) - [`path` / `file` / `directory` subject](#path--file--directory-subject) - [`function` subject](#function-subject) - [`value` subject](#value-subject) - [`variable` subject](#variable-subject) - [Modifiers](#modifiers) - [`line` modifier](#line-modifier) - [`lines` modifier](#lines-modifier) - [`word` modifier](#word-modifier) - [`length` modifier](#length-modifier) - [`contents` modifier](#contents-modifier) - [`result` modifier](#result-modifier) - [Matchers](#matchers) - [`satisfy` matcher](#satisfy-matcher) - [stat matchers](#stat-matchers) - [`be exist` matcher](#be-exist-matcher) - [`be file` matcher](#be-file-matcher) - [`be directory` matcher](#be-directory-matcher) - [`be empty file` matcher](#be-empty-file-matcher) - [`be empty directory` matcher](#be-empty-directory-matcher) - [`be symlink` matcher](#be-symlink-matcher) - [`be pipe` matcher](#be-pipe-matcher) - [`be socket` matcher](#be-socket-matcher) - [`be readable` matcher](#be-readable-matcher) - [`be writable` matcher](#be-writable-matcher) - [`be executable` matcher](#be-executable-matcher) - [`be block_device` matcher](#be-block_device-matcher) - [`be character_device` matcher](#be-character_device-matcher) - [`has setgid` matcher](#has-setgid-matcher) - [`has setuid` matcher](#has-setuid-matcher) - [status matchers](#status-matchers) - [`be success` matcher](#be-success-matcher) - [`be failure` matcher](#be-failure-matcher) - [string matchers](#string-matchers) - [`equal` matcher](#equal-matcher) - [`start with` matcher](#start-with-matcher) - [`end with` matcher](#end-with-matcher) - [`include` matcher](#include-matcher) - [`match pattern` matcher](#match-pattern-matcher) - [`successful` matcher](#successful-matcher) - [valid matchers](#valid-matchers) - [variable matchers](#variable-matchers) - [`be defined` matcher](#be-defined-matcher) - [`be undefined` matcher](#be-undefined-matcher) - [`be present` matcher](#be-present-matcher) - [`be blank` matcher](#be-blank-matcher) - [`be exported` matcher](#be-exported-matcher) - [`be readonly` matcher](#be-readonly-matcher) - [Helper](#helper) - [Skip / Pending](#skip--pending) - [`Skip`](#skip) - [`Skip if`](#skip-if) - [`Pending`](#pending) - [`Todo`](#todo) - [Data](#data) - [`Data[:raw]`](#dataraw) - [`Data:expand`](#dataexpand) - [`Data `](#data-function) - [`Data ""`](#data-string) - [`Data < ""`](#data--file) - [Parameters](#parameters) - [`Parameters[:block]`](#parametersblock) - [`Parameters:value`](#parametersvalue) - [`Parameters:matrix`](#parametersmatrix) - [`Parameters:dynamic`](#parametersdynamic) - [Others](#others) - [`Include`](#include) - [`Path` / `File` / `Dir`](#path--file--dir) - [`Intercept`](#intercept) - [`Set`](#set) - [`Dump`](#dump) - [Hooks](#hooks) - [`Before` / `After`](#before--after) - [`BeforeAll` / `AfterAll`](#beforeall--afterall) - [`BeforeCall` / `AfterCall`](#beforecall--aftercall) - [`BeforeRun` / `AfterRun`](#beforerun--afterrun) - [Directive](#directive) - [`%const` (`%`)](#const-) - [`%text`](#text) - [`%puts` (`%-`) / `%putsn` (`%=`)](#puts----putsn-) - [%preserve](#preserve) - [`%logger`](#logger) - [Special environment Variables](#special-environment-variables) ## Basic structure You can write a structured *Example* by using the DSL shown below: ### Example group | DSL | Description | | :------------------- | :-------------------------- | | ExampleGroup ... End | Define an example group. | | Describe ... End | Synonym for `ExampleGroup`. | | Context ... End | Synonym for `ExampleGroup`. | #### `ExampleGroup` / `Describe` / `Context` Example groups are nestable. ### Example | DSL | Description | | :-------------- | :--------------------- | | Example ... End | Define an example. | | It ... End | Synonym for `Example`. | | Specify ... End | Synonym for `Example`. | #### `Example` / `It` / `Specify` ### Evaluation The line beginning with `When` is the evaluation. | Evaluation | Description | | :--------------- | :------------------------------------------------------------------- | | When call | Call shell function without subshell. | | When run | Run shell function or external command within a subshell. | | When run command | Run external command (including non-shell scripts). | | When run script | Run shell script by new process of the current shell. | | When run source | Run shell script in the current shell by `.` command (aka `source`). | #### `When call` ```sh When call [ARGUMENTS...] ``` This is primarily designed for shell function calls. It is the recommended evaluation as a unit test. It does not use a subshell, therefore it is the fastest evaluation variant and you can assert variables. #### `When run` ```sh When run [ARGUMENTS...] ``` This is primarily designed for external command calls. The external command does not have to be a shell script. Even shell scripts are executed as external commands according to the shebang, so they are not covered by the coverage. ##### about calling shell function with run If a shell function is specified, it will be executed in a subshell. The slight advantage of executing shell functions with `run` is that you can trap errors with `set -e`. Unlike `call`, it does not cause an error, so you can assert the exit status. Also, because of the execution in the subshell, the variables which change values in the function are restored once `run` finishes. This is often a disadvantage, but tests of ShellSpec itself intentionally use `run` because changing internal variables confuses ShellSpec's behavior. If you want to assert variables with `run`, use the `%preserve` directive in function called by `AfterRun` hook. It can preserve variables even if `run` exits the subshell. #### `When run command` ```sh When run command [ARGUMENTS...] ``` Run an external command explicitly. The external command does not have to be a shell script. Even shell scripts are executed as external commands according to the shebang, so they are not covered by the coverage. #### `When run script` ```sh When run script