Пример #1
0
    public void ArrayOperations()
    {
        JsonValue test = new JsonValue() { 1, 2, 3 };

        Assert.That(test.Count, Is.EqualTo(3));
        Assert.That(test.Contains(1), Is.True);
        Assert.That(test.Contains(4), Is.False);

        JsonValue[] values = new JsonValue[3];
        test.CopyTo(values, 0);
        for (int i = 0; i < 3; i++) {
            Assert.That((int)values[i], Is.EqualTo((int)test[i]));
        }

        test.Remove(2);
        Assert.That(test.Contains(1), Is.True);
        Assert.That(test.Contains(2), Is.False);
        Assert.That(test.Contains(3), Is.True);

        Assert.That(test.IsReadOnly, Is.False);

        Assert.That(test.IndexOf(1), Is.EqualTo(0));

        test.Insert(1, 2);
        Assert.That((int)test[1], Is.EqualTo(2));
        Assert.That(test.Count, Is.EqualTo(3));

        test.RemoveAt(1);
        Assert.That((int)test[1], Is.EqualTo(3));
        Assert.That(test.Count, Is.EqualTo(2));

        test.Clear();
        Assert.That(test.Count, Is.EqualTo(0));

        test.Add(5);
        Assert.That(test.Count, Is.EqualTo(1));
        Assert.That((int)test[0], Is.EqualTo(5));
    }
Пример #2
0
    public void KVPCollection()
    {
        JsonValue test = new JsonValue() {
            {"one", 1},
            {"two", 2},
            {"three", 3},
        };

        test.Remove(new KeyValuePair<string, JsonValue>("one", 1));
        Assert.That(test.Count, Is.EqualTo(2));
        Assert.Throws<KeyNotFoundException>(() => test["one"].ToString());

        KeyValuePair<string, JsonValue>[] dest = new KeyValuePair<string, JsonValue>[2];
        test.CopyTo(dest, 0);
        for (int i = 0; i < 2; ++i) {
            Assert.That(test[dest[i].Key].Equals(dest[i].Value));
        }

        Assert.That(test.Contains(new KeyValuePair<string, JsonValue>("two", 2)));

        test.Add(new KeyValuePair<string, JsonValue>("four", 4));

        Assert.That(test.ContainsKey("four"), Is.True);
        Assert.That((int)test["four"], Is.EqualTo(4));
    }