1+ % Copyright 2024 The MathWorks, Inc.
2+ classdef TestExecuteFunction < matlab .unittest .TestCase
3+ % TestExecuteFunction contains unit tests for the execute function
4+ properties
5+ TestPaths
6+ end
7+
8+ methods (TestClassSetup )
9+ function addFunctionPath(testCase )
10+ testCase.TestPaths = cellfun(@(relative_path )(fullfile(pwd , relative_path )), {" ../../src/jupyter_matlab_kernel/matlab" }, ' UniformOutput' , false );
11+ cellfun(@addpath , testCase .TestPaths )
12+ end
13+ function suppressWarnings(testCase )
14+ warning(' off' , ' all' );
15+ testCase .addTeardown(@() warning(' on' , ' all' ));
16+ end
17+ end
18+
19+ methods (TestClassTeardown )
20+ function removeFunctionPath(testCase )
21+ cellfun(@rmpath , testCase .TestPaths )
22+ end
23+ end
24+ methods (Test )
25+ function testMatrixOutput(testCase )
26+ % Test execution of a code that generates a matrix output
27+ code = ' repmat([1 2 3 4],5,1)' ;
28+ kernelId = ' test_kernel_id' ;
29+ result = jupyter .execute(code , kernelId );
30+ testCase .verifyEqual(result{1 }.type, ' execute_result' , ' Expected execute_result type' );
31+ testCase .verifyTrue(any(strcmp(result{1 }.mimetype{1 }, ' text/html' )), ' Expected HTML output' );
32+ testCase .verifyTrue(any(strcmp(result{1 }.mimetype{2 }, ' text/plain' )), ' Expected HTML output' );
33+ testCase .verifySubstring(result{1 }.value{1 }, ' ans = 5' );
34+ end
35+
36+ function testVariableOutput(testCase )
37+ % Test execution of a code that generates a variable output
38+ code = ' var x' ;
39+ kernelId = ' test_kernel_id' ;
40+ result = jupyter .execute(code , kernelId );
41+ testCase .verifyEqual(result{1 }.type, ' execute_result' , ' Expected execute_result type' );
42+ testCase .verifyTrue(any(strcmp(result{1 }.mimetype{1 }, ' text/html' )), ' Expected HTML output' );
43+ testCase .verifyTrue(any(strcmp(result{1 }.mimetype{2 }, ' text/plain' )), ' Expected HTML output' );
44+ testCase .verifySubstring(result{1 }.value{1 }, ' ans = 0' );
45+ end
46+
47+ % Skipping the following test as it fails in public github run
48+ % function testSymbolicOutput(testCase)
49+ % %Test execution of a code that generates a symbolic output
50+ % code = 'x = sym(1/3); disp(x);';
51+ % kernelId = 'test_kernel_id';
52+ % result = jupyter.execute(code, kernelId);
53+ % testCase.verifyEqual(result{1}.type, 'execute_result', 'Expected execute_result type');
54+ % testCase.verifyTrue(any(strcmp(result{1}.mimetype{1}, ["text/latex", "text/html"])), 'Expected LaTeX or HTML output');
55+ % end
56+
57+ function testErrorOutput(testCase )
58+ % Test execution of a code that generates an error
59+ code = ' error('' Test error'' );' ;
60+ kernelId = ' test_kernel_id' ;
61+ result = jupyter .execute(code , kernelId );
62+ testCase .verifyEqual(result{1 }.type, ' stream' , ' Expected stream type' );
63+ testCase .verifyEqual(result{1 }.content.name, ' stderr' , ' Expected stderr stream' );
64+ testCase .verifyTrue(contains(result{1 }.content.text, ' Test error' ), ' Expected error message' );
65+ end
66+
67+ function testFigureOutput(testCase )
68+ % Test execution of a code that generates a figure output
69+ code = ' figure; plot(1:10); title('' Test Figure'' );' ;
70+ kernelId = ' test_kernel_id' ;
71+ result = jupyter .execute(code , kernelId );
72+ testCase .verifyEqual(result{1 }.type, ' execute_result' , ' Expected execute_result type' );
73+ testCase .verifyTrue(any(strcmp(result{1 }.mimetype, ' image/png' )), ' Expected PNG image output' );
74+ testCase .verifyTrue(~isempty(result{1 }.value{1 }));
75+ end
76+ end
77+ end
0 commit comments