|
1 | 1 | using Backtrace.Unity.Model; |
2 | 2 | using Backtrace.Unity.Model.JsonData; |
3 | 3 | using NUnit.Framework; |
| 4 | +using System; |
4 | 5 | using System.Collections; |
5 | 6 | using System.Collections.Generic; |
6 | 7 | using System.IO; |
7 | 8 | using System.Linq; |
| 9 | +using UnityEngine; |
8 | 10 | using UnityEngine.TestTools; |
9 | 11 | namespace Backtrace.Unity.Tests.Runtime |
10 | 12 | { |
11 | 13 | public class BacktraceAttributeTests |
12 | 14 | { |
| 15 | + [TestCase(null, null)] |
| 16 | + [TestCase("errorMessage", null)] |
| 17 | + [TestCase(null, "keyValue")] |
| 18 | + [TestCase("errorMessage", "keyValue")] |
| 19 | + public void TestAttributesGeneration_NullableValues_ValidAttributeObject(string errorMessage, string keyValue) |
| 20 | + { |
| 21 | + var report = string.IsNullOrEmpty(errorMessage) ? null : new BacktraceReport(errorMessage); |
| 22 | + var attributes = string.IsNullOrEmpty(keyValue) ? null : new Dictionary<string, string>() { { keyValue, keyValue } }; |
| 23 | + var backtraceAttributes = new BacktraceAttributes(report, attributes); |
| 24 | + Assert.IsNotNull(backtraceAttributes); |
| 25 | + } |
13 | 26 | [UnityTest] |
14 | | - public IEnumerator TestAttributesGeneration_CreateCorrectAttributes_WithDiffrentReportConfiguration() |
| 27 | + public IEnumerator TestAttributesGeneration_CreateCorrectMessageAttributes_WithDiffrentReportConfiguration() |
15 | 28 | { |
16 | | - var report = new BacktraceReport("message"); |
17 | | - Assert.DoesNotThrow(() => new BacktraceAttributes(report, null)); |
18 | | - var exception = new FileNotFoundException(); |
19 | | - var exceptionReport = new BacktraceReport(exception, new Dictionary<string, string>() { { "attr", "attr" } }); |
20 | | - var attributes = new BacktraceAttributes(exceptionReport, null); |
21 | | - Assert.IsTrue(attributes.Attributes.Count > 0); |
| 29 | + var clientAttributes = new Dictionary<string, string>() |
| 30 | + { |
| 31 | + ["foo"] = "foo", |
| 32 | + ["bar"] = "" |
| 33 | + }; |
| 34 | + |
| 35 | + var reportAttributes = new Dictionary<string, string>() |
| 36 | + { |
| 37 | + ["reportFoo"] = "foo", |
| 38 | + ["reportBar"] = "bar" |
| 39 | + }; |
| 40 | + |
| 41 | + var report = new BacktraceReport("message", reportAttributes); |
| 42 | + var attributes = new BacktraceAttributes(report, clientAttributes); |
| 43 | + foreach (var keyValuePair in clientAttributes) |
| 44 | + { |
| 45 | + Assert.AreEqual(attributes.Attributes[keyValuePair.Key].ToString(), clientAttributes[keyValuePair.Key]); |
| 46 | + } |
| 47 | + |
| 48 | + foreach (var keyValuePair in reportAttributes) |
| 49 | + { |
| 50 | + Assert.AreEqual(attributes.Attributes[keyValuePair.Key].ToString(), reportAttributes[keyValuePair.Key]); |
| 51 | + } |
| 52 | + |
| 53 | + Assert.AreEqual(report.Message, attributes.Attributes["error.message"]); |
22 | 54 | yield return null; |
23 | 55 | } |
24 | 56 |
|
25 | 57 | [UnityTest] |
26 | | - public IEnumerator TestCorrectDictionaryGeneration_CreateCorrectAttributesDictionary_WithDiffrentClientAttributes() |
| 58 | + public IEnumerator TestAttributesGeneration_CreateCorrectErrorAttributes_WithDiffrentReportConfiguration() |
27 | 59 | { |
28 | | - var exception = new FileNotFoundException(); |
29 | | - var reportAttributeKey = "report_attr"; |
30 | | - var reportAttributeValue = string.Format("{0}-value", reportAttributeKey); |
31 | | - var reportAttributes = new Dictionary<string, string>() { { reportAttributeKey, reportAttributeValue } }; |
32 | | - var exceptionReport = new BacktraceReport(exception, reportAttributes); |
33 | | - |
34 | | - string clientAttributeKey = "client_attr"; |
35 | | - string clientAttributeValue = string.Format("{0}-value", clientAttributeKey); |
36 | | - var clientAttributes = new Dictionary<string, string>() { { clientAttributeKey, clientAttributeValue } }; |
37 | | - |
38 | | - var testObject = new BacktraceAttributes(exceptionReport, clientAttributes); |
39 | | - Assert.IsTrue(testObject.Attributes.Keys.Any(n => n == clientAttributeKey)); |
40 | | - Assert.IsTrue(testObject.Attributes.Keys.Any(n => n == reportAttributeKey)); |
41 | | - Assert.IsTrue(testObject.Attributes[clientAttributeKey] as string == clientAttributeValue); |
42 | | - Assert.IsTrue(testObject.Attributes[reportAttributeKey] as string == reportAttributeValue); |
| 60 | + var clientAttributes = new Dictionary<string, string>() |
| 61 | + { |
| 62 | + ["foo"] = "foo", |
| 63 | + ["bar"] = "" |
| 64 | + }; |
| 65 | + |
| 66 | + var reportAttributes = new Dictionary<string, string>() |
| 67 | + { |
| 68 | + ["reportFoo"] = "foo", |
| 69 | + ["reportBar"] = "bar" |
| 70 | + }; |
| 71 | + |
| 72 | + var report = new BacktraceReport(new FileNotFoundException(), reportAttributes); |
| 73 | + var attributes = new BacktraceAttributes(report, clientAttributes); |
| 74 | + foreach (var keyValuePair in clientAttributes) |
| 75 | + { |
| 76 | + Assert.AreEqual(attributes.Attributes[keyValuePair.Key].ToString(), clientAttributes[keyValuePair.Key]); |
| 77 | + } |
| 78 | + |
| 79 | + foreach (var keyValuePair in reportAttributes) |
| 80 | + { |
| 81 | + Assert.AreEqual(attributes.Attributes[keyValuePair.Key].ToString(), reportAttributes[keyValuePair.Key]); |
| 82 | + } |
| 83 | + |
| 84 | + Assert.AreEqual(report.Message, attributes.Attributes["error.message"]); |
43 | 85 | yield return null; |
44 | 86 | } |
45 | 87 |
|
| 88 | + |
46 | 89 | [UnityTest] |
47 | 90 | public IEnumerator TestCorrectDictionaryGeneration_ReplaceAttributes_TheSameDictionaryAttributes() |
48 | 91 | { |
49 | 92 | var reportAttributeKey = "report_attr"; |
50 | 93 | var reportAttributeValue = string.Format("{0}-value", reportAttributeKey); |
51 | | - var clientAttributes = new Dictionary<string, string>() { { reportAttributeKey, |
52 | | - string.Format("{0}-client", reportAttributeValue) |
| 94 | + var reportAttributes = new Dictionary<string, string>() |
| 95 | + { |
| 96 | + { reportAttributeKey, reportAttributeValue} |
| 97 | + }; |
| 98 | + |
| 99 | + var clientAttributes = new Dictionary<string, string>() { |
| 100 | + {reportAttributeKey, string.Format("{0}-client", reportAttributeValue) |
53 | 101 | } }; |
54 | | - Assert.IsFalse(clientAttributes[reportAttributeKey] as string == reportAttributeValue); |
| 102 | + |
| 103 | + var report = new BacktraceReport("message", reportAttributes); |
| 104 | + var attributes = new BacktraceAttributes(report, clientAttributes); |
| 105 | + Assert.AreEqual(attributes.Attributes[reportAttributeKey], reportAttributes[reportAttributeKey]); |
| 106 | + |
| 107 | + yield return null; |
| 108 | + } |
| 109 | + |
| 110 | + [UnityTest] |
| 111 | + public IEnumerator TestLibraryAttributes_ValidApplicationAttributes_ShouldIncludeAllApplicationInformationInAttributes() |
| 112 | + { |
| 113 | + var attributes = new BacktraceAttributes(null, null); |
| 114 | + |
| 115 | + Assert.AreEqual(attributes.Attributes["application"], Application.productName); |
| 116 | + Assert.AreEqual(attributes.Attributes["application.version"], Application.version); |
| 117 | + Assert.AreEqual(attributes.Attributes["application.url"], Application.absoluteURL); |
| 118 | + Assert.AreEqual(attributes.Attributes["application.company.name"], Application.companyName); |
| 119 | + Assert.AreEqual(attributes.Attributes["application.data_path"], Application.dataPath); |
| 120 | + Assert.AreEqual(attributes.Attributes["application.id"], Application.identifier); |
| 121 | + Assert.AreEqual(attributes.Attributes["application.installer.name"], Application.installerName); |
| 122 | + Assert.AreEqual(attributes.Attributes["application.internet_reachability"], Application.internetReachability.ToString()); |
| 123 | + Assert.AreEqual(attributes.Attributes["application.editor"], Application.isEditor); |
| 124 | + Assert.AreEqual(attributes.Attributes["application.focused"], Application.isFocused); |
| 125 | + Assert.AreEqual(attributes.Attributes["application.mobile"], Application.isMobilePlatform); |
| 126 | + Assert.AreEqual(attributes.Attributes["application.playing"], Application.isPlaying); |
| 127 | + Assert.AreEqual(attributes.Attributes["application.background"], Application.runInBackground); |
| 128 | + Assert.AreEqual(attributes.Attributes["application.sandboxType"], Application.sandboxType.ToString()); |
| 129 | + Assert.AreEqual(attributes.Attributes["application.system.language"], Application.systemLanguage.ToString()); |
| 130 | + Assert.AreEqual(attributes.Attributes["application.unity.version"], Application.unityVersion); |
| 131 | + Assert.AreEqual(attributes.Attributes["application.temporary_cache"], Application.temporaryCachePath); |
| 132 | + Assert.AreEqual(attributes.Attributes["application.debug"], Debug.isDebugBuild); |
| 133 | + |
55 | 134 | yield return null; |
56 | 135 | } |
57 | 136 | } |
|
0 commit comments