...

Source file src/google.golang.org/api/internal/gensupport/jsonfloat_test.go

Documentation: google.golang.org/api/internal/gensupport

     1  // Copyright 2016 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gensupport
     6  
     7  import (
     8  	"encoding/json"
     9  	"math"
    10  	"testing"
    11  )
    12  
    13  func TestJSONFloat(t *testing.T) {
    14  	for _, test := range []struct {
    15  		in   string
    16  		want float64
    17  	}{
    18  		{"0", 0},
    19  		{"-10", -10},
    20  		{"1e23", 1e23},
    21  		{`"Infinity"`, math.Inf(1)},
    22  		{`"-Infinity"`, math.Inf(-1)},
    23  		{`"NaN"`, math.NaN()},
    24  	} {
    25  		var f64 JSONFloat64
    26  		if err := json.Unmarshal([]byte(test.in), &f64); err != nil {
    27  			t.Fatal(err)
    28  		}
    29  		got := float64(f64)
    30  		if got != test.want && math.IsNaN(got) != math.IsNaN(test.want) {
    31  			t.Errorf("%s: got %f, want %f", test.in, got, test.want)
    32  		}
    33  	}
    34  }
    35  
    36  func TestJSONFloatErrors(t *testing.T) {
    37  	var f64 JSONFloat64
    38  	for _, in := range []string{"", "a", `"Inf"`, `"-Inf"`, `"nan"`, `"nana"`} {
    39  		if err := json.Unmarshal([]byte(in), &f64); err == nil {
    40  			t.Errorf("%q: got nil, want error", in)
    41  		}
    42  	}
    43  }
    44  

View as plain text