Skip to content

Commit f11dc73

Browse files
committed
Unit tests upate with JSONUtility deserialization
1 parent 1a13ccc commit f11dc73

19 files changed

+184
-106
lines changed

Runtime/BacktraceDatabase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private void FlushRecord(BacktraceDatabaseRecord record)
292292
{
293293
return;
294294
}
295-
var backtraceData = record.BacktraceDataJson;
295+
var backtraceData = record.BacktraceDataJson();
296296
Delete(record);
297297
if (backtraceData == null)
298298
{
@@ -308,7 +308,7 @@ record = BacktraceDatabaseContext.FirstOrDefault();
308308

309309
private void SendData(BacktraceDatabaseRecord record)
310310
{
311-
var backtraceData = record != null ? record.BacktraceDataJson : null;
311+
var backtraceData = record != null ? record.BacktraceDataJson() : null;
312312
//check if report exists on hard drive
313313
// to avoid situation when someone manually remove data
314314
if (string.IsNullOrEmpty(backtraceData))
@@ -340,7 +340,6 @@ record = BacktraceDatabaseContext.FirstOrDefault();
340340
SendData(record);
341341
}));
342342
}
343-
344343
}
345344

346345
/// <summary>

Runtime/Json/BacktraceJObject.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@ public string ToJson()
4949

5050
foreach (var entry in Source)
5151
{
52+
// add to each property (expect first one)
53+
// ,\n for fomarring reasons.
54+
// without adding this condition at the beginning of the string builder,
55+
// we will generate invalid json with `,` character in the end of the json properties.
5256
if (stringBuilder.Length != 3)
5357
{
5458
stringBuilder.Append(",");
5559
stringBuilder.AppendLine();
5660
}
57-
var key = EscapeKey(entry.Key);
58-
stringBuilder.AppendFormat("\"{0}\":", key);
59-
60-
var value = entry.Value;
61-
var jsonValue = ConvertValue(value);
62-
stringBuilder.Append(jsonValue);
61+
// add key
62+
stringBuilder.AppendFormat("\"{0}\":", EscapeString(entry.Key));
63+
// add value to key
64+
stringBuilder.Append(ConvertValue(entry.Value));
6365
}
6466

6567
stringBuilder.AppendLine();
@@ -73,7 +75,7 @@ public string ToJson()
7375
/// </summary>
7476
/// <param name="value">string to escape</param>
7577
/// <returns>escaped string</returns>
76-
private string EscapeKey(string value)
78+
private string EscapeString(string value)
7779
{
7880
var output = new StringBuilder(value.Length);
7981
foreach (char c in value)
@@ -132,11 +134,11 @@ private string ConvertValue(object value)
132134
}
133135
else if (analysedType == typeof(double))
134136
{
135-
return Convert.ToDouble(value, CultureInfo.CurrentCulture).ToString();
137+
return Convert.ToDouble(value, CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture);
136138
}
137139
else if (analysedType == typeof(float))
138140
{
139-
return Convert.ToDouble(value, CultureInfo.CurrentCulture).ToString();
141+
return Convert.ToDouble(value, CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture);
140142
}
141143
else if (analysedType == typeof(int))
142144
{
@@ -148,9 +150,9 @@ private string ConvertValue(object value)
148150
}
149151
else if (analysedType == typeof(string))
150152
{
151-
return string.Format("\"{0}\"", EscapeKey(value as string));
153+
return string.Format("\"{0}\"", EscapeString(value as string));
152154
}
153-
else if (value is IEnumerable)
155+
else if (value is IEnumerable && !(value is IDictionary))
154156
{
155157
var collection = (value as IEnumerable);
156158
var builder = new StringBuilder();

Runtime/Model/BacktraceStackFrame.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,31 @@ public BacktraceStackFrame(StackFrame frame, bool generatedByException)
9898
InvalidFrame = true;
9999
return;
100100
}
101+
102+
var declaringType = method.DeclaringType;
103+
string assembly = "unknown";
104+
if(declaringType != null)
105+
{
106+
var assemblyName = declaringType.Assembly.GetName().Name;
107+
if (assemblyName!= null)
108+
{
109+
assembly = assemblyName;
110+
if(assemblyName == "Backtrace.Unity")
111+
{
112+
InvalidFrame = true;
113+
return;
114+
}
115+
}
116+
}
117+
118+
101119
SourceCodeFullPath = frame.GetFileName();
102120

