go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/syntax/quote_test.go (about) 1 // Copyright 2017 The Bazel Authors. All rights reserved. 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 syntax 6 7 import ( 8 "strings" 9 "testing" 10 ) 11 12 var quoteTests = []struct { 13 q string // quoted 14 s string // unquoted (actual string) 15 std bool // q is standard form for s 16 }{ 17 {`""`, "", true}, 18 {`''`, "", false}, 19 {`"hello"`, `hello`, true}, 20 {`'hello'`, `hello`, false}, 21 {`"quote\"here"`, `quote"here`, true}, 22 {`'quote"here'`, `quote"here`, false}, 23 {`"quote'here"`, `quote'here`, true}, 24 {`'quote\'here'`, `quote'here`, false}, 25 26 {`"\a\b\f\n\r\t\v\x00\x7f"`, "\a\b\f\n\r\t\v\000\x7F", true}, 27 {`"\a\b\f\n\r\t\v\x00\x7f"`, "\a\b\f\n\r\t\v\000\x7F", false}, 28 {`"\a\b\f\n\r\t\v\x00\x7f"`, "\a\b\f\n\r\t\v\000\x7F", false}, 29 {`"\a\b\f\n\r\t\v\x00\x7f\"'\\\x03"`, "\a\b\f\n\r\t\v\x00\x7F\"'\\\x03", true}, 30 {`"\a\b\f\n\r\t\v\x00\x7f\"'\\\x03"`, "\a\b\f\n\r\t\v\x00\x7F\"'\\\x03", false}, 31 {`"\a\b\f\n\r\t\v\x00\x7f\"'\\\x03"`, "\a\b\f\n\r\t\v\x00\x7F\"'\\\x03", false}, 32 {`"\a\b\f\n\r\t\v\x00\x7f\"\\\x03"`, "\a\b\f\n\r\t\v\x00\x7F\"\\\x03", false}, 33 { 34 `"cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ \x27\\1\x27,/g' >> $@; "`, 35 "cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; ", 36 false, 37 }, 38 { 39 `"cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; "`, 40 "cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; ", 41 true, 42 }, 43 } 44 45 func TestQuote(t *testing.T) { 46 for _, tt := range quoteTests { 47 if !tt.std { 48 continue 49 } 50 q := Quote(tt.s, false) 51 if q != tt.q { 52 t.Errorf("quote(%#q) = %s, want %s", tt.s, q, tt.q) 53 } 54 } 55 } 56 57 func TestUnquote(t *testing.T) { 58 for _, tt := range quoteTests { 59 s, triple, _, err := unquote(tt.q) 60 wantTriple := strings.HasPrefix(tt.q, `"""`) || strings.HasPrefix(tt.q, `'''`) 61 if s != tt.s || triple != wantTriple || err != nil { 62 t.Errorf("unquote(%s) = %#q, %v, %v want %#q, %v, nil", tt.q, s, triple, err, tt.s, wantTriple) 63 } 64 } 65 }