blob: be7498b08088f6fe5b0c65c3ce06d4c9d7f25055 [file] [log] [blame]
Alan Donovan312d1a52017-10-02 10:10:28 -04001// 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
5package syntax
6
7import (
8 "strings"
9 "testing"
10)
11
12var 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 Donovan312d1a52017-10-02 10:10:28 -040022 {`'quote"here'`, `quote"here`, false},
23 {`"quote'here"`, `quote'here`, true},
Alan Donovan312d1a52017-10-02 10:10:28 -040024 {`'quote\'here'`, `quote'here`, false},
Alan Donovan312d1a52017-10-02 10:10:28 -040025
alandonovanebe61bd2021-02-12 16:57:32 -050026 {`"\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 Donovan312d1a52017-10-02 10:10:28 -040033 {
alandonovan16e44b12020-03-26 10:23:16 -040034 `"cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ \x27\\1\x27,/g' >> $@; "`,
Alan Donovan312d1a52017-10-02 10:10:28 -040035 "cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; ",
36 false,
37 },
38 {
alandonovan16e44b12020-03-26 10:23:16 -040039 `"cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; "`,
Alan Donovan312d1a52017-10-02 10:10:28 -040040 "cat $(SRCS) | grep '\\s*ip_block:' | sed -e 's/\\s*ip_block: \"\\([^ ]*\\)\"/ '\\1',/g' >> $@; ",
41 true,
42 },
43}
44
45func TestQuote(t *testing.T) {
46 for _, tt := range quoteTests {
47 if !tt.std {
48 continue
49 }
alandonovanebe61bd2021-02-12 16:57:32 -050050 q := Quote(tt.s, false)
Alan Donovan312d1a52017-10-02 10:10:28 -040051 if q != tt.q {
52 t.Errorf("quote(%#q) = %s, want %s", tt.s, q, tt.q)
53 }
54 }
55}
56
57func TestUnquote(t *testing.T) {
58 for _, tt := range quoteTests {
alandonovanebe61bd2021-02-12 16:57:32 -050059 s, triple, _, err := unquote(tt.q)
Alan Donovan312d1a52017-10-02 10:10:28 -040060 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}