FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SignNow.NET/SignNow.Net.Test/AssertExtensions.cs at develop · Yagg/SignNow.NET · GitHub
Yagg
/
SignNow.NET
Public
forked from
signnow/SignNow.NET
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SignNow.NET
/
SignNow.Net.Test
/
AssertExtensions.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
90 lines (81 loc) · 3.7 KB
Breadcrumbs
SignNow.NET
/
SignNow.Net.Test
/
AssertExtensions.cs
Copy path
File metadata and controls
90 lines (81 loc) · 3.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using
System
.
IO
;
using
System
.
Text
;
using
Microsoft
.
VisualStudio
.
TestTools
.
UnitTesting
;
using
Newtonsoft
.
Json
;
namespace
UnitTests
{
/// <summary>
/// Extends Assertions for more comfortable Unit tests assertions
/// </summary>
public
static
class
AssertExtensions
{
/// <summary>
/// Tests whether the specified values are equal and throws an exception if the two values are not equal.
/// </summary>
/// <param name="assert"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
public
static
void
JsonEqual
(
this
Assert
assert
,
object
expected
,
string
actual
)
{
var
expectedJson
=
JsonConvert
.
SerializeObject
(
expected
,
Formatting
.
Indented
)
;
Assert
.
AreEqual
(
expectedJson
,
PrettifyJson
(
actual
)
)
;
}
/// <summary>
/// Tests whether the specified values are equal and throws an exception if the two values are not equal.
/// </summary>
/// <param name="assert"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
public
static
void
JsonEqual
(
this
Assert
assert
,
string
expected
,
object
actual
)
{
var
actualJson
=
JsonConvert
.
SerializeObject
(
actual
,
Formatting
.
Indented
)
;
Assert
.
AreEqual
(
PrettifyJson
(
expected
)
,
actualJson
)
;
}
private
static
string
PrettifyJson
(
string
json
)
{
using
var
stringReader
=
new
StringReader
(
json
)
;
using
var
stringWriter
=
new
StringWriter
(
)
;
using
var
jsonReader
=
new
JsonTextReader
(
stringReader
)
;
using
var
jsonWriter
=
new
JsonTextWriter
(
stringWriter
)
;
jsonWriter
.
Formatting
=
Formatting
.
Indented
;
jsonWriter
.
WriteToken
(
jsonReader
)
;
return
stringWriter
.
ToString
(
)
;
}
/// <summary>
/// Tests whether the specified Stream is a PDF file and throws an exception
/// if it is not a PDF.
/// </summary>
/// <param name="assert"></param>
/// <param name="file">The Stream the test expects to be a PDF file.</param>
/// <param name="message">
/// The message to include in the exception when <paramref name="file"/>
/// is not a PDF file. The message is shown in test results.
/// </param>
public
static
void
StreamIsPdf
(
this
Assert
assert
,
Stream
file
,
string
message
=
"Document content is not a PDF format"
)
{
if
(
file
!=
null
)
StreamContainsMagicBytes
(
file
,
"%PDF-1."
,
message
)
;
}
/// <summary>
/// Tests whether the specified Stream is a Zip file and throws an exception
/// if it is not a Zip.
/// </summary>
/// <param name="assert"></param>
/// <param name="file">The Stream the test expects to be a Zip file.</param>
/// <param name="message">
/// The message to include in the exception when <paramref name="file"/>
/// is not a Zip file. The message is shown in test results.
/// </param>
public
static
void
StreamIsZip
(
this
Assert
assert
,
Stream
file
,
string
message
=
"Document content is not a ZIP format"
)
{
if
(
file
!=
null
)
StreamContainsMagicBytes
(
file
,
"PK"
,
message
)
;
}
private
static
void
StreamContainsMagicBytes
(
Stream
file
,
string
byteSignature
,
string
message
)
{
Assert
.
IsNotNull
(
file
,
"Document is Empty or not exists"
)
;
Assert
.
IsTrue
(
file
.
CanRead
,
"Not readable Document content"
)
;
using
var
reader
=
new
StreamReader
(
file
,
Encoding
.
UTF8
)
;
var
actual
=
reader
.
ReadLine
(
)
;
StringAssert
.
StartsWith
(
actual
,
byteSignature
,
message
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL