Skip to content

Commit

Permalink
Use raw string literals to make json easier to read in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Aug 30, 2023
1 parent 6a5d89b commit cf340f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void AddExtensionAttributeTest()
var json = Encoding.UTF8.GetString(stream.ToArray());

Assert.Equal(
"{\"ext\":{\"something\":{\"field1\":1,\"field2\":2,\"field3\":3,\"field4\":6},\"food\":{\"field1\":4,\"field2\":5}}}",
"""{"ext":{"something":{"field1":1,"field2":2,"field3":3,"field4":6},"food":{"field1":4,"field2":5}}}""",
json);

stream.SetLength(0);
Expand All @@ -74,7 +74,7 @@ public void AddExtensionAttributeTest()
json = Encoding.UTF8.GetString(stream.ToArray());

Assert.Equal(
"{\"ext\":{\"something\":{\"field1\":1},\"food\":{\"field1\":1}}}",
"""{"ext":{"something":{"field1":1},"food":{"field1":1}}}""",
json);
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public void AddExtensionAttributeWithComplexTypeTest()
var json = Encoding.UTF8.GetString(stream.ToArray());

Assert.Equal(
"{\"ext\":{\"metadata\":{\"f\":{\"Id\":1234}}}}",
"""{"ext":{"metadata":{"f":{"Id":1234}}}}""",
json);
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public void AddExtensionAttributeDuplicatesTest()
var json = Encoding.UTF8.GetString(stream.ToArray());

Assert.Equal(
"{\"ext\":{\"something\":{\"field1\":1,\"field1\":2}}}",
"""{"ext":{"something":{"field1":1,"field1":2}}}""",
json);
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public void AddExtensionAttributeMixedFieldAndPropertyTest()
var json = Encoding.UTF8.GetString(stream.ToArray());

Assert.Equal(
"{\"ext\":{\"foo\":{\"foo_field1\":1,\"foo.field2\":2,\"foo_field3\":3},\"bar\":{\"bar_field1\":1,\"bar.field2\":2,\"bar_field3\":3}}}",
"""{"ext":{"foo":{"foo_field1":1,"foo.field2":2,"foo_field3":3},"bar":{"bar_field1":1,"bar.field2":2,"bar_field3":3}}}""",
json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void EmptyLogRecordJsonTest()
string json = GetLogRecordJson(1, (index, logRecord) => { });

Assert.Equal(
"{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n",
json);
}

Expand All @@ -42,8 +42,8 @@ public void MultipleEmptyLogRecordJsonTest()
string json = GetLogRecordJson(2, (index, logRecord) => { });

Assert.Equal(
"{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n"
+ "{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n"
+ """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n",
json);
}

Expand All @@ -63,7 +63,7 @@ public void LogRecordLogLevelJsonTest(LogLevel logLevel, string severityText, in
});

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"{severityText}\",\"severityNumber\":{severityNumber}}}}}\n",
$$$"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"{{{severityText}}}","severityNumber":{{{severityNumber}}}}}""" + "\n",
json);
}

Expand All @@ -79,7 +79,7 @@ public void LogRecordCategoryNameAndEventNameJsonTest(string categoryName, strin
});

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"{categoryName}.{eventName ?? "Name"}\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1}}}}\n",
$$$"""{"ver":"4.0","name":"{{{categoryName}}}.{{{eventName ?? "Name"}}}","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n",
json);
}

Expand All @@ -92,7 +92,7 @@ public void LogRecordEventIdJsonTest()
});

Assert.Equal(
"{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"eventId\":18,\"severityText\":\"Trace\",\"severityNumber\":1}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"eventId":18,"severityText":"Trace","severityNumber":1}}""" + "\n",
json);
}

Expand All @@ -105,7 +105,7 @@ public void LogRecordTimestampJsonTest()
});

Assert.Equal(
"{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2023-01-18T10:18:00Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2023-01-18T10:18:00Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n",
json);
}

