github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/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`, false}, 24 {`"quote'here"`, `quote'here`, true}, 25 {`"quote\'here"`, `quote'here`, false}, 26 {`'quote\'here'`, `quote'here`, false}, 27 {`"""hello " ' world "" asdf ''' foo"""`, `hello " ' world "" asdf ''' foo`, true}, 28 {`"foo\(bar"`, `foo\(bar`, true}, 29 {`"""hello 30 world"""`, "hello\nworld", true}, 31 32 {`"\a\b\f\n\r\t\v\000\377"`, "\a\b\f\n\r\t\v\000\xFF", true}, 33 {`"\a\b\f\n\r\t\v\x00\xff"`, "\a\b\f\n\r\t\v\000\xFF", false}, 34 {`"\a\b\f\n\r\t\v\000\xFF"`, "\a\b\f\n\r\t\v\000\xFF", false}, 35 {`"\a\b\f\n\r\t\v\000\377\"'\\\003\200"`, "\a\b\f\n\r\t\v\x00\xFF\"'\\\x03\x80", true}, 36 {`"\a\b\f\n\r\t\v\x00\xff\"'\\\x03\x80"`, "\a\b\f\n\r\t\v\x00\xFF\"'\\\x03\x80", false}, 37 {`"\a\b\f\n\r\t\v\000\xFF\"'\\\x03\x80"`, "\a\b\f\n\r\t\v\x00\xFF\"'\\\x03\x80", false}, 38 {`"\a\b\f\n\r\t\v\000\xFF\"\'\\\x03\x80"`, "\a\b\f\n\r\t\v\x00\xFF\"'\\\x03\x80", false}, 39 { 40 `"cat $(SRCS) | grep '\s*ip_block:' | sed -e 's/\s*ip_block: \"\([^ ]*\)\"/ \x27\\1\x27,/g' >> $@; "`, 41 "cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; ", 42 false, 43 }, 44 { 45 `"cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\([^ ]*\)\"/ '\\1',/g' >> $@; "`, 46 "cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; ", 47 true, 48 }, 49 } 50 51 func TestQuote(t *testing.T) { 52 for _, tt := range quoteTests { 53 if !tt.std { 54 continue 55 } 56 q := quote(tt.s, strings.HasPrefix(tt.q, `"""`)) 57 if q != tt.q { 58 t.Errorf("quote(%#q) = %s, want %s", tt.s, q, tt.q) 59 } 60 } 61 } 62 63 func TestUnquote(t *testing.T) { 64 for _, tt := range quoteTests { 65 s, triple, err := unquote(tt.q) 66 wantTriple := strings.HasPrefix(tt.q, `"""`) || strings.HasPrefix(tt.q, `'''`) 67 if s != tt.s || triple != wantTriple || err != nil { 68 t.Errorf("unquote(%s) = %#q, %v, %v want %#q, %v, nil", tt.q, s, triple, err, tt.s, wantTriple) 69 } 70 } 71 }