|
| 1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> |
| 2 | +<html xmlns:ext="http://io.github.nelson_lang/ext" lang="en"> |
| 3 | +<head> |
| 4 | +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 5 | +<title>assert_checkerror</title> |
| 6 | +<link rel="stylesheet" href="highlight.css"> |
| 7 | +<link rel="stylesheet" href="nelson_common.css"> |
| 8 | +<script src="nelson_help.js"></script><style> |
| 9 | + @media (prefers-color-scheme: dark) { |
| 10 | + /* target both class names used in different XSLT outputs */ |
| 11 | + .chapter-description, .chapter-desc { color: #e6eef8 !important; } |
| 12 | + .chapter-description p, .chapter-desc p { color: inherit !important; } |
| 13 | + } |
| 14 | + @media (prefers-color-scheme: light) { |
| 15 | + .chapter-description, .chapter-desc { color: #444 !important; } |
| 16 | + .chapter-description p, .chapter-desc p { color: inherit !important; } |
| 17 | + } |
| 18 | + </style> |
| 19 | +</head> |
| 20 | +<body> |
| 21 | +<a href="./index.html" class="home-summary-link"><button class="home-summary-btn" aria-label="Back to help index"><svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 12L12 3l9 9"></path><path d="M9 21V12h6v9"></path><path d="M9 21h6"></path></svg></button></a><div class="header"><div class="title"><span class="keyword-title">assert_checkerror</span></div></div> |
| 22 | +<div class="section syntax-section"><div class="subtitle">Check that a command raises the expected error.</div></div> |
| 23 | +<div class="section syntax-section"> |
| 24 | +<div class="section-title"> |
| 25 | +<span class="syntax-icon">📝</span>Syntax</div> |
| 26 | +<pre class="syntax-block"><code class="language-matlab">assert_checkerror(command, expected_error_message) |
| 27 | +r = assert_checkerror(command, expected_error_message) |
| 28 | +[r, msg] = assert_checkerror(command, expected_error_message) |
| 29 | +assert_checkerror(command, expected_error_message, expected_error_identifier) |
| 30 | +r = assert_checkerror(command, expected_error_message, expected_error_identifier) |
| 31 | +[r, msg] = assert_checkerror(command, expected_error_message, expected_error_identifier) |
| 32 | +</code></pre> |
| 33 | +</div> |
| 34 | +<div class="section"> |
| 35 | +<div class="section-title"> |
| 36 | +<span class="syntax-icon">📥</span>Input Arguments</div> |
| 37 | +<table class="param-table"> |
| 38 | +<tr> |
| 39 | +<th>Parameter</th> |
| 40 | +<th>Description</th> |
| 41 | +</tr> |
| 42 | +<tr> |
| 43 | +<td class="param-name">command</td> |
| 44 | +<td>a string containing the command to execute and test for errors.</td> |
| 45 | +</tr> |
| 46 | +<tr> |
| 47 | +<td class="param-name">expected_error_message</td> |
| 48 | +<td>a string containing the expected error message that should be raised.</td> |
| 49 | +</tr> |
| 50 | +<tr> |
| 51 | +<td class="param-name">expected_error_identifier</td> |
| 52 | +<td>a string containing the expected error identifier (optional).</td> |
| 53 | +</tr> |
| 54 | +</table> |
| 55 | +</div> |
| 56 | +<div class="section"> |
| 57 | +<div class="section-title"> |
| 58 | +<span class="syntax-icon">📤</span>Output Arguments</div> |
| 59 | +<table class="param-table"> |
| 60 | +<tr> |
| 61 | +<th>Parameter</th> |
| 62 | +<th>Description</th> |
| 63 | +</tr> |
| 64 | +<tr> |
| 65 | +<td class="param-name">r</td> |
| 66 | +<td>a logical value: true if the test passes, false otherwise.</td> |
| 67 | +</tr> |
| 68 | +<tr> |
| 69 | +<td class="param-name">msg</td> |
| 70 | +<td>a string containing the error message. If r == true, then msg == ''. If r == false, then msg contains the assertion failure message.</td> |
| 71 | +</tr> |
| 72 | +</table> |
| 73 | +</div> |
| 74 | +<div class="section"> |
| 75 | +<div class="section-title"> |
| 76 | +<span class="syntax-icon">📄</span>Description</div> |
| 77 | +<div> |
| 78 | + <p><b>assert_checkerror</b> verifies that executing a command raises the expected error message.</p> |
| 79 | + <p>If the command does not raise any error, or if it raises an error with a different message than expected, the assertion fails.</p> |
| 80 | + <p>When the optional <b>expected_error_identifier</b> parameter is provided, the function also checks that the error identifier matches the expected one.</p> |
| 81 | + <p>This function is particularly useful for unit testing to ensure that invalid inputs or operations properly generate the expected error conditions.</p> |
| 82 | + </div> |
| 83 | +</div> |
| 84 | +<div class="section"> |
| 85 | +<div class="section-title"> |
| 86 | +<span class="syntax-icon">💡</span>Examples</div> |
| 87 | +<div class="example"> |
| 88 | +<div class="example-title">Test that cos function with no arguments raises the expected error:</div> |
| 89 | +<div style="display:flex;align-items:flex-start;gap:8px;"> |
| 90 | +<pre class="syntax-block" style="flex:1 1 auto;margin:0;"><code class="language-matlab">assert_checkerror('cos', _('Wrong number of input arguments.'));</code></pre> |
| 91 | +<button class="copy-btn" type="button" onclick="copyExample(this)" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 20 20" fill="none" style="display:block;"><rect x="7" y="3" width="10" height="14" rx="2" fill="white" fill-opacity="0.15" stroke="white" stroke-width="1.2"></rect><rect x="3" y="7" width="10" height="10" rx="2" fill="currentColor"></rect></svg></button> |
| 92 | +</div> |
| 93 | +</div> |
| 94 | +<div class="example"> |
| 95 | +<div class="example-title">Example that demonstrates assertion failure with wrong expected message:</div> |
| 96 | +<div style="display:flex;align-items:flex-start;gap:8px;"> |
| 97 | +<pre class="syntax-block" style="flex:1 1 auto;margin:0;"><code class="language-matlab">try |
| 98 | + assert_checkerror('cos', _('Wrong error message.')); |
| 99 | +catch ME |
| 100 | + disp(['Error: ' ME.message]) |
| 101 | +end</code></pre> |
| 102 | +<button class="copy-btn" type="button" onclick="copyExample(this)" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 20 20" fill="none" style="display:block;"><rect x="7" y="3" width="10" height="14" rx="2" fill="white" fill-opacity="0.15" stroke="white" stroke-width="1.2"></rect><rect x="3" y="7" width="10" height="10" rx="2" fill="currentColor"></rect></svg></button> |
| 103 | +</div> |
| 104 | +</div> |
| 105 | +<div class="example"> |
| 106 | +<div class="example-title">Test with both error message and error identifier:</div> |
| 107 | +<div style="display:flex;align-items:flex-start;gap:8px;"> |
| 108 | +<pre class="syntax-block" style="flex:1 1 auto;margin:0;"><code class="language-matlab">assert_checkerror('mustBeFinite(NaN)', _('Value must be finite.'), 'Nelson:validators:mustBeFinite')</code></pre> |
| 109 | +<button class="copy-btn" type="button" onclick="copyExample(this)" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 20 20" fill="none" style="display:block;"><rect x="7" y="3" width="10" height="14" rx="2" fill="white" fill-opacity="0.15" stroke="white" stroke-width="1.2"></rect><rect x="3" y="7" width="10" height="10" rx="2" fill="currentColor"></rect></svg></button> |
| 110 | +</div> |
| 111 | +</div> |
| 112 | +<div class="example"> |
| 113 | +<div class="example-title">Using return values to handle assertion results:</div> |
| 114 | +<div style="display:flex;align-items:flex-start;gap:8px;"> |
| 115 | +<pre class="syntax-block" style="flex:1 1 auto;margin:0;"><code class="language-matlab">[r, msg] = assert_checkerror('cos', _('Wrong number of input arguments.')); |
| 116 | +if r |
| 117 | + disp('Test passed: cos function properly raises expected error') |
| 118 | +else |
| 119 | + disp(['Test failed: ' msg]) |
| 120 | +end</code></pre> |
| 121 | +<button class="copy-btn" type="button" onclick="copyExample(this)" title="Copy"><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 20 20" fill="none" style="display:block;"><rect x="7" y="3" width="10" height="14" rx="2" fill="white" fill-opacity="0.15" stroke="white" stroke-width="1.2"></rect><rect x="3" y="7" width="10" height="10" rx="2" fill="currentColor"></rect></svg></button> |
| 122 | +</div> |
| 123 | +</div> |
| 124 | +</div> |
| 125 | +<div class="section"> |
| 126 | +<div class="section-title"> |
| 127 | +<span class="syntax-icon">🔗</span>See Also</div> |
| 128 | +<div> |
| 129 | +<a class="see-also-link" href="../assert_functions/assert_istrue.html">assert_istrue</a><a class="see-also-link" href="../assert_functions/assert_isfalse.html">assert_isfalse</a> |
| 130 | +</div> |
| 131 | +</div> |
| 132 | +<div class="section"> |
| 133 | +<div class="section-title"> |
| 134 | +<span class="syntax-icon">🕔</span>Version History</div> |
| 135 | +<table class="param-table"> |
| 136 | +<tr> |
| 137 | +<th>Version</th> |
| 138 | +<th>Description</th> |
| 139 | +</tr> |
| 140 | +<tr> |
| 141 | +<td class="version">1.0.0</td> |
| 142 | +<td>initial version</td> |
| 143 | +</tr> |
| 144 | +</table> |
| 145 | +</div> |
| 146 | +<!--Author: Allan CORNET--><div class="section" style="margin-top:40px; text-align:center;"> |
| 147 | +<a id="github-edit-link" class="github-edit-btn" target="_blank" rel="noopener noreferrer"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"></path><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path></svg><span id="github-edit-text">Edit this page on GitHub</span></a><style> |
| 148 | + .github-edit-btn { display:inline-flex; align-items:center; gap:6px; font-size:14px; padding:6px 12px; border:1px solid #ddd; border-radius:4px; background:#f5f5f5; color:#333; text-decoration:none; transition:all 0.2s ease; } |
| 149 | + .github-edit-btn:hover { background:#e9e9e9; border-color:#ccc; color:#000; } |
| 150 | + </style> |
| 151 | +</div> |
| 152 | +</body> |
| 153 | +</html> |
0 commit comments