コード例 #1
0
        public void SetFieldsAddsKeyValuePairs()
        {
            dynamic person = new JsonObject();

            person.FirstName = "Mikael";
            person.LastName = "Waltersson";
            person.Age = 30;
            
            
            Assert.Equal(new[] { "FirstName", "LastName", "Age" }, person.GetDynamicMemberNames());
            Assert.Equal(
                new Dictionary<string, object>
                {
                    { "FirstName", "Mikael" },
                    { "LastName", "Waltersson" },
                    { "Age", 30 }
                },
                (IEnumerable<KeyValuePair<string, object>>)person);

            

            Assert.Equal("Mikael", person.FirstName);
            Assert.Equal("Waltersson", person.LastName);
            Assert.Equal(30, person.Age);            
        }
コード例 #2
0
        public void NonExistingFieldsHasUndefinedValue()
        {
            dynamic person = new JsonObject();

            person.FirstName = "Mikael";

            Assert.Equal(Undefined.Value, person.LastName);
        }
コード例 #3
0
ファイル: JsonValueTests.cs プロジェクト: shmao/corefx
 public void JsonObject_ToString_OrderingMaintained()
 {
     var obj = new JsonObject();
     obj["a"] = 1;
     obj["c"] = 3;
     obj["b"] = 2;
     Assert.Equal("{\"a\": 1, \"b\": 2, \"c\": 3}", obj.ToString());
 }
コード例 #4
0
        public void convert_large_number_to_json()
        {
            var obj = new JsonObject();
            obj["value"] = new JsonValue(long.MaxValue);

            var json = obj.ToString();

            obj = JsonObject.Parse(json);
            ((JsonValue)obj["value"]).Value.ShouldEqual(long.MaxValue);
        }
コード例 #5
0
        public void SettingUndefinedAsFieldValueRemovesField()
        {
            dynamic person = new JsonObject();

            person.FirstName = "Mikael";
            person.LastName = "Waltersson";

            person.FirstName = Undefined.Value;

            Assert.Equal(new[] { "LastName" }, person.GetPropertyNames());
        }
コード例 #6
0
        public void dynamic_case()
        {
            dynamic jobject = new JsonObject();
            jobject.name = "test";
            jobject.value = 1;
            jobject.list = new object[] { 5, "test" };

            string json = jobject.ToString();

            json.ShouldEqual("{\"name\":\"test\",\"value\":1,\"list\":[5,\"test\"]}");
        }
コード例 #7
0
        public void general_case()
        {
            var jobject = new JsonObject();
            jobject["name"] = new JsonValue("test");
            jobject["value"] = new JsonValue(1);
            jobject["list"] = new JsonArray { new JsonValue(5), new JsonValue("test") };

            var json = jobject.ToString();

            json.ShouldEqual("{\"name\":\"test\",\"value\":1,\"list\":[5,\"test\"]}");
        }
コード例 #8
0
        public void anonymous_case()
        {
            var obj = new
                {
                    name = "test",
                    value = 1,
                    list = new object[] { 5, "test" }
                };

            var json = new JsonObject(obj).ToString();

            json.ShouldEqual("{\"name\":\"test\",\"value\":1,\"list\":[5,\"test\"]}");
        }
コード例 #9
0
        public void opIndexer_int()
        {
            var expected = new JsonObject();

            var document = new JsonDocument
                               {
                                   expected
                               };

            var actual = document[0];

            Assert.Equal(expected, actual);
        }
コード例 #10
0
ファイル: JsonObject.Facts.cs プロジェクト: KarlDirck/cavity
        public void opIndexer_int()
        {
            var expected = new JsonPair("name", "value");

            var document = new JsonObject
                               {
                                   expected
                               };

            var actual = document[0];

            Assert.Equal(expected, actual);
        }
コード例 #11
0
        public void op_Add_JsonObject()
        {
            var expected = new JsonObject();

            var document = new JsonDocument
                               {
                                   expected
                               };

            var actual = document.First();

            Assert.Equal(expected, actual);
        }
