kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/encoding/rdf/rdf_test.go (about)

     1  /*
     2   * Copyright 2015 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package rdf
    18  
    19  import "testing"
    20  
    21  func q(s string) string { return `"` + s + `"` }
    22  
    23  func TestQuote(t *testing.T) {
    24  	tests := []struct {
    25  		input, want string
    26  	}{
    27  		{"", q("")},                               // empty
    28  		{"a b c", q("a b c")},                     // no escapes
    29  		{"\x00", q(`\u0000`)},                     // NUL
    30  		{"\x08\x09\x0a\x0c\x0d", q(`\b\t\n\f\r`)}, // C-style controls
    31  		{`" \ '`, q(`\" \\ \'`)},                  // metacharacters
    32  		{"§3.14 π", q(`\u00a73.14 \u03c0`)},       // non-ASCII (UTF-8)
    33  		{"\xfe", q(`\u00fe`)},                     // non-UTF-8 single-byte
    34  		{"\U0002A6D0", q(`\U0002a6d0`)},           // large UTF-8
    35  	}
    36  	for _, test := range tests {
    37  		got := Quote(test.input)
    38  		if got != test.want {
    39  			t.Errorf("Quote %q: got %s, want %s", test.input, got, test.want)
    40  		}
    41  	}
    42  }
    43  
    44  func TestEncoding(t *testing.T) {
    45  	tests := []struct {
    46  		s, p, o string
    47  		want    string
    48  	}{
    49  		{want: `"" "" "" .`}, // empty
    50  		{"Mary", "loves", "cabbáge", `"Mary" "loves" "cabb\u00e1ge" .`},
    51  		{"▷", "\xa7", `"`, `"\u25b7" "\u00a7" "\"" .`},
    52  		{" ", "----", "\tq\r\n", `" " "----" "\tq\r\n" .`},
    53  	}
    54  	for _, test := range tests {
    55  		triple := &Triple{test.s, test.p, test.o}
    56  		got := triple.String()
    57  		if got != test.want {
    58  			t.Errorf("Encoding %+v\n got: %s\nwant: %s", triple, got, test.want)
    59  		}
    60  	}
    61  }