-
Notifications
You must be signed in to change notification settings - Fork 17
/
PrimitiveFormatsSpec.scala
158 lines (119 loc) · 5.37 KB
/
PrimitiveFormatsSpec.scala
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (C) 2011-2016 Mathias Doenitz
* Adapted and extended in 2016 by Eugene Yokota
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sjsonnew
package support.spray
import spray.json.{ JsValue, JsNumber, JsString, JsNull, JsTrue, JsFalse }
object PritimiveFormatsSpec extends verify.BasicTestSuite {
test("The IntJsonFormat") {
// "convert an Int to a JsNumber"
Predef.assert(Converter.toJsonUnsafe[Int](42) == JsNumber(42))
// "convert a JsNumber to an Int"
Predef.assert(Converter.fromJsonUnsafe[Int](JsNumber(42)) == 42)
}
test("The LongJsonFormat") {
// "convert a Long to a JsNumber"
Predef.assert(Converter.toJsonUnsafe[Long](7563661897011259335L) == JsNumber(7563661897011259335L))
// "convert a JsNumber to a Long"
Predef.assert(Converter.fromJsonUnsafe[Long](JsNumber(7563661897011259335L)) == 7563661897011259335L)
}
test("The FloatJsonFormat") {
// "convert a Float to a JsNumber"
// Predef.assert(Converter.toJsonUnsafe(4.2f) == JsNumber(4.2f))
// "convert a Float.NaN to a JsNull"
Predef.assert(Converter.toJsonUnsafe(Float.NaN) == JsNull)
// "convert a Float.PositiveInfinity to a JsNull"
Predef.assert(Converter.toJsonUnsafe(Float.PositiveInfinity) == JsNull)
// "convert a Float.NegativeInfinity to a JsNull"
Predef.assert(Converter.toJsonUnsafe(Float.NegativeInfinity) == JsNull)
// "convert a JsNumber to a Float"
Predef.assert(Converter.fromJsonUnsafe[Float](JsNumber(4.2f)) == 4.2f)
// "convert a JsNull to a Float"
Predef.assert(Converter.fromJsonUnsafe[Float](JsNull).isNaN == Float.NaN.isNaN)
}
test("The DoubleJsonFormat") {
// "convert a Double to a JsNumber"
Predef.assert(Converter.toJsonUnsafe(4.2) == JsNumber(4.2))
// "convert a Double.NaN to a JsNull"
Predef.assert(Converter.toJsonUnsafe(Double.NaN) == JsNull)
// "convert a Double.PositiveInfinity to a JsNull"
Predef.assert(Converter.toJsonUnsafe(Double.PositiveInfinity) == JsNull)
// "convert a Double.NegativeInfinity to a JsNull"
Predef.assert(Converter.toJsonUnsafe(Double.NegativeInfinity) == JsNull)
// "convert a JsNumber to a Double"
Predef.assert(Converter.fromJsonUnsafe[Double](JsNumber(4.2)) == 4.2)
// "convert a JsNull to a Double"
Predef.assert(Converter.fromJsonUnsafe[Double](JsNull).isNaN == Double.NaN.isNaN)
}
test("The ByteJsonFormat") {
// "convert a Byte to a JsNumber"
Predef.assert(Converter.toJsonUnsafe(42.asInstanceOf[Byte]) == JsNumber(42))
// "convert a JsNumber to a Byte"
Predef.assert(Converter.fromJsonUnsafe[Byte](JsNumber(42)) == 42)
}
test("The ShortJsonFormat") {
// "convert a Short to a JsNumber"
Predef.assert(Converter.toJsonUnsafe(42.asInstanceOf[Short]) == JsNumber(42))
// "convert a JsNumber to a Short"
Predef.assert(Converter.fromJsonUnsafe[Short](JsNumber(42)) == 42)
}
test("The BigDecimalJsonFormat") {
// "convert a BigDecimal to a JsNumber"
Predef.assert(Converter.toJsonUnsafe(BigDecimal(42)) == JsNumber(42))
// "convert a JsNumber to a BigDecimal"
Predef.assert(Converter.fromJsonUnsafe[BigDecimal](JsNumber(42)) == BigDecimal(42))
}
test("The BigIntJsonFormat") {
// "convert a BigInt to a JsNumber"
Predef.assert(Converter.toJsonUnsafe(BigInt(42)) == JsNumber(42))
// "convert a JsNumber to a BigInt"
Predef.assert(Converter.fromJsonUnsafe[BigDecimal](JsNumber(42)) == BigInt(42))
}
test("The UnitJsonFormat") {
// "convert Unit to a JsNumber(1)"
Predef.assert(Converter.toJsonUnsafe(()) == JsNumber(1))
// "convert a JsNumber to Unit"
Predef.assert(Converter.fromJsonUnsafe[Unit](JsNumber(1)) == (()))
}
test("The BooleanJsonFormat") {
// "convert true to a JsTrue"
Predef.assert(Converter.toJsonUnsafe(true) == JsTrue)
// "convert false to a JsFalse"
Predef.assert(Converter.toJsonUnsafe(false) == JsFalse)
// "convert a JsTrue to true"
Predef.assert(Converter.fromJsonUnsafe[Boolean](JsTrue) == true)
// "convert a JsFalse to false"
Predef.assert(Converter.fromJsonUnsafe[Boolean](JsFalse) == false)
}
test("The CharJsonFormat") {
// "convert a Char to a JsString"
Predef.assert(Converter.toJsonUnsafe('c') == JsString("c"))
// "convert a JsString to a Char"
Predef.assert(Converter.fromJsonUnsafe[Char](JsString("c")) == 'c')
}
test("The StringJsonFormat") {
// "convert a String to a JsString"
Predef.assert(Converter.toJsonUnsafe("Hello") == JsString("Hello"))
// "convert a JsString to a String"
Predef.assert(Converter.fromJsonUnsafe[String](JsString("Hello")) == "Hello")
}
test("The SymbolJsonFormat") {
// "convert a Symbol to a JsString"
Predef.assert(Converter.toJsonUnsafe(Symbol("Hello")) == JsString("Hello"))
// "convert a JsString to a Symbol"
Predef.assert(Converter.fromJsonUnsafe[Symbol](JsString("Hello")) == Symbol("Hello"))
}
}