github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/json/parser_test.gno (about)

     1  package json
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  )
     7  
     8  func TestParseStringLiteral(t *testing.T) {
     9  	tests := []struct {
    10  		input    string
    11  		expected string
    12  		isError  bool
    13  	}{
    14  		{`"Hello, World!"`, "\"Hello, World!\"", false},
    15  		{`\uFF11`, "\uFF11", false},
    16  		{`\uFFFF`, "\uFFFF", false},
    17  		{`true`, "true", false},
    18  		{`false`, "false", false},
    19  		{`\uDF00`, "", true},
    20  	}
    21  
    22  	for i, tt := range tests {
    23  		s, err := ParseStringLiteral([]byte(tt.input))
    24  
    25  		if !tt.isError && err != nil {
    26  			t.Errorf("%d. unexpected error: %s", i, err)
    27  		}
    28  
    29  		if tt.isError && err == nil {
    30  			t.Errorf("%d. expected error, but not error", i)
    31  		}
    32  
    33  		if s != tt.expected {
    34  			t.Errorf("%d. expected=%s, but actual=%s", i, tt.expected, s)
    35  		}
    36  	}
    37  }
    38  
    39  func TestParseBoolLiteral(t *testing.T) {
    40  	tests := []struct {
    41  		input    string
    42  		expected bool
    43  		isError  bool
    44  	}{
    45  		{`true`, true, false},
    46  		{`false`, false, false},
    47  		{`TRUE`, false, true},
    48  		{`FALSE`, false, true},
    49  		{`foo`, false, true},
    50  		{`"true"`, false, true},
    51  		{`"false"`, false, true},
    52  	}
    53  
    54  	for i, tt := range tests {
    55  		b, err := ParseBoolLiteral([]byte(tt.input))
    56  
    57  		if !tt.isError && err != nil {
    58  			t.Errorf("%d. unexpected error: %s", i, err)
    59  		}
    60  
    61  		if tt.isError && err == nil {
    62  			t.Errorf("%d. expected error, but not error", i)
    63  		}
    64  
    65  		if b != tt.expected {
    66  			t.Errorf("%d. expected=%t, but actual=%t", i, tt.expected, b)
    67  		}
    68  	}
    69  }
    70  
    71  func TestParseFloatLiteral(t *testing.T) {
    72  	tests := []struct {
    73  		input    string
    74  		expected float64
    75  	}{
    76  		{"123", 123},
    77  		{"-123", -123},
    78  		{"123.456", 123.456},
    79  		{"-123.456", -123.456},
    80  		{"12345678.1234567890", 12345678.1234567890},
    81  		{"-12345678.09123456789", -12345678.09123456789},
    82  		{"0.123", 0.123},
    83  		{"-0.123", -0.123},
    84  		{"", -1},
    85  		{"abc", -1},
    86  		{"123.45.6", -1},
    87  		{"999999999999999999999", -1},
    88  	}
    89  
    90  	for _, tt := range tests {
    91  		t.Run(tt.input, func(t *testing.T) {
    92  			got, _ := ParseFloatLiteral([]byte(tt.input))
    93  			if got != tt.expected {
    94  				t.Errorf("ParseFloatLiteral(%s): got %v, want %v", tt.input, got, tt.expected)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func TestParseFloatWithScientificNotation(t *testing.T) {
   101  	tests := []struct {
   102  		input    string
   103  		expected float64
   104  	}{
   105  		{"1e6", 1000000},
   106  		{"1E6", 1000000},
   107  		{"1.23e10", 1.23e10},
   108  		{"1.23E10", 1.23e10},
   109  		{"-1.23e10", -1.23e10},
   110  		{"-1.23E10", -1.23e10},
   111  		{"2.45e-8", 2.45e-8},
   112  		{"2.45E-8", 2.45e-8},
   113  		{"-2.45e-8", -2.45e-8},
   114  		{"-2.45E-8", -2.45e-8},
   115  		{"5e0", 5},
   116  		{"-5e0", -5},
   117  		{"5E+0", 5},
   118  		{"5e+1", 50},
   119  		{"1e-1", 0.1},
   120  		{"1E-1", 0.1},
   121  		{"-1e-1", -0.1},
   122  		{"-1E-1", -0.1},
   123  	}
   124  
   125  	for _, tt := range tests {
   126  		t.Run(tt.input, func(t *testing.T) {
   127  			got, err := ParseFloatLiteral([]byte(tt.input))
   128  			if got != tt.expected {
   129  				t.Errorf("ParseFloatLiteral(%s): got %v, want %v", tt.input, got, tt.expected)
   130  			}
   131  
   132  			if err != nil {
   133  				t.Errorf("ParseFloatLiteral(%s): got error %v", tt.input, err)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func TestParseFloat_May_Interoperability_Problem(t *testing.T) {
   140  	tests := []struct {
   141  		input     string
   142  		shouldErr bool
   143  	}{
   144  		{"3.141592653589793238462643383279", true},
   145  		{"1E400", false}, // TODO: should error
   146  	}
   147  
   148  	for _, tt := range tests {
   149  		t.Run(tt.input, func(t *testing.T) {
   150  			_, err := ParseFloatLiteral([]byte(tt.input))
   151  			if tt.shouldErr && err == nil {
   152  				t.Errorf("ParseFloatLiteral(%s): expected error, but not error", tt.input)
   153  			}
   154  		})
   155  	}
   156  }
   157  
   158  func TestParseIntLiteral(t *testing.T) {
   159  	tests := []struct {
   160  		input    string
   161  		expected int64
   162  	}{
   163  		{"0", 0},
   164  		{"1", 1},
   165  		{"-1", -1},
   166  		{"12345", 12345},
   167  		{"-12345", -12345},
   168  		{"9223372036854775807", 9223372036854775807},
   169  		{"-9223372036854775808", -9223372036854775808},
   170  		{"-92233720368547758081", 0},
   171  		{"18446744073709551616", 0},
   172  		{"9223372036854775808", 0},
   173  		{"-9223372036854775809", 0},
   174  		{"", 0},
   175  		{"abc", 0},
   176  		{"12345x", 0},
   177  		{"123e5", 0},
   178  		{"9223372036854775807x", 0},
   179  		{"27670116110564327410", 0},
   180  		{"-27670116110564327410", 0},
   181  	}
   182  
   183  	for _, tt := range tests {
   184  		t.Run(tt.input, func(t *testing.T) {
   185  			got, _ := ParseIntLiteral([]byte(tt.input))
   186  			if got != tt.expected {
   187  				t.Errorf("ParseIntLiteral(%s): got %v, want %v", tt.input, got, tt.expected)
   188  			}
   189  		})
   190  	}
   191  }