Skip to content

Commit f996edf

Browse files
test(audience-sdk): centralise JsonReaderTests scenario fixtures and malformed inputs
JsonReaderTests had inline scenario fixtures across the deserialise tests: - "small" / "big" (IntAndLong) - "t" / "f" / "n" (BoolAndNull) - "arr" / "two" (Array) - "abc" (RoundTripViaSerializer anonymousId) - "76561198012345" (RoundTripViaSerializer userId; same string also used in ImmutableAudienceTests' Steam Identify test) - "{not valid}" / "{\"a\":}" / "{\"a\":\"unterminated" (three malformed inputs in MalformedThrows) Adds a file-local const block at the top of the fixture grouping the keys, the array-element value, the anonymousId placeholder, and the three malformed inputs. Each test now interpolates the encoded JSON form from its key consts so a key rename touches one place. The "76561198012345" duplication is removed by referencing TestFixtures.SteamId64 (already centralised). Per the user's "everything random goes in a constant" stance. Follow-up to SDK-272 (centralisation of duplicated literals).
1 parent 8d6e312 commit f996edf

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

src/Packages/Audience/Tests/Runtime/Utility/JsonReaderTests.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ namespace Immutable.Audience.Tests
66
[TestFixture]
77
public class JsonReaderTests
88
{
9+
// Per-scenario fixture keys / values used by the deserialise tests.
10+
private const string IntFixtureKey = "small";
11+
private const string LongFixtureKey = "big";
12+
private const string BoolTrueKey = "t";
13+
private const string BoolFalseKey = "f";
14+
private const string NullKey = "n";
15+
private const string ArrayKey = "arr";
16+
private const string StringElementValue = "two";
17+
18+
// Anonymous ID placeholder for RoundTripViaSerializer.
19+
private const string AnonymousIdFixture = "abc";
20+
21+
// MalformedThrows test: three deliberately invalid JSON inputs that
22+
// exercise distinct parser failure modes.
23+
private const string MalformedNotValid = "{not valid}";
24+
private const string MalformedEmptyValue = "{\"a\":}";
25+
private const string MalformedUnterminatedString = "{\"a\":\"unterminated";
26+
927
[Test]
1028
public void EmptyObject()
1129
{
@@ -30,18 +48,18 @@ public void StringWithEscapes()
3048
[Test]
3149
public void IntAndLong()
3250
{
33-
var result = JsonReader.DeserializeObject("{\"small\":42,\"big\":12345678901234}");
34-
Assert.AreEqual(42, result["small"]);
35-
Assert.AreEqual(12345678901234L, result["big"]);
51+
var result = JsonReader.DeserializeObject($"{{\"{IntFixtureKey}\":42,\"{LongFixtureKey}\":12345678901234}}");
52+
Assert.AreEqual(42, result[IntFixtureKey]);
53+
Assert.AreEqual(12345678901234L, result[LongFixtureKey]);
3654
}
3755

3856
[Test]
3957
public void BoolAndNull()
4058
{
41-
var result = JsonReader.DeserializeObject("{\"t\":true,\"f\":false,\"n\":null}");
42-
Assert.AreEqual(true, result["t"]);
43-
Assert.AreEqual(false, result["f"]);
44-
Assert.IsNull(result["n"]);
59+
var result = JsonReader.DeserializeObject($"{{\"{BoolTrueKey}\":true,\"{BoolFalseKey}\":false,\"{NullKey}\":null}}");
60+
Assert.AreEqual(true, result[BoolTrueKey]);
61+
Assert.AreEqual(false, result[BoolFalseKey]);
62+
Assert.IsNull(result[NullKey]);
4563
}
4664

4765
[Test]
@@ -55,11 +73,11 @@ public void NestedObject()
5573
[Test]
5674
public void Array()
5775
{
58-
var result = JsonReader.DeserializeObject("{\"arr\":[1,\"two\",true,null]}");
59-
var arr = (List<object>)result["arr"];
76+
var result = JsonReader.DeserializeObject($"{{\"{ArrayKey}\":[1,\"{StringElementValue}\",true,null]}}");
77+
var arr = (List<object>)result[ArrayKey];
6078
Assert.AreEqual(4, arr.Count);
6179
Assert.AreEqual(1, arr[0]);
62-
Assert.AreEqual("two", arr[1]);
80+
Assert.AreEqual(StringElementValue, arr[1]);
6381
Assert.AreEqual(true, arr[2]);
6482
Assert.IsNull(arr[3]);
6583
}
@@ -76,17 +94,17 @@ public void RoundTripViaSerializer()
7694
[EventPropertyKeys.Status] = ProgressionStatus.Complete.ToLowercaseString(),
7795
[EventPropertyKeys.Score] = 1500
7896
},
79-
[MessageFields.AnonymousId] = "abc",
80-
[MessageFields.UserId] = "76561198012345"
97+
[MessageFields.AnonymousId] = AnonymousIdFixture,
98+
[MessageFields.UserId] = TestFixtures.SteamId64
8199
};
82100

83101
var serialized = Json.Serialize(original);
84102
var parsed = JsonReader.DeserializeObject(serialized);
85103

86104
Assert.AreEqual(MessageTypes.Track, parsed[MessageFields.Type]);
87105
Assert.AreEqual(EventNames.Progression, parsed[MessageFields.EventName]);
88-
Assert.AreEqual("abc", parsed[MessageFields.AnonymousId]);
89-
Assert.AreEqual("76561198012345", parsed[MessageFields.UserId]);
106+
Assert.AreEqual(AnonymousIdFixture, parsed[MessageFields.AnonymousId]);
107+
Assert.AreEqual(TestFixtures.SteamId64, parsed[MessageFields.UserId]);
90108
var props = (Dictionary<string, object>)parsed[MessageFields.Properties];
91109
Assert.AreEqual(ProgressionStatus.Complete.ToLowercaseString(), props[EventPropertyKeys.Status]);
92110
Assert.AreEqual(1500, props[EventPropertyKeys.Score]);
@@ -95,9 +113,9 @@ public void RoundTripViaSerializer()
95113
[Test]
96114
public void MalformedThrows()
97115
{
98-
Assert.Throws<System.FormatException>(() => JsonReader.DeserializeObject("{not valid}"));
99-
Assert.Throws<System.FormatException>(() => JsonReader.DeserializeObject("{\"a\":}"));
100-
Assert.Throws<System.FormatException>(() => JsonReader.DeserializeObject("{\"a\":\"unterminated"));
116+
Assert.Throws<System.FormatException>(() => JsonReader.DeserializeObject(MalformedNotValid));
117+
Assert.Throws<System.FormatException>(() => JsonReader.DeserializeObject(MalformedEmptyValue));
118+
Assert.Throws<System.FormatException>(() => JsonReader.DeserializeObject(MalformedUnterminatedString));
101119
}
102120
}
103121
}

0 commit comments

Comments
 (0)