public void Contain() { foreach (var number in _array) { var value = JsonValue.Number(number); _jsonArray.Contains(value).Should().BeTrue(); } }
public void Contains() { JsonValue[] items = new JsonValue[] { new JsonPrimitive(true) }; JsonArray array = new JsonArray((IEnumerable <JsonValue>)items); Assert.True(array.Contains(items[0])); Assert.False(array.Contains(new JsonPrimitive(false))); }
public void TestContains() { var obj = new JsonArray("a", "b", "c"); Assert.True(obj.Contains("b")); obj.Remove(1); Assert.False(obj.Contains("b")); Assert.False(obj.Contains(JsonValue.Null)); }
public void Contains() { int v = 1; JsonArray a = new JsonArray { v }; Assert.IsFalse(a.Contains(2)); Assert.IsFalse(a.Contains(null)); Assert.IsTrue(a.Contains(v)); }
public void AddTest() { JsonArray target = new JsonArray(); JsonValue item = AnyInstance.AnyJsonValue1; Assert.IsFalse(target.Contains(item)); target.Add(item); Assert.AreEqual(1, target.Count); Assert.AreEqual(item, target[0]); Assert.IsTrue(target.Contains(item)); ExceptionTestHelper.ExpectException <ArgumentException>(() => target.Add(AnyInstance.DefaultJsonValue)); }
public static void NullHandling() { var jArray = new JsonArray() { "to be replaced" }; jArray[0] = null; Assert.Equal(1, jArray.Count); Assert.Null(jArray[0]); jArray.Add(null); Assert.Equal(2, jArray.Count); Assert.Null(jArray[1]); jArray.Add(null); Assert.Equal(3, jArray.Count); Assert.Null(jArray[2]); jArray.Insert(3, null); Assert.Equal(4, jArray.Count); Assert.Null(jArray[3]); jArray.Insert(4, null); Assert.Equal(5, jArray.Count); Assert.Null(jArray[4]); Assert.True(jArray.Contains(null)); Assert.Equal(0, jArray.IndexOf(null)); jArray.Remove(null); Assert.Equal(4, jArray.Count); }
public static void TestHandlingNulls() { var jsonArray = new JsonArray() { "to be replaced" }; jsonArray[0] = null; Assert.Equal(1, jsonArray.Count()); Assert.IsType <JsonNull>(jsonArray[0]); jsonArray.Add(null); Assert.Equal(2, jsonArray.Count()); Assert.IsType <JsonNull>(jsonArray[1]); jsonArray.Add(new JsonNull()); Assert.Equal(3, jsonArray.Count()); Assert.IsType <JsonNull>(jsonArray[2]); jsonArray.Insert(3, null); Assert.Equal(4, jsonArray.Count()); Assert.IsType <JsonNull>(jsonArray[3]); jsonArray.Insert(4, new JsonNull()); Assert.Equal(5, jsonArray.Count()); Assert.IsType <JsonNull>(jsonArray[4]); Assert.True(jsonArray.Contains(null)); Assert.Equal(0, jsonArray.IndexOf(null)); Assert.Equal(4, jsonArray.LastIndexOf(null)); jsonArray.Remove(null); Assert.Equal(4, jsonArray.Count()); }
public void ArrayContainsJsonElement() { var e = new JsonLiteral(1); var array = new JsonArray(e); Assert.IsTrue(array.Contains(e)); }
public void ArrayContainsJsonElementWithSameValue() { var e1 = new JsonLiteral(1); var e2 = new JsonLiteral(1); var array = new JsonArray(e1); Assert.IsTrue(array.Contains(e2)); }
public void JsonArrayItemsFunctionalTest() { int seed = 1; for (int i = 0; i < iterationCount / 10; i++) { seed++; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); bool retValue = true; // JsonArray.Contains(JsonValue) // JsonArray.IndexOf(JsonValue) JsonArray sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength); for (int j = 0; j < sourceJson.Count; j++) { if (!sourceJson.Contains(sourceJson[j])) { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Contains(JsonValue) failed to function properly."); retValue = false; } else { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Contains(JsonValue) passed test."); } if (sourceJson.IndexOf(sourceJson[j]) != j) { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.IndexOf(JsonValue) failed to function properly."); retValue = false; } else { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.IndexOf(JsonValue) passed test."); } } // JsonArray.Insert(int, JsonValue) JsonValue newItem = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); sourceJson.Insert(3, newItem); if (sourceJson[3] != newItem || sourceJson.Count != arrayLength + 1) { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Insert(int, JsonValue) failed to function properly."); retValue = false; } else { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Insert(int, JsonValue) passed test."); } if (!retValue) { Assert.Fail("[JsonArrayItemsFunctionalTest] Test failed!"); } } }
public static void Contains_IndexOf_Remove_Insert() { JsonNode node1 = 1; JsonNode node2 = 2; JsonNode node3 = 3; var jArray = new JsonArray(node1, node2, node3); Assert.Equal(3, jArray.Count); Assert.Equal(0, jArray.IndexOf(node1)); Assert.Equal(1, jArray.IndexOf(node2)); Assert.Equal(2, jArray.IndexOf(node3)); // Remove bool success = jArray.Remove(node2); Assert.True(success); Assert.Equal(2, jArray.Count); Assert.Equal(0, jArray.IndexOf(node1)); Assert.Equal(-1, jArray.IndexOf(node2)); Assert.Equal(1, jArray.IndexOf(node3)); Assert.False(jArray.Remove(node2)); // remove an already removed node. Assert.Equal(2, jArray.Count); // Contains Assert.True(jArray.Contains(node1)); Assert.False(jArray.Contains(node2)); Assert.True(jArray.Contains(node3)); // Insert jArray.Insert(1, node2); Assert.Equal(3, jArray.Count); Assert.Equal(0, jArray.IndexOf(node1)); Assert.Equal(1, jArray.IndexOf(node2)); Assert.Equal(2, jArray.IndexOf(node3)); }
public void RemoveAt() { object v1 = 1; object v2 = 2; object v3 = 3; JsonArray j = new JsonArray(); j.Add(v1); j.Add(v2); j.Add(v3); Assert.AreEqual(true, j.Contains(v1)); j.RemoveAt(0); Assert.AreEqual(false, j.Contains(v1)); Assert.AreEqual(true, j.Contains(v3)); j.RemoveAt(1); Assert.AreEqual(false, j.Contains(v3)); Assert.AreEqual(1, j.Count); }
/// <summary> /// 在Json文档中添加信息 /// </summary> public async static Task AddVideoInfo(string title, string cid, string sid = null) { JsonObject json = new JsonObject(); JsonObject jsonAppend = new JsonObject { { title, new JsonObject { { "cid", JsonValue.CreateStringValue(cid) }, { "sid", sid == null ? JsonValue.CreateNullValue() : JsonValue.CreateStringValue(sid) }, } } }; StorageFile file = await KnownFolders.VideosLibrary.CreateFileAsync("list.json", CreationCollisionOption.OpenIfExists); using (Stream file0 = await file.OpenStreamForReadAsync()) { StreamReader reader = new StreamReader(file0); string txt = await reader.ReadToEndAsync(); if (string.IsNullOrEmpty(txt)) { json.Add("data", new JsonArray()); json.Add("mid", UserHelper.Mid == null ? JsonValue.CreateNullValue() : JsonValue.CreateStringValue(UserHelper.Mid)); json.Add("count", JsonValue.CreateNumberValue(0)); } else { json = JsonObject.Parse(txt); } JsonArray array = json["data"].GetArray(); if (array.Contains(jsonAppend)) { return; } array.Add(jsonAppend); json["count"] = JsonValue.CreateNumberValue(json["count"].GetNumber() + 1); } using (Stream file1 = await file.OpenStreamForWriteAsync()) { using (StreamWriter writer = new StreamWriter(file1)) { await writer.WriteAsync(json.ToString()); } } }
public void ContainsTest() { JsonValue item1 = AnyInstance.AnyJsonValue1; JsonValue item2 = AnyInstance.AnyJsonValue2; JsonArray target = new JsonArray(item1); Assert.IsTrue(target.Contains(item1)); Assert.IsFalse(target.Contains(item2)); target.Add(item2); Assert.IsTrue(target.Contains(item1)); Assert.IsTrue(target.Contains(item2)); target.Remove(item1); Assert.IsFalse(target.Contains(item1)); Assert.IsTrue(target.Contains(item2)); }
private static void TestArray <T>(T value1, T value2, Func <JsonArray, T> getter, Func <T, JsonNode> nodeCtor) { var value1Casted = value1 as dynamic; var value2Casted = value2 as dynamic; JsonNode value1Node = nodeCtor(value1); JsonNode value2Node = nodeCtor(value2); var list = new List <T>() { value1 }; JsonArray jsonArray = new JsonArray(list as dynamic); Assert.Equal(1, jsonArray.Count); Assert.True(jsonArray.Contains(value1Node)); Assert.Equal(value1, getter(jsonArray)); jsonArray.Insert(0, value2 as dynamic); Assert.Equal(2, jsonArray.Count); Assert.True(jsonArray.Contains(value2Node)); Assert.Equal(value2, getter(jsonArray)); Assert.Equal(1, jsonArray.IndexOf(value1Node)); Assert.Equal(1, jsonArray.LastIndexOf(value1Node)); jsonArray.RemoveAt(0); Assert.False(jsonArray.Contains(value2Node)); jsonArray.Remove(value1Node); Assert.False(jsonArray.Contains(value1Node)); Assert.Equal(0, jsonArray.Count); jsonArray.Add(value2Casted); Assert.Equal(1, jsonArray.Count); Assert.True(jsonArray.Contains(value2Node)); Assert.Equal(value2, getter(jsonArray)); jsonArray[0] = value1Node; Assert.Equal(1, jsonArray.Count); Assert.True(jsonArray.Contains(value1Node)); Assert.Equal(value1, getter(jsonArray)); }