github.com/neilotoole/jsoncolor@v0.7.2-0.20231115150201-1637fae69be1/parse_test.go (about)

     1  package jsoncolor
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestParseString(t *testing.T) {
    10  	tests := []struct {
    11  		in  string
    12  		out string
    13  		ext string
    14  	}{
    15  		{`""`, `""`, ``},
    16  		{`"1234567890"`, `"1234567890"`, ``},
    17  		{`"Hello World!"`, `"Hello World!"`, ``},
    18  		{`"Hello\"World!"`, `"Hello\"World!"`, ``},
    19  		{`"\\"`, `"\\"`, ``},
    20  	}
    21  
    22  	for _, test := range tests {
    23  		t.Run(test.in, func(t *testing.T) {
    24  			out, ext, err := parseString([]byte(test.in))
    25  			if err != nil {
    26  				t.Errorf("%s => %s", test.in, err)
    27  				return
    28  			}
    29  
    30  			if s := string(out); s != test.out {
    31  				t.Error("invalid output")
    32  				t.Logf("expected: %s", test.out)
    33  				t.Logf("found:    %s", s)
    34  			}
    35  
    36  			if s := string(ext); s != test.ext {
    37  				t.Error("invalid extra bytes")
    38  				t.Logf("expected: %s", test.ext)
    39  				t.Logf("found:    %s", s)
    40  			}
    41  		})
    42  	}
    43  }
    44  
    45  func TestAppendToLower(t *testing.T) {
    46  	tests := []string{
    47  		"",
    48  		"A",
    49  		"a",
    50  		"__segment_internal",
    51  		"someFieldWithALongBName",
    52  		"Hello World!",
    53  		"Hello\"World!",
    54  		"Hello\\World!",
    55  		"Hello\nWorld!",
    56  		"Hello\rWorld!",
    57  		"Hello\tWorld!",
    58  		"Hello\bWorld!",
    59  		"Hello\fWorld!",
    60  		"你好",
    61  		"<",
    62  		">",
    63  		"&",
    64  		"\u001944",
    65  		"\u00c2e>",
    66  		"\u00c2V?",
    67  		"\u000e=8",
    68  		"\u001944\u00c2e>\u00c2V?\u000e=8",
    69  		"ir\u001bQJ\u007f\u0007y\u0015)",
    70  	}
    71  
    72  	for _, test := range tests {
    73  		s1 := strings.ToLower(test)
    74  		s2 := string(appendToLower(nil, []byte(test)))
    75  
    76  		if s1 != s2 {
    77  			t.Error("lowercase values mismatch")
    78  			t.Log("expected:", s1)
    79  			t.Log("found:   ", s2)
    80  		}
    81  	}
    82  }
    83  
    84  func BenchmarkParseString(b *testing.B) {
    85  	s := []byte(`"__segment_internal"`)
    86  
    87  	for i := 0; i != b.N; i++ {
    88  		parseString(s)
    89  	}
    90  }
    91  
    92  func BenchmarkToLower(b *testing.B) {
    93  	s := []byte("someFieldWithALongName")
    94  
    95  	for i := 0; i != b.N; i++ {
    96  		bytes.ToLower(s)
    97  	}
    98  }
    99  
   100  func BenchmarkAppendToLower(b *testing.B) {
   101  	a := []byte(nil)
   102  	s := []byte("someFieldWithALongName")
   103  
   104  	for i := 0; i != b.N; i++ {
   105  		a = appendToLower(a[:0], s)
   106  	}
   107  }
   108  
   109  var (
   110  	benchmarkHasPrefixString = []byte("some random string")
   111  	benchmarkHasPrefixResult = false
   112  )
   113  
   114  func BenchmarkHasPrefix(b *testing.B) {
   115  	for i := 0; i < b.N; i++ {
   116  		benchmarkHasPrefixResult = hasPrefix(benchmarkHasPrefixString, "null")
   117  	}
   118  }
   119  
   120  func BenchmarkHasNullPrefix(b *testing.B) {
   121  	for i := 0; i < b.N; i++ {
   122  		benchmarkHasPrefixResult = hasNullPrefix(benchmarkHasPrefixString)
   123  	}
   124  }
   125  
   126  func BenchmarkHasTruePrefix(b *testing.B) {
   127  	for i := 0; i < b.N; i++ {
   128  		benchmarkHasPrefixResult = hasTruePrefix(benchmarkHasPrefixString)
   129  	}
   130  }
   131  
   132  func BenchmarkHasFalsePrefix(b *testing.B) {
   133  	for i := 0; i < b.N; i++ {
   134  		benchmarkHasPrefixResult = hasFalsePrefix(benchmarkHasPrefixString)
   135  	}
   136  }
   137  
   138  func BenchmarkParseStringEscapeNone(b *testing.B) {
   139  	j := []byte(`"` + strings.Repeat(`a`, 1000) + `"`)
   140  	var s string
   141  	b.SetBytes(int64(len(j)))
   142  
   143  	for i := 0; i < b.N; i++ {
   144  		if err := Unmarshal(j, &s); err != nil {
   145  			b.Fatal(err)
   146  		}
   147  		s = ""
   148  	}
   149  }
   150  
   151  func BenchmarkParseStringEscapeOne(b *testing.B) {
   152  	j := []byte(`"` + strings.Repeat(`a`, 998) + `\n"`)
   153  	var s string
   154  	b.SetBytes(int64(len(j)))
   155  
   156  	for i := 0; i < b.N; i++ {
   157  		if err := Unmarshal(j, &s); err != nil {
   158  			b.Fatal(err)
   159  		}
   160  		s = ""
   161  	}
   162  }
   163  
   164  func BenchmarkParseStringEscapeAll(b *testing.B) {
   165  	j := []byte(`"` + strings.Repeat(`\`, 1000) + `"`)
   166  	var s string
   167  	b.SetBytes(int64(len(j)))
   168  
   169  	for i := 0; i < b.N; i++ {
   170  		if err := Unmarshal(j, &s); err != nil {
   171  			b.Fatal(err)
   172  		}
   173  		s = ""
   174  	}
   175  }