mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-04 03:16:11 +00:00
Cloning ApiInfo object should work when some fields are null (#1580)
* Adjust ApiInfo.Clone() to work even if some elements (eg ETag) are null * Remove c# 6 language feature and do it the old school way * Add a test for cloning ApiInfo when some fields are null * The 3 lists can never be null anyway so remove some un-needed statements * Add test for null RateLimit
This commit is contained in:
@@ -94,6 +94,92 @@ namespace Octokit.Tests.Http
|
||||
Assert.Equal(original.RateLimit.Reset, clone.RateLimit.Reset);
|
||||
Assert.NotSame(original.RateLimit.Reset, clone.RateLimit.Reset);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanCloneWithNullETag()
|
||||
{
|
||||
var original = new ApiInfo(
|
||||
new Dictionary<string, Uri>
|
||||
{
|
||||
{
|
||||
"next",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=4&per_page=5")
|
||||
},
|
||||
{
|
||||
"last",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=131&per_page=5")
|
||||
},
|
||||
{
|
||||
"first",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=1&per_page=5")
|
||||
},
|
||||
{
|
||||
"prev",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=2&per_page=5")
|
||||
}
|
||||
},
|
||||
new List<string>
|
||||
{
|
||||
"user"
|
||||
},
|
||||
new List<string>(),
|
||||
null,
|
||||
new RateLimit(100, 75, 1372700873)
|
||||
);
|
||||
|
||||
var clone = original.Clone();
|
||||
|
||||
Assert.NotNull(clone);
|
||||
Assert.Equal(4, clone.Links.Count);
|
||||
Assert.Equal(1, clone.OauthScopes.Count);
|
||||
Assert.Equal(0, clone.AcceptedOauthScopes.Count);
|
||||
Assert.Null(clone.Etag);
|
||||
Assert.Equal(100, clone.RateLimit.Limit);
|
||||
Assert.Equal(75, clone.RateLimit.Remaining);
|
||||
Assert.Equal(1372700873, clone.RateLimit.ResetAsUtcEpochSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanCloneWithNullRateLimit()
|
||||
{
|
||||
var original = new ApiInfo(
|
||||
new Dictionary<string, Uri>
|
||||
{
|
||||
{
|
||||
"next",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=4&per_page=5")
|
||||
},
|
||||
{
|
||||
"last",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=131&per_page=5")
|
||||
},
|
||||
{
|
||||
"first",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=1&per_page=5")
|
||||
},
|
||||
{
|
||||
"prev",
|
||||
new Uri("https://api.github.com/repos/rails/rails/issues?page=2&per_page=5")
|
||||
}
|
||||
},
|
||||
new List<string>
|
||||
{
|
||||
"user"
|
||||
},
|
||||
new List<string>(),
|
||||
"123abc",
|
||||
null
|
||||
);
|
||||
|
||||
var clone = original.Clone();
|
||||
|
||||
Assert.NotNull(clone);
|
||||
Assert.Equal(4, clone.Links.Count);
|
||||
Assert.Equal(1, clone.OauthScopes.Count);
|
||||
Assert.Equal(0, clone.AcceptedOauthScopes.Count);
|
||||
Assert.Equal("123abc", clone.Etag);
|
||||
Assert.Null(clone.RateLimit);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ namespace Octokit
|
||||
{
|
||||
Ensure.ArgumentNotNull(links, "links");
|
||||
Ensure.ArgumentNotNull(oauthScopes, "oauthScopes");
|
||||
Ensure.ArgumentNotNull(acceptedOauthScopes, "acceptedOauthScopes");
|
||||
|
||||
Links = new ReadOnlyDictionary<string, Uri>(links);
|
||||
OauthScopes = new ReadOnlyCollection<string>(oauthScopes);
|
||||
@@ -58,16 +59,11 @@ namespace Octokit
|
||||
/// <returns>A clone of <seealso cref="ApiInfo"/></returns>
|
||||
public ApiInfo Clone()
|
||||
{
|
||||
// Seem to have to do this to pass a whole bunch of tests (for example Octokit.Tests.Clients.EventsClientTests.DeserializesCommitCommentEventCorrectly)
|
||||
// I believe this has something to do with the Mocking framework.
|
||||
if (Links == null || OauthScopes == null || RateLimit == null || Etag == null)
|
||||
return null;
|
||||
|
||||
return new ApiInfo(Links.Clone(),
|
||||
OauthScopes.Clone(),
|
||||
AcceptedOauthScopes.Clone(),
|
||||
new string(Etag.ToCharArray()),
|
||||
RateLimit.Clone());
|
||||
OauthScopes.Clone(),
|
||||
AcceptedOauthScopes.Clone(),
|
||||
Etag != null ? new string(Etag.ToCharArray()) : null,
|
||||
RateLimit != null ? RateLimit.Clone() : null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user