%% Abort Test Session if State Is Corrupted
% Test a function that sets the value of an operating system environment variable. If the environment variable cannot be reset to its original value after the test, abort the test session using a fatal assertion failure.
% In a file in your current folder, create the setUserName function. The function uses a call to setenv to set the 'UserName' environment variable.
% function setUserName(name)
% setenv('UserName',name)
% end
% To test the setUserName function, create a test class named SetUserNameTest in your current folder. Define the necessary class members for your test:
% OriginalUserName property — Use this property to reset the environment variable after the test.
% testUpdate Test method — Store the original value of the environment variable, call the function under test, and verify that the function sets the environment variable to the expected value. Because the state changes during the test, include a call to addTeardown to restore the state once the test runs to completion.
% resetUserName helper method — Call the setUserName function to reset the environment variable. If the operation is not successful, abort the test session using a fatal assertion failure.
% classdef SetUserNameTest < matlab.unittest.TestCase
%     properties (SetAccess = private)
%         OriginalUserName
%     end
%
%     methods (Test)
%         function testUpdate(testCase)
%             testCase.OriginalUserName = getenv('UserName');
%             setUserName('David')
%             testCase.addTeardown(@() testCase.resetUserName)
%             testCase.verifyEqual(getenv('UserName'),'David')
%         end
%     end
%
%     methods (Access = private)
%         function resetUserName(testCase)
%             setUserName(testCase.OriginalUserName)
%             testCase.fatalAssertEqual(getenv('UserName'),testCase.OriginalUserName)
%         end
%     end
% end
% Run the SetUserNameTest class. The test passes.

runtests("SetUserNameTest")

% Copyright 2021 The MathWorks, Inc.