Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 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}, |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 22 | {`'quote"here'`, `quote"here`, false}, |
| 23 | {`"quote'here"`, `quote'here`, true}, |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 24 | {`'quote\'here'`, `quote'here`, false}, |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 25 | |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 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}, |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 33 | { |
alandonovan | 16e44b1 | 2020-03-26 10:23:16 -0400 | [diff] [blame] | 34 | `"cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ \x27\\1\x27,/g' >> $@; "`, |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 35 | "cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; ", |
| 36 | false, |
| 37 | }, |
| 38 | { |
alandonovan | 16e44b1 | 2020-03-26 10:23:16 -0400 | [diff] [blame] | 39 | `"cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; "`, |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 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 | } |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 50 | q := Quote(tt.s, false) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 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 { |
alandonovan | ebe61bd | 2021-02-12 16:57:32 -0500 | [diff] [blame] | 59 | s, triple, _, err := unquote(tt.q) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 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 | } |