@@ -234,3 +234,97 @@ func TestObjectToFloat_negativeOneThird(t *testing.T) {
234
234
t .Fatalf ("bad: %#v, expected: %v" , v .ToFloat (), g )
235
235
}
236
236
}
237
+
238
+ func TestIntToObject (t * testing.T ) {
239
+ var testValue int64 = 42
240
+ obj := NewIntegerObject (testValue )
241
+ defer obj .Close ()
242
+ v := obj .ToInt ()
243
+ if v != testValue {
244
+ t .Fatalf ("bad: %d, expected: %d" , v , testValue )
245
+ }
246
+ }
247
+
248
+ func TestBoolToObject (t * testing.T ) {
249
+ objTrue := NewBoolObject (true )
250
+ defer objTrue .Close ()
251
+ if ! objTrue .ToBool () {
252
+ t .Fatalf ("bad: false, expected: true" )
253
+ }
254
+ objFalse := NewBoolObject (false )
255
+ defer objFalse .Close ()
256
+ if objFalse .ToBool () {
257
+ t .Fatalf ("bad: true, expected: false" )
258
+ }
259
+ }
260
+
261
+ func TestFloatToObject (t * testing.T ) {
262
+ testValue := - 1.0 / 3.0
263
+ obj := NewDoubleObject (testValue )
264
+ defer obj .Close ()
265
+ if obj .ToFloat () != testValue {
266
+ t .Fatalf ("bad: %#v, expected: %v" , obj .ToFloat (), testValue )
267
+ }
268
+ }
269
+
270
+ func TestSimpleStringToObject (t * testing.T ) {
271
+ s := "Hello World!"
272
+ obj := NewObject (s )
273
+ defer obj .Close ()
274
+ if obj .ToString () != s {
275
+ t .Fatalf ("bad: \" %s\" , expected: \" %s\" " , obj .ToString (), s )
276
+ }
277
+ }
278
+
279
+ func TestJSONEscapeStringToObject (t * testing.T ) {
280
+ inputString := "complex\t string\n from\" hell\" "
281
+ escapedString := "complex\\ tstring\\ nfrom\\ \" hell\\ \" "
282
+ obj := NewObject (inputString )
283
+ defer obj .Close ()
284
+ if obj .ToString () != escapedString {
285
+ t .Fatalf ("bad: \" %s\" , expected: \" %s\" " , obj .ToString (), escapedString )
286
+ }
287
+ }
288
+
289
+ func TestStringIntToObject (t * testing.T ) {
290
+ var i int64 = 42
291
+ s := "42"
292
+ obj := NewFormattedObject (s , StringParseInt )
293
+ defer obj .Close ()
294
+ if obj .ToInt () != i {
295
+ t .Fatalf ("bad: %d, expected: %d" , obj .ToInt (), i )
296
+ }
297
+ }
298
+
299
+ func TestStringFloatToObject (t * testing.T ) {
300
+ expectedValue := - 1.0 / 3.0
301
+ testString := "-0.3333333333333333148296162562473909929394721984863281"
302
+ obj := NewFormattedObject (testString , StringParseDouble )
303
+ defer obj .Close ()
304
+ if obj .ToFloat () != expectedValue {
305
+ t .Fatalf ("bad: %#v, expected: %v" , obj .ToFloat (), expectedValue )
306
+ }
307
+ }
308
+
309
+ func TestStringBoolToObject (t * testing.T ) {
310
+ objFalse := NewFormattedObject ("false" , StringParseBoolean )
311
+ defer objFalse .Close ()
312
+ if objFalse .ToBool () {
313
+ t .Fatal ("bad: true, expected: false" )
314
+ }
315
+ objTrue := NewFormattedObject ("true" , StringParseBoolean )
316
+ defer objTrue .Close ()
317
+ if ! objTrue .ToBool () {
318
+ t .Fatal ("bad: false, expected: true" )
319
+ }
320
+ }
321
+
322
+ func TestStringTrimToObject (t * testing.T ) {
323
+ rawString := " Hello World\n \t \n \t \n \t "
324
+ expectedResult := "Hello World"
325
+ obj := NewFormattedObject (rawString , StringTrim )
326
+ defer obj .Close ()
327
+ if obj .ToString () != expectedResult {
328
+ t .Fatalf ("bad: \" %s\" , expected: \" %s\" " , obj .ToString (), expectedResult )
329
+ }
330
+ }
0 commit comments