Expand All @@ -119,7 +119,7 @@ public void LogRecordOriginalFormatBodyJsonTest()
});

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"body\":\"hello world\",\"formattedMessage\":\"goodbye world\"}}}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"body":"hello world","formattedMessage":"goodbye world"}}""" + "\n",
json);
}

Expand All @@ -133,7 +133,7 @@ public void LogRecordBodyJsonTest()
});

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"body\":\"hello world\",\"formattedMessage\":\"goodbye world\"}}}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"body":"hello world","formattedMessage":"goodbye world"}}""" + "\n",
json);
}

Expand All @@ -146,7 +146,7 @@ public void LogRecordFormattedMessageBodyJsonTest()
});

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"body\":\"goodbye world\",\"formattedMessage\":\"goodbye world\"}}}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"body":"goodbye world","formattedMessage":"goodbye world"}}""" + "\n",
json);
}

Expand All @@ -164,7 +164,7 @@ public void LogRecordResourceJsonTest()
string json = GetLogRecordJson(1, (index, logRecord) => { }, resource);

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"resourceKey1\":\"resourceValue1\",\"resourceKey2\":\"resourceValue2\"}}}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"resourceKey1":"resourceValue1","resourceKey2":"resourceValue2"}}""" + "\n",
json);
}

Expand All @@ -178,7 +178,7 @@ public void LogRecordScopesJsonTest()
string json = GetLogRecordJson(1, (index, logRecord) => { }, scopeProvider: scopeProvider);

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"scope1Key1\":\"scope1Value1\",\"scope1Key2\":\"scope1Value2\",\"scope2Key1\":\"scope2Value1\"}}}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"scope1Key1":"scope1Value1","scope1Key2":"scope1Value2","scope2Key1":"scope2Value1"}}""" + "\n",
json);
}

Expand All @@ -191,7 +191,7 @@ public void LogRecordStateValuesJsonTest()
});

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"stateKey1\":\"stateValue1\",\"stateKey2\":\"stateValue2\"}}}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"stateKey1":"stateValue1","stateKey2":"stateValue2"}}""" + "\n",
json);
}

Expand All @@ -209,7 +209,9 @@ public void LogRecordTraceContextJsonTest()
});

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1}},\"ext\":{{\"dt\":{{\"traceId\":\"{traceId}\",\"spanId\":\"{spanId}\",\"traceFlags\":1}}}}}}\n",
$$$$"""
{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1},"ext":{"dt":{"traceId":"{{{{traceId}}}}","spanId":"{{{{spanId}}}}","traceFlags":1}}}
""" + "\n",
json);
}

Expand All @@ -227,11 +229,13 @@ public void LogRecordExceptionJsonTest(bool includeStackTraceAsString)
includeStackTraceAsString: includeStackTraceAsString);

var stackJson = includeStackTraceAsString
? $",\"stack\":\"System.InvalidOperationException: Operation is not valid due to the current state of the object.\""
? ",\"stack\":\"System.InvalidOperationException: Operation is not valid due to the current state of the object.\""
: string.Empty;

Assert.Equal(
$"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1}},\"ext\":{{\"ex\":{{\"type\":\"System.InvalidOperationException\",\"msg\":\"Operation is not valid due to the current state of the object.\"{stackJson}}}}}}}\n",
$$$$"""
{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1},"ext":{"ex":{"type":"System.InvalidOperationException","msg":"Operation is not valid due to the current state of the object."{{{{stackJson}}}}}}}
""" + "\n",
json);
}

Expand All @@ -258,7 +262,7 @@ public void LogRecordExtensionsJsonTest()
scopeProvider);

Assert.Equal(
"{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1},\"ext\":{\"state\":{\"field\":\"stateValue1\"},\"resource\":{\"field\":\"resourceValue1\"},\"scope\":{\"field\":\"scopeValue1\"}}}\n",
"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1},"ext":{"state":{"field":"stateValue1"},"resource":{"field":"resourceValue1"},"scope":{"field":"scopeValue1"}}}""" + "\n",
json);
}

Expand Down

0 comments on commit cf340f1

Please sign in to comment.