Skip to content

Commit

Permalink
Merge branch 'refactoring'
Browse files Browse the repository at this point in the history
  • Loading branch information
Happi-cat committed Dec 6, 2015
2 parents 3fc4e26 + 961e49b commit 4551091
Show file tree
Hide file tree
Showing 52 changed files with 1,805 additions and 523 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Untech.SharePoint.Client.Converters.BuiltIn;
using Untech.SharePoint.Common.Converters;
using Untech.SharePoint.Common.Test.Converters;

namespace Untech.SharePoint.Client.Test.Converters.BuiltIn
{
[TestClass]
public class ContentTypeIdFieldConverterTest : BaseConverterTest
{
protected override IFieldConverter GetConverter()
{
return new ContentTypeIdConverter();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Untech.SharePoint.Client.Converters.BuiltIn;
using Untech.SharePoint.Common.Converters;
using Untech.SharePoint.Common.Models;
using Untech.SharePoint.Common.Test.Converters;

namespace Untech.SharePoint.Client.Test.Converters.BuiltIn
{
[TestClass]
public class LookupMultiFieldConverterTest : BaseConverterTest
{
[TestMethod]
public void CanSupportArray()
{
Given<ObjectReference[]>();
}

[TestMethod]
public void CanSupportTypesAssignableFromList()
{
Given<IEnumerable<ObjectReference>>();
Given<ICollection<ObjectReference>>();
Given<IReadOnlyCollection<ObjectReference>>();
Given<IList<ObjectReference>>();
Given<List<ObjectReference>>();
}

protected override IFieldConverter GetConverter()
{
return new LookupMultiFieldConverter();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Untech.SharePoint.Client.Converters.BuiltIn;
using Untech.SharePoint.Common.Converters;
using Untech.SharePoint.Common.Models;
using Untech.SharePoint.Common.Test.Converters;

namespace Untech.SharePoint.Client.Test.Converters.BuiltIn
{
[TestClass]
public class UrlFieldConverterTest : BaseConverterTest
{
[TestMethod]
public void CanConvertString()
{
Given<string>()
.CanConvertFromSp(null, null)
.CanConvertFromSp(new FieldUrlValue {Url = "http://google.com", Description = "Google It!"}, "http://google.com")
.CanConvertToSp(null, null)
.CanConvertToSp("http://google.com", new FieldUrlValue {Url = "http://google.com"}, new FieldUrlValueComparer())
.CanConvertToCaml(null, "")
.CanConvertToCaml("http://google.com", "http://google.com");
}

[TestMethod]
public void CanConvertUrlInfo()
{
Given<UrlInfo>()
.CanConvertFromSp(null, null)
.CanConvertFromSp(new FieldUrlValue { Url = "http://google.com", Description = "Google It!" }, new UrlInfo{ Url = "http://google.com", Description = "Google It!" }, new UrlInfoComparer())
.CanConvertToSp(null, null)
.CanConvertToSp(new UrlInfo { Url = "http://google.com", Description = "Google It!" }, new FieldUrlValue { Url = "http://google.com", Description = "Google It!" }, new FieldUrlValueComparer())
.CanConvertToCaml(null, "")
.CanConvertToCaml(new UrlInfo { Url = "http://google.com", Description = "Google It!" }, "http://google.com;#Google It!");
}

protected override IFieldConverter GetConverter()
{
return new UrlFieldConverter();
}

public class FieldUrlValueComparer : EqualityComparer<FieldUrlValue>
{
public override bool Equals(FieldUrlValue x, FieldUrlValue y)
{
return x.Url == y.Url && x.Description == y.Description;

}

public override int GetHashCode(FieldUrlValue obj)
{
if (obj == null) return 0;
var hash1 = (obj.Url ?? "").GetHashCode();
var hash2 = (obj.Description ?? "").GetHashCode();
return hash1 ^ hash2;
}
}

public class UrlInfoComparer : EqualityComparer<UrlInfo>
{
public override bool Equals(UrlInfo x, UrlInfo y)
{
return x.Url == y.Url && x.Description == y.Description;

}

public override int GetHashCode(UrlInfo obj)
{
if (obj == null) return 0;
var hash1 = (obj.Url ?? "").GetHashCode();
var hash2 = (obj.Description ?? "").GetHashCode();
return hash1 ^ hash2;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Untech.SharePoint.Client.Converters.BuiltIn;
using Untech.SharePoint.Common.Converters;
using Untech.SharePoint.Common.Models;
using Untech.SharePoint.Common.Test.Converters;

namespace Untech.SharePoint.Client.Test.Converters.BuiltIn
{
[TestClass]
public class UserMultiFieldConverterTest : BaseConverterTest
{
[TestMethod]
public void CanSupportArray()
{
Given<UserInfo[]>();
}

[TestMethod]
public void CanSupportTypesAssignableFromList()
{
Given<IEnumerable<UserInfo>>();
Given<ICollection<UserInfo>>();
Given<IReadOnlyCollection<UserInfo>>();
Given<IList<UserInfo>>();
Given<List<UserInfo>>();
}

protected override IFieldConverter GetConverter()
{
return new UserMultiFieldConverter();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Converters\BuiltIn\ContentTypeIdFieldConverterTest.cs" />
<Compile Include="Converters\BuiltIn\LookupMultiFieldConverterTest.cs" />
<Compile Include="Converters\BuiltIn\UrlFieldConverterTest.cs" />
<Compile Include="Converters\BuiltIn\UserMultiFieldConverterTest.cs" />
<Compile Include="Models\Bootstrap.cs" />
<Compile Include="Models\TestListItem.cs" />
<Compile Include="Models\WebDataContext.cs" />
Expand All @@ -108,6 +112,10 @@
<Project>{873D0ED4-C941-4320-AECF-5D7F09369A1E}</Project>
<Name>Untech.SharePoint.Client</Name>
</ProjectReference>
<ProjectReference Include="..\Untech.SharePoint.Common.Test\Untech.SharePoint.Common.Test.csproj">
<Project>{672adf77-0d40-4cb1-821e-54ded0e8aaa2}</Project>
<Name>Untech.SharePoint.Common.Test</Name>
</ProjectReference>
<ProjectReference Include="..\Untech.SharePoint.Common\Untech.SharePoint.Common.csproj">
<Project>{8836BB37-FECF-4503-933D-479A3D404900}</Project>
<Name>Untech.SharePoint.Common</Name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Untech.SharePoint.Common.CodeAnnotations;
using Untech.SharePoint.Common.CodeAnnotations;
using Untech.SharePoint.Common.Converters;
using Untech.SharePoint.Common.MetaModels;
using Untech.SharePoint.Common.Utils;
Expand All @@ -22,12 +21,12 @@ public object FromSpValue(object value)

public object ToSpValue(object value)
{
throw new NotImplementedException();
return (string) value;
}

public string ToCamlValue(object value)
{
return value != null ? value.ToString() : null;
return (string)value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Globalization;
using Microsoft.SharePoint.Client;
using Untech.SharePoint.Common.CodeAnnotations;
using Untech.SharePoint.Common.Converters;
Expand Down Expand Up @@ -74,8 +74,15 @@ public object ToSpValue(object value)
/// <returns>Caml value.</returns>
public string ToCamlValue(object value)
{
// TODO: .ToString() will return type
throw new NotImplementedException();
if (value == null)
{
return "";
}

var geoInfo = (GeoInfo)value;

return string.Format(CultureInfo.InvariantCulture, "Point ({0} {1} {2} {3})", geoInfo.Longitude, geoInfo.Latitude,
geoInfo.Altitude, geoInfo.Measure);
}
}
}
76 changes: 17 additions & 59 deletions Untech.SharePoint.Client/Converters/BuiltIn/LookupFieldConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using Untech.SharePoint.Common.CodeAnnotations;
using Untech.SharePoint.Common.Converters;
Expand All @@ -11,94 +9,54 @@
namespace Untech.SharePoint.Client.Converters.BuiltIn
{
[SpFieldConverter("Lookup")]
[SpFieldConverter("LookupMulti")]
[UsedImplicitly]
internal class LookupFieldConverter : IFieldConverter
{
private MetaField Field { get; set; }

/// <summary>
/// Initialzes current instance with the specified <see cref="MetaField"/>
/// </summary>
/// <param name="field"></param>
public void Initialize(MetaField field)
{
Guard.CheckNotNull("field", field);

if (field.MemberType != typeof(ObjectReference))
{
throw new ArgumentException("Only ObjectReference can be used as a member type.");
}
Field = field;
}

/// <summary>
/// Converts SP field value to <see cref="MetaField.MemberType"/>.
/// </summary>
/// <param name="value">SP value to convert.</param>
/// <returns>Member value.</returns>
public object FromSpValue(object value)
{
if (value == null || string.IsNullOrEmpty(Field.LookupList))
return null;

if (!Field.AllowMultipleValues)
{
return FieldValueToObjectReference((FieldLookupValue) value);
}

var fieldValues = (IEnumerable<FieldLookupValue>) value;
if (value == null) return null;

return fieldValues.Select(FieldValueToObjectReference).ToList();
return ConvertToObjRef((FieldLookupValue) value);
}


/// <summary>
/// Converts <see cref="MetaField.Member"/> value to SP field value.
/// </summary>
/// <param name="value">Member value to convert.</param>
/// <returns>SP field value.</returns>
public object ToSpValue(object value)
{
if (value == null)
return null;

if (!Field.AllowMultipleValues)
{
return ObjectReferenceToFieldValue((ObjectReference)value);
}

var references = (IEnumerable<ObjectReference>)value;
if (value == null) return null;

var fieldValues = new List<FieldLookupValue>();
fieldValues.AddRange(references.Select(ObjectReferenceToFieldValue));
var lookupValue = (ObjectReference)value;

return fieldValues;
return new FieldLookupValue{ LookupId = lookupValue.Id };
}



/// <summary>
/// Converts <see cref="MetaField.Member"/> value to SP Caml value.
/// </summary>
/// <param name="value"></param>
/// <returns>Caml value.</returns>
public string ToCamlValue(object value)
{
throw new NotImplementedException();
if (value == null) return null;

var lookupValue = (ObjectReference)value;

return string.Format("{0};#{1}", lookupValue.Id, lookupValue.Value);
}

private ObjectReference FieldValueToObjectReference(FieldLookupValue fieldValue)
private ObjectReference ConvertToObjRef(FieldLookupValue lookupValue)
{
return new ObjectReference
{
Id = fieldValue.LookupId,
Id = lookupValue.LookupId,
ListId = new Guid(Field.LookupList),
Value = fieldValue.LookupValue
};
}

private static FieldLookupValue ObjectReferenceToFieldValue(ObjectReference reference)
{
return new FieldLookupValue
{
LookupId = reference.Id
Value = lookupValue.LookupValue
};
}
}
Expand Down
Loading

0 comments on commit 4551091

Please sign in to comment.