コード例 #12
0
        public void op_GetEnumerator()
        {
            var expected = new JsonObject();

            IEnumerable document = new JsonDocument
                                       {
                                           expected
                                       };

            foreach (var actual in document)
            {
                Assert.Equal(expected, actual);
            }
        }
コード例 #13
0
ファイル: JsonObject.Facts.cs プロジェクト: KarlDirck/cavity
        public void opIndexer_string()
        {
            const string name = "name";
            var expected = new JsonPair(name, "value");

            var document = new JsonObject
                               {
                                   expected
                               };

            var actual = document[name];

            Assert.Equal(expected, actual);
        }
コード例 #14
0
ファイル: JsonObjectTests.cs プロジェクト: artikh/CouchDude
        public void ShouldCastToDynamicAsRawJson()
        {
            var jsonObject = new JsonObject("{\"_id\":\"8A7FD19B\",\"_rev\":\"1-42\",\"type\":\"simpleEntity\",\"name\":\"John\"}");
            dynamic dynamicObject = jsonObject;

            Assert.Null((string)dynamicObject.Id);
            Assert.Null((string)dynamicObject.Revision);
            Assert.Null((string)dynamicObject.Type);

            Assert.Equal("8A7FD19B",     (string)dynamicObject._id);
            Assert.Equal("1-42",         (string)dynamicObject._rev);
            Assert.Equal("simpleEntity", (string)dynamicObject.type);
            Assert.Equal("John",         (string)dynamicObject.name);
        }
コード例 #15
0
ファイル: JsonObjectTests.cs プロジェクト: Corillian/corefx
        public void Ctor_IEnumerable()
        {
            // Workaround xunit/xunit#987: InvalidOperationException thrown if this is in MemberData
            KeyValuePair<string, JsonValue>[] items = new KeyValuePair<string, JsonValue>[] { new KeyValuePair<string, JsonValue>("key", new JsonPrimitive(true)) };
            JsonObject obj = new JsonObject((IEnumerable<KeyValuePair<string, JsonValue>>)items);

            Assert.Equal(items.Length, obj.Count);
            for (int i = 0; i < items.Length; i++)
            {
                Assert.Equal(items[i].Value.ToString(), obj[items[i].Key].ToString());

                JsonValue value;
                Assert.True(obj.TryGetValue(items[i].Key, out value));
                Assert.Equal(items[i].Value.ToString(), value.ToString());
            }
        }
コード例 #16
0
        public void PropertyOrderIsSetByFirstSetFieldInvocation()
        {
            dynamic person = new JsonObject();

            person.LastName = "";
            person.FirstName = "Mikael";
            person.LastName = "Waltersson";

            Assert.Equal(
                new Dictionary<string, object>
                {
                    { "LastName", "Waltersson" },
                    { "FirstName", "Mikael" }                 
                },
                (IEnumerable<KeyValuePair<string, object>>)person);
        }
コード例 #17
0
ファイル: JsonObjectTests.cs プロジェクト: Corillian/corefx
 public void Item_NoSuchKey_ThrowsKeyNotFoundException()
 {
     JsonObject obj = new JsonObject();
     Assert.Throws<KeyNotFoundException>(() => obj["no-such-key"]);
 }
コード例 #18
0
        public async Task DeserializesStarredEventCorrectly()
        {
            var jsonObj = new JsonObject
            {
                { "type", "WatchEvent" },
                {
                    "payload", new
                    {
                        action = "started"
                    }
                }
            };

            var client = GetTestingEventsClient(jsonObj);
            var activities = await client.GetAll();
            Assert.Equal(1, activities.Count);

            var payload = activities.FirstOrDefault().Payload as StarredEventPayload;
            Assert.Equal("started", payload.Action);
        }
