Repository: microsoft/terminal
File: src/cascadia/LocalTests_SettingsModel/TerminalSettingsTests.cpp (C++)

Code before:
        auto newFontSize = selfSettings->FontSize();
        VERIFY_ARE_NOT_EQUAL(oldFontSize, newFontSize);
    }

    // CascadiaSettings::_normalizeCommandLine abuses some aspects from CommandLineToArgvW
    // to simplify the implementation. It assumes that all arguments returned by
    // CommandLineToArgvW are returned back to back in memory as "arg1\0arg2\0arg3\0...".
    // This test ensures CommandLineToArgvW doesn't change just to be sure.
    void TerminalSettingsTests::CommandLineToArgvW()
    {
        pcg_engines::oneseq_dxsm_64_32 rng{ til::gen_random<uint64_t>() };

        const auto expectedArgc = static_cast<int>(rng(16) + 1);
        std::wstring expectedArgv;
        std::wstring input;

        // We generate up to 16 arguments. Each argument is up to 64 chars long, is quoted
        // (2 chars, only applies to the input) and separated by a whitespace (1 char).
        expectedArgv.reserve(expectedArgc * 65);
        input.reserve(expectedArgc * 67);

        for (int i = 0; i < expectedArgc; ++i)
        {
            const bool useQuotes = static_cast<bool>(rng(2));
            const auto count = static_cast<size_t>(rng(64));
            const auto ch = static_cast<wchar_t>(rng('z' - 'a' + 1) + 'a');

            if (i != 0)
            {
                expectedArgv.push_back(L'\0');
                input.push_back(L' ');
            }

            if (useQuotes)
            {
                input.push_back(L'"');
            }

            expectedArgv.append(count, ch);
            input.append(count, ch);

            if (useQuotes)
            {
                input.push_back(L'"');
            }
        }

        int argc;
        wil::unique_hlocal_ptr<PWSTR[]> argv{ ::CommandLineToArgvW(input.c_str(), &argc) };
        VERIFY_ARE_EQUAL(expectedArgc, argc);
        VERIFY_IS_NOT_NULL(argv);

Review comment (suggestion):
I believe this comment (and change) isn't correct, because `CommandLineToArgvW` should work even if you pass it an empty string.

Code after:
        auto newFontSize = selfSettings->FontSize();
        VERIFY_ARE_NOT_EQUAL(oldFontSize, newFontSize);
    }

    // CascadiaSettings::_normalizeCommandLine abuses some aspects from CommandLineToArgvW
    // to simplify the implementation. It assumes that all arguments returned by
    // CommandLineToArgvW are returned back to back in memory as "arg1\0arg2\0arg3\0...".
    // This test ensures CommandLineToArgvW doesn't change just to be sure.
    void TerminalSettingsTests::CommandLineToArgvW()
    {
        pcg_engines::oneseq_dxsm_64_32 rng{ til::gen_random<uint64_t>() };

        const auto expectedArgc = static_cast<int>(rng(16) + 1);
        std::wstring expectedArgv;
        std::wstring input;

        // We generate up to 16 arguments. Each argument is up to 64 chars long, is quoted
        // (2 chars, only applies to the input) and separated by a whitespace (1 char).
        expectedArgv.reserve(expectedArgc * 65);
        input.reserve(expectedArgc * 67);

        for (int i = 0; i < expectedArgc; ++i)
        {
            const bool useQuotes = static_cast<bool>(rng(2));
            // We need to ensure there is at least one character
            const auto count = static_cast<size_t>(rng(64) + 1);
            const auto ch = static_cast<wchar_t>(rng('z' - 'a' + 1) + 'a');

            if (i != 0)
            {
                expectedArgv.push_back(L'\0');
                input.push_back(L' ');
            }

            if (useQuotes)
            {
                input.push_back(L'"');
            }

            expectedArgv.append(count, ch);
            input.append(count, ch);

            if (useQuotes)
            {
                input.push_back(L'"');
            }
        }
        Log::Comment(NoThrowString().Format(input.c_str()));

        int argc;
        wil::unique_hlocal_ptr<PWSTR[]> argv{ ::CommandLineToArgvW(input.c_str(), &argc) };