-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
BuildExceptionConstraint.cs
53 lines (45 loc) · 1.42 KB
/
BuildExceptionConstraint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using NUnit.Framework.Constraints;
using Xamarin.Forms.Build.Tasks;
namespace Xamarin.Forms.Xaml.UnitTests
{
public class BuildExceptionConstraint : ExceptionTypeConstraint
{
readonly bool _haslineinfo;
readonly int _linenumber;
readonly int _lineposition;
readonly Func<string, bool> _messagePredicate;
BuildExceptionConstraint(bool haslineinfo) : base(typeof(BuildException)) => _haslineinfo = haslineinfo;
public override string DisplayName => "xamlparse";
public BuildExceptionConstraint() : this(false)
{
}
public BuildExceptionConstraint(int linenumber, int lineposition, Func<string, bool> messagePredicate = null) : this(true)
{
_linenumber = linenumber;
_lineposition = lineposition;
_messagePredicate = messagePredicate;
}
protected override bool Matches(object actual)
{
if (!base.Matches(actual))
return false;
var xmlInfo = ((BuildException)actual).XmlInfo;
if (!_haslineinfo)
return true;
if (xmlInfo == null || !xmlInfo.HasLineInfo())
return false;
if (_messagePredicate != null && !_messagePredicate(((BuildException)actual).Message))
return false;
return xmlInfo.LineNumber == _linenumber && xmlInfo.LinePosition == _lineposition;
}
public override string Description
{
get {
if (_haslineinfo)
return string.Format($"{base.Description} line {_linenumber}, position {_lineposition}");
return base.Description;
}
}
}
}