Skip to content

Commit c48084b

Browse files
committed
Clean up of tests for package object of the core module
1 parent e131166 commit c48084b

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

core/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/core/PackageSpec.scala

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,52 @@ import org.scalatest.{Matchers, WordSpec}
88
import org.scalatest.prop.PropertyChecks
99

1010
class PackageSpec extends WordSpec with Matchers with PropertyChecks {
11-
"read" should {
11+
"readFromStream" should {
1212
"parse JSON from the provided input stream" in {
1313
readFromStream(getClass.getResourceAsStream("user_api_response.json"))(codec) shouldBe user
1414
}
15+
"throw an exception if cannot parse input with message containing input offset & hex dump of affected part" in {
16+
assert(intercept[JsonParseException](readFromStream(new ByteArrayInputStream(httpMessage))(codec)).getMessage ==
17+
"""expected '{', offset: 0x00000000, buf:
18+
| +-------------------------------------------------+
19+
| | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
20+
|+----------+-------------------------------------------------+------------------+
21+
|| 00000000 | 48 54 54 50 2f 31 2e 30 20 32 30 30 20 4f 4b 0a | HTTP/1.0 200 OK. |
22+
|| 00000010 | 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 | Content-Type: ap |
23+
|| 00000020 | 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 0a 43 | plication/json.C |
24+
|+----------+-------------------------------------------------+------------------+""".stripMargin)
25+
}
26+
"throw an exception in case of the provided params are invalid" in {
27+
intercept[NullPointerException](readFromStream(new ByteArrayInputStream(compactJson))(null))
28+
intercept[NullPointerException](readFromStream(null)(codec))
29+
}
30+
}
31+
"readFromArray" should {
1532
"parse JSON from the byte array" in {
1633
readFromArray(compactJson)(codec) shouldBe user
1734
}
35+
"throw an exception if cannot parse input with message containing input offset & hex dump of affected part" in {
36+
assert(intercept[JsonParseException](readFromArray(httpMessage)(codec)).getMessage ==
37+
"""expected '{', offset: 0x00000000, buf:
38+
| +-------------------------------------------------+
39+
| | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
40+
|+----------+-------------------------------------------------+------------------+
41+
|| 00000000 | 48 54 54 50 2f 31 2e 30 20 32 30 30 20 4f 4b 0a | HTTP/1.0 200 OK. |
42+
|| 00000010 | 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 | Content-Type: ap |
43+
|| 00000020 | 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 0a 43 | plication/json.C |
44+
|+----------+-------------------------------------------------+------------------+""".stripMargin)
45+
}
46+
"throw an exception in case of the provided params are invalid" in {
47+
intercept[NullPointerException](readFromArray(compactJson)(null))
48+
intercept[NullPointerException](readFromArray(null.asInstanceOf[Array[Byte]])(codec))
49+
}
50+
}
51+
"readFromSubArray" should {
1852
"parse JSON from the byte array within specified positions" in {
1953
readFromSubArray(httpMessage, 66, httpMessage.length)(codec) shouldBe user
2054
}
2155
"throw an exception if cannot parse input with message containing input offset & hex dump of affected part" in {
22-
assert(intercept[JsonParseException](readFromArray(httpMessage)(codec)).getMessage ==
56+
assert(intercept[JsonParseException](readFromSubArray(httpMessage, 0, httpMessage.length)(codec)).getMessage ==
2357
"""expected '{', offset: 0x00000000, buf:
2458
| +-------------------------------------------------+
2559
| | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
@@ -30,13 +64,8 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
3064
|+----------+-------------------------------------------------+------------------+""".stripMargin)
3165
}
3266
"throw an exception in case of the provided params are invalid" in {
33-
intercept[NullPointerException](readFromArray(compactJson)(null))
34-
intercept[NullPointerException](readFromStream(new ByteArrayInputStream(compactJson))(null))
3567
intercept[NullPointerException](readFromSubArray(httpMessage, 66, httpMessage.length)(null))
36-
intercept[NullPointerException](readFromArray(null.asInstanceOf[Array[Byte]])(codec))
3768
intercept[NullPointerException](readFromSubArray(null.asInstanceOf[Array[Byte]], 0, 50)(codec))
38-
intercept[NullPointerException](readFromStream(null.asInstanceOf[InputStream])(codec))
39-
intercept[NullPointerException](readFromStream(new ByteArrayInputStream(compactJson), null)(codec))
4069
intercept[NullPointerException](readFromSubArray(httpMessage, 66, httpMessage.length, null)(codec))
4170
assert(intercept[ArrayIndexOutOfBoundsException](readFromSubArray(httpMessage, 50, 200)(codec))
4271
.getMessage.contains("`to` should be positive and not greater than `buf` length"))
@@ -98,9 +127,7 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
98127
intercept[NullPointerException](scanJsonArrayFromStream(inputStream)(npe)(codec))
99128
}
100129
}
101-
"write" should {
102-
val buf = new Array[Byte](150)
103-
130+
"writeToStream" should {
104131
"serialize an object to the provided output stream" in {
105132
val out1 = new ByteArrayOutputStream()
106133
writeToStream(user, out1)(codec)
@@ -109,10 +136,25 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
109136
writeToStream(user, out2, WriterConfig(indentionStep = 2))(codec)
110137
out2.toString("UTF-8") shouldBe toString(prettyJson)
111138
}
139+
"throw i/o exception in case of the provided params are invalid" in {
140+
intercept[NullPointerException](writeToStream(user, new ByteArrayOutputStream())(null))
141+
intercept[NullPointerException](writeToStream(user, null.asInstanceOf[OutputStream])(codec))
142+
intercept[NullPointerException](writeToStream(user, new ByteArrayOutputStream(), null)(codec))
143+
}
144+
}
145+
"writeToArray" should {
112146
"serialize an object to a new instance of byte array" in {
113147
toString(writeToArray(user)(codec)) shouldBe toString(compactJson)
114148
toString(writeToArray(user, WriterConfig(indentionStep = 2))(codec)) shouldBe toString(prettyJson)
115149
}
150+
"throw i/o exception in case of the provided params are invalid" in {
151+
intercept[NullPointerException](writeToArray(user)(null))
152+
intercept[NullPointerException](writeToArray(user, null.asInstanceOf[WriterConfig])(codec))
153+
}
154+
}
155+
"writeToPreallocatedArray" should {
156+
val buf = new Array[Byte](150)
157+
116158
"serialize an object to the provided byte array from specified position" in {
117159
val from1 = 10
118160
val to1 = writeToPreallocatedArray(user, buf, from1)(codec)
@@ -126,13 +168,8 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
126168
.getMessage.contains("`buf` length exceeded"))
127169
}
128170
"throw i/o exception in case of the provided params are invalid" in {
129-
intercept[NullPointerException](writeToArray(user)(null))
130-
intercept[NullPointerException](writeToStream(user, new ByteArrayOutputStream())(null))
131171
intercept[NullPointerException](writeToPreallocatedArray(user, buf, 0)(null))
132-
intercept[NullPointerException](writeToStream(user, null.asInstanceOf[OutputStream])(codec))
133172
intercept[NullPointerException](writeToPreallocatedArray(user, null, 50)(codec))
134-
intercept[NullPointerException](writeToArray(user, null.asInstanceOf[WriterConfig])(codec))
135-
intercept[NullPointerException](writeToStream(user, new ByteArrayOutputStream(), null)(codec))
136173
intercept[NullPointerException](writeToPreallocatedArray(user, buf, 0, null)(codec))
137174
assert(intercept[ArrayIndexOutOfBoundsException](writeToPreallocatedArray(user, new Array[Byte](10), 50)(codec))
138175
.getMessage.contains("`from` should be positive and not greater than `buf` length"))

0 commit comments

Comments
 (0)