103121
FunctionName = GetMethodName(method);
104122
Line = frame.GetFileLineNumber();
105123
Il = frame.GetILOffset();
106124
ILOffset = Il;
107-
Assembly = method.DeclaringType.Assembly.GetName().Name ?? "unknown";
125+
Assembly = assembly;
108126
Library = string.IsNullOrEmpty(SourceCodeFullPath) ? method.DeclaringType.ToString() : SourceCodeFullPath;
109127

110128
SourceCode = generatedByException

Runtime/Model/BacktraceStackTrace.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ private void SetStacktraceInformation(StackFrame[] frames, bool generatedByExcep
6363
int startingIndex = 0;
6464
foreach (var frame in frames)
6565
{
66-
string name;
67-
if (frame == null || frame.GetMethod() == null)
68-
name = string.Empty;
69-
else
70-
name = frame.GetMethod().DeclaringType.ToString() ?? string.Empty;
71-
72-
if (name.ToLower().Contains("backtrace.unity"))
73-
{
74-
continue;
75-
}
7666
var backtraceFrame = new BacktraceStackFrame(frame, generatedByException);
7767
if (backtraceFrame.InvalidFrame)
7868
{

Runtime/Model/Database/BacktraceDatabaseRecord.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,17 @@ public int Count
7373
/// </summary>
7474
internal IBacktraceDatabaseRecordWriter RecordWriter;
7575

76-
public string BacktraceDataJson
76+
public string BacktraceDataJson()
7777
{
78-
get
78+
if (Record != null)
7979
{
80-
if (Record != null)
81-
{
82-
return Record.ToJson();
83-
}
84-
if (string.IsNullOrEmpty(DiagnosticDataPath))
85-
{
86-
return null;
87-
}
88-
return File.ReadAllText(DiagnosticDataPath);
80+
return Record.ToJson();
81+
}
82+
if (string.IsNullOrEmpty(DiagnosticDataPath))
83+
{
84+
return null;
8985
}
86+
return File.ReadAllText(DiagnosticDataPath);
9087
}
9188

9289
/// <summary>
@@ -122,22 +119,19 @@ public string ToJson()
122119
public static BacktraceDatabaseRecord Deserialize(string json)
123120
{
124121
var rawRecord = JsonUtility.FromJson<BacktraceDatabaseRawRecord>(json);
125-
return new BacktraceDatabaseRecord()
126-
{
127-
Id = Guid.Parse(rawRecord.Id),
128-
RecordPath = rawRecord.recordName,
129-
DiagnosticDataPath = rawRecord.dataPath,
130-
MiniDumpPath = rawRecord.minidumpPath,
131-
Size = rawRecord.size,
132-
Hash = rawRecord.hash
133-
};
122+
return new BacktraceDatabaseRecord(rawRecord);
134123
}
135124
/// <summary>
136125
/// Constructor for serialization purpose
137126
/// </summary>
138-
internal BacktraceDatabaseRecord()
127+
private BacktraceDatabaseRecord(BacktraceDatabaseRawRecord rawRecord)
139128
{
140-
RecordPath = string.Format("{0}-record.json", Id);
129+
Id = Guid.Parse(rawRecord.Id);
130+
RecordPath = rawRecord.recordName;
131+
DiagnosticDataPath = rawRecord.dataPath;
132+
MiniDumpPath = rawRecord.minidumpPath;
133+
Size = rawRecord.size;
134+
Hash = rawRecord.hash;
141135
}
142136

143137
/// <summary>

Tests/Runtime/BacktraceAttributeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.IO;
77
using System.Linq;
88
using UnityEngine.TestTools;
9-
namespace Tests
9+
namespace Backtrace.Unity.Tests.Runtime
1010
{
1111
public class BacktraceAttributeTests
1212
{

Tests/Runtime/BacktraceClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using UnityEngine;
77
using UnityEngine.TestTools;
88

9-
namespace Tests
9+
namespace Backtrace.Unity.Tests.Runtime
1010
{
1111
public class BacktraceClientTests: BacktraceBaseTest
1212
{

Tests/Runtime/BacktraceCredentialsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using NUnit.Framework;
33
using System;
44

5-
namespace Tests
5+
namespace Backtrace.Unity.Tests.Runtime
66
{
77
internal class BacktraceCredentialsTests
88
{

Tests/Runtime/BacktraceDatabaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using UnityEngine;
66
using UnityEngine.TestTools;
77

8-
namespace Tests
8+
namespace Backtrace.Unity.Tests.Runtime
99
{
1010
public class BacktraceDatabaseTests: BacktraceBaseTest
1111
{

0 commit comments

Comments
 (0)