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