コード例 #19
0
        private EventsClient GetTestingEventsClient(JsonObject response)
        {
            var responseString = response.ToString();
            var httpClientMock = Substitute.For<IHttpClient>();
            httpClientMock.Send(Arg.Is((IRequest r) => r.Endpoint.ToString().Contains("events")), Arg.Any<CancellationToken>()).Returns(Task.FromResult(
                new Response(HttpStatusCode.Accepted, responseString, new Dictionary<string, string>(), "application/json") as IResponse));

            return new EventsClient(new ApiConnection(new Connection(new ProductHeaderValue("mock"), httpClientMock)));
        }
コード例 #20
0
        public async Task DeserializesPullRequestCommentEventCorrectly()
        {
            var jsonObj = new JsonObject
            {
                { "type", "PullRequestReviewCommentEvent" },
                {
                    "payload", new
                    {
                        action = "assigned",
                        pull_request = new
                        {
                            title = "PR Title"
                        },
                        comment = new
                        {
                            id = 1337
                        }
                    }
                }
            };

            var client = GetTestingEventsClient(jsonObj);
            var activities = await client.GetAll();
            Assert.Equal(1, activities.Count);

            var payload = activities.FirstOrDefault().Payload as PullRequestCommentPayload;
            Assert.Equal("assigned", payload.Action);
            Assert.Equal("PR Title", payload.PullRequest.Title);
            Assert.Equal(1337, payload.Comment.Id);
        }
コード例 #21
0
        public async Task DeserializesPushEventCorrectly()
        {
            var jsonObj = new JsonObject
            {
                { "type", "PushEvent" },
                {
                    "payload", new
                    {
                        head = "head",
                        @ref = "ref",
                        size = 1337,
                        commits = new []
                        {
                            new
                            {
                                message = "message"
                            }
                        }
                    }
                }
            };

            var client = GetTestingEventsClient(jsonObj);
            var activities = await client.GetAll();
            Assert.Equal(1, activities.Count);

            var payload = activities.FirstOrDefault().Payload as PushEventPayload;
            Assert.Equal("head", payload.Head);
            Assert.Equal("ref", payload.Ref);
            Assert.Equal(1337, payload.Size);
            Assert.NotNull(payload.Commits);
            Assert.Equal(1, payload.Commits.Count);
            Assert.Equal("message", payload.Commits.FirstOrDefault().Message);
        }
コード例 #22
0
        public async Task DeserializesIssueCommentEventCorrectly()
        {
            var jsonObj = new JsonObject
            {
                { "type", "IssueCommentEvent" },
                {
                    "payload", new
                    {
                        action = "created",
                        issue = new
                        {
                            number = 1337
                        },
                        comment = new
                        {
                            id = 1337
                        }
                    }
                }
            };

            var client = GetTestingEventsClient(jsonObj);
            var activities = await client.GetAll();
            Assert.Equal(1, activities.Count);

            var payload = activities.FirstOrDefault().Payload as IssueCommentPayload;
            Assert.Equal("created", payload.Action);
            Assert.Equal(1337, payload.Comment.Id);
            Assert.Equal(1337, payload.Issue.Number);
        }
コード例 #23
0
        public async Task DeserializesIssueEventCorrectly()
        {
            var jsonObj = new JsonObject
            {
                { "type", "IssuesEvent" },
                {
                    "payload", new
                    {
                        action = "created",
                        issue = new
                        {
                            number = 1337,
                            assignee = new
                            {
                                id = 1337
                            },
                            labels = new[]
                            {
                                new { name = "bug"}
                            }
                        }
                    }
                }
            };

            var client = GetTestingEventsClient(jsonObj);
            var activities = await client.GetAll();
            Assert.Equal(1, activities.Count);

            var payload = activities.FirstOrDefault().Payload as IssueEventPayload;
            Assert.Equal("created", payload.Action);
            Assert.Equal(1337, payload.Issue.Number);
            Assert.Equal(1337, payload.Issue.Assignee.Id);
            Assert.Equal("bug", payload.Issue.Labels.First().Name);
        }
