Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestContext.WriteLine throwing System.FormatException when json string is passed. #8

Closed
harshjain2 opened this issue Sep 8, 2016 · 2 comments
Assignees
Milestone

Comments

@harshjain2
Copy link
Contributor

harshjain2 commented Sep 8, 2016

Description

TestContext.WriteLine throwing System.FormatException when json string is passed.

Method Signature:
public abstract void WriteLine(string format, params object[] args);

Steps to reproduce

  • Create a unit test project, add MSTest.TestFramework dll and execute the below test method
[TestClass]
    public class UnitTest1
    {
        private TestContext testContextInstance;
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        [TestMethod]
        public void TestMethod1()
        {
            var msg = "{curly braces}";
            //Debug.WriteLine(msg);
            TestContext.WriteLine(msg);
        }
    }

Expected behavior

Test sholuld pass, user should be able to write the json in output.

Actual behavior

Test is failing with System.FormatException.

RCA :
This is happening because string.format is used for implementing this API and it expects format which placeholders {0}, {1}, etc. and corresponding arguments.

But when json string is passed, it just throws System.FormatException as string is present inside curly braces.

Workaround:
As a workaround, user has to add one extra curly brace for every curly brace present in string.

string str = "{{mystring}}";

Fix :
Do not use string.format api when count of arguments are null in implementation of API
public abstract void WriteLine(string format, params object[] args);

@AbhitejJohn
Copy link
Contributor

@harshjain2 : This is actually an API with a format and an args. I think the right fix here is to have another API like so
public abstract void WriteLine(string message)
and the user takes care of formatting in any way he/she chooses.

This is similar to how Console.WriteLine works today.
Reference: http://referencesource.microsoft.com/#mscorlib/system/console.cs,2141

@AbhitejJohn AbhitejJohn added this to the Dev15 RC milestone Sep 29, 2016
@harshjain2 harshjain2 self-assigned this Oct 20, 2016
@AbhitejJohn
Copy link
Contributor

👍

singhsarab pushed a commit to singhsarab/testfx that referenced this issue Apr 8, 2019
Support for runtime in build/test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants