Skip to content

Commit e51313e

Browse files
committed
增加测试项目
1 parent db20854 commit e51313e

62 files changed

Lines changed: 982 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Linq.Expressions;
4+
using Machine.Specifications;
5+
using Smallcode.Net.Exceptions;
6+
7+
namespace Smallcode.Net.Test.Extensions
8+
{
9+
[Tags("字符", "扩展")]
10+
public class Html解析扩展规范
11+
{
12+
public static Html EmptyHtml;
13+
public static Html Html;
14+
public Establish Content = () =>
15+
{
16+
EmptyHtml = Html.Parse(string.Empty);
17+
Html = Html.Parse("<div class=\"class\" id=\"id\" name=\"name\"><a href=\"http://www.test.com\">click me</a></div><div class=\"class other\" id=\"other\"><a href=\"http://www.test1.com\">click me</a></div>");
18+
};
19+
20+
#region 单节点
21+
public It 无法解析时能抛出异常 = () => Catch.Exception(() => EmptyHtml.FindElement(By.CssSelector("div"))).ShouldBeOfType<NotFoundException>();
22+
23+
public It 必须能用TagName解析 = () => Html.FindElement(By.TagName("div")).ShouldMatch(ConditionId());
24+
25+
public It 必须能用id解析 = () => Html.FindElement(By.Id("id")).ShouldMatch(ConditionId());
26+
27+
public It 必须能用ClassName解析 = () => Html.FindElement(By.ClassName("class")).ShouldMatch(ConditionId());
28+
29+
public It 必须能用xPath解析 = () => Html.FindElement(By.XPath("//div")).ShouldMatch(ConditionId());
30+
31+
public It 必须能用LinkText解析 = () => Html.FindElement(By.LinkText("click me")).ShouldMatch(ConditionHref());
32+
33+
public It 必须能用PartialLinkText解析 = () => Html.FindElement(By.PartialLinkText("click")).ShouldMatch(ConditionHref());
34+
35+
public It 必须能用Css解析 = () =>
36+
{
37+
Html.FindElement(By.CssSelector(".class")).ShouldMatch(ConditionId());
38+
Html.FindElement(By.CssSelector("#id")).ShouldMatch(ConditionId());
39+
Html.FindElement(By.CssSelector("div")).ShouldMatch(ConditionId());
40+
Html.FindElement(By.CssSelector("div.class")).ShouldMatch(ConditionId());
41+
Html.FindElement(By.CssSelector("div#id")).ShouldMatch(ConditionId());
42+
Html.FindElement(By.CssSelector("div.class.other")).ShouldMatch(h => h.GetAttribute("id") == "other");//必须同时匹配两个class
43+
Html.FindElement(By.CssSelector("div#other.other")).ShouldMatch(h => h.GetAttribute("id") == "other");//必须同时匹配id和class
44+
Html.FindElement(By.CssSelector("div.class > a")).ShouldMatch(ConditionHref());//选择div.class的直接子标签a
45+
Html.FindElement(By.CssSelector("div[name=name].class")).ShouldMatch(ConditionId());//必须同时匹配属性name和类class
46+
};
47+
#endregion
48+
49+
#region 多节点
50+
public It 无法解析集合时返回空集合 = () => EmptyHtml.FindElements(By.CssSelector("div")).Count.ShouldEqual(0);
51+
52+
public It 必须能用TagName解析集合 = () => Html.FindElements(By.TagName("div")).ShouldMatch(ConditionIds());
53+
54+
public It 必须能用ClassName解析集合 = () => Html.FindElements(By.ClassName("class")).ShouldMatch(ConditionIds());
55+
56+
public It 必须能用xPath解析集合 = () => Html.FindElements(By.XPath("//div")).ShouldMatch(ConditionIds());
57+
58+
public It 必须能用LinkText解析集合 = () => Html.FindElements(By.LinkText("click me")).ShouldMatch(ConditionHrefs());
59+
60+
public It 必须能用PartialLinkText解析集合 = () => Html.FindElements(By.PartialLinkText("click")).ShouldMatch(ConditionHrefs());
61+
62+
public It 必须能用Css解析集合 = () =>
63+
{
64+
Html.FindElements(By.CssSelector("div")).ShouldMatch(ConditionIds());
65+
Html.FindElements(By.CssSelector("div.class")).ShouldMatch(ConditionIds());
66+
};
67+
#endregion
68+
69+
private static Expression<Func<Html, bool>> ConditionId()
70+
{
71+
return h => h["class"] == "class" && h["id"] == "id" && h["name"] == "name"; ;
72+
}
73+
74+
private static Expression<Func<ReadOnlyCollection<Html>, bool>> ConditionIds()
75+
{
76+
return list => list.Count == 2 && list[0].GetAttribute("id") == "id" && list[1].GetAttribute("id") == "other";
77+
}
78+
79+
private static Expression<Func<Html, bool>> ConditionHref()
80+
{
81+
return h => h.GetAttribute("href") == "http://www.test.com";
82+
}
83+
84+
private static Expression<Func<ReadOnlyCollection<Html>, bool>> ConditionHrefs()
85+
{
86+
return list => list.Count == 2 && list[0].GetAttribute("href") == "http://www.test.com" && list[1].GetAttribute("href") == "http://www.test1.com";
87+
}
88+
89+
}
90+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using Machine.Specifications;
3+
using Smallcode.Net.Extensions;
4+
5+
namespace Smallcode.Net.Test.Extensions
6+
{
7+
public class Ajax
8+
{
9+
public int Code { get; set; }
10+
public string Msg { get; set; }
11+
public string Style { get; set; }
12+
public Data Data { get; set; }
13+
}
14+
public class Data
15+
{
16+
public string Html { get; set; }
17+
public IList<Book> Bookes { get; set; }
18+
}
19+
public class Book
20+
{
21+
public string Name { get; set; }
22+
}
23+
[Tags("字符", "扩展")]
24+
public class Json解析扩展规范
25+
{
26+
public static string JsonString;
27+
public Establish Content = () =>
28+
{
29+
JsonString = @"{ code: '10000', msg: 'success', data:{html:'html',bookes:[{name:'book1'},{name:'book2'}]},""style"":""color: rgb(153, 153, 153);"" }";
30+
};
31+
public It 必须正确将字符串转换为jObject = () =>
32+
{
33+
var json = JsonString.ToJObject();
34+
json["code"].ShouldEqual(10000);
35+
json["msg"].ShouldEqual("success");
36+
json["style"].ShouldEqual("color: rgb(153, 153, 153);");
37+
json["data"]["bookes"][0]["name"].ShouldEqual("book1");
38+
};
39+
40+
public It 必须正确将字符串转换为自定义类型 = () =>
41+
{
42+
var test = JsonString.ToJson<Ajax>();
43+
test.Code.ShouldEqual(10000);
44+
test.Msg.ShouldEqual("success");
45+
test.Style.ShouldEqual("color: rgb(153, 153, 153);");
46+
test.Data.Html.ShouldEqual("html");
47+
test.Data.Bookes.Count.ShouldEqual(2);
48+
test.Data.Bookes[0].Name.ShouldEqual("book1");
49+
test.Data.Bookes[1].Name.ShouldEqual("book2");
50+
};
51+
52+
}
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using Machine.Specifications;
3+
using Smallcode.Net.Extensions;
4+
5+
namespace Smallcode.Net.Test.Extensions
6+
{
7+
[Tags("字符", "生成")]
8+
public class Random扩展规范
9+
{
10+
private enum TestEnum
11+
{
12+
Boy,
13+
Girl
14+
}
15+
public static Random Random;
16+
public Establish Content = () => Random = new Random();
17+
18+
public It 必须正确生成指定长度的随即小数字符串 =
19+
() =>
20+
Random.NextDoubleString(10)
21+
.ShouldMatch(
22+
s =>
23+
s.Substring(s.IndexOf(".", StringComparison.Ordinal) + 1).Length == 10 &&
24+
s.StartsWith("0."));
25+
26+
public It 必须正确生成指定长度的随机整数字符串 = () => Random.NextIntString(10).ShouldMatch(s => s.Length == 10 && s.IsNumberic());
27+
public It 必须正确生成指定长度的随机字母串 = () => Random.NextLetters(10).ShouldMatch(s => s.Length == 10);
28+
public It 必须正确生成指定长度的随机字符串 = () => Random.NextString(10).ShouldMatch(s => s.Length == 10);
29+
30+
public It 能获取随机的男性姓名 = () => Random.NextChineseBoyFullname().Length.ShouldEqual(3);
31+
public It 能获取随机的布尔值 = () => Random.NextBool().ShouldBeOfType<bool>();
32+
public It 能获取随机的枚举值 = () => Random.NextEnum<TestEnum>().ShouldBeOfType<TestEnum>();
33+
34+
public It 能获取随机的日期 = () => Random.NextDateTime().ShouldBeOfType<DateTime>();
35+
36+
public It 能获取随机范围内的日期 = () =>
37+
{
38+
var min = DateTime.Now.AddDays(-1);
39+
var max = DateTime.Now;
40+
Random.NextDateTime(min, max).ShouldMatch(date => date > min && date < max);
41+
};
42+
43+
public It 能获取随机生日 = () =>
44+
{
45+
var min = 20;
46+
var max = 30;
47+
var nextBirthday = Random.NextBirthday(min, max);
48+
nextBirthday.ShouldMatch(date => date > DateTime.Now.AddYears(-max) && date < DateTime.Now.AddYears(-min));
49+
};
50+
}
51+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Machine.Specifications;
2+
using Smallcode.Net.Extensions;
3+
4+
namespace Smallcode.Net.Test.Extensions
5+
{
6+
[Tags("Uri", "扩展"), Ignore("需要打开浏览器")]
7+
public class Uri扩展规范
8+
{
9+
public It 能在浏览器中打开链接 = () => "http://www.baidu.com/".OpenInBrower();
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Machine.Specifications;
3+
using Smallcode.Net.Extensions;
4+
5+
namespace Smallcode.Net.Test.Extensions
6+
{
7+
[Tags("字符", "扩展")]
8+
public class Uri生成扩展规范
9+
{
10+
public It 必须正确获取绝对地址 =
11+
() => "/b.php".MakeAbsoluteUri(HttpWwwBaiduComAPhp).ShouldEqual(HttpWwwBaiduComBPhp);
12+
13+
public It 必须正确由Uri获取绝对地址 =
14+
() => "/b.php".MakeAbsoluteUri(new Uri(HttpWwwBaiduComAPhp))
15+
.ShouldEqual(HttpWwwBaiduComBPhp);
16+
17+
public static string HttpWwwBaiduComAPhp = "http://www.baidu.com/a.php";
18+
public static string HttpWwwBaiduComBPhp = "http://www.baidu.com/b.php";
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Text;
2+
using Machine.Specifications;
3+
using Smallcode.Net.Extensions;
4+
5+
namespace Smallcode.Net.Test.Extensions
6+
{
7+
[Tags("字符", "扩展")]
8+
public class UrlEncoding扩展规范
9+
{
10+
public It 能获取Gb2312编码 =
11+
() => UrlEncoding.GB2312.ToEncoding().ShouldEqual(Encoding.GetEncoding("gb2312"));
12+
public It 能获取Utf8编码 =
13+
() => UrlEncoding.UTF8.ToEncoding().ShouldEqual(Encoding.UTF8);
14+
public It 能获取默认编码 =
15+
() => UrlEncoding.NONE.ToEncoding().ShouldEqual(Encoding.Default);
16+
17+
public It Gb2312编码和gbk编码是相同的 =
18+
() => (Encoding.GetEncoding("gb2312").Equals(Encoding.GetEncoding("gbk"))).ShouldBeTrue();
19+
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Machine.Specifications;
2+
using Smallcode.Net.Extensions;
3+
4+
namespace Smallcode.Net.Test.Extensions
5+
{
6+
[Tags("字符", "扩展")]
7+
public class 其他扩展规范
8+
{
9+
public It 必须正确截取字符串 = () =>
10+
{
11+
"you are the best one".CutString("are", "best").ShouldEqual("the");
12+
"you the best one".CutString("are", "best").ShouldBeEmpty();
13+
"you are the one".CutString("are", "best").ShouldBeEmpty();
14+
"you the one".CutString("are", "best").ShouldBeEmpty();
15+
16+
"you are the best one".CutString("you", "one", true, true).ShouldEqual("you are the best one");
17+
"you are the best one".CutString("you", "one", false, true).ShouldEqual("are the best one");
18+
"you are the best one".CutString("you", "one").ShouldEqual("are the best");
19+
};
20+
21+
public It 必须正确判断是否为数字 = () =>
22+
{
23+
"187".IsNumberic().ShouldBeTrue();
24+
"&#187;".IsNumberic().ShouldBeFalse();
25+
};
26+
27+
//public It 必须正确计算字符串长度 = () =>
28+
// {
29+
// "我".HalfOfByteCount().ShouldEqual(1);
30+
// "我我".HalfOfByteCount().ShouldEqual(2);
31+
// "aa".HalfOfByteCount().ShouldEqual(1);
32+
// "aaa".HalfOfByteCount().ShouldEqual(2);
33+
// "。".HalfOfByteCount().ShouldEqual(1);
34+
// "..".HalfOfByteCount().ShouldEqual(1);
35+
// };
36+
}
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Machine.Specifications;
2+
using Smallcode.Net.Extensions;
3+
4+
namespace Smallcode.Net.Test.Extensions
5+
{
6+
[Tags("×Ö·û", "À©Õ¹")]
7+
public class ¼ÓÃÜÀ©Õ¹¹æ·
8+
{
9+
public It ±ØÐëÕýÈ·¼ÓÃÜmd5 = () => { "ABCD1234".CalculateMd5Hash().ShouldEqual("361633153A464830A1FE85DEC5EFAB17"); };
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Machine.Specifications;
3+
using Smallcode.Net.Extensions;
4+
5+
namespace Smallcode.Net.Test.Extensions
6+
{
7+
[Tags("日期", "扩展")]
8+
public class 日期扩展规范
9+
{
10+
public It 能转换为10位Unix时间戳 = () => DateTime.Now.ToUnixTimeString().ShouldMatch(s => s.Length == 10);
11+
public It 能转换为大于10位Unix时间戳 = () => DateTime.Now.ToUnixTimeString(13).ShouldMatch(s => s.Length == 13);
12+
public It 能转换为小于10位Unix时间戳 = () => DateTime.Now.ToUnixTimeString(8).ShouldMatch(s => s.Length == 8);
13+
}
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Machine.Specifications;
2+
using Smallcode.Net.Extensions;
3+
4+
namespace Smallcode.Net.Test.Extensions
5+
{
6+
[Tags("字符", "扩展")]
7+
public class 替换扩展规范
8+
{
9+
public It 必须正确清除或替换空字符 = () =>
10+
{
11+
@" hello
12+
world ".ReplaceBlankSpaces().ShouldEqual("hello world");
13+
14+
@" hello
15+
world ".ReplaceBlankSpaces("").ShouldEqual("helloworld");
16+
};
17+
18+
public It 必须正确清除或替换换行 = () =>
19+
{
20+
@"\r\n hello \r\n world \r\n".RepalceLine().ShouldEqual(" hello world ");
21+
@"hello\r\nworld".RepalceLine("").ShouldEqual("helloworld");
22+
};
23+
24+
public It 必须正确清除或替换所有网址格式 = () =>
25+
{
26+
@"hello http://www.baidu.com world".RepalceUrl().ShouldEqual("hello world");
27+
@"hello http://www.baidu.com world".RepalceUrl("the").ShouldEqual("hello the world");
28+
};
29+
30+
public It 必须正确清除或替换a标签中的href属性 =
31+
() => { @"<a href=""http://www.baidu.com"">baidu</a>".ReplaceHref().ShouldEqual("<a >baidu</a>"); };
32+
33+
public It 必须正确清除或替换所有html标签 =
34+
() => { @"<a href=""http://www.baidu.com"">baidu</a>".ClearHtmlTags().ShouldEqual("baidu"); };
35+
36+
public It 必须正确清除转义字符 = () =>
37+
{
38+
var str = @"<a class=\""hi\""><\/a>";
39+
str.ClearEscape().ShouldEqual(@"<a class=""hi""></a>");
40+
};
41+
42+
public It 必须正确替换匹配项 =
43+
() => { "......aa11我我.....".RegexReplace(@"(\W)\1+", "$1").ShouldEqual(".aa11我我."); //替换掉连续相同的非字符(即符号之类的)
44+
};
45+
}
46+
}

0 commit comments

Comments
 (0)