コード例 #24
0
        public async Task DeserializesForkEventCorrectly()
        {
            var jsonObj = new JsonObject
            {
                { "type", "ForkEvent" },
                {
                    "payload", new
                    {
                        forkee = new
                        {
                            id = 1337
                        }
                    }
                }
            };

            var client = GetTestingEventsClient(jsonObj);
            var activities = await client.GetAll();
            Assert.Equal(1, activities.Count);

            var payload = activities.FirstOrDefault().Payload as ForkEventPayload;
            Assert.Equal(1337, payload.Forkee.Id);
        }
コード例 #25
0
ファイル: JsonObjectTests.cs プロジェクト: Corillian/corefx
 public void Save_NullStream_ThrowsArgumentNullException()
 {
     JsonObject obj = new JsonObject();
     Assert.Throws<ArgumentNullException>("stream", () => obj.Save((Stream)null));
     Assert.Throws<ArgumentNullException>("textWriter", () => obj.Save((TextWriter)null));
 }
コード例 #26
0
ファイル: JsonObjectTests.cs プロジェクト: Corillian/corefx
        public void GetEnumerator_NonGenericIEnumerable()
        {
            KeyValuePair<string, JsonValue>[] items = new KeyValuePair<string, JsonValue>[] { new KeyValuePair<string, JsonValue>("key", new JsonPrimitive(true)) };
            JsonObject obj = new JsonObject(items);

            IEnumerator enumerator = ((IEnumerable)obj).GetEnumerator();
            for (int i = 0; i < 2; i++)
            {
                int counter = 0;
                while (enumerator.MoveNext())
                {
                    KeyValuePair<string, JsonValue> current = (KeyValuePair<string, JsonValue>)enumerator.Current;
                    Assert.Equal(items[counter].Key, current.Key);
                    Assert.Equal(items[counter].Value.ToString(), current.Value.ToString());
                    counter++;
                }
                Assert.Equal(obj.Count, counter);

                enumerator.Reset();
            }
        }
コード例 #27
0
ファイル: JsonObjectTests.cs プロジェクト: Corillian/corefx
        public void Item_Set_Get()
        {
            JsonObject obj = new JsonObject();

            string key = "key";
            JsonValue value = new JsonPrimitive(true);
            obj[key] = value;

            Assert.Equal(1, obj.Count);
            Assert.Same(value, obj[key]);
        }
コード例 #28
0
ファイル: JsonObjectTests.cs プロジェクト: Corillian/corefx
 public void IsReadOnly_ReturnsFalse()
 {
     ICollection<KeyValuePair<string, JsonValue>> iCollection = new JsonObject();
     Assert.False(iCollection.IsReadOnly);
 }
コード例 #29
0
ファイル: JsonObjectTests.cs プロジェクト: Corillian/corefx
 public void Ctor_NullArray_Works()
 {
     JsonObject obj = new JsonObject(null);
     Assert.Equal(0, obj.Count);
 }
コード例 #30
0
        public async Task DeserializesPayloadToCorrectType()
        {
            _activityTypes.ToList().ForEach(async kvp =>
            {
                var jsonObj = new JsonObject {{ "type", kvp.Key }, {"payload", new
                {
                    repository = new
                    {
                        id = 1337
                    },
                    sender = new
                    {
                       id = 1337
                    }
                }}};

                var client = GetTestingEventsClient(jsonObj);

                var activities = await client.GetAll();
                Assert.Equal(1, activities.Count);
                var activity = activities.FirstOrDefault();
                Assert.Equal(kvp.Value, activity.Payload.GetType());
                Assert.NotNull(activity.Payload.Repository);
                Assert.NotNull(activity.Payload.Sender);
                Assert.Equal(1337, activity.Payload.Repository.Id);
                Assert.Equal(1337, activity.Payload.Sender.Id);
            });
        }